##// END OF EJS Templates
API create user use UserModel new method
marcink -
r1649:57a0e240 beta
parent child Browse files
Show More
@@ -1,374 +1,367
1 import traceback
1 import traceback
2 import logging
2 import logging
3
3
4 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
4 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
5 from rhodecode.lib.auth import HasPermissionAllDecorator, \
5 from rhodecode.lib.auth import HasPermissionAllDecorator, \
6 HasPermissionAnyDecorator
6 HasPermissionAnyDecorator
7 from rhodecode.model.scm import ScmModel
7 from rhodecode.model.scm import ScmModel
8
8
9 from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository
9 from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository
10 from rhodecode.model.repo import RepoModel
10 from rhodecode.model.repo import RepoModel
11 from rhodecode.model.user import UserModel
11 from rhodecode.model.user import UserModel
12 from rhodecode.model.repo_permission import RepositoryPermissionModel
12 from rhodecode.model.repo_permission import RepositoryPermissionModel
13 from rhodecode.model.users_group import UsersGroupModel
13 from rhodecode.model.users_group import UsersGroupModel
14 from rhodecode.model import users_group
14 from rhodecode.model import users_group
15 from rhodecode.model.repos_group import ReposGroupModel
15 from rhodecode.model.repos_group import ReposGroupModel
16 from sqlalchemy.orm.exc import NoResultFound
16 from sqlalchemy.orm.exc import NoResultFound
17
17
18 log = logging.getLogger(__name__)
18 log = logging.getLogger(__name__)
19
19
20
20
21 class ApiController(JSONRPCController):
21 class ApiController(JSONRPCController):
22 """
22 """
23 API Controller
23 API Controller
24
24
25
25
26 Each method needs to have USER as argument this is then based on given
26 Each method needs to have USER as argument this is then based on given
27 API_KEY propagated as instance of user object
27 API_KEY propagated as instance of user object
28
28
29 Preferably this should be first argument also
29 Preferably this should be first argument also
30
30
31
31
32 Each function should also **raise** JSONRPCError for any
32 Each function should also **raise** JSONRPCError for any
33 errors that happens
33 errors that happens
34
34
35 """
35 """
36
36
37 @HasPermissionAllDecorator('hg.admin')
37 @HasPermissionAllDecorator('hg.admin')
38 def pull(self, apiuser, repo):
38 def pull(self, apiuser, repo):
39 """
39 """
40 Dispatch pull action on given repo
40 Dispatch pull action on given repo
41
41
42
42
43 :param user:
43 :param user:
44 :param repo:
44 :param repo:
45 """
45 """
46
46
47 if Repository.is_valid(repo) is False:
47 if Repository.is_valid(repo) is False:
48 raise JSONRPCError('Unknown repo "%s"' % repo)
48 raise JSONRPCError('Unknown repo "%s"' % repo)
49
49
50 try:
50 try:
51 ScmModel().pull_changes(repo, self.rhodecode_user.username)
51 ScmModel().pull_changes(repo, self.rhodecode_user.username)
52 return 'Pulled from %s' % repo
52 return 'Pulled from %s' % repo
53 except Exception:
53 except Exception:
54 raise JSONRPCError('Unable to pull changes from "%s"' % repo)
54 raise JSONRPCError('Unable to pull changes from "%s"' % repo)
55
55
56 @HasPermissionAllDecorator('hg.admin')
56 @HasPermissionAllDecorator('hg.admin')
57 def get_user(self, apiuser, username):
57 def get_user(self, apiuser, username):
58 """"
58 """"
59 Get a user by username
59 Get a user by username
60
60
61 :param apiuser
61 :param apiuser
62 :param username
62 :param username
63 """
63 """
64
64
65 user = User.get_by_username(username)
65 user = User.get_by_username(username)
66 if not user:
66 if not user:
67 return None
67 return None
68
68
69 return dict(id=user.user_id,
69 return dict(id=user.user_id,
70 username=user.username,
70 username=user.username,
71 firstname=user.name,
71 firstname=user.name,
72 lastname=user.lastname,
72 lastname=user.lastname,
73 email=user.email,
73 email=user.email,
74 active=user.active,
74 active=user.active,
75 admin=user.admin,
75 admin=user.admin,
76 ldap=user.ldap_dn)
76 ldap=user.ldap_dn)
77
77
78 @HasPermissionAllDecorator('hg.admin')
78 @HasPermissionAllDecorator('hg.admin')
79 def get_users(self, apiuser):
79 def get_users(self, apiuser):
80 """"
80 """"
81 Get all users
81 Get all users
82
82
83 :param apiuser
83 :param apiuser
84 """
84 """
85
85
86 result = []
86 result = []
87 for user in User.getAll():
87 for user in User.getAll():
88 result.append(dict(id=user.user_id,
88 result.append(dict(id=user.user_id,
89 username=user.username,
89 username=user.username,
90 firstname=user.name,
90 firstname=user.name,
91 lastname=user.lastname,
91 lastname=user.lastname,
92 email=user.email,
92 email=user.email,
93 active=user.active,
93 active=user.active,
94 admin=user.admin,
94 admin=user.admin,
95 ldap=user.ldap_dn))
95 ldap=user.ldap_dn))
96 return result
96 return result
97
97
98 @HasPermissionAllDecorator('hg.admin')
98 @HasPermissionAllDecorator('hg.admin')
99 def create_user(self, apiuser, username, password, firstname,
99 def create_user(self, apiuser, username, password, firstname,
100 lastname, email, active=True, admin=False, ldap_dn=None):
100 lastname, email, active=True, admin=False, ldap_dn=None):
101 """
101 """
102 Create new user
102 Create new user
103
103
104 :param apiuser:
104 :param apiuser:
105 :param username:
105 :param username:
106 :param password:
106 :param password:
107 :param name:
107 :param name:
108 :param lastname:
108 :param lastname:
109 :param email:
109 :param email:
110 :param active:
110 :param active:
111 :param admin:
111 :param admin:
112 :param ldap_dn:
112 :param ldap_dn:
113 """
113 """
114
114
115 if self.get_user(apiuser, username):
115 if self.get_user(apiuser, username):
116 raise JSONRPCError("user %s already exist" % username)
116 raise JSONRPCError("user %s already exist" % username)
117
117
118 try:
118 try:
119 form_data = dict(username=username,
119 UserModel().create_or_update(username, password, email, firstname,
120 password=password,
120 lastname, active, admin, ldap_dn)
121 active=active,
122 admin=admin,
123 name=firstname,
124 lastname=lastname,
125 email=email,
126 ldap_dn=ldap_dn)
127 UserModel().create_ldap(username, password, ldap_dn, form_data)
128 return dict(msg='created new user %s' % username)
121 return dict(msg='created new user %s' % username)
129 except Exception:
122 except Exception:
130 log.error(traceback.format_exc())
123 log.error(traceback.format_exc())
131 raise JSONRPCError('failed to create user %s' % username)
124 raise JSONRPCError('failed to create user %s' % username)
132
125
133 @HasPermissionAllDecorator('hg.admin')
126 @HasPermissionAllDecorator('hg.admin')
134 def get_users_group(self, apiuser, group_name):
127 def get_users_group(self, apiuser, group_name):
135 """"
128 """"
136 Get users group by name
129 Get users group by name
137
130
138 :param apiuser
131 :param apiuser
139 :param group_name
132 :param group_name
140 """
133 """
141
134
142 users_group = UsersGroup.get_by_group_name(group_name)
135 users_group = UsersGroup.get_by_group_name(group_name)
143 if not users_group:
136 if not users_group:
144 return None
137 return None
145
138
146 members = []
139 members = []
147 for user in users_group.members:
140 for user in users_group.members:
148 user = user.user
141 user = user.user
149 members.append(dict(id=user.user_id,
142 members.append(dict(id=user.user_id,
150 username=user.username,
143 username=user.username,
151 firstname=user.name,
144 firstname=user.name,
152 lastname=user.lastname,
145 lastname=user.lastname,
153 email=user.email,
146 email=user.email,
154 active=user.active,
147 active=user.active,
155 admin=user.admin,
148 admin=user.admin,
156 ldap=user.ldap_dn))
149 ldap=user.ldap_dn))
157
150
158 return dict(id=users_group.users_group_id,
151 return dict(id=users_group.users_group_id,
159 name=users_group.users_group_name,
152 name=users_group.users_group_name,
160 active=users_group.users_group_active,
153 active=users_group.users_group_active,
161 members=members)
154 members=members)
162
155
163 @HasPermissionAllDecorator('hg.admin')
156 @HasPermissionAllDecorator('hg.admin')
164 def get_users_groups(self, apiuser):
157 def get_users_groups(self, apiuser):
165 """"
158 """"
166 Get all users groups
159 Get all users groups
167
160
168 :param apiuser
161 :param apiuser
169 """
162 """
170
163
171 result = []
164 result = []
172 for users_group in UsersGroup.getAll():
165 for users_group in UsersGroup.getAll():
173 members = []
166 members = []
174 for user in users_group.members:
167 for user in users_group.members:
175 user = user.user
168 user = user.user
176 members.append(dict(id=user.user_id,
169 members.append(dict(id=user.user_id,
177 username=user.username,
170 username=user.username,
178 firstname=user.name,
171 firstname=user.name,
179 lastname=user.lastname,
172 lastname=user.lastname,
180 email=user.email,
173 email=user.email,
181 active=user.active,
174 active=user.active,
182 admin=user.admin,
175 admin=user.admin,
183 ldap=user.ldap_dn))
176 ldap=user.ldap_dn))
184
177
185 result.append(dict(id=users_group.users_group_id,
178 result.append(dict(id=users_group.users_group_id,
186 name=users_group.users_group_name,
179 name=users_group.users_group_name,
187 active=users_group.users_group_active,
180 active=users_group.users_group_active,
188 members=members))
181 members=members))
189 return result
182 return result
190
183
191 @HasPermissionAllDecorator('hg.admin')
184 @HasPermissionAllDecorator('hg.admin')
192 def create_users_group(self, apiuser, name, active=True):
185 def create_users_group(self, apiuser, name, active=True):
193 """
186 """
194 Creates an new usergroup
187 Creates an new usergroup
195
188
196 :param name:
189 :param name:
197 :param active:
190 :param active:
198 """
191 """
199
192
200 if self.get_users_group(apiuser, name):
193 if self.get_users_group(apiuser, name):
201 raise JSONRPCError("users group %s already exist" % name)
194 raise JSONRPCError("users group %s already exist" % name)
202
195
203 try:
196 try:
204 form_data = dict(users_group_name=name,
197 form_data = dict(users_group_name=name,
205 users_group_active=active)
198 users_group_active=active)
206 ug = UsersGroup.create(form_data)
199 ug = UsersGroup.create(form_data)
207 return dict(id=ug.users_group_id,
200 return dict(id=ug.users_group_id,
208 msg='created new users group %s' % name)
201 msg='created new users group %s' % name)
209 except Exception:
202 except Exception:
210 log.error(traceback.format_exc())
203 log.error(traceback.format_exc())
211 raise JSONRPCError('failed to create group %s' % name)
204 raise JSONRPCError('failed to create group %s' % name)
212
205
213 @HasPermissionAllDecorator('hg.admin')
206 @HasPermissionAllDecorator('hg.admin')
214 def add_user_to_users_group(self, apiuser, group_name, user_name):
207 def add_user_to_users_group(self, apiuser, group_name, user_name):
215 """"
208 """"
216 Add a user to a group
209 Add a user to a group
217
210
218 :param apiuser
211 :param apiuser
219 :param group_name
212 :param group_name
220 :param user_name
213 :param user_name
221 """
214 """
222
215
223 try:
216 try:
224 users_group = UsersGroup.get_by_group_name(group_name)
217 users_group = UsersGroup.get_by_group_name(group_name)
225 if not users_group:
218 if not users_group:
226 raise JSONRPCError('unknown users group %s' % group_name)
219 raise JSONRPCError('unknown users group %s' % group_name)
227
220
228 try:
221 try:
229 user = User.get_by_username(user_name)
222 user = User.get_by_username(user_name)
230 except NoResultFound:
223 except NoResultFound:
231 raise JSONRPCError('unknown user %s' % user_name)
224 raise JSONRPCError('unknown user %s' % user_name)
232
225
233 ugm = UsersGroupModel().add_user_to_group(users_group, user)
226 ugm = UsersGroupModel().add_user_to_group(users_group, user)
234
227
235 return dict(id=ugm.users_group_member_id,
228 return dict(id=ugm.users_group_member_id,
236 msg='created new users group member')
229 msg='created new users group member')
237 except Exception:
230 except Exception:
238 log.error(traceback.format_exc())
231 log.error(traceback.format_exc())
239 raise JSONRPCError('failed to create users group member')
232 raise JSONRPCError('failed to create users group member')
240
233
241 @HasPermissionAnyDecorator('hg.admin')
234 @HasPermissionAnyDecorator('hg.admin')
242 def get_repo(self, apiuser, repo_name):
235 def get_repo(self, apiuser, repo_name):
243 """"
236 """"
244 Get repository by name
237 Get repository by name
245
238
246 :param apiuser
239 :param apiuser
247 :param repo_name
240 :param repo_name
248 """
241 """
249
242
250 try:
243 try:
251 repo = Repository.get_by_repo_name(repo_name)
244 repo = Repository.get_by_repo_name(repo_name)
252 except NoResultFound:
245 except NoResultFound:
253 return None
246 return None
254
247
255 members = []
248 members = []
256 for user in repo.repo_to_perm:
249 for user in repo.repo_to_perm:
257 perm = user.permission.permission_name
250 perm = user.permission.permission_name
258 user = user.user
251 user = user.user
259 members.append(dict(type_="user",
252 members.append(dict(type_="user",
260 id=user.user_id,
253 id=user.user_id,
261 username=user.username,
254 username=user.username,
262 firstname=user.name,
255 firstname=user.name,
263 lastname=user.lastname,
256 lastname=user.lastname,
264 email=user.email,
257 email=user.email,
265 active=user.active,
258 active=user.active,
266 admin=user.admin,
259 admin=user.admin,
267 ldap=user.ldap_dn,
260 ldap=user.ldap_dn,
268 permission=perm))
261 permission=perm))
269 for users_group in repo.users_group_to_perm:
262 for users_group in repo.users_group_to_perm:
270 perm = users_group.permission.permission_name
263 perm = users_group.permission.permission_name
271 users_group = users_group.users_group
264 users_group = users_group.users_group
272 members.append(dict(type_="users_group",
265 members.append(dict(type_="users_group",
273 id=users_group.users_group_id,
266 id=users_group.users_group_id,
274 name=users_group.users_group_name,
267 name=users_group.users_group_name,
275 active=users_group.users_group_active,
268 active=users_group.users_group_active,
276 permission=perm))
269 permission=perm))
277
270
278 return dict(id=repo.repo_id,
271 return dict(id=repo.repo_id,
279 name=repo.repo_name,
272 name=repo.repo_name,
280 type=repo.repo_type,
273 type=repo.repo_type,
281 description=repo.description,
274 description=repo.description,
282 members=members)
275 members=members)
283
276
284 @HasPermissionAnyDecorator('hg.admin')
277 @HasPermissionAnyDecorator('hg.admin')
285 def get_repos(self, apiuser):
278 def get_repos(self, apiuser):
286 """"
279 """"
287 Get all repositories
280 Get all repositories
288
281
289 :param apiuser
282 :param apiuser
290 """
283 """
291
284
292 result = []
285 result = []
293 for repository in Repository.getAll():
286 for repository in Repository.getAll():
294 result.append(dict(id=repository.repo_id,
287 result.append(dict(id=repository.repo_id,
295 name=repository.repo_name,
288 name=repository.repo_name,
296 type=repository.repo_type,
289 type=repository.repo_type,
297 description=repository.description))
290 description=repository.description))
298 return result
291 return result
299
292
300 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
293 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
301 def create_repo(self, apiuser, name, owner_name, description='',
294 def create_repo(self, apiuser, name, owner_name, description='',
302 repo_type='hg', private=False):
295 repo_type='hg', private=False):
303 """
296 """
304 Create a repository
297 Create a repository
305
298
306 :param apiuser
299 :param apiuser
307 :param name
300 :param name
308 :param description
301 :param description
309 :param type
302 :param type
310 :param private
303 :param private
311 :param owner_name
304 :param owner_name
312 """
305 """
313
306
314 try:
307 try:
315 try:
308 try:
316 owner = User.get_by_username(owner_name)
309 owner = User.get_by_username(owner_name)
317 except NoResultFound:
310 except NoResultFound:
318 raise JSONRPCError('unknown user %s' % owner)
311 raise JSONRPCError('unknown user %s' % owner)
319
312
320 if self.get_repo(apiuser, name):
313 if self.get_repo(apiuser, name):
321 raise JSONRPCError("repo %s already exist" % name)
314 raise JSONRPCError("repo %s already exist" % name)
322
315
323 groups = name.split('/')
316 groups = name.split('/')
324 real_name = groups[-1]
317 real_name = groups[-1]
325 groups = groups[:-1]
318 groups = groups[:-1]
326 parent_id = None
319 parent_id = None
327 for g in groups:
320 for g in groups:
328 group = RepoGroup.get_by_group_name(g)
321 group = RepoGroup.get_by_group_name(g)
329 if not group:
322 if not group:
330 group = ReposGroupModel().create(dict(group_name=g,
323 group = ReposGroupModel().create(dict(group_name=g,
331 group_description='',
324 group_description='',
332 group_parent_id=parent_id))
325 group_parent_id=parent_id))
333 parent_id = group.group_id
326 parent_id = group.group_id
334
327
335 RepoModel().create(dict(repo_name=real_name,
328 RepoModel().create(dict(repo_name=real_name,
336 repo_name_full=name,
329 repo_name_full=name,
337 description=description,
330 description=description,
338 private=private,
331 private=private,
339 repo_type=repo_type,
332 repo_type=repo_type,
340 repo_group=parent_id,
333 repo_group=parent_id,
341 clone_uri=None), owner)
334 clone_uri=None), owner)
342 except Exception:
335 except Exception:
343 log.error(traceback.format_exc())
336 log.error(traceback.format_exc())
344 raise JSONRPCError('failed to create repository %s' % name)
337 raise JSONRPCError('failed to create repository %s' % name)
345
338
346 @HasPermissionAnyDecorator('hg.admin')
339 @HasPermissionAnyDecorator('hg.admin')
347 def add_user_to_repo(self, apiuser, repo_name, user_name, perm):
340 def add_user_to_repo(self, apiuser, repo_name, user_name, perm):
348 """
341 """
349 Add permission for a user to a repository
342 Add permission for a user to a repository
350
343
351 :param apiuser
344 :param apiuser
352 :param repo_name
345 :param repo_name
353 :param user_name
346 :param user_name
354 :param perm
347 :param perm
355 """
348 """
356
349
357 try:
350 try:
358 try:
351 try:
359 repo = Repository.get_by_repo_name(repo_name)
352 repo = Repository.get_by_repo_name(repo_name)
360 except NoResultFound:
353 except NoResultFound:
361 raise JSONRPCError('unknown repository %s' % repo)
354 raise JSONRPCError('unknown repository %s' % repo)
362
355
363 try:
356 try:
364 user = User.get_by_username(user_name)
357 user = User.get_by_username(user_name)
365 except NoResultFound:
358 except NoResultFound:
366 raise JSONRPCError('unknown user %s' % user)
359 raise JSONRPCError('unknown user %s' % user)
367
360
368 RepositoryPermissionModel()\
361 RepositoryPermissionModel()\
369 .update_or_delete_user_permission(repo, user, perm)
362 .update_or_delete_user_permission(repo, user, perm)
370 except Exception:
363 except Exception:
371 log.error(traceback.format_exc())
364 log.error(traceback.format_exc())
372 raise JSONRPCError('failed to edit permission %(repo)s for %(user)s'
365 raise JSONRPCError('failed to edit permission %(repo)s for %(user)s'
373 % dict(user=user_name, repo=repo_name))
366 % dict(user=user_name, repo=repo_name))
374
367
General Comments 0
You need to be logged in to leave comments. Login now