Show More
@@ -1,150 +1,151 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.users_group |
|
3 | rhodecode.model.users_group | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | users group model for RhodeCode |
|
6 | users group model for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Oct 1, 2011 |
|
8 | :created_on: Oct 1, 2011 | |
9 | :author: nvinot |
|
9 | :author: nvinot | |
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> |
|
10 | :copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | |
11 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
12 | :license: GPLv3, see COPYING for more details. |
|
12 | :license: GPLv3, see COPYING for more details. | |
13 | """ |
|
13 | """ | |
14 | # This program is free software: you can redistribute it and/or modify |
|
14 | # This program is free software: you can redistribute it and/or modify | |
15 | # it under the terms of the GNU General Public License as published by |
|
15 | # it under the terms of the GNU General Public License as published by | |
16 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | # the Free Software Foundation, either version 3 of the License, or | |
17 | # (at your option) any later version. |
|
17 | # (at your option) any later version. | |
18 | # |
|
18 | # | |
19 | # This program is distributed in the hope that it will be useful, |
|
19 | # This program is distributed in the hope that it will be useful, | |
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | # GNU General Public License for more details. |
|
22 | # GNU General Public License for more details. | |
23 | # |
|
23 | # | |
24 | # You should have received a copy of the GNU General Public License |
|
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
26 |
|
26 | |||
27 | import logging |
|
27 | import logging | |
28 | import traceback |
|
28 | import traceback | |
29 |
|
29 | |||
30 | from rhodecode.model import BaseModel |
|
30 | from rhodecode.model import BaseModel | |
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup,\ |
|
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup,\ | |
32 | UsersGroupRepoToPerm, Permission, UsersGroupToPerm |
|
32 | UsersGroupRepoToPerm, Permission, UsersGroupToPerm | |
33 | from rhodecode.lib.exceptions import UsersGroupsAssignedException |
|
33 | from rhodecode.lib.exceptions import UsersGroupsAssignedException | |
34 |
|
34 | |||
35 | log = logging.getLogger(__name__) |
|
35 | log = logging.getLogger(__name__) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class UsersGroupModel(BaseModel): |
|
38 | class UsersGroupModel(BaseModel): | |
39 |
|
39 | |||
40 | def __get_users_group(self, users_group): |
|
40 | def __get_users_group(self, users_group): | |
41 | return self._get_instance(UsersGroup, users_group) |
|
41 | return self._get_instance(UsersGroup, users_group) | |
42 |
|
42 | |||
43 | def get(self, users_group_id, cache=False): |
|
43 | def get(self, users_group_id, cache=False): | |
44 | return UsersGroup.get(users_group_id) |
|
44 | return UsersGroup.get(users_group_id) | |
45 |
|
45 | |||
46 | def get_by_name(self, name, cache=False, case_insensitive=False): |
|
46 | def get_by_name(self, name, cache=False, case_insensitive=False): | |
47 | return UsersGroup.get_by_group_name(name, cache, case_insensitive) |
|
47 | return UsersGroup.get_by_group_name(name, cache, case_insensitive) | |
48 |
|
48 | |||
49 | def create(self, name, active=True): |
|
49 | def create(self, name, active=True): | |
50 | try: |
|
50 | try: | |
51 | new = UsersGroup() |
|
51 | new = UsersGroup() | |
52 | new.users_group_name = name |
|
52 | new.users_group_name = name | |
53 | new.users_group_active = active |
|
53 | new.users_group_active = active | |
54 | self.sa.add(new) |
|
54 | self.sa.add(new) | |
55 | return new |
|
55 | return new | |
56 | except: |
|
56 | except: | |
57 | log.error(traceback.format_exc()) |
|
57 | log.error(traceback.format_exc()) | |
58 | raise |
|
58 | raise | |
59 |
|
59 | |||
60 | def update(self, users_group, form_data): |
|
60 | def update(self, users_group, form_data): | |
61 |
|
61 | |||
62 | try: |
|
62 | try: | |
63 | users_group = self.__get_users_group(users_group) |
|
63 | users_group = self.__get_users_group(users_group) | |
64 |
|
64 | |||
65 | for k, v in form_data.items(): |
|
65 | for k, v in form_data.items(): | |
66 | if k == 'users_group_members': |
|
66 | if k == 'users_group_members': | |
67 | users_group.members = [] |
|
67 | users_group.members = [] | |
68 | self.sa.flush() |
|
68 | self.sa.flush() | |
69 | members_list = [] |
|
69 | members_list = [] | |
70 | if v: |
|
70 | if v: | |
71 | v = [v] if isinstance(v, basestring) else v |
|
71 | v = [v] if isinstance(v, basestring) else v | |
72 | for u_id in set(v): |
|
72 | for u_id in set(v): | |
73 | member = UsersGroupMember(users_group.users_group_id, u_id) |
|
73 | member = UsersGroupMember(users_group.users_group_id, u_id) | |
74 | members_list.append(member) |
|
74 | members_list.append(member) | |
75 | setattr(users_group, 'members', members_list) |
|
75 | setattr(users_group, 'members', members_list) | |
76 | setattr(users_group, k, v) |
|
76 | setattr(users_group, k, v) | |
77 |
|
77 | |||
78 | self.sa.add(users_group) |
|
78 | self.sa.add(users_group) | |
79 | except: |
|
79 | except: | |
80 | log.error(traceback.format_exc()) |
|
80 | log.error(traceback.format_exc()) | |
81 | raise |
|
81 | raise | |
82 |
|
82 | |||
83 | def delete(self, users_group): |
|
83 | def delete(self, users_group): | |
84 | try: |
|
84 | try: | |
85 | users_group = self.__get_users_group(users_group) |
|
85 | users_group = self.__get_users_group(users_group) | |
86 |
|
86 | |||
87 | # check if this group is not assigned to repo |
|
87 | # check if this group is not assigned to repo | |
88 | assigned_groups = UsersGroupRepoToPerm.query()\ |
|
88 | assigned_groups = UsersGroupRepoToPerm.query()\ | |
89 | .filter(UsersGroupRepoToPerm.users_group == users_group).all() |
|
89 | .filter(UsersGroupRepoToPerm.users_group == users_group).all() | |
90 |
|
90 | |||
91 | if assigned_groups: |
|
91 | if assigned_groups: | |
92 | raise UsersGroupsAssignedException('RepoGroup assigned to %s' % |
|
92 | raise UsersGroupsAssignedException('RepoGroup assigned to %s' % | |
93 | assigned_groups) |
|
93 | assigned_groups) | |
94 |
|
94 | |||
95 | self.sa.delete(users_group) |
|
95 | self.sa.delete(users_group) | |
96 | except: |
|
96 | except: | |
97 | log.error(traceback.format_exc()) |
|
97 | log.error(traceback.format_exc()) | |
98 | raise |
|
98 | raise | |
99 |
|
99 | |||
100 | def add_user_to_group(self, users_group, user): |
|
100 | def add_user_to_group(self, users_group, user): | |
101 | for m in users_group.members: |
|
101 | for m in users_group.members: | |
102 | u = m.user |
|
102 | u = m.user | |
103 | if u.user_id == user.user_id: |
|
103 | if u.user_id == user.user_id: | |
104 | return m |
|
104 | return m | |
105 |
|
105 | |||
106 | try: |
|
106 | try: | |
107 | users_group_member = UsersGroupMember() |
|
107 | users_group_member = UsersGroupMember() | |
108 | users_group_member.user = user |
|
108 | users_group_member.user = user | |
109 | users_group_member.users_group = users_group |
|
109 | users_group_member.users_group = users_group | |
110 |
|
110 | |||
111 | users_group.members.append(users_group_member) |
|
111 | users_group.members.append(users_group_member) | |
112 | user.group_member.append(users_group_member) |
|
112 | user.group_member.append(users_group_member) | |
113 |
|
113 | |||
114 | self.sa.add(users_group_member) |
|
114 | self.sa.add(users_group_member) | |
115 | return users_group_member |
|
115 | return users_group_member | |
116 | except: |
|
116 | except: | |
117 | log.error(traceback.format_exc()) |
|
117 | log.error(traceback.format_exc()) | |
118 | raise |
|
118 | raise | |
119 |
|
119 | |||
120 | def has_perm(self, users_group, perm): |
|
120 | def has_perm(self, users_group, perm): | |
121 | if not isinstance(perm, Permission): |
|
121 | if not isinstance(perm, Permission): | |
122 | raise Exception('perm needs to be an instance of Permission class') |
|
122 | raise Exception('perm needs to be an instance of Permission class') | |
123 |
|
123 | |||
124 | users_group = self.__get_users_group(users_group) |
|
124 | users_group = self.__get_users_group(users_group) | |
125 |
|
125 | |||
126 | return UsersGroupToPerm.query()\ |
|
126 | return UsersGroupToPerm.query()\ | |
127 | .filter(UsersGroupToPerm.users_group == users_group)\ |
|
127 | .filter(UsersGroupToPerm.users_group == users_group)\ | |
128 | .filter(UsersGroupToPerm.permission == perm).scalar() is not None |
|
128 | .filter(UsersGroupToPerm.permission == perm).scalar() is not None | |
129 |
|
129 | |||
130 | def grant_perm(self, users_group, perm): |
|
130 | def grant_perm(self, users_group, perm): | |
131 | if not isinstance(perm, Permission): |
|
131 | if not isinstance(perm, Permission): | |
132 | raise Exception('perm needs to be an instance of Permission class') |
|
132 | raise Exception('perm needs to be an instance of Permission class') | |
133 |
|
133 | |||
134 | users_group = self.__get_users_group(users_group) |
|
134 | users_group = self.__get_users_group(users_group) | |
135 |
|
135 | |||
136 | new = UsersGroupToPerm() |
|
136 | new = UsersGroupToPerm() | |
137 | new.users_group = users_group |
|
137 | new.users_group = users_group | |
138 | new.permission = perm |
|
138 | new.permission = perm | |
139 | self.sa.add(new) |
|
139 | self.sa.add(new) | |
140 |
|
140 | |||
141 | def revoke_perm(self, users_group, perm): |
|
141 | def revoke_perm(self, users_group, perm): | |
142 | if not isinstance(perm, Permission): |
|
142 | if not isinstance(perm, Permission): | |
143 | raise Exception('perm needs to be an instance of Permission class') |
|
143 | raise Exception('perm needs to be an instance of Permission class') | |
144 |
|
144 | |||
145 | users_group = self.__get_users_group(users_group) |
|
145 | users_group = self.__get_users_group(users_group) | |
146 |
|
146 | |||
147 | obj = UsersGroupToPerm.query()\ |
|
147 | obj = UsersGroupToPerm.query()\ | |
148 | .filter(UsersGroupToPerm.users_group == users_group)\ |
|
148 | .filter(UsersGroupToPerm.users_group == users_group)\ | |
149 |
.filter(UsersGroupToPerm.permission == perm). |
|
149 | .filter(UsersGroupToPerm.permission == perm).scalar() | |
150 | self.sa.delete(obj) |
|
150 | if obj: | |
|
151 | self.sa.delete(obj) |
General Comments 0
You need to be logged in to leave comments.
Login now