Show More
@@ -1,102 +1,102 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-2017 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | """ |
|
22 | """ | |
23 | repository permission model for RhodeCode |
|
23 | repository permission model for RhodeCode | |
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
26 | import logging |
|
26 | import logging | |
27 | from rhodecode.model import BaseModel |
|
27 | from rhodecode.model import BaseModel | |
28 | from rhodecode.model.db import UserRepoToPerm, UserGroupRepoToPerm, \ |
|
28 | from rhodecode.model.db import UserRepoToPerm, UserGroupRepoToPerm, \ | |
29 | Permission |
|
29 | Permission | |
30 |
|
30 | |||
31 | log = logging.getLogger(__name__) |
|
31 | log = logging.getLogger(__name__) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class RepositoryPermissionModel(BaseModel): |
|
34 | class RepositoryPermissionModel(BaseModel): | |
35 |
|
35 | |||
36 | cls = UserRepoToPerm |
|
36 | cls = UserRepoToPerm | |
37 |
|
37 | |||
38 | def get_user_permission(self, repository, user): |
|
38 | def get_user_permission(self, repository, user): | |
39 | repository = self._get_repo(repository) |
|
39 | repository = self._get_repo(repository) | |
40 | user = self._get_user(user) |
|
40 | user = self._get_user(user) | |
41 |
|
41 | |||
42 | return UserRepoToPerm.query() \ |
|
42 | return UserRepoToPerm.query() \ | |
43 | .filter(UserRepoToPerm.user == user) \ |
|
43 | .filter(UserRepoToPerm.user == user) \ | |
44 | .filter(UserRepoToPerm.repository == repository) \ |
|
44 | .filter(UserRepoToPerm.repository == repository) \ | |
45 | .scalar() |
|
45 | .scalar() | |
46 |
|
46 | |||
47 | def update_user_permission(self, repository, user, permission): |
|
47 | def update_user_permission(self, repository, user, permission): | |
48 | permission = Permission.get_by_key(permission) |
|
48 | permission = Permission.get_by_key(permission) | |
49 | current = self.get_user_permission(repository, user) |
|
49 | current = self.get_user_permission(repository, user) | |
50 | if current: |
|
50 | if current: | |
51 |
if |
|
51 | if current.permission is not permission: | |
52 | current.permission = permission |
|
52 | current.permission = permission | |
53 | else: |
|
53 | else: | |
54 | p = UserRepoToPerm() |
|
54 | p = UserRepoToPerm() | |
55 | p.user = user |
|
55 | p.user = user | |
56 | p.repository = repository |
|
56 | p.repository = repository | |
57 | p.permission = permission |
|
57 | p.permission = permission | |
58 | self.sa.add(p) |
|
58 | self.sa.add(p) | |
59 |
|
59 | |||
60 | def delete_user_permission(self, repository, user): |
|
60 | def delete_user_permission(self, repository, user): | |
61 | current = self.get_user_permission(repository, user) |
|
61 | current = self.get_user_permission(repository, user) | |
62 | if current: |
|
62 | if current: | |
63 | self.sa.delete(current) |
|
63 | self.sa.delete(current) | |
64 |
|
64 | |||
65 | def get_users_group_permission(self, repository, users_group): |
|
65 | def get_users_group_permission(self, repository, users_group): | |
66 | return UserGroupRepoToPerm.query() \ |
|
66 | return UserGroupRepoToPerm.query() \ | |
67 | .filter(UserGroupRepoToPerm.users_group == users_group) \ |
|
67 | .filter(UserGroupRepoToPerm.users_group == users_group) \ | |
68 | .filter(UserGroupRepoToPerm.repository == repository) \ |
|
68 | .filter(UserGroupRepoToPerm.repository == repository) \ | |
69 | .scalar() |
|
69 | .scalar() | |
70 |
|
70 | |||
71 | def update_user_group_permission(self, repository, users_group, |
|
71 | def update_user_group_permission(self, repository, users_group, | |
72 | permission): |
|
72 | permission): | |
73 | permission = Permission.get_by_key(permission) |
|
73 | permission = Permission.get_by_key(permission) | |
74 | current = self.get_users_group_permission(repository, users_group) |
|
74 | current = self.get_users_group_permission(repository, users_group) | |
75 | if current: |
|
75 | if current: | |
76 |
if |
|
76 | if current.permission is not permission: | |
77 | current.permission = permission |
|
77 | current.permission = permission | |
78 | else: |
|
78 | else: | |
79 | p = UserGroupRepoToPerm() |
|
79 | p = UserGroupRepoToPerm() | |
80 | p.users_group = users_group |
|
80 | p.users_group = users_group | |
81 | p.repository = repository |
|
81 | p.repository = repository | |
82 | p.permission = permission |
|
82 | p.permission = permission | |
83 | self.sa.add(p) |
|
83 | self.sa.add(p) | |
84 |
|
84 | |||
85 | def delete_users_group_permission(self, repository, users_group): |
|
85 | def delete_users_group_permission(self, repository, users_group): | |
86 | current = self.get_users_group_permission(repository, users_group) |
|
86 | current = self.get_users_group_permission(repository, users_group) | |
87 | if current: |
|
87 | if current: | |
88 | self.sa.delete(current) |
|
88 | self.sa.delete(current) | |
89 |
|
89 | |||
90 | def update_or_delete_user_permission(self, repository, user, permission): |
|
90 | def update_or_delete_user_permission(self, repository, user, permission): | |
91 | if permission: |
|
91 | if permission: | |
92 | self.update_user_permission(repository, user, permission) |
|
92 | self.update_user_permission(repository, user, permission) | |
93 | else: |
|
93 | else: | |
94 | self.delete_user_permission(repository, user) |
|
94 | self.delete_user_permission(repository, user) | |
95 |
|
95 | |||
96 |
def update_or_delete_users_group_permission( |
|
96 | def update_or_delete_users_group_permission( | |
97 | permission): |
|
97 | self, repository, user_group, permission): | |
98 | if permission: |
|
98 | if permission: | |
99 |
self.update_user_group_permission( |
|
99 | self.update_user_group_permission( | |
100 | permission) |
|
100 | repository, user_group, permission) | |
101 | else: |
|
101 | else: | |
102 | self.delete_users_group_permission(repository, user_group) |
|
102 | self.delete_users_group_permission(repository, user_group) |
General Comments 0
You need to be logged in to leave comments.
Login now