62f80ef51ea8428f4c1806ece7d0515cb612b07f
[cascardo/ipsilon.git] / ipsilon / info / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon Project Contributors
4 #
5 # See the file named COPYING for the project license
6
7 from ipsilon.util.log import Log
8 from ipsilon.util.plugin import PluginLoader, PluginObject
9 from ipsilon.util.plugin import PluginInstaller
10
11
12 class InfoProviderBase(PluginObject, Log):
13
14     def __init__(self):
15         super(InfoProviderBase, self).__init__()
16         self._site = None
17
18     def get_user_attrs(self, user):
19         raise NotImplementedError
20
21     @property
22     def is_enabled(self):
23         if self._site:
24             return self in self._site[FACILITY]['enabled']
25         return False
26
27     def enable(self, site):
28         self._site = site
29         plugins = site[FACILITY]
30         if self in plugins['enabled']:
31             return
32
33         # configure self
34         if self.name in plugins['config']:
35             self.set_config(plugins['config'][self.name])
36
37         plugins['enabled'].append(self)
38         self.debug('Info plugin enabled: %s' % self.name)
39
40     def disable(self, site):
41         self._site = site
42         plugins = site[FACILITY]
43         if self not in plugins['enabled']:
44             return
45
46         plugins['enabled'].remove(self)
47         self.debug('Info plugin disabled: %s' % self.name)
48
49
50 class InfoMapping(Log):
51
52     def __init__(self):
53         self.standard_attributes = {
54             'fullname': 'Full Name',
55             'nickname': 'Nickname',
56             'surname': 'Last Name',
57             'firstname': 'First Name',
58             'title': 'Title',
59             'dob': 'Date of Birth',
60             'email': 'E-mail Address',
61             'gender': 'Gender',
62             'postcode': 'Postal Code',
63             'street': 'Street Address',
64             'state': 'State or Province',
65             'country': 'Country',
66             'phone': 'Telephone Number',
67             'language': 'Language',
68             'timezone': 'Time Zone',
69         }
70         self.mapping = dict()
71
72     def set_mapping(self, attrs_map):
73         self.mapping = attrs_map
74
75     def display_name(self, name):
76         if name in self.standard_attributes:
77             return self.standard_attributes[name]
78         return name
79
80     def map_attrs(self, attrs):
81         s = dict()
82         e = dict()
83         for a in attrs:
84             if a in self.mapping:
85                 s[self.mapping[a]] = attrs[a]
86             else:
87                 e[a] = attrs[a]
88
89         return s, e
90
91
92 FACILITY = 'info_config'
93
94
95 class Info(Log):
96
97     def __init__(self, site):
98         self._site = site
99
100         loader = PluginLoader(Info, FACILITY, 'InfoProvider')
101         self._site[FACILITY] = loader.get_plugin_data()
102         plugins = self._site[FACILITY]
103
104         available = plugins['available'].keys()
105         self.debug('Available info providers: %s' % str(available))
106
107         plugins['root'] = self
108         for item in plugins['whitelist']:
109             self.debug('Login plugin in whitelist: %s' % item)
110             if item not in plugins['available']:
111                 self.debug('Info Plugin %s not found' % item)
112                 continue
113             plugins['available'][item].enable(self._site)
114
115     def get_user_attrs(self, user, requested=None):
116         plugins = self._site[FACILITY]['available']
117         result = dict()
118
119         for _, p in plugins.items():
120             if requested is None:
121                 if not p.is_enabled:
122                     continue
123             else:
124                 if requested != p.name:
125                     continue
126             result = p.get_user_attrs(user)
127             if result:
128                 break
129
130         return result
131
132
133 class InfoProviderInstaller(object):
134
135     def __init__(self):
136         self.facility = FACILITY
137         self.ptype = 'info'
138         self.name = None
139
140     def install_args(self, group):
141         raise NotImplementedError
142
143     def configure(self, opts):
144         raise NotImplementedError
145
146
147 class InfoProviderInstall(object):
148
149     def __init__(self):
150         pi = PluginInstaller(InfoProviderInstall)
151         self.plugins = pi.get_plugins()