summaryrefslogtreecommitdiffstats
path: root/idp/extra/modules/backoffice.ptl
blob: 3d7a4485ec7ce5e9f15065021f3b90d2c594367b (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
from quixote import get_request, get_response, get_session, redirect
from quixote.directory import AccessControlled, Directory
from qommon.publisher import get_publisher_class

from qommon.backoffice.menu import html_top

from qommon import errors
from qommon.form import *

import identities_ui
import services_ui
import directory

class BackofficeRootDirectory(AccessControlled, Directory):
    _q_exports = ['', 'identities', 'services']
    items = [
        ('identities/', N_('Identities Management')),
        ('services/', N_('Services Management')),
        ('/', N_('Pr@tic Backoffice'))]

    identities = identities_ui.IdentitiesDirectory()
    services = services_ui.ServicesDirectory()

    collectivity = None

    def _q_access(self):
        get_response().breadcrumb.append( ('backoffice/', _('Back Office')) )
        user = get_session().get_user_object()
        if not user.is_admin() and not user.is_local_admin():
            raise errors.AccessForbiddenError()

        if user.is_admin():
            # reset directory attributes
            self.collectivity = None
            self.identities.collectivity = None
            self.services.collectivity = None
        else:
            self.collectivity = user.get_collectivity()
            self.identities.collectivity = self.collectivity
            self.services.collectivity = self.collectivity

    def _q_index [html] (self):
        html_top(_('Back Office'))

        if not self.collectivity:
            '<ul class="biglist">'
            for collectivity in directory.get_sorted_collectivities():
                '<li class="biglistitem">'
                '<strong class="label">%s</<strong>' % collectivity.cn
                '<p class="commands">'
                if collectivity.ou != 'admin':
                    ' <a href="%s/">%s</a> - ' % (collectivity.ou, _('Edit'))
                ' <a href="identities/%s/">%s</a>' % (collectivity.ou, _('Identities'))
                ' - '
                ' <a href="services/%s/">%s</a> ' % (collectivity.ou, _('Services'))
                '</p>'
                '</li>'
            '</ul>'
        else:
            '<h3>%s</h3>' % self.collectivity.cn
            '\n<dl>\n'
            for k, l, a in self.collectivity.get_attrs():
                if k == 'cn':
                    continue
                if getattr(self.collectivity, k):
                    '  <dt>%s</dt>' % _(l)
                    '  <dd>%s</dd>\n' % getattr(self.collectivity, k)
            '\n</dl>'

            '<p>'
            '<a href="identities/">%s</a>' % _('Identities')
            ' - '
            '<a href="services/">%s</a>' % _('Services')
            '</p>'

    def _q_lookup(self, component):
        if not self.collectivity:
            for collectivity in directory.get_collectivities():
                if collectivity.ou == component:
                    return CollectivityDirectory(collectivity)
        raise errors.TraversalError()


class CollectivityDirectory(Directory):
    _q_exports = ['']

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

    def _q_index [html] (self):
        form = Form(enctype='multipart/form-data')
        for k, l, attrs in self.collectivity.get_attrs():
            form.add(StringWidget, k, title = _(l),
                    value = getattr(self.collectivity, 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(self.collectivity.cn)
            '<h2>%s</h2>' % self.collectivity.cn
            form.render()
        else:
            self.submit(form)
            return redirect('..')

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



get_publisher_class().backoffice_directory_class = BackofficeRootDirectory