Add city to info plugin, fetch correct attrs in SSSD
[cascardo/ipsilon.git] / ipsilon / info / common.py
1 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
2
3 from ipsilon.util.log import Log
4 from ipsilon.util.plugin import PluginInstaller, PluginLoader
5 from ipsilon.util.plugin import PluginObject
6 from ipsilon.util.config import ConfigHelper
7
8
9 class InfoProviderBase(ConfigHelper, PluginObject):
10
11     def __init__(self, *pargs):
12         ConfigHelper.__init__(self)
13         PluginObject.__init__(self, *pargs)
14
15     def get_user_attrs(self, user):
16         raise NotImplementedError
17
18
19 class InfoMapping(Log):
20
21     def __init__(self):
22         self.standard_attributes = {
23             'fullname': 'Full Name',
24             'nickname': 'Nickname',
25             'surname': 'Last Name',
26             'firstname': 'First Name',
27             'title': 'Title',
28             'dob': 'Date of Birth',
29             'email': 'E-mail Address',
30             'gender': 'Gender',
31             'postcode': 'Postal Code',
32             'street': 'Street Address',
33             'city': 'City',
34             'state': 'State or Province',
35             'country': 'Country',
36             'phone': 'Telephone Number',
37             'language': 'Language',
38             'timezone': 'Time Zone',
39         }
40
41     def display_name(self, name):
42         if name in self.standard_attributes:
43             return self.standard_attributes[name]
44         return name
45
46
47 FACILITY = 'info_config'
48
49
50 class Info(Log):
51
52     def __init__(self, site):
53         self._site = site
54
55         plugins = PluginLoader(Info, FACILITY, 'InfoProvider')
56         plugins.get_plugin_data()
57         self._site[FACILITY] = plugins
58
59         available = plugins.available.keys()
60         self.debug('Available info providers: %s' % str(available))
61
62         for item in plugins.enabled:
63             self.debug('Info plugin in enabled list: %s' % item)
64             if item not in plugins.available:
65                 self.debug('Info Plugin %s not found' % item)
66                 continue
67             try:
68                 plugins.available[item].enable()
69             except Exception as e:  # pylint: disable=broad-except
70                 while item in plugins.enabled:
71                     plugins.enabled.remove(item)
72                 self.debug("Info Plugin %s couldn't be enabled: %s" % (
73                     item, str(e)))
74
75     def get_user_attrs(self, user, requested=None):
76         plugins = self._site[FACILITY].available
77         result = dict()
78
79         for _, p in plugins.items():
80             if requested is None:
81                 if not p.is_enabled:
82                     continue
83             else:
84                 if requested != p.name:
85                     continue
86             result = p.get_user_attrs(user)
87             if result:
88                 break
89
90         return result
91
92
93 class InfoProviderInstaller(object):
94
95     def __init__(self):
96         self.facility = FACILITY
97         self.ptype = 'info'
98         self.name = None
99
100     def install_args(self, group):
101         raise NotImplementedError
102
103     def validate_args(self, args):
104         return
105
106     def unconfigure(self, opts, changes):
107         return
108
109     def configure(self, opts, changes):
110         raise NotImplementedError
111
112
113 class InfoProviderInstall(object):
114
115     def __init__(self):
116         pi = PluginInstaller(InfoProviderInstall, FACILITY)
117         self.plugins = pi.get_plugins()