##// END OF EJS Templates
repo group: always update fullpath group_name when changing parent...
Mads Kiilerich -
r8732:03d51a2a stable
parent child Browse files
Show More
@@ -1,531 +1,532 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # This program is free software: you can redistribute it and/or modify
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
5 # (at your option) any later version.
6 #
6 #
7 # This program is distributed in the hope that it will be useful,
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
10 # GNU General Public License for more details.
11 #
11 #
12 # You should have received a copy of the GNU General Public License
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 """
14 """
15 kallithea.model.repo_group
15 kallithea.model.repo_group
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~
17
17
18 repo group model for Kallithea
18 repo group model for Kallithea
19
19
20 This file was forked by the Kallithea project in July 2014.
20 This file was forked by the Kallithea project in July 2014.
21 Original author and date, and relevant copyright and licensing information is below:
21 Original author and date, and relevant copyright and licensing information is below:
22 :created_on: Jan 25, 2011
22 :created_on: Jan 25, 2011
23 :author: marcink
23 :author: marcink
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
24 :copyright: (c) 2013 RhodeCode GmbH, and others.
25 :license: GPLv3, see LICENSE.md for more details.
25 :license: GPLv3, see LICENSE.md for more details.
26 """
26 """
27
27
28
28
29 import datetime
29 import datetime
30 import logging
30 import logging
31 import os
31 import os
32 import shutil
32 import shutil
33 import traceback
33 import traceback
34
34
35 import kallithea.lib.utils2
35 import kallithea.lib.utils2
36 from kallithea.lib.utils2 import LazyProperty
36 from kallithea.lib.utils2 import LazyProperty
37 from kallithea.model import db, meta, repo
37 from kallithea.model import db, meta, repo
38
38
39
39
40 log = logging.getLogger(__name__)
40 log = logging.getLogger(__name__)
41
41
42
42
43 class RepoGroupModel(object):
43 class RepoGroupModel(object):
44
44
45 @LazyProperty
45 @LazyProperty
46 def repos_path(self):
46 def repos_path(self):
47 """
47 """
48 Gets the repositories root path from database
48 Gets the repositories root path from database
49 """
49 """
50
50
51 q = db.Ui.get_by_key('paths', '/')
51 q = db.Ui.get_by_key('paths', '/')
52 return q.ui_value
52 return q.ui_value
53
53
54 def _create_default_perms(self, new_group):
54 def _create_default_perms(self, new_group):
55 # create default permission
55 # create default permission
56 default_perm = 'group.read'
56 default_perm = 'group.read'
57 def_user = db.User.get_default_user()
57 def_user = db.User.get_default_user()
58 for p in def_user.user_perms:
58 for p in def_user.user_perms:
59 if p.permission.permission_name.startswith('group.'):
59 if p.permission.permission_name.startswith('group.'):
60 default_perm = p.permission.permission_name
60 default_perm = p.permission.permission_name
61 break
61 break
62
62
63 repo_group_to_perm = db.UserRepoGroupToPerm()
63 repo_group_to_perm = db.UserRepoGroupToPerm()
64 repo_group_to_perm.permission = db.Permission.get_by_key(default_perm)
64 repo_group_to_perm.permission = db.Permission.get_by_key(default_perm)
65
65
66 repo_group_to_perm.group = new_group
66 repo_group_to_perm.group = new_group
67 repo_group_to_perm.user_id = def_user.user_id
67 repo_group_to_perm.user_id = def_user.user_id
68 meta.Session().add(repo_group_to_perm)
68 meta.Session().add(repo_group_to_perm)
69 return repo_group_to_perm
69 return repo_group_to_perm
70
70
71 def _create_group(self, group_name):
71 def _create_group(self, group_name):
72 """
72 """
73 makes repository group on filesystem
73 makes repository group on filesystem
74
74
75 :param repo_name:
75 :param repo_name:
76 :param parent_id:
76 :param parent_id:
77 """
77 """
78
78
79 create_path = os.path.join(self.repos_path, group_name)
79 create_path = os.path.join(self.repos_path, group_name)
80 log.debug('creating new group in %s', create_path)
80 log.debug('creating new group in %s', create_path)
81
81
82 if os.path.isdir(create_path):
82 if os.path.isdir(create_path):
83 raise Exception('That directory already exists !')
83 raise Exception('That directory already exists !')
84
84
85 os.makedirs(create_path)
85 os.makedirs(create_path)
86 log.debug('Created group in %s', create_path)
86 log.debug('Created group in %s', create_path)
87
87
88 def _rename_group(self, old, new):
88 def _rename_group(self, old, new):
89 """
89 """
90 Renames a group on filesystem
90 Renames a group on filesystem
91
91
92 :param group_name:
92 :param group_name:
93 """
93 """
94
94
95 if old == new:
95 if old == new:
96 log.debug('skipping group rename')
96 log.debug('skipping group rename')
97 return
97 return
98
98
99 log.debug('renaming repository group from %s to %s', old, new)
99 log.debug('renaming repository group from %s to %s', old, new)
100
100
101 old_path = os.path.join(self.repos_path, old)
101 old_path = os.path.join(self.repos_path, old)
102 new_path = os.path.join(self.repos_path, new)
102 new_path = os.path.join(self.repos_path, new)
103
103
104 log.debug('renaming repos paths from %s to %s', old_path, new_path)
104 log.debug('renaming repos paths from %s to %s', old_path, new_path)
105
105
106 if os.path.isdir(new_path):
106 if os.path.isdir(new_path):
107 raise Exception('Was trying to rename to already '
107 raise Exception('Was trying to rename to already '
108 'existing dir %s' % new_path)
108 'existing dir %s' % new_path)
109 shutil.move(old_path, new_path)
109 shutil.move(old_path, new_path)
110
110
111 def _delete_group(self, group, force_delete=False):
111 def _delete_group(self, group, force_delete=False):
112 """
112 """
113 Deletes a group from a filesystem
113 Deletes a group from a filesystem
114
114
115 :param group: instance of group from database
115 :param group: instance of group from database
116 :param force_delete: use shutil rmtree to remove all objects
116 :param force_delete: use shutil rmtree to remove all objects
117 """
117 """
118 paths = group.full_path.split(kallithea.URL_SEP)
118 paths = group.full_path.split(kallithea.URL_SEP)
119 paths = os.sep.join(paths)
119 paths = os.sep.join(paths)
120
120
121 rm_path = os.path.join(self.repos_path, paths)
121 rm_path = os.path.join(self.repos_path, paths)
122 log.info("Removing group %s", rm_path)
122 log.info("Removing group %s", rm_path)
123 # delete only if that path really exists
123 # delete only if that path really exists
124 if os.path.isdir(rm_path):
124 if os.path.isdir(rm_path):
125 if force_delete:
125 if force_delete:
126 shutil.rmtree(rm_path)
126 shutil.rmtree(rm_path)
127 else:
127 else:
128 # archive that group
128 # archive that group
129 _now = datetime.datetime.now()
129 _now = datetime.datetime.now()
130 _ms = str(_now.microsecond).rjust(6, '0')
130 _ms = str(_now.microsecond).rjust(6, '0')
131 _d = 'rm__%s_GROUP_%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms),
131 _d = 'rm__%s_GROUP_%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms),
132 group.name)
132 group.name)
133 shutil.move(rm_path, os.path.join(self.repos_path, _d))
133 shutil.move(rm_path, os.path.join(self.repos_path, _d))
134
134
135 def create(self, group_name, group_description, owner, parent=None,
135 def create(self, group_name, group_description, owner, parent=None,
136 just_db=False, copy_permissions=False):
136 just_db=False, copy_permissions=False):
137 try:
137 try:
138 if kallithea.lib.utils2.repo_name_slug(group_name) != group_name:
138 if kallithea.lib.utils2.repo_name_slug(group_name) != group_name:
139 raise Exception('invalid repo group name %s' % group_name)
139 raise Exception('invalid repo group name %s' % group_name)
140
140
141 owner = db.User.guess_instance(owner)
141 owner = db.User.guess_instance(owner)
142 parent_group = db.RepoGroup.guess_instance(parent)
142 parent_group = db.RepoGroup.guess_instance(parent)
143 new_repo_group = db.RepoGroup()
143 new_repo_group = db.RepoGroup()
144 new_repo_group.owner = owner
144 new_repo_group.owner = owner
145 new_repo_group.group_description = group_description or group_name
145 new_repo_group.group_description = group_description or group_name
146 new_repo_group.parent_group = parent_group
146 new_repo_group.parent_group = parent_group
147 new_repo_group.group_name = new_repo_group.get_new_name(group_name)
147 new_repo_group.group_name = new_repo_group.get_new_name(group_name)
148
148
149 meta.Session().add(new_repo_group)
149 meta.Session().add(new_repo_group)
150
150
151 # create an ADMIN permission for owner except if we're super admin,
151 # create an ADMIN permission for owner except if we're super admin,
152 # later owner should go into the owner field of groups
152 # later owner should go into the owner field of groups
153 if not owner.is_admin:
153 if not owner.is_admin:
154 self.grant_user_permission(repo_group=new_repo_group,
154 self.grant_user_permission(repo_group=new_repo_group,
155 user=owner, perm='group.admin')
155 user=owner, perm='group.admin')
156
156
157 if parent_group and copy_permissions:
157 if parent_group and copy_permissions:
158 # copy permissions from parent
158 # copy permissions from parent
159 user_perms = db.UserRepoGroupToPerm.query() \
159 user_perms = db.UserRepoGroupToPerm.query() \
160 .filter(db.UserRepoGroupToPerm.group == parent_group).all()
160 .filter(db.UserRepoGroupToPerm.group == parent_group).all()
161
161
162 group_perms = db.UserGroupRepoGroupToPerm.query() \
162 group_perms = db.UserGroupRepoGroupToPerm.query() \
163 .filter(db.UserGroupRepoGroupToPerm.group == parent_group).all()
163 .filter(db.UserGroupRepoGroupToPerm.group == parent_group).all()
164
164
165 for perm in user_perms:
165 for perm in user_perms:
166 # don't copy over the permission for user who is creating
166 # don't copy over the permission for user who is creating
167 # this group, if he is not super admin he get's admin
167 # this group, if he is not super admin he get's admin
168 # permission set above
168 # permission set above
169 if perm.user != owner or owner.is_admin:
169 if perm.user != owner or owner.is_admin:
170 db.UserRepoGroupToPerm.create(perm.user, new_repo_group, perm.permission)
170 db.UserRepoGroupToPerm.create(perm.user, new_repo_group, perm.permission)
171
171
172 for perm in group_perms:
172 for perm in group_perms:
173 db.UserGroupRepoGroupToPerm.create(perm.users_group, new_repo_group, perm.permission)
173 db.UserGroupRepoGroupToPerm.create(perm.users_group, new_repo_group, perm.permission)
174 else:
174 else:
175 self._create_default_perms(new_repo_group)
175 self._create_default_perms(new_repo_group)
176
176
177 if not just_db:
177 if not just_db:
178 # we need to flush here, in order to check if database won't
178 # we need to flush here, in order to check if database won't
179 # throw any exceptions, create filesystem dirs at the very end
179 # throw any exceptions, create filesystem dirs at the very end
180 meta.Session().flush()
180 meta.Session().flush()
181 self._create_group(new_repo_group.group_name)
181 self._create_group(new_repo_group.group_name)
182
182
183 return new_repo_group
183 return new_repo_group
184 except Exception:
184 except Exception:
185 log.error(traceback.format_exc())
185 log.error(traceback.format_exc())
186 raise
186 raise
187
187
188 def _update_permissions(self, repo_group, perms_new=None,
188 def _update_permissions(self, repo_group, perms_new=None,
189 perms_updates=None, recursive=None,
189 perms_updates=None, recursive=None,
190 check_perms=True):
190 check_perms=True):
191 from kallithea.lib.auth import HasUserGroupPermissionLevel
191 from kallithea.lib.auth import HasUserGroupPermissionLevel
192
192
193 if not perms_new:
193 if not perms_new:
194 perms_new = []
194 perms_new = []
195 if not perms_updates:
195 if not perms_updates:
196 perms_updates = []
196 perms_updates = []
197
197
198 def _set_perm_user(obj, user, perm):
198 def _set_perm_user(obj, user, perm):
199 if isinstance(obj, db.RepoGroup):
199 if isinstance(obj, db.RepoGroup):
200 self.grant_user_permission(repo_group=obj, user=user, perm=perm)
200 self.grant_user_permission(repo_group=obj, user=user, perm=perm)
201 elif isinstance(obj, db.Repository):
201 elif isinstance(obj, db.Repository):
202 user = db.User.guess_instance(user)
202 user = db.User.guess_instance(user)
203
203
204 # private repos will not allow to change the default permissions
204 # private repos will not allow to change the default permissions
205 # using recursive mode
205 # using recursive mode
206 if obj.private and user.is_default_user:
206 if obj.private and user.is_default_user:
207 return
207 return
208
208
209 # we set group permission but we have to switch to repo
209 # we set group permission but we have to switch to repo
210 # permission
210 # permission
211 perm = perm.replace('group.', 'repository.')
211 perm = perm.replace('group.', 'repository.')
212 repo.RepoModel().grant_user_permission(
212 repo.RepoModel().grant_user_permission(
213 repo=obj, user=user, perm=perm
213 repo=obj, user=user, perm=perm
214 )
214 )
215
215
216 def _set_perm_group(obj, users_group, perm):
216 def _set_perm_group(obj, users_group, perm):
217 if isinstance(obj, db.RepoGroup):
217 if isinstance(obj, db.RepoGroup):
218 self.grant_user_group_permission(repo_group=obj,
218 self.grant_user_group_permission(repo_group=obj,
219 group_name=users_group,
219 group_name=users_group,
220 perm=perm)
220 perm=perm)
221 elif isinstance(obj, db.Repository):
221 elif isinstance(obj, db.Repository):
222 # we set group permission but we have to switch to repo
222 # we set group permission but we have to switch to repo
223 # permission
223 # permission
224 perm = perm.replace('group.', 'repository.')
224 perm = perm.replace('group.', 'repository.')
225 repo.RepoModel().grant_user_group_permission(
225 repo.RepoModel().grant_user_group_permission(
226 repo=obj, group_name=users_group, perm=perm
226 repo=obj, group_name=users_group, perm=perm
227 )
227 )
228
228
229 # start updates
229 # start updates
230 updates = []
230 updates = []
231 log.debug('Now updating permissions for %s in recursive mode:%s',
231 log.debug('Now updating permissions for %s in recursive mode:%s',
232 repo_group, recursive)
232 repo_group, recursive)
233
233
234 for obj in repo_group.recursive_groups_and_repos():
234 for obj in repo_group.recursive_groups_and_repos():
235 # iterated obj is an instance of a repos group or repository in
235 # iterated obj is an instance of a repos group or repository in
236 # that group, recursive option can be: none, repos, groups, all
236 # that group, recursive option can be: none, repos, groups, all
237 if recursive == 'all':
237 if recursive == 'all':
238 pass
238 pass
239 elif recursive == 'repos':
239 elif recursive == 'repos':
240 # skip groups, other than this one
240 # skip groups, other than this one
241 if isinstance(obj, db.RepoGroup) and not obj == repo_group:
241 if isinstance(obj, db.RepoGroup) and not obj == repo_group:
242 continue
242 continue
243 elif recursive == 'groups':
243 elif recursive == 'groups':
244 # skip repos
244 # skip repos
245 if isinstance(obj, db.Repository):
245 if isinstance(obj, db.Repository):
246 continue
246 continue
247 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
247 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
248 obj = repo_group
248 obj = repo_group
249 # also we do a break at the end of this loop.
249 # also we do a break at the end of this loop.
250
250
251 # update permissions
251 # update permissions
252 for member, perm, member_type in perms_updates:
252 for member, perm, member_type in perms_updates:
253 ## set for user
253 ## set for user
254 if member_type == 'user':
254 if member_type == 'user':
255 # this updates also current one if found
255 # this updates also current one if found
256 _set_perm_user(obj, user=member, perm=perm)
256 _set_perm_user(obj, user=member, perm=perm)
257 ## set for user group
257 ## set for user group
258 else:
258 else:
259 # check if we have permissions to alter this usergroup's access
259 # check if we have permissions to alter this usergroup's access
260 if not check_perms or HasUserGroupPermissionLevel('read')(member):
260 if not check_perms or HasUserGroupPermissionLevel('read')(member):
261 _set_perm_group(obj, users_group=member, perm=perm)
261 _set_perm_group(obj, users_group=member, perm=perm)
262 # set new permissions
262 # set new permissions
263 for member, perm, member_type in perms_new:
263 for member, perm, member_type in perms_new:
264 if member_type == 'user':
264 if member_type == 'user':
265 _set_perm_user(obj, user=member, perm=perm)
265 _set_perm_user(obj, user=member, perm=perm)
266 else:
266 else:
267 # check if we have permissions to alter this usergroup's access
267 # check if we have permissions to alter this usergroup's access
268 if not check_perms or HasUserGroupPermissionLevel('read')(member):
268 if not check_perms or HasUserGroupPermissionLevel('read')(member):
269 _set_perm_group(obj, users_group=member, perm=perm)
269 _set_perm_group(obj, users_group=member, perm=perm)
270 updates.append(obj)
270 updates.append(obj)
271 # if it's not recursive call for all,repos,groups
271 # if it's not recursive call for all,repos,groups
272 # break the loop and don't proceed with other changes
272 # break the loop and don't proceed with other changes
273 if recursive not in ['all', 'repos', 'groups']:
273 if recursive not in ['all', 'repos', 'groups']:
274 break
274 break
275
275
276 return updates
276 return updates
277
277
278 def update(self, repo_group, repo_group_args):
278 def update(self, repo_group, repo_group_args):
279 try:
279 try:
280 repo_group = db.RepoGroup.guess_instance(repo_group)
280 repo_group = db.RepoGroup.guess_instance(repo_group)
281 old_path = repo_group.full_path
281 old_path = repo_group.full_path
282
282
283 # change properties
283 # change properties
284 if 'group_description' in repo_group_args:
284 if 'group_description' in repo_group_args:
285 repo_group.group_description = repo_group_args['group_description']
285 repo_group.group_description = repo_group_args['group_description']
286 if 'parent_group_id' in repo_group_args:
286 if 'parent_group_id' in repo_group_args:
287 assert repo_group_args['parent_group_id'] != '-1', repo_group_args # RepoGroupForm should have converted to None
287 assert repo_group_args['parent_group_id'] != '-1', repo_group_args # RepoGroupForm should have converted to None
288 repo_group.parent_group = db.RepoGroup.get(repo_group_args['parent_group_id'])
288 repo_group.parent_group = db.RepoGroup.get(repo_group_args['parent_group_id'])
289 repo_group.group_name = repo_group.get_new_name(repo_group.name)
289 if 'group_name' in repo_group_args:
290 if 'group_name' in repo_group_args:
290 group_name = repo_group_args['group_name']
291 group_name = repo_group_args['group_name']
291 if kallithea.lib.utils2.repo_name_slug(group_name) != group_name:
292 if kallithea.lib.utils2.repo_name_slug(group_name) != group_name:
292 raise Exception('invalid repo group name %s' % group_name)
293 raise Exception('invalid repo group name %s' % group_name)
293 repo_group.group_name = repo_group.get_new_name(group_name)
294 repo_group.group_name = repo_group.get_new_name(group_name)
294 new_path = repo_group.full_path
295 new_path = repo_group.full_path
295 meta.Session().add(repo_group)
296 meta.Session().add(repo_group)
296
297
297 # iterate over all members of this groups and do fixes
298 # iterate over all members of this groups and do fixes
298 # if obj is a repoGroup also fix the name of the group according
299 # if obj is a repoGroup also fix the name of the group according
299 # to the parent
300 # to the parent
300 # if obj is a Repo fix it's name
301 # if obj is a Repo fix it's name
301 # this can be potentially heavy operation
302 # this can be potentially heavy operation
302 for obj in repo_group.recursive_groups_and_repos():
303 for obj in repo_group.recursive_groups_and_repos():
303 # set the value from it's parent
304 # set the value from it's parent
304 if isinstance(obj, db.RepoGroup):
305 if isinstance(obj, db.RepoGroup):
305 new_name = obj.get_new_name(obj.name)
306 new_name = obj.get_new_name(obj.name)
306 log.debug('Fixing group %s to new name %s'
307 log.debug('Fixing group %s to new name %s'
307 % (obj.group_name, new_name))
308 % (obj.group_name, new_name))
308 obj.group_name = new_name
309 obj.group_name = new_name
309 elif isinstance(obj, db.Repository):
310 elif isinstance(obj, db.Repository):
310 # we need to get all repositories from this new group and
311 # we need to get all repositories from this new group and
311 # rename them accordingly to new group path
312 # rename them accordingly to new group path
312 new_name = obj.get_new_name(obj.just_name)
313 new_name = obj.get_new_name(obj.just_name)
313 log.debug('Fixing repo %s to new name %s'
314 log.debug('Fixing repo %s to new name %s'
314 % (obj.repo_name, new_name))
315 % (obj.repo_name, new_name))
315 obj.repo_name = new_name
316 obj.repo_name = new_name
316
317
317 self._rename_group(old_path, new_path)
318 self._rename_group(old_path, new_path)
318
319
319 return repo_group
320 return repo_group
320 except Exception:
321 except Exception:
321 log.error(traceback.format_exc())
322 log.error(traceback.format_exc())
322 raise
323 raise
323
324
324 def delete(self, repo_group, force_delete=False):
325 def delete(self, repo_group, force_delete=False):
325 repo_group = db.RepoGroup.guess_instance(repo_group)
326 repo_group = db.RepoGroup.guess_instance(repo_group)
326 try:
327 try:
327 meta.Session().delete(repo_group)
328 meta.Session().delete(repo_group)
328 self._delete_group(repo_group, force_delete)
329 self._delete_group(repo_group, force_delete)
329 except Exception:
330 except Exception:
330 log.error('Error removing repo_group %s', repo_group)
331 log.error('Error removing repo_group %s', repo_group)
331 raise
332 raise
332
333
333 def add_permission(self, repo_group, obj, obj_type, perm, recursive):
334 def add_permission(self, repo_group, obj, obj_type, perm, recursive):
334 repo_group = db.RepoGroup.guess_instance(repo_group)
335 repo_group = db.RepoGroup.guess_instance(repo_group)
335 perm = db.Permission.guess_instance(perm)
336 perm = db.Permission.guess_instance(perm)
336
337
337 for el in repo_group.recursive_groups_and_repos():
338 for el in repo_group.recursive_groups_and_repos():
338 # iterated obj is an instance of a repos group or repository in
339 # iterated obj is an instance of a repos group or repository in
339 # that group, recursive option can be: none, repos, groups, all
340 # that group, recursive option can be: none, repos, groups, all
340 if recursive == 'all':
341 if recursive == 'all':
341 pass
342 pass
342 elif recursive == 'repos':
343 elif recursive == 'repos':
343 # skip groups, other than this one
344 # skip groups, other than this one
344 if isinstance(el, db.RepoGroup) and not el == repo_group:
345 if isinstance(el, db.RepoGroup) and not el == repo_group:
345 continue
346 continue
346 elif recursive == 'groups':
347 elif recursive == 'groups':
347 # skip repos
348 # skip repos
348 if isinstance(el, db.Repository):
349 if isinstance(el, db.Repository):
349 continue
350 continue
350 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
351 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
351 el = repo_group
352 el = repo_group
352 # also we do a break at the end of this loop.
353 # also we do a break at the end of this loop.
353
354
354 if isinstance(el, db.RepoGroup):
355 if isinstance(el, db.RepoGroup):
355 if obj_type == 'user':
356 if obj_type == 'user':
356 RepoGroupModel().grant_user_permission(el, user=obj, perm=perm)
357 RepoGroupModel().grant_user_permission(el, user=obj, perm=perm)
357 elif obj_type == 'user_group':
358 elif obj_type == 'user_group':
358 RepoGroupModel().grant_user_group_permission(el, group_name=obj, perm=perm)
359 RepoGroupModel().grant_user_group_permission(el, group_name=obj, perm=perm)
359 else:
360 else:
360 raise Exception('undefined object type %s' % obj_type)
361 raise Exception('undefined object type %s' % obj_type)
361 elif isinstance(el, db.Repository):
362 elif isinstance(el, db.Repository):
362 # for repos we need to hotfix the name of permission
363 # for repos we need to hotfix the name of permission
363 _perm = perm.permission_name.replace('group.', 'repository.')
364 _perm = perm.permission_name.replace('group.', 'repository.')
364 if obj_type == 'user':
365 if obj_type == 'user':
365 repo.RepoModel().grant_user_permission(el, user=obj, perm=_perm)
366 repo.RepoModel().grant_user_permission(el, user=obj, perm=_perm)
366 elif obj_type == 'user_group':
367 elif obj_type == 'user_group':
367 repo.RepoModel().grant_user_group_permission(el, group_name=obj, perm=_perm)
368 repo.RepoModel().grant_user_group_permission(el, group_name=obj, perm=_perm)
368 else:
369 else:
369 raise Exception('undefined object type %s' % obj_type)
370 raise Exception('undefined object type %s' % obj_type)
370 else:
371 else:
371 raise Exception('el should be instance of Repository or '
372 raise Exception('el should be instance of Repository or '
372 'RepositoryGroup got %s instead' % type(el))
373 'RepositoryGroup got %s instead' % type(el))
373
374
374 # if it's not recursive call for all,repos,groups
375 # if it's not recursive call for all,repos,groups
375 # break the loop and don't proceed with other changes
376 # break the loop and don't proceed with other changes
376 if recursive not in ['all', 'repos', 'groups']:
377 if recursive not in ['all', 'repos', 'groups']:
377 break
378 break
378
379
379 def delete_permission(self, repo_group, obj, obj_type, recursive):
380 def delete_permission(self, repo_group, obj, obj_type, recursive):
380 """
381 """
381 Revokes permission for repo_group for given obj(user or users_group),
382 Revokes permission for repo_group for given obj(user or users_group),
382 obj_type can be user or user group
383 obj_type can be user or user group
383
384
384 :param repo_group:
385 :param repo_group:
385 :param obj: user or user group id
386 :param obj: user or user group id
386 :param obj_type: user or user group type
387 :param obj_type: user or user group type
387 :param recursive: recurse to all children of group
388 :param recursive: recurse to all children of group
388 """
389 """
389 repo_group = db.RepoGroup.guess_instance(repo_group)
390 repo_group = db.RepoGroup.guess_instance(repo_group)
390
391
391 for el in repo_group.recursive_groups_and_repos():
392 for el in repo_group.recursive_groups_and_repos():
392 # iterated obj is an instance of a repos group or repository in
393 # iterated obj is an instance of a repos group or repository in
393 # that group, recursive option can be: none, repos, groups, all
394 # that group, recursive option can be: none, repos, groups, all
394 if recursive == 'all':
395 if recursive == 'all':
395 pass
396 pass
396 elif recursive == 'repos':
397 elif recursive == 'repos':
397 # skip groups, other than this one
398 # skip groups, other than this one
398 if isinstance(el, db.RepoGroup) and not el == repo_group:
399 if isinstance(el, db.RepoGroup) and not el == repo_group:
399 continue
400 continue
400 elif recursive == 'groups':
401 elif recursive == 'groups':
401 # skip repos
402 # skip repos
402 if isinstance(el, db.Repository):
403 if isinstance(el, db.Repository):
403 continue
404 continue
404 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
405 else: # recursive == 'none': # DEFAULT don't apply to iterated objects
405 el = repo_group
406 el = repo_group
406 # also we do a break at the end of this loop.
407 # also we do a break at the end of this loop.
407
408
408 if isinstance(el, db.RepoGroup):
409 if isinstance(el, db.RepoGroup):
409 if obj_type == 'user':
410 if obj_type == 'user':
410 RepoGroupModel().revoke_user_permission(el, user=obj)
411 RepoGroupModel().revoke_user_permission(el, user=obj)
411 elif obj_type == 'user_group':
412 elif obj_type == 'user_group':
412 RepoGroupModel().revoke_user_group_permission(el, group_name=obj)
413 RepoGroupModel().revoke_user_group_permission(el, group_name=obj)
413 else:
414 else:
414 raise Exception('undefined object type %s' % obj_type)
415 raise Exception('undefined object type %s' % obj_type)
415 elif isinstance(el, db.Repository):
416 elif isinstance(el, db.Repository):
416 if obj_type == 'user':
417 if obj_type == 'user':
417 repo.RepoModel().revoke_user_permission(el, user=obj)
418 repo.RepoModel().revoke_user_permission(el, user=obj)
418 elif obj_type == 'user_group':
419 elif obj_type == 'user_group':
419 repo.RepoModel().revoke_user_group_permission(el, group_name=obj)
420 repo.RepoModel().revoke_user_group_permission(el, group_name=obj)
420 else:
421 else:
421 raise Exception('undefined object type %s' % obj_type)
422 raise Exception('undefined object type %s' % obj_type)
422 else:
423 else:
423 raise Exception('el should be instance of Repository or '
424 raise Exception('el should be instance of Repository or '
424 'RepositoryGroup got %s instead' % type(el))
425 'RepositoryGroup got %s instead' % type(el))
425
426
426 # if it's not recursive call for all,repos,groups
427 # if it's not recursive call for all,repos,groups
427 # break the loop and don't proceed with other changes
428 # break the loop and don't proceed with other changes
428 if recursive not in ['all', 'repos', 'groups']:
429 if recursive not in ['all', 'repos', 'groups']:
429 break
430 break
430
431
431 def grant_user_permission(self, repo_group, user, perm):
432 def grant_user_permission(self, repo_group, user, perm):
432 """
433 """
433 Grant permission for user on given repository group, or update
434 Grant permission for user on given repository group, or update
434 existing one if found
435 existing one if found
435
436
436 :param repo_group: Instance of RepoGroup, repositories_group_id,
437 :param repo_group: Instance of RepoGroup, repositories_group_id,
437 or repositories_group name
438 or repositories_group name
438 :param user: Instance of User, user_id or username
439 :param user: Instance of User, user_id or username
439 :param perm: Instance of Permission, or permission_name
440 :param perm: Instance of Permission, or permission_name
440 """
441 """
441
442
442 repo_group = db.RepoGroup.guess_instance(repo_group)
443 repo_group = db.RepoGroup.guess_instance(repo_group)
443 user = db.User.guess_instance(user)
444 user = db.User.guess_instance(user)
444 permission = db.Permission.guess_instance(perm)
445 permission = db.Permission.guess_instance(perm)
445
446
446 # check if we have that permission already
447 # check if we have that permission already
447 obj = db.UserRepoGroupToPerm.query() \
448 obj = db.UserRepoGroupToPerm.query() \
448 .filter(db.UserRepoGroupToPerm.user == user) \
449 .filter(db.UserRepoGroupToPerm.user == user) \
449 .filter(db.UserRepoGroupToPerm.group == repo_group) \
450 .filter(db.UserRepoGroupToPerm.group == repo_group) \
450 .scalar()
451 .scalar()
451 if obj is None:
452 if obj is None:
452 # create new !
453 # create new !
453 obj = db.UserRepoGroupToPerm()
454 obj = db.UserRepoGroupToPerm()
454 meta.Session().add(obj)
455 meta.Session().add(obj)
455 obj.group = repo_group
456 obj.group = repo_group
456 obj.user = user
457 obj.user = user
457 obj.permission = permission
458 obj.permission = permission
458 log.debug('Granted perm %s to %s on %s', perm, user, repo_group)
459 log.debug('Granted perm %s to %s on %s', perm, user, repo_group)
459 return obj
460 return obj
460
461
461 def revoke_user_permission(self, repo_group, user):
462 def revoke_user_permission(self, repo_group, user):
462 """
463 """
463 Revoke permission for user on given repository group
464 Revoke permission for user on given repository group
464
465
465 :param repo_group: Instance of RepoGroup, repositories_group_id,
466 :param repo_group: Instance of RepoGroup, repositories_group_id,
466 or repositories_group name
467 or repositories_group name
467 :param user: Instance of User, user_id or username
468 :param user: Instance of User, user_id or username
468 """
469 """
469
470
470 repo_group = db.RepoGroup.guess_instance(repo_group)
471 repo_group = db.RepoGroup.guess_instance(repo_group)
471 user = db.User.guess_instance(user)
472 user = db.User.guess_instance(user)
472
473
473 obj = db.UserRepoGroupToPerm.query() \
474 obj = db.UserRepoGroupToPerm.query() \
474 .filter(db.UserRepoGroupToPerm.user == user) \
475 .filter(db.UserRepoGroupToPerm.user == user) \
475 .filter(db.UserRepoGroupToPerm.group == repo_group) \
476 .filter(db.UserRepoGroupToPerm.group == repo_group) \
476 .scalar()
477 .scalar()
477 if obj is not None:
478 if obj is not None:
478 meta.Session().delete(obj)
479 meta.Session().delete(obj)
479 log.debug('Revoked perm on %s on %s', repo_group, user)
480 log.debug('Revoked perm on %s on %s', repo_group, user)
480
481
481 def grant_user_group_permission(self, repo_group, group_name, perm):
482 def grant_user_group_permission(self, repo_group, group_name, perm):
482 """
483 """
483 Grant permission for user group on given repository group, or update
484 Grant permission for user group on given repository group, or update
484 existing one if found
485 existing one if found
485
486
486 :param repo_group: Instance of RepoGroup, repositories_group_id,
487 :param repo_group: Instance of RepoGroup, repositories_group_id,
487 or repositories_group name
488 or repositories_group name
488 :param group_name: Instance of UserGroup, users_group_id,
489 :param group_name: Instance of UserGroup, users_group_id,
489 or user group name
490 or user group name
490 :param perm: Instance of Permission, or permission_name
491 :param perm: Instance of Permission, or permission_name
491 """
492 """
492 repo_group = db.RepoGroup.guess_instance(repo_group)
493 repo_group = db.RepoGroup.guess_instance(repo_group)
493 group_name = db.UserGroup.guess_instance(group_name)
494 group_name = db.UserGroup.guess_instance(group_name)
494 permission = db.Permission.guess_instance(perm)
495 permission = db.Permission.guess_instance(perm)
495
496
496 # check if we have that permission already
497 # check if we have that permission already
497 obj = db.UserGroupRepoGroupToPerm.query() \
498 obj = db.UserGroupRepoGroupToPerm.query() \
498 .filter(db.UserGroupRepoGroupToPerm.group == repo_group) \
499 .filter(db.UserGroupRepoGroupToPerm.group == repo_group) \
499 .filter(db.UserGroupRepoGroupToPerm.users_group == group_name) \
500 .filter(db.UserGroupRepoGroupToPerm.users_group == group_name) \
500 .scalar()
501 .scalar()
501
502
502 if obj is None:
503 if obj is None:
503 # create new
504 # create new
504 obj = db.UserGroupRepoGroupToPerm()
505 obj = db.UserGroupRepoGroupToPerm()
505 meta.Session().add(obj)
506 meta.Session().add(obj)
506
507
507 obj.group = repo_group
508 obj.group = repo_group
508 obj.users_group = group_name
509 obj.users_group = group_name
509 obj.permission = permission
510 obj.permission = permission
510 log.debug('Granted perm %s to %s on %s', perm, group_name, repo_group)
511 log.debug('Granted perm %s to %s on %s', perm, group_name, repo_group)
511 return obj
512 return obj
512
513
513 def revoke_user_group_permission(self, repo_group, group_name):
514 def revoke_user_group_permission(self, repo_group, group_name):
514 """
515 """
515 Revoke permission for user group on given repository group
516 Revoke permission for user group on given repository group
516
517
517 :param repo_group: Instance of RepoGroup, repositories_group_id,
518 :param repo_group: Instance of RepoGroup, repositories_group_id,
518 or repositories_group name
519 or repositories_group name
519 :param group_name: Instance of UserGroup, users_group_id,
520 :param group_name: Instance of UserGroup, users_group_id,
520 or user group name
521 or user group name
521 """
522 """
522 repo_group = db.RepoGroup.guess_instance(repo_group)
523 repo_group = db.RepoGroup.guess_instance(repo_group)
523 group_name = db.UserGroup.guess_instance(group_name)
524 group_name = db.UserGroup.guess_instance(group_name)
524
525
525 obj = db.UserGroupRepoGroupToPerm.query() \
526 obj = db.UserGroupRepoGroupToPerm.query() \
526 .filter(db.UserGroupRepoGroupToPerm.group == repo_group) \
527 .filter(db.UserGroupRepoGroupToPerm.group == repo_group) \
527 .filter(db.UserGroupRepoGroupToPerm.users_group == group_name) \
528 .filter(db.UserGroupRepoGroupToPerm.users_group == group_name) \
528 .scalar()
529 .scalar()
529 if obj is not None:
530 if obj is not None:
530 meta.Session().delete(obj)
531 meta.Session().delete(obj)
531 log.debug('Revoked perm to %s on %s', repo_group, group_name)
532 log.debug('Revoked perm to %s on %s', repo_group, group_name)
General Comments 0
You need to be logged in to leave comments. Login now