##// END OF EJS Templates
[API] Create groups needed when creating repo
Nicolas VINOT -
r1589:307ec693 beta
parent child Browse files
Show More
@@ -1,8 +1,6 b''
1 syntax: glob
1 syntax: glob
2 *.pyc
2 *.pyc
3 *.swp
3 *.swp
4 *.ini
5 Paste*
6
4
7 syntax: regexp
5 syntax: regexp
8 ^build
6 ^build
@@ -16,4 +14,3 b' syntax: regexp'
16 ^test\.db$
14 ^test\.db$
17 ^repositories\.config$
15 ^repositories\.config$
18 ^RhodeCode\.egg-info$
16 ^RhodeCode\.egg-info$
19 ^env$
@@ -11,6 +11,8 b' from rhodecode.model.user import UserMod'
11 from rhodecode.model.repo_permission import RepositoryPermissionModel
11 from rhodecode.model.repo_permission import RepositoryPermissionModel
12 from rhodecode.model.users_group import UsersGroupModel
12 from rhodecode.model.users_group import UsersGroupModel
13 from rhodecode.model import users_group
13 from rhodecode.model import users_group
14 from rhodecode.model.repos_group import ReposGroupModel
15 from sqlalchemy.orm.exc import NoResultFound
14
16
15 log = logging.getLogger( __name__ )
17 log = logging.getLogger( __name__ )
16
18
@@ -59,8 +61,9 b' class ApiController( JSONRPCController )'
59 :param username
61 :param username
60 """
62 """
61
63
62 user = User.by_username( username )
64 try:
63 if not user:
65 user = User.by_username( username )
66 except NoResultFound:
64 return None
67 return None
65
68
66 return dict( id = user.user_id,
69 return dict( id = user.user_id,
@@ -109,6 +112,9 b' class ApiController( JSONRPCController )'
109 :param ldap_dn:
112 :param ldap_dn:
110 """
113 """
111
114
115 if self.get_user( apiuser, username ):
116 raise JSONRPCError( "user %s already exist" % username )
117
112 try:
118 try:
113 form_data = dict( username = username,
119 form_data = dict( username = username,
114 password = password,
120 password = password,
@@ -191,6 +197,9 b' class ApiController( JSONRPCController )'
191 :param active:
197 :param active:
192 """
198 """
193
199
200 if self.get_users_group( apiuser, name ):
201 raise JSONRPCError( "users group %s already exist" % name )
202
194 try:
203 try:
195 form_data = dict( users_group_name = name,
204 form_data = dict( users_group_name = name,
196 users_group_active = active )
205 users_group_active = active )
@@ -216,8 +225,9 b' class ApiController( JSONRPCController )'
216 if not users_group:
225 if not users_group:
217 raise JSONRPCError( 'unknown users group %s' % group_name )
226 raise JSONRPCError( 'unknown users group %s' % group_name )
218
227
219 user = User.by_username( user_name )
228 try:
220 if not user:
229 user = User.by_username( user_name )
230 except NoResultFound:
221 raise JSONRPCError( 'unknown user %s' % user_name )
231 raise JSONRPCError( 'unknown user %s' % user_name )
222
232
223 ugm = UsersGroupModel().add_user_to_group( users_group, user )
233 ugm = UsersGroupModel().add_user_to_group( users_group, user )
@@ -237,8 +247,9 b' class ApiController( JSONRPCController )'
237 :param repo_name
247 :param repo_name
238 """
248 """
239
249
240 repo = Repository.by_repo_name( repo_name )
250 try:
241 if not repo:
251 repo = Repository.by_repo_name( repo_name )
252 except NoResultFound:
242 return None
253 return None
243
254
244 members = []
255 members = []
@@ -287,8 +298,8 b' class ApiController( JSONRPCController )'
287 return result
298 return result
288
299
289 @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' )
300 @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' )
290 def create_repo( self, apiuser, name, owner_name, description = None, repo_type = 'hg', \
301 def create_repo( self, apiuser, name, owner_name, description = '', repo_type = 'hg', \
291 private = False, group_name = None ):
302 private = False ):
292 """
303 """
293 Create a repository
304 Create a repository
294
305
@@ -298,29 +309,36 b' class ApiController( JSONRPCController )'
298 :param type
309 :param type
299 :param private
310 :param private
300 :param owner_name
311 :param owner_name
301 :param group_name
302 :param clone
303 """
312 """
304
313
305 try:
314 try:
306 if group_name:
315 try:
307 group = Group.get_by_group_name( group_name )
316 owner = User.by_username( owner_name )
308 if group is None:
317 except NoResultFound:
309 raise JSONRPCError( 'unknown group %s' % group_name )
310 else:
311 group = None
312
313 owner = User.by_username( owner_name )
314 if owner is None:
315 raise JSONRPCError( 'unknown user %s' % owner )
318 raise JSONRPCError( 'unknown user %s' % owner )
316
319
317 RepoModel().create( { "repo_name" : name,
320 if self.get_repo( apiuser, name ):
318 "repo_name_full" : name,
321 raise JSONRPCError( "repo %s already exist" % name )
319 "description" : description,
322
320 "private" : private,
323 groups = name.split( '/' )
321 "repo_type" : repo_type,
324 real_name = groups[-1]
322 "repo_group" : group,
325 groups = groups[:-1]
323 "clone_uri" : None }, owner )
326 parent_id = None
327 for g in groups:
328 group = Group.get_by_group_name( g )
329 if not group:
330 group = ReposGroupModel().create( dict( group_name = g,
331 group_description = '',
332 group_parent_id = parent_id ) )
333 parent_id = group.group_id
334
335 RepoModel().create( dict( repo_name = real_name,
336 repo_name_full = name,
337 description = description,
338 private = private,
339 repo_type = repo_type,
340 repo_group = parent_id,
341 clone_uri = None ), owner )
324 except Exception:
342 except Exception:
325 log.error( traceback.format_exc() )
343 log.error( traceback.format_exc() )
326 raise JSONRPCError( 'failed to create repository %s' % name )
344 raise JSONRPCError( 'failed to create repository %s' % name )
@@ -337,12 +355,14 b' class ApiController( JSONRPCController )'
337 """
355 """
338
356
339 try:
357 try:
340 repo = Repository.by_repo_name( repo_name )
358 try:
341 if not repo:
359 repo = Repository.by_repo_name( repo_name )
360 except NoResultFound:
342 raise JSONRPCError( 'unknown repository %s' % repo )
361 raise JSONRPCError( 'unknown repository %s' % repo )
343
362
344 user = User.by_username( user_name )
363 try:
345 if not user:
364 user = User.by_username( user_name )
365 except NoResultFound:
346 raise JSONRPCError( 'unknown user %s' % user )
366 raise JSONRPCError( 'unknown user %s' % user )
347
367
348 RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm )
368 RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm )
@@ -27,18 +27,18 b' import logging'
27 from rhodecode.model.db import BaseModel, RepoToPerm, Permission
27 from rhodecode.model.db import BaseModel, RepoToPerm, Permission
28 from rhodecode.model.meta import Session
28 from rhodecode.model.meta import Session
29
29
30 log = logging.getLogger(__name__)
30 log = logging.getLogger( __name__ )
31
31
32 class RepositoryPermissionModel(BaseModel):
32 class RepositoryPermissionModel( BaseModel ):
33 def getUserPermission(self, repository, user):
33 def getUserPermission( self, repository, user ):
34 return RepoToPerm.query() \
34 return RepoToPerm.query() \
35 .filter(RepoToPerm.user == user) \
35 .filter( RepoToPerm.user == user ) \
36 .filter(RepoToPerm.repository == repository) \
36 .filter( RepoToPerm.repository == repository ) \
37 .scalar()
37 .scalar()
38
38
39 def updateUserPermission(self, repository, user, permission):
39 def updateUserPermission( self, repository, user, permission ):
40 permission = Permission.get_by_key(permission)
40 permission = Permission.get_by_key( permission )
41 current = self.getUserPermission(repository, user)
41 current = self.getUserPermission( repository, user )
42 if current:
42 if current:
43 if not current.permission is permission:
43 if not current.permission is permission:
44 current.permission = permission
44 current.permission = permission
@@ -47,16 +47,17 b' class RepositoryPermissionModel(BaseMode'
47 p.user = user
47 p.user = user
48 p.repository = repository
48 p.repository = repository
49 p.permission = permission
49 p.permission = permission
50 Session.add(p)
50 Session.add( p )
51 Session.commit()
51 Session.commit()
52
52
53 def deleteUserPermission(self, repository, user):
53 def deleteUserPermission( self, repository, user ):
54 current = self.getUserPermission(repository, user)
54 current = self.getUserPermission( repository, user )
55 Session.delete(current)
55 if current:
56 Session.commit()
56 Session.delete( current )
57 Session.commit()
57
58
58 def updateOrDeleteUserPermission(self, repository, user, permission):
59 def updateOrDeleteUserPermission( self, repository, user, permission ):
59 if permission:
60 if permission:
60 self.updateUserPermission(repository, user, permission)
61 self.updateUserPermission( repository, user, permission )
61 else:
62 else:
62 self.deleteUserPermission(repository, user) No newline at end of file
63 self.deleteUserPermission( repository, user )
@@ -36,21 +36,21 b' from rhodecode.model import BaseModel'
36 from rhodecode.model.caching_query import FromCache
36 from rhodecode.model.caching_query import FromCache
37 from rhodecode.model.db import Group, RhodeCodeUi
37 from rhodecode.model.db import Group, RhodeCodeUi
38
38
39 log = logging.getLogger(__name__)
39 log = logging.getLogger( __name__ )
40
40
41
41
42 class ReposGroupModel(BaseModel):
42 class ReposGroupModel( BaseModel ):
43
43
44 @LazyProperty
44 @LazyProperty
45 def repos_path(self):
45 def repos_path( self ):
46 """
46 """
47 Get's the repositories root path from database
47 Get's the repositories root path from database
48 """
48 """
49
49
50 q = RhodeCodeUi.get_by_key('/').one()
50 q = RhodeCodeUi.get_by_key( '/' ).one()
51 return q.ui_value
51 return q.ui_value
52
52
53 def __create_group(self, group_name, parent_id):
53 def __create_group( self, group_name, parent_id ):
54 """
54 """
55 makes repositories group on filesystem
55 makes repositories group on filesystem
56
56
@@ -59,64 +59,64 b' class ReposGroupModel(BaseModel):'
59 """
59 """
60
60
61 if parent_id:
61 if parent_id:
62 paths = Group.get(parent_id).full_path.split(Group.url_sep())
62 paths = Group.get( parent_id ).full_path.split( Group.url_sep() )
63 parent_path = os.sep.join(paths)
63 parent_path = os.sep.join( paths )
64 else:
64 else:
65 parent_path = ''
65 parent_path = ''
66
66
67 create_path = os.path.join(self.repos_path, parent_path, group_name)
67 create_path = os.path.join( self.repos_path, parent_path, group_name )
68 log.debug('creating new group in %s', create_path)
68 log.debug( 'creating new group in %s', create_path )
69
69
70 if os.path.isdir(create_path):
70 if os.path.isdir( create_path ):
71 raise Exception('That directory already exists !')
71 raise Exception( 'That directory already exists !' )
72
72
73
73
74 os.makedirs(create_path)
74 os.makedirs( create_path )
75
75
76
76
77 def __rename_group(self, old, old_parent_id, new, new_parent_id):
77 def __rename_group( self, old, old_parent_id, new, new_parent_id ):
78 """
78 """
79 Renames a group on filesystem
79 Renames a group on filesystem
80
80
81 :param group_name:
81 :param group_name:
82 """
82 """
83 log.debug('renaming repos group from %s to %s', old, new)
83 log.debug( 'renaming repos group from %s to %s', old, new )
84
84
85 if new_parent_id:
85 if new_parent_id:
86 paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
86 paths = Group.get( new_parent_id ).full_path.split( Group.url_sep() )
87 new_parent_path = os.sep.join(paths)
87 new_parent_path = os.sep.join( paths )
88 else:
88 else:
89 new_parent_path = ''
89 new_parent_path = ''
90
90
91 if old_parent_id:
91 if old_parent_id:
92 paths = Group.get(old_parent_id).full_path.split(Group.url_sep())
92 paths = Group.get( old_parent_id ).full_path.split( Group.url_sep() )
93 old_parent_path = os.sep.join(paths)
93 old_parent_path = os.sep.join( paths )
94 else:
94 else:
95 old_parent_path = ''
95 old_parent_path = ''
96
96
97 old_path = os.path.join(self.repos_path, old_parent_path, old)
97 old_path = os.path.join( self.repos_path, old_parent_path, old )
98 new_path = os.path.join(self.repos_path, new_parent_path, new)
98 new_path = os.path.join( self.repos_path, new_parent_path, new )
99
99
100 log.debug('renaming repos paths from %s to %s', old_path, new_path)
100 log.debug( 'renaming repos paths from %s to %s', old_path, new_path )
101
101
102 if os.path.isdir(new_path):
102 if os.path.isdir( new_path ):
103 raise Exception('Was trying to rename to already '
103 raise Exception( 'Was trying to rename to already '
104 'existing dir %s' % new_path)
104 'existing dir %s' % new_path )
105 shutil.move(old_path, new_path)
105 shutil.move( old_path, new_path )
106
106
107 def __delete_group(self, group):
107 def __delete_group( self, group ):
108 """
108 """
109 Deletes a group from a filesystem
109 Deletes a group from a filesystem
110
110
111 :param group: instance of group from database
111 :param group: instance of group from database
112 """
112 """
113 paths = group.full_path.split(Group.url_sep())
113 paths = group.full_path.split( Group.url_sep() )
114 paths = os.sep.join(paths)
114 paths = os.sep.join( paths )
115
115
116 rm_path = os.path.join(self.repos_path, paths)
116 rm_path = os.path.join( self.repos_path, paths )
117 os.rmdir(rm_path)
117 os.rmdir( rm_path )
118
118
119 def create(self, form_data):
119 def create( self, form_data ):
120 try:
120 try:
121 new_repos_group = Group()
121 new_repos_group = Group()
122 new_repos_group.group_name = form_data['group_name']
122 new_repos_group.group_name = form_data['group_name']
@@ -124,21 +124,22 b' class ReposGroupModel(BaseModel):'
124 form_data['group_description']
124 form_data['group_description']
125 new_repos_group.group_parent_id = form_data['group_parent_id']
125 new_repos_group.group_parent_id = form_data['group_parent_id']
126
126
127 self.sa.add(new_repos_group)
127 self.sa.add( new_repos_group )
128
128
129 self.__create_group(form_data['group_name'],
129 self.__create_group( form_data['group_name'],
130 form_data['group_parent_id'])
130 form_data['group_parent_id'] )
131
131
132 self.sa.commit()
132 self.sa.commit()
133 return new_repos_group
133 except:
134 except:
134 log.error(traceback.format_exc())
135 log.error( traceback.format_exc() )
135 self.sa.rollback()
136 self.sa.rollback()
136 raise
137 raise
137
138
138 def update(self, repos_group_id, form_data):
139 def update( self, repos_group_id, form_data ):
139
140
140 try:
141 try:
141 repos_group = Group.get(repos_group_id)
142 repos_group = Group.get( repos_group_id )
142 old_name = repos_group.group_name
143 old_name = repos_group.group_name
143 old_parent_id = repos_group.group_parent_id
144 old_parent_id = repos_group.group_parent_id
144
145
@@ -147,27 +148,27 b' class ReposGroupModel(BaseModel):'
147 form_data['group_description']
148 form_data['group_description']
148 repos_group.group_parent_id = form_data['group_parent_id']
149 repos_group.group_parent_id = form_data['group_parent_id']
149
150
150 self.sa.add(repos_group)
151 self.sa.add( repos_group )
151
152
152 if old_name != form_data['group_name'] or (old_parent_id !=
153 if old_name != form_data['group_name'] or ( old_parent_id !=
153 form_data['group_parent_id']):
154 form_data['group_parent_id'] ):
154 self.__rename_group(old=old_name, old_parent_id=old_parent_id,
155 self.__rename_group( old = old_name, old_parent_id = old_parent_id,
155 new=form_data['group_name'],
156 new = form_data['group_name'],
156 new_parent_id=form_data['group_parent_id'])
157 new_parent_id = form_data['group_parent_id'] )
157
158
158 self.sa.commit()
159 self.sa.commit()
159 except:
160 except:
160 log.error(traceback.format_exc())
161 log.error( traceback.format_exc() )
161 self.sa.rollback()
162 self.sa.rollback()
162 raise
163 raise
163
164
164 def delete(self, users_group_id):
165 def delete( self, users_group_id ):
165 try:
166 try:
166 users_group = Group.get(users_group_id)
167 users_group = Group.get( users_group_id )
167 self.sa.delete(users_group)
168 self.sa.delete( users_group )
168 self.__delete_group(users_group)
169 self.__delete_group( users_group )
169 self.sa.commit()
170 self.sa.commit()
170 except:
171 except:
171 log.error(traceback.format_exc())
172 log.error( traceback.format_exc() )
172 self.sa.rollback()
173 self.sa.rollback()
173 raise
174 raise
@@ -67,6 +67,11 b' class UsersGroupModel( BaseModel ):'
67 raise
67 raise
68
68
69 def add_user_to_group( self, users_group, user ):
69 def add_user_to_group( self, users_group, user ):
70 for m in users_group.members:
71 u = m.user
72 if u.user_id == user.user_id:
73 return m
74
70 try:
75 try:
71 users_group_member = UsersGroupMember()
76 users_group_member = UsersGroupMember()
72 users_group_member.user = user
77 users_group_member.user = user
General Comments 0
You need to be logged in to leave comments. Login now