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