summaryrefslogtreecommitdiffstats
path: root/idp/extra/modules/identities_ui.ptl
blob: 4a31508ee115d8c428340e60f6f3e7d1abdf583b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from quixote import get_request, get_response, get_session, redirect
from quixote.directory import Directory

from qommon.backoffice.menu import html_top
from qommon.admin.menu import command_icon

from qommon import errors
from qommon.form import *

import directory

class AgentDirectory(Directory):
    _q_exports = ['', 'edit', 'delete', 'disable', 'enable', 'password']

    attrs = [
        ('cdg59agentSirhCode', _('SIRH Code'), {'required': True, 'size': 30}),
        ('sn', N_('Last Name'), {'required': True, 'size': 30}),
        ('givenName', N_('First Name'), {'required': True, 'size': 30}),
        ('cdg59direction', N_('Direction'), {'size': 30}),
        ('ou', N_('Service'), {'size': 30}),
        ('employeeType', N_('Functions'), {'size': 30}),
        ('postalAddress', N_('Geopostal Address'), {'size': 30}),
        ('mail', N_('Mail'), {'size': 30, 'required': True}),
        ('telephoneNumber', N_('Telephone Number'), {'size': 30}),
        ('facsimileTelephoneNumber', N_('Fax Number'), {'size': 30}),
        ('mobile', N_('Professional Mobile Number'), {'size': 30}),
        ('cdg59isAdmin', _('Collectivity Admin'), {'widget_class': CheckboxWidget}),
        ]

    def __init__(self, agent):
        self.agent = agent
        get_response().breadcrumb.append((self.agent.username + '/', self.agent.cn))

    def _q_index [html] (self):
        html_top('identities', title = _('Agent: %s') % self.agent.cn)
        '<h2>%s</h2>' % _('Agent: %s') % self.agent.cn

        if self.agent.cdg59isDisabled:
            '<p>'
            _('This account is disabled.')
            '</p>'

        '\n<dl>\n'
        for k, l, attrs in self.attrs:
            if getattr(self.agent, k):
                '  <dt>%s</dt>' % _(l)
                '  <dd>%s</dd>\n' % getattr(self.agent,k)
        '\n</dl>'

        '<p>'
        '<a href="edit">%s</a> - ' % _('Edit')

        user = get_session().get_user_object()
        if user.is_admin() or user.is_local_admin():
            if self.agent.cdg59isDisabled:
                '<a href="enable">%s</a> - ' % _('Enable Account')
            else:
                '<a href="disable">%s</a> - ' % _('Disable Account')
        if user.is_admin():
            '<a href="delete" rel="popup">%s</a> - ' % _('Delete')
        '<a href="password">%s</a>' % _('Change Password')
        '</p>'

    def edit [html] (self):
        user = get_session().get_user_object()
        form = Form(enctype='multipart/form-data')
        for k, l, attrs in self.attrs:
            if k == 'cdg59isAdmin' and not user.is_admin():
                continue
            if attrs.has_key('widget_class'):
                widget_class = attrs.get('widget_class')
                attrs = attrs.copy()
                del attrs['widget_class']
            else:
                widget_class = StringWidget
            form.add(widget_class, k, title = _(l),
                    value = getattr(self.agent, k),
                    **attrs)
        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_submit() == 'cancel':
            return redirect('..')

        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('edit', _('Edit')))
            html_top('identities', title = _('Agent: %s') % self.agent.cn)
            '<h2>%s</h2>' % self.agent.cn
            form.render()
        else:
            self.submit(form)
            return redirect('..')

    def submit(self, form):
        changed = []
        for k, l, attrs in self.attrs:
            widget = form.get_widget(k)
            if widget:
                v = widget.parse()
                old_v = getattr(self.agent, k)
                if v != old_v:
                    setattr(self.agent, k, v)
                    changed.append(k)
        self.agent.save(changed)

    def disable(self):
        user = get_session().get_user_object()
        if user.is_admin() or user.is_local_admin():
            self.agent.cdg59isDisabled = True
            self.agent.save(['cdg59isDisabled'])
            return redirect('.')
        else:
            raise errors.AccessForbiddenError()

    def enable(self):
        user = get_session().get_user_object()
        if user.is_admin() or user.is_local_admin():
            self.agent.cdg59isDisabled = False
            self.agent.save(['cdg59isDisabled'])
            return redirect('.')
        else:
            raise errors.AccessForbiddenError()

    def delete [html] (self):
        user = get_session().get_user_object()
        if not user.is_admin():
            raise errors.AccessForbiddenError()
        form = Form(enctype='multipart/form-data')
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'You are about to irrevocably delete this agent.')))
        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_submit() == 'cancel':
            return redirect('..')
        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('delete', _('Delete')))
            html_top('identities', title = _('Delete Agent'))
            '<h2>%s</h2>' % _('Deleting Agent: %s') % self.agent.cn
            form.render()
        else:
            self.agent.delete()
            return redirect('..')

    def password [html] (self):
        form = Form(enctype='multipart/form-data')
        form.add(PasswordWidget, 'new_password', title=_('New Password'), required=True)

        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))

        if form.get_submit() == 'cancel':
            return redirect('.')

        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('password', _('Change Password')))
            html_top('identities', title = _('Agent: %s') % self.agent.cn)
            '<h2>%s</h2>' % self.agent.cn
            form.render()
        else:
            self.agent.change_password(str(form.get_widget('new_password').parse()))
            return redirect('..')


