Projet

Général

Profil

0007-admin-change-to-be-a-redirect-to-admin-1-6726.patch

Frédéric Péters, 22 avril 2015 12:31

Télécharger (8,85 ko)

Voir les différences:

Subject: [PATCH 07/11] admin: change to be a redirect to /admin/$1 (#6726)

 wcs/admin/root.py             | 155 +++---------------------------------------
 wcs/qommon/backoffice/root.py |  24 +++++--
 2 files changed, 26 insertions(+), 153 deletions(-)
wcs/admin/root.py
16 16

  
17 17
import os
18 18

  
19
from quixote import get_session, get_publisher, get_request, get_response
20
from quixote.directory import Directory, AccessControlled
21
from quixote.html import htmltext, TemplateIO
22

  
23
from qommon.admin.root import AdminDirectory
24

  
25
import settings
26
import forms
27
import roles
28
import users
29
import categories
30
import logger
31
import workflows
32
import bounces
33

  
34
from qommon import errors, get_cfg
19
from quixote import get_request, redirect
20
from quixote.directory import Directory
21
from quixote.html import htmltext
35 22

  
36 23
def gpl():
37 24
    return htmltext("""<p>This program is free software; you can redistribute it and/or modify it
......
50 37
    """)
51 38

  
52 39

  
53
class RootDirectory(AccessControlled, AdminDirectory):
54
    _q_exports = ['']
55

  
56
    settings = settings.SettingsDirectory()
57
    forms = forms.FormsDirectory()
58
    roles = roles.RolesDirectory()
59
    users = users.UsersDirectory()
60
    categories = categories.CategoriesDirectory()
61
    logger = logger.LoggerDirectory()
62
    workflows = workflows.WorkflowsDirectory()
63
    bounces = bounces.BouncesDirectory()
64

  
65
    menu_items = [
40
class RootDirectory(Directory):
41
    menu_items = [ # still used for access control (permissions panel)
66 42
        ('forms/', N_('Forms')),
67 43
        ('workflows/', N_('Workflows')),
68 44
        ('users/', N_('Users')),
......
73 49
        ('settings/', N_('Settings')),
74 50
        ('/', N_('WCS Form Server'))]
75 51

  
76
    def _q_access(self):
77
        get_response().breadcrumb.append( ('admin/', _('Administration')) )
78

  
79
        req = get_request()
80

  
81
        if os.path.exists(os.path.join(get_publisher().app_dir, 'ADMIN_FOR_ALL')):
82
            get_response().filter['admin_for_all'] = True
83
            if req.user and req.user.is_admin:
84
                # if the user had access to the admin, ADMIN_FOR_ALL was
85
                # certainly added because something wrong happened when setting
86
                # fine-grained access permissions with roles; so we give the
87
                # user all possible roles.
88
                req.user.roles = [x.id for x in roles.Role.select()]
89
            return
90

  
91
        session = get_session()
92

  
93
        if req.user:
94
            if get_publisher().user_class.count() == 0:
95
                # this means user logged in anonymously
96
                pass
97
            elif not req.user.is_admin:
98
                raise errors.AccessForbiddenError()
99
        else:
100
            if get_publisher().user_class.count() > 0:
101
                raise errors.AccessUnauthorizedError()
102

  
103
        return
104

  
105
    def get_intro_text(self):
106
        return _('''
107
w.c.s. is a web application which allows to design and set up online forms.
108
It gives users the ability to create web forms easily without requiring any
109
other skill than familiarity with web surfing.''')
110

  
111
    def _q_index(self):
112
        from menu import html_top
113
        html_top('/')
114
        r = TemplateIO(html=True)
115
        r += htmltext('<div class="bo-block"><p>%s</p></div>') % self.get_intro_text()
116

  
117
        r += htmltext('<ul class="apps">')
118
        for k, v in self.get_menu_items():
119
            if k.strip('/') not in ('forms', 'workflows', 'users', 'roles',
120
                    'categories', 'settings'):
121
                # limit this space to menu entries that have icons.
122
                continue
123
            r += htmltext('<li class="zone-%s"><a href="%s">') % (k.strip('/'), k)
124
            if callable(v):
125
                r += v()
126
            else:
127
                r += _(v)
128
            r += htmltext('</a></li>')
129
        r += htmltext('</ul>')
130

  
131
        r += htmltext('<br class="clear">')
132
        r += htmltext('<p id="for-more-info">%s</p>') % _('For more information:')
133

  
134
        r += htmltext('<ul>')
135
        r += htmltext('<li><a href="http://wcs.labs.libre-entreprise.org">%s</a></li>') % _('Web site')
136
        if get_publisher().admin_help_url and get_request().language in get_publisher().admin_help_url:
137
            doc_url = get_publisher().admin_help_url[get_request().language]
138
            r += htmltext('<li><a href="%s">%s</a></li>') % (doc_url, _('Online documentation'))
139
        r += htmltext('</ul>')
140

  
141
        get_response().filter['sidebar'] = str(self.get_sidebar())
142

  
143
        return r.getvalue()
144

  
145
    def get_sidebar(self):
146
        from menu import get_vc_version
147
        r = TemplateIO(html=True)
148
        version = get_vc_version()
149
        if version:
150
            r += htmltext('<div class="bo-block"><p class="version-info">')
151
            r += _('Version:')
152
            r += ' '
153
            r += version
154
            r += htmltext('</p></div>')
155

  
156
        r += htmltext('<div class="bo-block">')
157
        r += gpl()
158
        r += htmltext('</div>')
159

  
160
        return r.getvalue()
52
    def _q_traverse(self, path):
53
        url = get_request().get_path_query()
54
        url = url.replace('/admin/', '/backoffice/', 1)
55
        return redirect(url)
161 56

  
162 57
    def register_page(cls, url_name, directory = None, label = None):
163 58
        if directory:
......
173 68
            cls.menu_items.insert(logger_index, (url_name, label))
174 69

  
175 70
    register_page = classmethod(register_page)
176

  
177
    def _q_lookup(self, component):
178
        if not component in [str(x[0]).strip('/') for x in self.menu_items]:
179
            raise errors.TraversalError()
180

  
181
        authorised_roles = get_cfg('admin-permissions', {}).get(component)
182
        if authorised_roles and not os.path.exists(os.path.join(get_publisher().app_dir, 'ADMIN_FOR_ALL')):
183
            user_roles = set(get_request().user.roles)
184
            if not user_roles.intersection(authorised_roles):
185
                raise errors.AccessForbiddenError()
186

  
187
        return getattr(self, component)
188

  
189
    def get_menu_items(self):
190
        if not get_request().user:
191
            return self.menu_items
192
        user_roles = set(get_request().user.roles or [])
193
        if not get_cfg('admin-permissions', {}):
194
            return self.menu_items
195
        menu_items = self.menu_items[:]
196
        for k, v in self.menu_items:
197
            if not k.endswith(str('/')):
198
                continue
199
            k = k.strip(str('/'))
200
            if not k:
201
                continue
202
            authorised_roles = get_cfg('admin-permissions', {}).get(k)
203
            if not authorised_roles:
204
                continue
205
            if not user_roles.intersection(authorised_roles):
206
                menu_items.remove((k+'/', v))
207
        return menu_items
wcs/qommon/backoffice/root.py
59 59

  
60 60
    def _q_access(self):
61 61
        get_response().breadcrumb.append( ('backoffice/', _('Back Office')) )
62
        req = get_request()
62 63

  
63 64
        if os.path.exists(os.path.join(get_publisher().app_dir, 'ADMIN_FOR_ALL')):
65
            get_response().filter['admin_for_all'] = True
66
            if req.user and req.user.is_admin:
67
                # if the user had access to the admin, ADMIN_FOR_ALL was
68
                # certainly added because something wrong happened when setting
69
                # fine-grained access permissions with roles; so we give the
70
                # user all possible roles.
71
                req.user.roles = [x.id for x in roles.Role.select()]
64 72
            return
65
        user = get_request().user
66
        if not user and get_publisher().user_class.count() > 0:
67
            raise errors.AccessUnauthorizedError(
68
                    public_msg = _('Access to backoffice is restricted to authorized persons only. '\
69
                                   'Please login.'))
70
        if user and not user.can_go_in_backoffice():
71
            raise errors.AccessForbiddenError()
73

  
74
        if get_publisher().user_class.count() > 0:
75
            user = req.user
76
            if not user:
77
                raise errors.AccessUnauthorizedError(
78
                        public_msg = _('Access to backoffice is restricted to authorized persons only. '\
79
                                       'Please login.'))
80
            if not user.can_go_in_backoffice():
81
                raise errors.AccessForbiddenError()
72 82

  
73 83
        get_response().filter['in_backoffice'] = True
74 84

  
75
-