Show More
@@ -42,7 +42,7 b'' | |||||
42 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
42 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
43 |
|
43 | |||
44 | import logging |
|
44 | import logging | |
45 |
|
45 | from rhodecode.model.db import User, Repository, Permission | ||
46 | from rhodecode.model import meta |
|
46 | from rhodecode.model import meta | |
47 |
|
47 | |||
48 | log = logging.getLogger(__name__) |
|
48 | log = logging.getLogger(__name__) | |
@@ -96,3 +96,33 b' class BaseModel(object):' | |||||
96 | ) |
|
96 | ) | |
97 | else: |
|
97 | else: | |
98 | return callback(instance) |
|
98 | return callback(instance) | |
|
99 | ||||
|
100 | def _get_user(self, user): | |||
|
101 | """ | |||
|
102 | Helper method to get user by ID, or username fallback | |||
|
103 | ||||
|
104 | :param user: | |||
|
105 | :type user: UserID, username, or User instance | |||
|
106 | """ | |||
|
107 | return self._get_instance(User, user, | |||
|
108 | callback=User.get_by_username) | |||
|
109 | ||||
|
110 | def _get_repo(self, repository): | |||
|
111 | """ | |||
|
112 | Helper method to get repository by ID, or repository name | |||
|
113 | ||||
|
114 | :param repository: | |||
|
115 | :type repository: RepoID, repository name or Repository Instance | |||
|
116 | """ | |||
|
117 | return self._get_instance(Repository, repository, | |||
|
118 | callback=Repository.get_by_repo_name) | |||
|
119 | ||||
|
120 | def _get_perm(self, permission): | |||
|
121 | """ | |||
|
122 | Helper method to get permission by ID, or permission name | |||
|
123 | ||||
|
124 | :param permission: | |||
|
125 | :type permission: PermissionID, permission_name or Permission instance | |||
|
126 | """ | |||
|
127 | return self._get_instance(Permission, permission, | |||
|
128 | callback=Permission.get_by_key) |
@@ -24,13 +24,9 b'' | |||||
24 |
|
24 | |||
25 |
|
25 | |||
26 | import logging |
|
26 | import logging | |
27 | import traceback |
|
|||
28 |
|
27 | |||
29 | from pylons.i18n.translation import _ |
|
|||
30 |
|
||||
31 | from rhodecode.lib.utils2 import safe_unicode |
|
|||
32 | from rhodecode.model import BaseModel |
|
28 | from rhodecode.model import BaseModel | |
33 |
from rhodecode.model.db import ChangesetStatus |
|
29 | from rhodecode.model.db import ChangesetStatus | |
34 |
|
30 | |||
35 | log = logging.getLogger(__name__) |
|
31 | log = logging.getLogger(__name__) | |
36 |
|
32 | |||
@@ -40,13 +36,6 b' class ChangesetStatusModel(BaseModel):' | |||||
40 | def __get_changeset_status(self, changeset_status): |
|
36 | def __get_changeset_status(self, changeset_status): | |
41 | return self._get_instance(ChangesetStatus, changeset_status) |
|
37 | return self._get_instance(ChangesetStatus, changeset_status) | |
42 |
|
38 | |||
43 | def __get_repo(self, repository): |
|
|||
44 | return self._get_instance(Repository, repository, |
|
|||
45 | callback=Repository.get_by_repo_name) |
|
|||
46 |
|
||||
47 | def __get_user(self, user): |
|
|||
48 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
49 |
|
||||
50 | def get_status(self, repo, revision): |
|
39 | def get_status(self, repo, revision): | |
51 | """ |
|
40 | """ | |
52 | Returns status of changeset for given revision and version 0 |
|
41 | Returns status of changeset for given revision and version 0 | |
@@ -58,7 +47,7 b' class ChangesetStatusModel(BaseModel):' | |||||
58 | :param revision: 40char hash |
|
47 | :param revision: 40char hash | |
59 | :type revision: str |
|
48 | :type revision: str | |
60 | """ |
|
49 | """ | |
61 |
repo = self. |
|
50 | repo = self._get_repo(repo) | |
62 |
|
51 | |||
63 | status = ChangesetStatus.query()\ |
|
52 | status = ChangesetStatus.query()\ | |
64 | .filter(ChangesetStatus.repo == repo)\ |
|
53 | .filter(ChangesetStatus.repo == repo)\ | |
@@ -84,7 +73,7 b' class ChangesetStatusModel(BaseModel):' | |||||
84 | :param comment: |
|
73 | :param comment: | |
85 | :type comment: |
|
74 | :type comment: | |
86 | """ |
|
75 | """ | |
87 |
repo = self. |
|
76 | repo = self._get_repo(repo) | |
88 |
|
77 | |||
89 | cur_statuses = ChangesetStatus.query()\ |
|
78 | cur_statuses = ChangesetStatus.query()\ | |
90 | .filter(ChangesetStatus.repo == repo)\ |
|
79 | .filter(ChangesetStatus.repo == repo)\ | |
@@ -95,8 +84,8 b' class ChangesetStatusModel(BaseModel):' | |||||
95 | st.version += 1 |
|
84 | st.version += 1 | |
96 | self.sa.add(st) |
|
85 | self.sa.add(st) | |
97 | new_status = ChangesetStatus() |
|
86 | new_status = ChangesetStatus() | |
98 |
new_status.author = self. |
|
87 | new_status.author = self._get_user(user) | |
99 |
new_status.repo = self. |
|
88 | new_status.repo = self._get_repo(repo) | |
100 | new_status.status = status |
|
89 | new_status.status = status | |
101 | new_status.revision = revision |
|
90 | new_status.revision = revision | |
102 | new_status.comment = comment |
|
91 | new_status.comment = comment |
@@ -42,9 +42,6 b' log = logging.getLogger(__name__)' | |||||
42 |
|
42 | |||
43 | class NotificationModel(BaseModel): |
|
43 | class NotificationModel(BaseModel): | |
44 |
|
44 | |||
45 | def __get_user(self, user): |
|
|||
46 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
47 |
|
||||
48 | def __get_notification(self, notification): |
|
45 | def __get_notification(self, notification): | |
49 | if isinstance(notification, Notification): |
|
46 | if isinstance(notification, Notification): | |
50 | return notification |
|
47 | return notification | |
@@ -77,12 +74,12 b' class NotificationModel(BaseModel):' | |||||
77 | if recipients and not getattr(recipients, '__iter__', False): |
|
74 | if recipients and not getattr(recipients, '__iter__', False): | |
78 | raise Exception('recipients must be a list of iterable') |
|
75 | raise Exception('recipients must be a list of iterable') | |
79 |
|
76 | |||
80 |
created_by_obj = self. |
|
77 | created_by_obj = self._get_user(created_by) | |
81 |
|
78 | |||
82 | if recipients: |
|
79 | if recipients: | |
83 | recipients_objs = [] |
|
80 | recipients_objs = [] | |
84 | for u in recipients: |
|
81 | for u in recipients: | |
85 |
obj = self. |
|
82 | obj = self._get_user(u) | |
86 | if obj: |
|
83 | if obj: | |
87 | recipients_objs.append(obj) |
|
84 | recipients_objs.append(obj) | |
88 | recipients_objs = set(recipients_objs) |
|
85 | recipients_objs = set(recipients_objs) | |
@@ -126,7 +123,7 b' class NotificationModel(BaseModel):' | |||||
126 | # we don't want to remove actual notification just the assignment |
|
123 | # we don't want to remove actual notification just the assignment | |
127 | try: |
|
124 | try: | |
128 | notification = self.__get_notification(notification) |
|
125 | notification = self.__get_notification(notification) | |
129 |
user = self. |
|
126 | user = self._get_user(user) | |
130 | if notification and user: |
|
127 | if notification and user: | |
131 | obj = UserNotification.query()\ |
|
128 | obj = UserNotification.query()\ | |
132 | .filter(UserNotification.user == user)\ |
|
129 | .filter(UserNotification.user == user)\ | |
@@ -140,29 +137,29 b' class NotificationModel(BaseModel):' | |||||
140 | raise |
|
137 | raise | |
141 |
|
138 | |||
142 | def get_for_user(self, user): |
|
139 | def get_for_user(self, user): | |
143 |
user = self. |
|
140 | user = self._get_user(user) | |
144 | return user.notifications |
|
141 | return user.notifications | |
145 |
|
142 | |||
146 | def mark_all_read_for_user(self, user): |
|
143 | def mark_all_read_for_user(self, user): | |
147 |
user = self. |
|
144 | user = self._get_user(user) | |
148 | UserNotification.query()\ |
|
145 | UserNotification.query()\ | |
149 | .filter(UserNotification.read == False)\ |
|
146 | .filter(UserNotification.read == False)\ | |
150 | .update({'read': True}) |
|
147 | .update({'read': True}) | |
151 |
|
148 | |||
152 | def get_unread_cnt_for_user(self, user): |
|
149 | def get_unread_cnt_for_user(self, user): | |
153 |
user = self. |
|
150 | user = self._get_user(user) | |
154 | return UserNotification.query()\ |
|
151 | return UserNotification.query()\ | |
155 | .filter(UserNotification.read == False)\ |
|
152 | .filter(UserNotification.read == False)\ | |
156 | .filter(UserNotification.user == user).count() |
|
153 | .filter(UserNotification.user == user).count() | |
157 |
|
154 | |||
158 | def get_unread_for_user(self, user): |
|
155 | def get_unread_for_user(self, user): | |
159 |
user = self. |
|
156 | user = self._get_user(user) | |
160 | return [x.notification for x in UserNotification.query()\ |
|
157 | return [x.notification for x in UserNotification.query()\ | |
161 | .filter(UserNotification.read == False)\ |
|
158 | .filter(UserNotification.read == False)\ | |
162 | .filter(UserNotification.user == user).all()] |
|
159 | .filter(UserNotification.user == user).all()] | |
163 |
|
160 | |||
164 | def get_user_notification(self, user, notification): |
|
161 | def get_user_notification(self, user, notification): | |
165 |
user = self. |
|
162 | user = self._get_user(user) | |
166 | notification = self.__get_notification(notification) |
|
163 | notification = self.__get_notification(notification) | |
167 |
|
164 | |||
168 | return UserNotification.query()\ |
|
165 | return UserNotification.query()\ |
@@ -48,9 +48,6 b' log = logging.getLogger(__name__)' | |||||
48 |
|
48 | |||
49 | class RepoModel(BaseModel): |
|
49 | class RepoModel(BaseModel): | |
50 |
|
50 | |||
51 | def __get_user(self, user): |
|
|||
52 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
53 |
|
||||
54 | def __get_users_group(self, users_group): |
|
51 | def __get_users_group(self, users_group): | |
55 | return self._get_instance(UsersGroup, users_group, |
|
52 | return self._get_instance(UsersGroup, users_group, | |
56 | callback=UsersGroup.get_by_group_name) |
|
53 | callback=UsersGroup.get_by_group_name) | |
@@ -59,14 +56,6 b' class RepoModel(BaseModel):' | |||||
59 | return self._get_instance(RepoGroup, repos_group, |
|
56 | return self._get_instance(RepoGroup, repos_group, | |
60 | callback=RepoGroup.get_by_group_name) |
|
57 | callback=RepoGroup.get_by_group_name) | |
61 |
|
58 | |||
62 | def __get_repo(self, repository): |
|
|||
63 | return self._get_instance(Repository, repository, |
|
|||
64 | callback=Repository.get_by_repo_name) |
|
|||
65 |
|
||||
66 | def __get_perm(self, permission): |
|
|||
67 | return self._get_instance(Permission, permission, |
|
|||
68 | callback=Permission.get_by_key) |
|
|||
69 |
|
||||
70 | @LazyProperty |
|
59 | @LazyProperty | |
71 | def repos_path(self): |
|
60 | def repos_path(self): | |
72 | """ |
|
61 | """ | |
@@ -86,7 +75,7 b' class RepoModel(BaseModel):' | |||||
86 | return repo.scalar() |
|
75 | return repo.scalar() | |
87 |
|
76 | |||
88 | def get_repo(self, repository): |
|
77 | def get_repo(self, repository): | |
89 |
return self. |
|
78 | return self._get_repo(repository) | |
90 |
|
79 | |||
91 | def get_by_repo_name(self, repo_name, cache=False): |
|
80 | def get_by_repo_name(self, repo_name, cache=False): | |
92 | repo = self.sa.query(Repository)\ |
|
81 | repo = self.sa.query(Repository)\ | |
@@ -311,7 +300,7 b' class RepoModel(BaseModel):' | |||||
311 | run_task(tasks.create_repo_fork, form_data, cur_user) |
|
300 | run_task(tasks.create_repo_fork, form_data, cur_user) | |
312 |
|
301 | |||
313 | def delete(self, repo): |
|
302 | def delete(self, repo): | |
314 |
repo = self. |
|
303 | repo = self._get_repo(repo) | |
315 | try: |
|
304 | try: | |
316 | self.sa.delete(repo) |
|
305 | self.sa.delete(repo) | |
317 | self.__delete_repo(repo) |
|
306 | self.__delete_repo(repo) | |
@@ -328,9 +317,9 b' class RepoModel(BaseModel):' | |||||
328 | :param user: Instance of User, user_id or username |
|
317 | :param user: Instance of User, user_id or username | |
329 | :param perm: Instance of Permission, or permission_name |
|
318 | :param perm: Instance of Permission, or permission_name | |
330 | """ |
|
319 | """ | |
331 |
user = self. |
|
320 | user = self._get_user(user) | |
332 |
repo = self. |
|
321 | repo = self._get_repo(repo) | |
333 |
permission = self. |
|
322 | permission = self._get_perm(perm) | |
334 |
|
323 | |||
335 | # check if we have that permission already |
|
324 | # check if we have that permission already | |
336 | obj = self.sa.query(UserRepoToPerm)\ |
|
325 | obj = self.sa.query(UserRepoToPerm)\ | |
@@ -353,8 +342,8 b' class RepoModel(BaseModel):' | |||||
353 | :param user: Instance of User, user_id or username |
|
342 | :param user: Instance of User, user_id or username | |
354 | """ |
|
343 | """ | |
355 |
|
344 | |||
356 |
user = self. |
|
345 | user = self._get_user(user) | |
357 |
repo = self. |
|
346 | repo = self._get_repo(repo) | |
358 |
|
347 | |||
359 | obj = self.sa.query(UserRepoToPerm)\ |
|
348 | obj = self.sa.query(UserRepoToPerm)\ | |
360 | .filter(UserRepoToPerm.repository == repo)\ |
|
349 | .filter(UserRepoToPerm.repository == repo)\ | |
@@ -372,9 +361,9 b' class RepoModel(BaseModel):' | |||||
372 | or users group name |
|
361 | or users group name | |
373 | :param perm: Instance of Permission, or permission_name |
|
362 | :param perm: Instance of Permission, or permission_name | |
374 | """ |
|
363 | """ | |
375 |
repo = self. |
|
364 | repo = self._get_repo(repo) | |
376 | group_name = self.__get_users_group(group_name) |
|
365 | group_name = self.__get_users_group(group_name) | |
377 |
permission = self. |
|
366 | permission = self._get_perm(perm) | |
378 |
|
367 | |||
379 | # check if we have that permission already |
|
368 | # check if we have that permission already | |
380 | obj = self.sa.query(UsersGroupRepoToPerm)\ |
|
369 | obj = self.sa.query(UsersGroupRepoToPerm)\ | |
@@ -399,7 +388,7 b' class RepoModel(BaseModel):' | |||||
399 | :param group_name: Instance of UserGroup, users_group_id, |
|
388 | :param group_name: Instance of UserGroup, users_group_id, | |
400 | or users group name |
|
389 | or users group name | |
401 | """ |
|
390 | """ | |
402 |
repo = self. |
|
391 | repo = self._get_repo(repo) | |
403 | group_name = self.__get_users_group(group_name) |
|
392 | group_name = self.__get_users_group(group_name) | |
404 |
|
393 | |||
405 | obj = self.sa.query(UsersGroupRepoToPerm)\ |
|
394 | obj = self.sa.query(UsersGroupRepoToPerm)\ |
@@ -26,28 +26,17 b'' | |||||
26 |
|
26 | |||
27 | import logging |
|
27 | import logging | |
28 | from rhodecode.model import BaseModel |
|
28 | from rhodecode.model import BaseModel | |
29 |
from rhodecode.model.db import UserRepoToPerm, UsersGroupRepoToPerm, |
|
29 | from rhodecode.model.db import UserRepoToPerm, UsersGroupRepoToPerm, \ | |
30 | User, Repository |
|
30 | Permission | |
31 |
|
31 | |||
32 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | class RepositoryPermissionModel(BaseModel): |
|
35 | class RepositoryPermissionModel(BaseModel): | |
36 |
|
36 | |||
37 | def __get_user(self, user): |
|
|||
38 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
39 |
|
||||
40 | def __get_repo(self, repository): |
|
|||
41 | return self._get_instance(Repository, repository, |
|
|||
42 | callback=Repository.get_by_repo_name) |
|
|||
43 |
|
||||
44 | def __get_perm(self, permission): |
|
|||
45 | return self._get_instance(Permission, permission, |
|
|||
46 | callback=Permission.get_by_key) |
|
|||
47 |
|
||||
48 | def get_user_permission(self, repository, user): |
|
37 | def get_user_permission(self, repository, user): | |
49 |
repository = self. |
|
38 | repository = self._get_repo(repository) | |
50 |
user = self. |
|
39 | user = self._get_user(user) | |
51 |
|
40 | |||
52 | return UserRepoToPerm.query() \ |
|
41 | return UserRepoToPerm.query() \ | |
53 | .filter(UserRepoToPerm.user == user) \ |
|
42 | .filter(UserRepoToPerm.user == user) \ |
@@ -39,9 +39,6 b' log = logging.getLogger(__name__)' | |||||
39 |
|
39 | |||
40 | class ReposGroupModel(BaseModel): |
|
40 | class ReposGroupModel(BaseModel): | |
41 |
|
41 | |||
42 | def __get_user(self, user): |
|
|||
43 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
44 |
|
||||
45 | def __get_users_group(self, users_group): |
|
42 | def __get_users_group(self, users_group): | |
46 | return self._get_instance(UsersGroup, users_group, |
|
43 | return self._get_instance(UsersGroup, users_group, | |
47 | callback=UsersGroup.get_by_group_name) |
|
44 | callback=UsersGroup.get_by_group_name) | |
@@ -50,10 +47,6 b' class ReposGroupModel(BaseModel):' | |||||
50 | return self._get_instance(RepoGroup, repos_group, |
|
47 | return self._get_instance(RepoGroup, repos_group, | |
51 | callback=RepoGroup.get_by_group_name) |
|
48 | callback=RepoGroup.get_by_group_name) | |
52 |
|
49 | |||
53 | def __get_perm(self, permission): |
|
|||
54 | return self._get_instance(Permission, permission, |
|
|||
55 | callback=Permission.get_by_key) |
|
|||
56 |
|
||||
57 | @LazyProperty |
|
50 | @LazyProperty | |
58 | def repos_path(self): |
|
51 | def repos_path(self): | |
59 | """ |
|
52 | """ | |
@@ -227,8 +220,8 b' class ReposGroupModel(BaseModel):' | |||||
227 | """ |
|
220 | """ | |
228 |
|
221 | |||
229 | repos_group = self.__get_repos_group(repos_group) |
|
222 | repos_group = self.__get_repos_group(repos_group) | |
230 |
user = self. |
|
223 | user = self._get_user(user) | |
231 |
permission = self. |
|
224 | permission = self._get_perm(perm) | |
232 |
|
225 | |||
233 | # check if we have that permission already |
|
226 | # check if we have that permission already | |
234 | obj = self.sa.query(UserRepoGroupToPerm)\ |
|
227 | obj = self.sa.query(UserRepoGroupToPerm)\ | |
@@ -253,7 +246,7 b' class ReposGroupModel(BaseModel):' | |||||
253 | """ |
|
246 | """ | |
254 |
|
247 | |||
255 | repos_group = self.__get_repos_group(repos_group) |
|
248 | repos_group = self.__get_repos_group(repos_group) | |
256 |
user = self. |
|
249 | user = self._get_user(user) | |
257 |
|
250 | |||
258 | obj = self.sa.query(UserRepoGroupToPerm)\ |
|
251 | obj = self.sa.query(UserRepoGroupToPerm)\ | |
259 | .filter(UserRepoGroupToPerm.user == user)\ |
|
252 | .filter(UserRepoGroupToPerm.user == user)\ | |
@@ -274,7 +267,7 b' class ReposGroupModel(BaseModel):' | |||||
274 | """ |
|
267 | """ | |
275 | repos_group = self.__get_repos_group(repos_group) |
|
268 | repos_group = self.__get_repos_group(repos_group) | |
276 | group_name = self.__get_users_group(group_name) |
|
269 | group_name = self.__get_users_group(group_name) | |
277 |
permission = self. |
|
270 | permission = self._get_perm(perm) | |
278 |
|
271 | |||
279 | # check if we have that permission already |
|
272 | # check if we have that permission already | |
280 | obj = self.sa.query(UsersGroupRepoGroupToPerm)\ |
|
273 | obj = self.sa.query(UsersGroupRepoGroupToPerm)\ |
@@ -35,8 +35,8 b' from rhodecode.lib.caching_query import ' | |||||
35 | from rhodecode.model import BaseModel |
|
35 | from rhodecode.model import BaseModel | |
36 | from rhodecode.model.db import User, UserRepoToPerm, Repository, Permission, \ |
|
36 | from rhodecode.model.db import User, UserRepoToPerm, Repository, Permission, \ | |
37 | UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember, \ |
|
37 | UserToPerm, UsersGroupRepoToPerm, UsersGroupToPerm, UsersGroupMember, \ | |
38 | Notification, RepoGroup, UserRepoGroupToPerm, UsersGroup,\ |
|
38 | Notification, RepoGroup, UserRepoGroupToPerm, UsersGroupRepoGroupToPerm, \ | |
39 | UsersGroupRepoGroupToPerm, UserEmailMap |
|
39 | UserEmailMap | |
40 | from rhodecode.lib.exceptions import DefaultUserException, \ |
|
40 | from rhodecode.lib.exceptions import DefaultUserException, \ | |
41 | UserOwnsReposException |
|
41 | UserOwnsReposException | |
42 |
|
42 | |||
@@ -61,13 +61,6 b' PERM_WEIGHTS = {' | |||||
61 |
|
61 | |||
62 | class UserModel(BaseModel): |
|
62 | class UserModel(BaseModel): | |
63 |
|
63 | |||
64 | def __get_user(self, user): |
|
|||
65 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
66 |
|
||||
67 | def __get_perm(self, permission): |
|
|||
68 | return self._get_instance(Permission, permission, |
|
|||
69 | callback=Permission.get_by_key) |
|
|||
70 |
|
||||
71 | def get(self, user_id, cache=False): |
|
64 | def get(self, user_id, cache=False): | |
72 | user = self.sa.query(User) |
|
65 | user = self.sa.query(User) | |
73 | if cache: |
|
66 | if cache: | |
@@ -76,7 +69,7 b' class UserModel(BaseModel):' | |||||
76 | return user.get(user_id) |
|
69 | return user.get(user_id) | |
77 |
|
70 | |||
78 | def get_user(self, user): |
|
71 | def get_user(self, user): | |
79 |
return self. |
|
72 | return self._get_user(user) | |
80 |
|
73 | |||
81 | def get_by_username(self, username, cache=False, case_insensitive=False): |
|
74 | def get_by_username(self, username, cache=False, case_insensitive=False): | |
82 |
|
75 | |||
@@ -292,7 +285,7 b' class UserModel(BaseModel):' | |||||
292 | raise |
|
285 | raise | |
293 |
|
286 | |||
294 | def delete(self, user): |
|
287 | def delete(self, user): | |
295 |
user = self. |
|
288 | user = self._get_user(user) | |
296 |
|
289 | |||
297 | try: |
|
290 | try: | |
298 | if user.username == 'default': |
|
291 | if user.username == 'default': | |
@@ -545,7 +538,7 b' class UserModel(BaseModel):' | |||||
545 | raise Exception('perm needs to be an instance of Permission class ' |
|
538 | raise Exception('perm needs to be an instance of Permission class ' | |
546 | 'got %s instead' % type(perm)) |
|
539 | 'got %s instead' % type(perm)) | |
547 |
|
540 | |||
548 |
user = self. |
|
541 | user = self._get_user(user) | |
549 |
|
542 | |||
550 | return UserToPerm.query().filter(UserToPerm.user == user)\ |
|
543 | return UserToPerm.query().filter(UserToPerm.user == user)\ | |
551 | .filter(UserToPerm.permission == perm).scalar() is not None |
|
544 | .filter(UserToPerm.permission == perm).scalar() is not None | |
@@ -557,8 +550,8 b' class UserModel(BaseModel):' | |||||
557 | :param user: |
|
550 | :param user: | |
558 | :param perm: |
|
551 | :param perm: | |
559 | """ |
|
552 | """ | |
560 |
user = self. |
|
553 | user = self._get_user(user) | |
561 |
perm = self. |
|
554 | perm = self._get_perm(perm) | |
562 | # if this permission is already granted skip it |
|
555 | # if this permission is already granted skip it | |
563 | _perm = UserToPerm.query()\ |
|
556 | _perm = UserToPerm.query()\ | |
564 | .filter(UserToPerm.user == user)\ |
|
557 | .filter(UserToPerm.user == user)\ | |
@@ -578,8 +571,8 b' class UserModel(BaseModel):' | |||||
578 | :param user: |
|
571 | :param user: | |
579 | :param perm: |
|
572 | :param perm: | |
580 | """ |
|
573 | """ | |
581 |
user = self. |
|
574 | user = self._get_user(user) | |
582 |
perm = self. |
|
575 | perm = self._get_perm(perm) | |
583 |
|
576 | |||
584 | obj = UserToPerm.query()\ |
|
577 | obj = UserToPerm.query()\ | |
585 | .filter(UserToPerm.user == user)\ |
|
578 | .filter(UserToPerm.user == user)\ | |
@@ -595,7 +588,7 b' class UserModel(BaseModel):' | |||||
595 | :param user: |
|
588 | :param user: | |
596 | :param email: |
|
589 | :param email: | |
597 | """ |
|
590 | """ | |
598 |
user = self. |
|
591 | user = self._get_user(user) | |
599 | obj = UserEmailMap() |
|
592 | obj = UserEmailMap() | |
600 | obj.user = user |
|
593 | obj.user = user | |
601 | obj.email = email |
|
594 | obj.email = email | |
@@ -609,7 +602,7 b' class UserModel(BaseModel):' | |||||
609 | :param user: |
|
602 | :param user: | |
610 | :param email_id: |
|
603 | :param email_id: | |
611 | """ |
|
604 | """ | |
612 |
user = self. |
|
605 | user = self._get_user(user) | |
613 | obj = UserEmailMap.query().get(email_id) |
|
606 | obj = UserEmailMap.query().get(email_id) | |
614 | if obj: |
|
607 | if obj: | |
615 | self.sa.delete(obj) No newline at end of file |
|
608 | self.sa.delete(obj) |
@@ -37,17 +37,10 b' log = logging.getLogger(__name__)' | |||||
37 |
|
37 | |||
38 | class UsersGroupModel(BaseModel): |
|
38 | class UsersGroupModel(BaseModel): | |
39 |
|
39 | |||
40 | def __get_user(self, user): |
|
|||
41 | return self._get_instance(User, user, callback=User.get_by_username) |
|
|||
42 |
|
||||
43 | def __get_users_group(self, users_group): |
|
40 | def __get_users_group(self, users_group): | |
44 | return self._get_instance(UsersGroup, users_group, |
|
41 | return self._get_instance(UsersGroup, users_group, | |
45 | callback=UsersGroup.get_by_group_name) |
|
42 | callback=UsersGroup.get_by_group_name) | |
46 |
|
43 | |||
47 | def __get_perm(self, permission): |
|
|||
48 | return self._get_instance(Permission, permission, |
|
|||
49 | callback=Permission.get_by_key) |
|
|||
50 |
|
||||
51 | def get(self, users_group_id, cache=False): |
|
44 | def get(self, users_group_id, cache=False): | |
52 | return UsersGroup.get(users_group_id) |
|
45 | return UsersGroup.get(users_group_id) | |
53 |
|
46 | |||
@@ -115,7 +108,7 b' class UsersGroupModel(BaseModel):' | |||||
115 |
|
108 | |||
116 | def add_user_to_group(self, users_group, user): |
|
109 | def add_user_to_group(self, users_group, user): | |
117 | users_group = self.__get_users_group(users_group) |
|
110 | users_group = self.__get_users_group(users_group) | |
118 |
user = self. |
|
111 | user = self._get_user(user) | |
119 |
|
112 | |||
120 | for m in users_group.members: |
|
113 | for m in users_group.members: | |
121 | u = m.user |
|
114 | u = m.user | |
@@ -138,7 +131,7 b' class UsersGroupModel(BaseModel):' | |||||
138 |
|
131 | |||
139 | def remove_user_from_group(self, users_group, user): |
|
132 | def remove_user_from_group(self, users_group, user): | |
140 | users_group = self.__get_users_group(users_group) |
|
133 | users_group = self.__get_users_group(users_group) | |
141 |
user = self. |
|
134 | user = self._get_user(user) | |
142 |
|
135 | |||
143 | users_group_member = None |
|
136 | users_group_member = None | |
144 | for m in users_group.members: |
|
137 | for m in users_group.members: | |
@@ -160,7 +153,7 b' class UsersGroupModel(BaseModel):' | |||||
160 |
|
153 | |||
161 | def has_perm(self, users_group, perm): |
|
154 | def has_perm(self, users_group, perm): | |
162 | users_group = self.__get_users_group(users_group) |
|
155 | users_group = self.__get_users_group(users_group) | |
163 |
perm = self. |
|
156 | perm = self._get_perm(perm) | |
164 |
|
157 | |||
165 | return UsersGroupToPerm.query()\ |
|
158 | return UsersGroupToPerm.query()\ | |
166 | .filter(UsersGroupToPerm.users_group == users_group)\ |
|
159 | .filter(UsersGroupToPerm.users_group == users_group)\ | |
@@ -187,7 +180,7 b' class UsersGroupModel(BaseModel):' | |||||
187 |
|
180 | |||
188 | def revoke_perm(self, users_group, perm): |
|
181 | def revoke_perm(self, users_group, perm): | |
189 | users_group = self.__get_users_group(users_group) |
|
182 | users_group = self.__get_users_group(users_group) | |
190 |
perm = self. |
|
183 | perm = self._get_perm(perm) | |
191 |
|
184 | |||
192 | obj = UsersGroupToPerm.query()\ |
|
185 | obj = UsersGroupToPerm.query()\ | |
193 | .filter(UsersGroupToPerm.users_group == users_group)\ |
|
186 | .filter(UsersGroupToPerm.users_group == users_group)\ |
General Comments 0
You need to be logged in to leave comments.
Login now