class IdentitiesDirectory(Directory):
    _q_exports = ['', 'new', 'lookup_sirh', 'authorizations']

    collectivity = None

    def __init__(self, collectivity = None):
        Directory.__init__(self)
        self.collectivity = collectivity

    def _q_traverse(self, path):
        if not self.collectivity:
            get_response().breadcrumb.append(('identities/', _('Identity Management')))
            user = get_session().get_user_object()
            if not user.is_admin():
                self.collectivity = user.get_collectivity()
        return Directory._q_traverse(self, path)

    def _q_index [html] (self):
        html_top('identities', _('Identity Management'))

        get_session().display_message()

        user = get_session().get_user_object()
        if user.is_admin() and not self.collectivity:
            # list all collectivities

            form = Form(action = 'lookup_sirh')
            form.add(StringWidget, 'sirh', title = _('Search on SIRH Code'),
                    size = 10)
            #form.add_submit('submit', _('Search'))
            form.render()

            '<ul>'
            for collectivity in directory.get_sorted_collectivities():
                '<li><a href="%s/">' % collectivity.ou
                collectivity.cn
                '</a></li>'
            '</ul>'
        else:
            '<h2>'
            self.collectivity.cn
            '</h2>'

            '<p>'
            '<a href="new">%s</a>' % _('Add a new Agent')
            if user.is_admin() or user.is_local_admin():
                ' <a href="authorizations">%s</a>' % _('Manage Authorizations')
            '</p>'
            '<ul class="biglist">'
            for agent in directory.get_agents(self.collectivity, sorted = True):
                if agent.cdg59isDisabled:
                    '<li class="biglistitem disabled">'
                elif agent.cdg59isAdmin:
                    '<li class="biglistitem admin">'
                else:
                    '<li class="biglistitem">'
                '<strong class="label">%s</strong>' % agent.cn
                '<p class="commands">'
                command_icon('%s/' % agent.username, 'view')
                command_icon('%s/edit' % agent.username, 'edit')
                if user.is_admin():
                    command_icon('%s/delete' % agent.username, 'remove', popup = True)
                '</p></li>'
            '</ul>'
            if user.is_admin():
                '<p>'
                '<a href="../">%s</a>' % _('Back')
                '</p>'

    def authorizations [html] (self):
        if not self.collectivity:
            raise errors.TraversalError()

        user = get_session().get_user_object()
        if not (user.is_admin() or user.is_local_admin()):
            raise errors.AccessForbiddenError()

        if get_request().get_method() == 'POST':
            return self.authorizations_submit()

        html_top('identities', _('Authorizations Management'))
        get_response().breadcrumb.append(('authorizations', _('Authorizations Management')))

        get_session().display_message()

        services = directory.get_service_instances(self.collectivity)

        '<form method="post">'
        '<table>'
        '<thead>'
        '<tr>'
        '<td></td>'
        for service in services:
            '<th>%s</th>' % service.name
        '</tr>'
        '</thead>'
        '<tbody>'
        for agent in directory.get_agents(self.collectivity, sorted = True):
            if agent.cdg59isDisabled:
                '<tr class="disabled">'
            else:
                '<tr>'
            '<th>%s</th>' % agent.cn
            for service in services:
                '<td>'
                '<input name="%s" type="checkbox" value="%s"' % (agent.username, service.cdg59siid)
                if service.cdg59siid in agent.cdg59serviceAccesses:
                    ' checked="checked"'
                '/>'
                '</td>'
            '</tr>'
        '</tbody>'
        '</table>'

        '<input type="submit" value="%s"/>' % _('Submit')
        '</form>'

    def authorizations_submit(self):
        form = get_request().form
        for agent in directory.get_agents(self.collectivity):
            accesses = agent.cdg59serviceAccesses
            new_value = form.get(agent.username, [])
            if sorted(accesses) != sorted(new_value):
                agent.cdg59serviceAccesses = new_value
                agent.save(changed = ['cdg59serviceAccesses'])
        get_session().message = ('info', _('New authorizations saved succesfully.'))
        return redirect('authorizations')

    def lookup_sirh(self):
        form = Form(action = 'lookup_sirh')
        form.add(StringWidget, 'sirh', title = _('Search on SIRH Code'), size = 10,
                required = True)
        if not form.is_submitted() or form.has_errors():
            get_session().message = ('error', _('Error looking up agent'))
            return redirect('.')

        sirh = form.get_widget('sirh').parse()
        try:
            agent = directory.get_agent_by_sirh(sirh)
        except KeyError:
            get_session().message = ('error', _('No agent found with this SIRH'))
            return redirect('.')

        uid, collectivity, foobar = agent.dn.split(',', 2)
        uid = uid.split('=')[1]
        collectivity = collectivity.split('=')[1]

        if self.collectivity:
            return redirect('../%s/%s/' % (collectivity, uid))
        else:
            return redirect('%s/%s/' % (collectivity, uid))

    
    def new [html] (self):
        form = Form(enctype='multipart/form-data')
        form.add(StringWidget, 'uid', title = _('Pr@tic Username'), required = True, size = 30)
        form.add(StringWidget, 'sn', title = _('Last Name'), required = True, size = 30)
        form.add(StringWidget, 'givenName', title = _('First Name'), required = True, size = 30)
        form.add(StringWidget, 'mail', title = _('Mail'), size = 30, required = True)

        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_submit() == 'cancel':
            return redirect('.')

        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('new', _('New')))
            html_top('identities', title = _('New Agent'))
            '<h2>%s</h2>' % _('New Agent')
            form.render()
        else:
            agent = self.submit_new(form)
            return redirect('%s/edit' % form.get_widget('uid').parse())

    def submit_new(self, form):
        directory.add_agent(self.collectivity, {
                'uid': form.get_widget('uid').parse(),
                'sn': form.get_widget('sn').parse(),
                'givenName': form.get_widget('givenName').parse(),
                'mail': form.get_widget('mail').parse()})


    def _q_lookup(self, component):
        if not self.collectivity:
            for collectivity in directory.get_collectivities():
                if collectivity.ou == component:
                    get_response().breadcrumb.append((component + '/', collectivity.cn))
                    return IdentitiesDirectory(collectivity)
        else:
            # lookup for agent
            try:
                agent = directory.get_agent(self.collectivity, component)
            except KeyError:
                pass
            else:
                return AgentDirectory(agent)
        raise errors.TraversalError()