Show More
@@ -1,98 +1,99 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | # Model for permissions |
|
4 | 4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
5 | 5 | |
|
6 | 6 | # This program is free software; you can redistribute it and/or |
|
7 | 7 | # modify it under the terms of the GNU General Public License |
|
8 | 8 | # as published by the Free Software Foundation; version 2 |
|
9 | 9 | # of the License or (at your opinion) any later version of the license. |
|
10 | 10 | # |
|
11 | 11 | # This program is distributed in the hope that it will be useful, |
|
12 | 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 | # GNU General Public License for more details. |
|
15 | 15 | # |
|
16 | 16 | # You should have received a copy of the GNU General Public License |
|
17 | 17 | # along with this program; if not, write to the Free Software |
|
18 | 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 | 19 | # MA 02110-1301, USA. |
|
20 | 20 | """ |
|
21 | 21 | Created on Aug 20, 2010 |
|
22 | 22 | Model for permissions |
|
23 | 23 | @author: marcink |
|
24 | 24 | """ |
|
25 | 25 | |
|
26 | 26 | from rhodecode.model.db import User, Permission, UserToPerm, RepoToPerm |
|
27 | 27 | from rhodecode.model.caching_query import FromCache |
|
28 | 28 | from rhodecode.model.meta import Session |
|
29 | 29 | import logging |
|
30 | 30 | import traceback |
|
31 | 31 | log = logging.getLogger(__name__) |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | class PermissionModel(object): |
|
35 | 35 | |
|
36 | 36 | def __init__(self): |
|
37 | 37 | self.sa = Session() |
|
38 | 38 | |
|
39 | 39 | def get_permission(self, permission_id, cache=False): |
|
40 | 40 | perm = self.sa.query(Permission) |
|
41 | 41 | if cache: |
|
42 | 42 | perm = perm.options(FromCache("sql_cache_short", |
|
43 | 43 | "get_permission_%s" % permission_id)) |
|
44 | 44 | return perm.get(permission_id) |
|
45 | 45 | |
|
46 | 46 | def get_permission_by_name(self, name, cache=False): |
|
47 | 47 | perm = self.sa.query(Permission)\ |
|
48 | 48 | .filter(Permission.permission_name == name) |
|
49 | 49 | if cache: |
|
50 | 50 | perm = perm.options(FromCache("sql_cache_short", |
|
51 | 51 | "get_permission_%s" % name)) |
|
52 | 52 | return perm.scalar() |
|
53 | 53 | |
|
54 | 54 | def update(self, form_result): |
|
55 | 55 | perm_user = self.sa.query(User)\ |
|
56 | 56 | .filter(User.username == form_result['perm_user_name']).scalar() |
|
57 | 57 | u2p = self.sa.query(UserToPerm).filter(UserToPerm.user == perm_user).all() |
|
58 | 58 | if len(u2p) != 3: |
|
59 | 59 | raise Exception('Defined: %s should be 3 permissions for default' |
|
60 | 60 | ' user. This should not happen please verify' |
|
61 | 61 | ' your database' % len(u2p)) |
|
62 | 62 | |
|
63 | 63 | try: |
|
64 | 64 | #stage 1 change defaults |
|
65 | 65 | for p in u2p: |
|
66 | 66 | if p.permission.permission_name.startswith('repository.'): |
|
67 | 67 | p.permission = self.get_permission_by_name( |
|
68 | 68 | form_result['default_perm']) |
|
69 | 69 | self.sa.add(p) |
|
70 | 70 | |
|
71 | 71 | if p.permission.permission_name.startswith('hg.register.'): |
|
72 | 72 | p.permission = self.get_permission_by_name( |
|
73 | 73 | form_result['default_register']) |
|
74 | 74 | self.sa.add(p) |
|
75 | 75 | |
|
76 | 76 | if p.permission.permission_name.startswith('hg.create.'): |
|
77 | 77 | p.permission = self.get_permission_by_name( |
|
78 | 78 | form_result['default_create']) |
|
79 | 79 | self.sa.add(p) |
|
80 | ||
|
80 | 81 | #stage 2 update all default permissions for repos if checked |
|
81 |
if form_result['overwrite_default'] == |
|
|
82 | if form_result['overwrite_default'] == True: | |
|
82 | 83 | for r2p in self.sa.query(RepoToPerm)\ |
|
83 | 84 | .filter(RepoToPerm.user == perm_user).all(): |
|
84 | 85 | r2p.permission = self.get_permission_by_name( |
|
85 | 86 | form_result['default_perm']) |
|
86 | 87 | self.sa.add(r2p) |
|
87 | 88 | |
|
88 | 89 | #stage 3 set anonymous access |
|
89 | 90 | if perm_user.username == 'default': |
|
90 | 91 | perm_user.active = bool(form_result['anonymous']) |
|
91 | 92 | self.sa.add(perm_user) |
|
92 | 93 | |
|
93 | 94 | |
|
94 | 95 | self.sa.commit() |
|
95 | 96 | except: |
|
96 | 97 | log.error(traceback.format_exc()) |
|
97 | 98 | self.sa.rollback() |
|
98 | 99 | raise |
General Comments 0
You need to be logged in to leave comments.
Login now