Show More
@@ -1,162 +1,162 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | # permissions controller for pylons |
|
3 | # permissions controller for pylons | |
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; version 2 |
|
8 | # as published by the Free Software Foundation; version 2 | |
9 | # of the License or (at your opinion) any later version of the license. |
|
9 | # of the License or (at your opinion) any later version of the license. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 | # MA 02110-1301, USA. |
|
19 | # MA 02110-1301, USA. | |
20 | """ |
|
20 | """ | |
21 | Created on April 27, 2010 |
|
21 | Created on April 27, 2010 | |
22 | permissions controller for pylons |
|
22 | permissions controller for pylons | |
23 | @author: marcink |
|
23 | @author: marcink | |
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
26 | from formencode import htmlfill |
|
26 | from formencode import htmlfill | |
27 | from pylons import request, session, tmpl_context as c, url |
|
27 | from pylons import request, session, tmpl_context as c, url | |
28 | from pylons.controllers.util import abort, redirect |
|
28 | from pylons.controllers.util import abort, redirect | |
29 | from pylons.i18n.translation import _ |
|
29 | from pylons.i18n.translation import _ | |
30 | from rhodecode.lib import helpers as h |
|
30 | from rhodecode.lib import helpers as h | |
31 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator |
|
31 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator | |
32 | from rhodecode.lib.base import BaseController, render |
|
32 | from rhodecode.lib.base import BaseController, render | |
33 | from rhodecode.model.forms import UserForm, DefaultPermissionsForm |
|
33 | from rhodecode.model.forms import UserForm, DefaultPermissionsForm | |
34 | from rhodecode.model.permission_model import PermissionModel |
|
34 | from rhodecode.model.permission_model import PermissionModel | |
35 | from rhodecode.model.user import UserModel |
|
35 | from rhodecode.model.user import UserModel | |
36 | import formencode |
|
36 | import formencode | |
37 | import logging |
|
37 | import logging | |
38 | import traceback |
|
38 | import traceback | |
39 |
|
39 | |||
40 | log = logging.getLogger(__name__) |
|
40 | log = logging.getLogger(__name__) | |
41 |
|
41 | |||
42 | class PermissionsController(BaseController): |
|
42 | class PermissionsController(BaseController): | |
43 | """REST Controller styled on the Atom Publishing Protocol""" |
|
43 | """REST Controller styled on the Atom Publishing Protocol""" | |
44 | # To properly map this controller, ensure your config/routing.py |
|
44 | # To properly map this controller, ensure your config/routing.py | |
45 | # file has a resource setup: |
|
45 | # file has a resource setup: | |
46 | # map.resource('permission', 'permissions') |
|
46 | # map.resource('permission', 'permissions') | |
47 |
|
47 | |||
48 | @LoginRequired() |
|
48 | @LoginRequired() | |
49 | @HasPermissionAllDecorator('hg.admin') |
|
49 | @HasPermissionAllDecorator('hg.admin') | |
50 | def __before__(self): |
|
50 | def __before__(self): | |
51 | c.admin_user = session.get('admin_user') |
|
51 | c.admin_user = session.get('admin_user') | |
52 | c.admin_username = session.get('admin_username') |
|
52 | c.admin_username = session.get('admin_username') | |
53 | super(PermissionsController, self).__before__() |
|
53 | super(PermissionsController, self).__before__() | |
54 |
|
54 | |||
55 | self.perms_choices = [('repository.none', _('None'),), |
|
55 | self.perms_choices = [('repository.none', _('None'),), | |
56 | ('repository.read', _('Read'),), |
|
56 | ('repository.read', _('Read'),), | |
57 | ('repository.write', _('Write'),), |
|
57 | ('repository.write', _('Write'),), | |
58 | ('repository.admin', _('Admin'),)] |
|
58 | ('repository.admin', _('Admin'),)] | |
59 | self.register_choices = [ |
|
59 | self.register_choices = [ | |
60 | ('hg.register.none', 'disabled'), |
|
60 | ('hg.register.none', 'disabled'), | |
61 | ('hg.register.manual_activate', |
|
61 | ('hg.register.manual_activate', | |
62 | _('allowed with manual account activation')), |
|
62 | _('allowed with manual account activation')), | |
63 | ('hg.register.auto_activate', |
|
63 | ('hg.register.auto_activate', | |
64 | _('allowed with automatic account activation')), ] |
|
64 | _('allowed with automatic account activation')), ] | |
65 |
|
65 | |||
66 | self.create_choices = [('hg.create.none', _('Disabled')), |
|
66 | self.create_choices = [('hg.create.none', _('Disabled')), | |
67 | ('hg.create.repository', _('Enabled'))] |
|
67 | ('hg.create.repository', _('Enabled'))] | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | def index(self, format='html'): |
|
70 | def index(self, format='html'): | |
71 | """GET /permissions: All items in the collection""" |
|
71 | """GET /permissions: All items in the collection""" | |
72 | # url('permissions') |
|
72 | # url('permissions') | |
73 |
|
73 | |||
74 | def create(self): |
|
74 | def create(self): | |
75 | """POST /permissions: Create a new item""" |
|
75 | """POST /permissions: Create a new item""" | |
76 | # url('permissions') |
|
76 | # url('permissions') | |
77 |
|
77 | |||
78 | def new(self, format='html'): |
|
78 | def new(self, format='html'): | |
79 | """GET /permissions/new: Form to create a new item""" |
|
79 | """GET /permissions/new: Form to create a new item""" | |
80 | # url('new_permission') |
|
80 | # url('new_permission') | |
81 |
|
81 | |||
82 | def update(self, id): |
|
82 | def update(self, id): | |
83 | """PUT /permissions/id: Update an existing item""" |
|
83 | """PUT /permissions/id: Update an existing item""" | |
84 | # Forms posted to this method should contain a hidden field: |
|
84 | # Forms posted to this method should contain a hidden field: | |
85 | # <input type="hidden" name="_method" value="PUT" /> |
|
85 | # <input type="hidden" name="_method" value="PUT" /> | |
86 | # Or using helpers: |
|
86 | # Or using helpers: | |
87 | # h.form(url('permission', id=ID), |
|
87 | # h.form(url('permission', id=ID), | |
88 | # method='put') |
|
88 | # method='put') | |
89 | # url('permission', id=ID) |
|
89 | # url('permission', id=ID) | |
90 |
|
90 | |||
91 | permission_model = PermissionModel() |
|
91 | permission_model = PermissionModel() | |
92 |
|
92 | |||
93 | _form = DefaultPermissionsForm([x[0] for x in self.perms_choices], |
|
93 | _form = DefaultPermissionsForm([x[0] for x in self.perms_choices], | |
94 | [x[0] for x in self.register_choices], |
|
94 | [x[0] for x in self.register_choices], | |
95 | [x[0] for x in self.create_choices])() |
|
95 | [x[0] for x in self.create_choices])() | |
96 |
|
96 | |||
97 | try: |
|
97 | try: | |
98 | form_result = _form.to_python(dict(request.POST)) |
|
98 | form_result = _form.to_python(dict(request.POST)) | |
99 | form_result.update({'perm_user_name':id}) |
|
99 | form_result.update({'perm_user_name':id}) | |
100 | permission_model.update(form_result) |
|
100 | permission_model.update(form_result) | |
101 | h.flash(_('Default permissions updated succesfully'), |
|
101 | h.flash(_('Default permissions updated succesfully'), | |
102 | category='success') |
|
102 | category='success') | |
103 |
|
103 | |||
104 | except formencode.Invalid, errors: |
|
104 | except formencode.Invalid, errors: | |
105 | c.perms_choices = self.perms_choices |
|
105 | c.perms_choices = self.perms_choices | |
106 | c.register_choices = self.register_choices |
|
106 | c.register_choices = self.register_choices | |
107 | c.create_choices = self.create_choices |
|
107 | c.create_choices = self.create_choices | |
108 |
|
108 | |||
109 | return htmlfill.render( |
|
109 | return htmlfill.render( | |
110 | render('admin/permissions/permissions.html'), |
|
110 | render('admin/permissions/permissions.html'), | |
111 | defaults=errors.value, |
|
111 | defaults=errors.value, | |
112 | errors=errors.error_dict or {}, |
|
112 | errors=errors.error_dict or {}, | |
113 | prefix_error=False, |
|
113 | prefix_error=False, | |
114 | encoding="UTF-8") |
|
114 | encoding="UTF-8") | |
115 | except Exception: |
|
115 | except Exception: | |
116 | log.error(traceback.format_exc()) |
|
116 | log.error(traceback.format_exc()) | |
117 | h.flash(_('error occured during update of permissions'), |
|
117 | h.flash(_('error occured during update of permissions'), | |
118 | category='error') |
|
118 | category='error') | |
119 |
|
119 | |||
120 | return redirect(url('edit_permission', id=id)) |
|
120 | return redirect(url('edit_permission', id=id)) | |
121 |
|
121 | |||
122 |
|
122 | |||
123 |
|
123 | |||
124 | def delete(self, id): |
|
124 | def delete(self, id): | |
125 | """DELETE /permissions/id: Delete an existing item""" |
|
125 | """DELETE /permissions/id: Delete an existing item""" | |
126 | # Forms posted to this method should contain a hidden field: |
|
126 | # Forms posted to this method should contain a hidden field: | |
127 | # <input type="hidden" name="_method" value="DELETE" /> |
|
127 | # <input type="hidden" name="_method" value="DELETE" /> | |
128 | # Or using helpers: |
|
128 | # Or using helpers: | |
129 | # h.form(url('permission', id=ID), |
|
129 | # h.form(url('permission', id=ID), | |
130 | # method='delete') |
|
130 | # method='delete') | |
131 | # url('permission', id=ID) |
|
131 | # url('permission', id=ID) | |
132 |
|
132 | |||
133 | def show(self, id, format='html'): |
|
133 | def show(self, id, format='html'): | |
134 | """GET /permissions/id: Show a specific item""" |
|
134 | """GET /permissions/id: Show a specific item""" | |
135 | # url('permission', id=ID) |
|
135 | # url('permission', id=ID) | |
136 |
|
136 | |||
137 | def edit(self, id, format='html'): |
|
137 | def edit(self, id, format='html'): | |
138 | """GET /permissions/id/edit: Form to edit an existing item""" |
|
138 | """GET /permissions/id/edit: Form to edit an existing item""" | |
139 | #url('edit_permission', id=ID) |
|
139 | #url('edit_permission', id=ID) | |
140 | c.perms_choices = self.perms_choices |
|
140 | c.perms_choices = self.perms_choices | |
141 | c.register_choices = self.register_choices |
|
141 | c.register_choices = self.register_choices | |
142 | c.create_choices = self.create_choices |
|
142 | c.create_choices = self.create_choices | |
143 |
|
143 | |||
144 | if id == 'default': |
|
144 | if id == 'default': | |
145 | defaults = {'_method':'put'} |
|
145 | defaults = {'_method':'put'} | |
146 |
for p in UserModel().get_ |
|
146 | for p in UserModel().get_by_username('default').user_perms: | |
147 | if p.permission.permission_name.startswith('repository.'): |
|
147 | if p.permission.permission_name.startswith('repository.'): | |
148 | defaults['default_perm'] = p.permission.permission_name |
|
148 | defaults['default_perm'] = p.permission.permission_name | |
149 |
|
149 | |||
150 | if p.permission.permission_name.startswith('hg.register.'): |
|
150 | if p.permission.permission_name.startswith('hg.register.'): | |
151 | defaults['default_register'] = p.permission.permission_name |
|
151 | defaults['default_register'] = p.permission.permission_name | |
152 |
|
152 | |||
153 | if p.permission.permission_name.startswith('hg.create.'): |
|
153 | if p.permission.permission_name.startswith('hg.create.'): | |
154 | defaults['default_create'] = p.permission.permission_name |
|
154 | defaults['default_create'] = p.permission.permission_name | |
155 |
|
155 | |||
156 | return htmlfill.render( |
|
156 | return htmlfill.render( | |
157 | render('admin/permissions/permissions.html'), |
|
157 | render('admin/permissions/permissions.html'), | |
158 | defaults=defaults, |
|
158 | defaults=defaults, | |
159 | encoding="UTF-8", |
|
159 | encoding="UTF-8", | |
160 | force_defaults=True,) |
|
160 | force_defaults=True,) | |
161 | else: |
|
161 | else: | |
162 | return redirect(url('admin_home')) |
|
162 | return redirect(url('admin_home')) |
General Comments 0
You need to be logged in to leave comments.
Login now