##// END OF EJS Templates
API: create_repo returns now repo object after creation
marcink -
r2378:04ef27ce beta
parent child Browse files
Show More
@@ -1,702 +1,711 b''
1 .. _api:
1 .. _api:
2
2
3 ===
3 ===
4 API
4 API
5 ===
5 ===
6
6
7
7
8 Starting from RhodeCode version 1.2 a simple API was implemented.
8 Starting from RhodeCode version 1.2 a simple API was implemented.
9 There's a single schema for calling all api methods. API is implemented
9 There's a single schema for calling all api methods. API is implemented
10 with JSON protocol both ways. An url to send API request in RhodeCode is
10 with JSON protocol both ways. An url to send API request in RhodeCode is
11 <your_server>/_admin/api
11 <your_server>/_admin/api
12
12
13 API ACCESS FOR WEB VIEWS
13 API ACCESS FOR WEB VIEWS
14 ++++++++++++++++++++++++
14 ++++++++++++++++++++++++
15
15
16 API access can also be turned on for each web view in RhodeCode that is
16 API access can also be turned on for each web view in RhodeCode that is
17 decorated with `@LoginRequired` decorator. To enable API access simple change
17 decorated with `@LoginRequired` decorator. To enable API access simple change
18 the standard login decorator to `@LoginRequired(api_access=True)`.
18 the standard login decorator to `@LoginRequired(api_access=True)`.
19 After this change, a rhodecode view can be accessed without login by adding a
19 After this change, a rhodecode view can be accessed without login by adding a
20 GET parameter `?api_key=<api_key>` to url. By default this is only
20 GET parameter `?api_key=<api_key>` to url. By default this is only
21 enabled on RSS/ATOM feed views.
21 enabled on RSS/ATOM feed views.
22
22
23
23
24 API ACCESS
24 API ACCESS
25 ++++++++++
25 ++++++++++
26
26
27 All clients are required to send JSON-RPC spec JSON data::
27 All clients are required to send JSON-RPC spec JSON data::
28
28
29 {
29 {
30 "id:"<id>",
30 "id:"<id>",
31 "api_key":"<api_key>",
31 "api_key":"<api_key>",
32 "method":"<method_name>",
32 "method":"<method_name>",
33 "args":{"<arg_key>":"<arg_val>"}
33 "args":{"<arg_key>":"<arg_val>"}
34 }
34 }
35
35
36 Example call for autopulling remotes repos using curl::
36 Example call for autopulling remotes repos using curl::
37 curl https://server.com/_admin/api -X POST -H 'content-type:text/plain' --data-binary '{"id":1,"api_key":"xe7cdb2v278e4evbdf5vs04v832v0efvcbcve4a3","method":"pull","args":{"repo":"CPython"}}'
37 curl https://server.com/_admin/api -X POST -H 'content-type:text/plain' --data-binary '{"id":1,"api_key":"xe7cdb2v278e4evbdf5vs04v832v0efvcbcve4a3","method":"pull","args":{"repo":"CPython"}}'
38
38
39 Simply provide
39 Simply provide
40 - *id* A value of any type, which is used to match the response with the request that it is replying to.
40 - *id* A value of any type, which is used to match the response with the request that it is replying to.
41 - *api_key* for access and permission validation.
41 - *api_key* for access and permission validation.
42 - *method* is name of method to call
42 - *method* is name of method to call
43 - *args* is an key:value list of arguments to pass to method
43 - *args* is an key:value list of arguments to pass to method
44
44
45 .. note::
45 .. note::
46
46
47 api_key can be found in your user account page
47 api_key can be found in your user account page
48
48
49
49
50 RhodeCode API will return always a JSON-RPC response::
50 RhodeCode API will return always a JSON-RPC response::
51
51
52 {
52 {
53 "id":<id>, # matching id sent by request
53 "id":<id>, # matching id sent by request
54 "result": "<result>"|null, # JSON formatted result, null if any errors
54 "result": "<result>"|null, # JSON formatted result, null if any errors
55 "error": "null"|<error_message> # JSON formatted error (if any)
55 "error": "null"|<error_message> # JSON formatted error (if any)
56 }
56 }
57
57
58 All responses from API will be `HTTP/1.0 200 OK`, if there's an error while
58 All responses from API will be `HTTP/1.0 200 OK`, if there's an error while
59 calling api *error* key from response will contain failure description
59 calling api *error* key from response will contain failure description
60 and result will be null.
60 and result will be null.
61
61
62 API METHODS
62 API METHODS
63 +++++++++++
63 +++++++++++
64
64
65
65
66 pull
66 pull
67 ----
67 ----
68
68
69 Pulls given repo from remote location. Can be used to automatically keep
69 Pulls given repo from remote location. Can be used to automatically keep
70 remote repos up to date. This command can be executed only using api_key
70 remote repos up to date. This command can be executed only using api_key
71 belonging to user with admin rights
71 belonging to user with admin rights
72
72
73 INPUT::
73 INPUT::
74
74
75 id : <id_for_response>
75 id : <id_for_response>
76 api_key : "<api_key>"
76 api_key : "<api_key>"
77 method : "pull"
77 method : "pull"
78 args : {
78 args : {
79 "repo_name" : "<reponame>"
79 "repo_name" : "<reponame>"
80 }
80 }
81
81
82 OUTPUT::
82 OUTPUT::
83
83
84 result : "Pulled from <reponame>"
84 result : "Pulled from <reponame>"
85 error : null
85 error : null
86
86
87
87
88 get_user
88 get_user
89 --------
89 --------
90
90
91 Get's an user by username or user_id, Returns empty result if user is not found.
91 Get's an user by username or user_id, Returns empty result if user is not found.
92 This command can be executed only using api_key belonging to user with admin
92 This command can be executed only using api_key belonging to user with admin
93 rights.
93 rights.
94
94
95
95
96 INPUT::
96 INPUT::
97
97
98 id : <id_for_response>
98 id : <id_for_response>
99 api_key : "<api_key>"
99 api_key : "<api_key>"
100 method : "get_user"
100 method : "get_user"
101 args : {
101 args : {
102 "userid" : "<username or user_id>"
102 "userid" : "<username or user_id>"
103 }
103 }
104
104
105 OUTPUT::
105 OUTPUT::
106
106
107 result: None if user does not exist or
107 result: None if user does not exist or
108 {
108 {
109 "id" : "<id>",
109 "id" : "<id>",
110 "username" : "<username>",
110 "username" : "<username>",
111 "firstname": "<firstname>",
111 "firstname": "<firstname>",
112 "lastname" : "<lastname>",
112 "lastname" : "<lastname>",
113 "email" : "<email>",
113 "email" : "<email>",
114 "active" : "<bool>",
114 "active" : "<bool>",
115 "admin" :Β  "<bool>",
115 "admin" :Β  "<bool>",
116 "ldap_dn" : "<ldap_dn>",
116 "ldap_dn" : "<ldap_dn>",
117 "last_login": "<last_login>",
117 "last_login": "<last_login>",
118 "permissions": {
118 "permissions": {
119 "global": ["hg.create.repository",
119 "global": ["hg.create.repository",
120 "repository.read",
120 "repository.read",
121 "hg.register.manual_activate"],
121 "hg.register.manual_activate"],
122 "repositories": {"repo1": "repository.none"},
122 "repositories": {"repo1": "repository.none"},
123 "repositories_groups": {"Group1": "group.read"}
123 "repositories_groups": {"Group1": "group.read"}
124 },
124 },
125 }
125 }
126
126
127 error: null
127 error: null
128
128
129
129
130 get_users
130 get_users
131 ---------
131 ---------
132
132
133 Lists all existing users. This command can be executed only using api_key
133 Lists all existing users. This command can be executed only using api_key
134 belonging to user with admin rights.
134 belonging to user with admin rights.
135
135
136
136
137 INPUT::
137 INPUT::
138
138
139 id : <id_for_response>
139 id : <id_for_response>
140 api_key : "<api_key>"
140 api_key : "<api_key>"
141 method : "get_users"
141 method : "get_users"
142 args : { }
142 args : { }
143
143
144 OUTPUT::
144 OUTPUT::
145
145
146 result: [
146 result: [
147 {
147 {
148 "id" : "<id>",
148 "id" : "<id>",
149 "username" : "<username>",
149 "username" : "<username>",
150 "firstname": "<firstname>",
150 "firstname": "<firstname>",
151 "lastname" : "<lastname>",
151 "lastname" : "<lastname>",
152 "email" : "<email>",
152 "email" : "<email>",
153 "active" : "<bool>",
153 "active" : "<bool>",
154 "admin" :Β  "<bool>",
154 "admin" :Β  "<bool>",
155 "ldap_dn" : "<ldap_dn>",
155 "ldap_dn" : "<ldap_dn>",
156 "last_login": "<last_login>",
156 "last_login": "<last_login>",
157 },
157 },
158 …
158 …
159 ]
159 ]
160 error: null
160 error: null
161
161
162
162
163 create_user
163 create_user
164 -----------
164 -----------
165
165
166 Creates new user. This command can
166 Creates new user. This command can
167 be executed only using api_key belonging to user with admin rights.
167 be executed only using api_key belonging to user with admin rights.
168
168
169
169
170 INPUT::
170 INPUT::
171
171
172 id : <id_for_response>
172 id : <id_for_response>
173 api_key : "<api_key>"
173 api_key : "<api_key>"
174 method : "create_user"
174 method : "create_user"
175 args : {
175 args : {
176 "username" : "<username>",
176 "username" : "<username>",
177 "password" : "<password>",
177 "password" : "<password>",
178 "email" : "<useremail>",
178 "email" : "<useremail>",
179 "firstname" : "<firstname> = None",
179 "firstname" : "<firstname> = None",
180 "lastname" : "<lastname> = None",
180 "lastname" : "<lastname> = None",
181 "active" : "<bool> = True",
181 "active" : "<bool> = True",
182 "admin" : "<bool> = False",
182 "admin" : "<bool> = False",
183 "ldap_dn" : "<ldap_dn> = None"
183 "ldap_dn" : "<ldap_dn> = None"
184 }
184 }
185
185
186 OUTPUT::
186 OUTPUT::
187
187
188 result: {
188 result: {
189 "id" : "<new_user_id>",
189 "id" : "<new_user_id>",
190 "msg" : "created new user <username>",
190 "msg" : "created new user <username>",
191 "user": {
191 "user": {
192 "id" : "<id>",
192 "id" : "<id>",
193 "username" : "<username>",
193 "username" : "<username>",
194 "firstname": "<firstname>",
194 "firstname": "<firstname>",
195 "lastname" : "<lastname>",
195 "lastname" : "<lastname>",
196 "email" : "<email>",
196 "email" : "<email>",
197 "active" : "<bool>",
197 "active" : "<bool>",
198 "admin" :Β  "<bool>",
198 "admin" :Β  "<bool>",
199 "ldap_dn" : "<ldap_dn>",
199 "ldap_dn" : "<ldap_dn>",
200 "last_login": "<last_login>",
200 "last_login": "<last_login>",
201 },
201 },
202 }
202 }
203 error: null
203 error: null
204
204
205
205
206 update_user
206 update_user
207 -----------
207 -----------
208
208
209 updates given user if such user exists. This command can
209 updates given user if such user exists. This command can
210 be executed only using api_key belonging to user with admin rights.
210 be executed only using api_key belonging to user with admin rights.
211
211
212
212
213 INPUT::
213 INPUT::
214
214
215 id : <id_for_response>
215 id : <id_for_response>
216 api_key : "<api_key>"
216 api_key : "<api_key>"
217 method : "update_user"
217 method : "update_user"
218 args : {
218 args : {
219 "userid" : "<user_id or username>",
219 "userid" : "<user_id or username>",
220 "username" : "<username>",
220 "username" : "<username>",
221 "password" : "<password>",
221 "password" : "<password>",
222 "email" : "<useremail>",
222 "email" : "<useremail>",
223 "firstname" : "<firstname>",
223 "firstname" : "<firstname>",
224 "lastname" : "<lastname>",
224 "lastname" : "<lastname>",
225 "active" : "<bool>",
225 "active" : "<bool>",
226 "admin" : "<bool>",
226 "admin" : "<bool>",
227 "ldap_dn" : "<ldap_dn>"
227 "ldap_dn" : "<ldap_dn>"
228 }
228 }
229
229
230 OUTPUT::
230 OUTPUT::
231
231
232 result: {
232 result: {
233 "id" : "<edited_user_id>",
233 "id" : "<edited_user_id>",
234 "msg" : "updated user ID:<userid> <username>"
234 "msg" : "updated user ID:<userid> <username>"
235 }
235 }
236 error: null
236 error: null
237
237
238
238
239 delete_user
239 delete_user
240 -----------
240 -----------
241
241
242
242
243 deletes givenuser if such user exists. This command can
243 deletes givenuser if such user exists. This command can
244 be executed only using api_key belonging to user with admin rights.
244 be executed only using api_key belonging to user with admin rights.
245
245
246
246
247 INPUT::
247 INPUT::
248
248
249 id : <id_for_response>
249 id : <id_for_response>
250 api_key : "<api_key>"
250 api_key : "<api_key>"
251 method : "delete_user"
251 method : "delete_user"
252 args : {
252 args : {
253 "userid" : "<user_id or username>",
253 "userid" : "<user_id or username>",
254 }
254 }
255
255
256 OUTPUT::
256 OUTPUT::
257
257
258 result: {
258 result: {
259 "id" : "<edited_user_id>",
259 "id" : "<edited_user_id>",
260 "msg" : "deleted user ID:<userid> <username>"
260 "msg" : "deleted user ID:<userid> <username>"
261 }
261 }
262 error: null
262 error: null
263
263
264
264
265 get_users_group
265 get_users_group
266 ---------------
266 ---------------
267
267
268 Gets an existing users group. This command can be executed only using api_key
268 Gets an existing users group. This command can be executed only using api_key
269 belonging to user with admin rights.
269 belonging to user with admin rights.
270
270
271
271
272 INPUT::
272 INPUT::
273
273
274 id : <id_for_response>
274 id : <id_for_response>
275 api_key : "<api_key>"
275 api_key : "<api_key>"
276 method : "get_users_group"
276 method : "get_users_group"
277 args : {
277 args : {
278 "group_name" : "<name>"
278 "group_name" : "<name>"
279 }
279 }
280
280
281 OUTPUT::
281 OUTPUT::
282
282
283 result : None if group not exist
283 result : None if group not exist
284 {
284 {
285 "id" : "<id>",
285 "id" : "<id>",
286 "group_name" : "<groupname>",
286 "group_name" : "<groupname>",
287 "active": "<bool>",
287 "active": "<bool>",
288 "members" : [
288 "members" : [
289 { "id" : "<userid>",
289 { "id" : "<userid>",
290 "username" : "<username>",
290 "username" : "<username>",
291 "firstname": "<firstname>",
291 "firstname": "<firstname>",
292 "lastname" : "<lastname>",
292 "lastname" : "<lastname>",
293 "email" : "<email>",
293 "email" : "<email>",
294 "active" : "<bool>",
294 "active" : "<bool>",
295 "admin" :Β  "<bool>",
295 "admin" :Β  "<bool>",
296 "ldap" : "<ldap_dn>"
296 "ldap" : "<ldap_dn>"
297 },
297 },
298 …
298 …
299 ]
299 ]
300 }
300 }
301 error : null
301 error : null
302
302
303
303
304 get_users_groups
304 get_users_groups
305 ----------------
305 ----------------
306
306
307 Lists all existing users groups. This command can be executed only using
307 Lists all existing users groups. This command can be executed only using
308 api_key belonging to user with admin rights.
308 api_key belonging to user with admin rights.
309
309
310
310
311 INPUT::
311 INPUT::
312
312
313 id : <id_for_response>
313 id : <id_for_response>
314 api_key : "<api_key>"
314 api_key : "<api_key>"
315 method : "get_users_groups"
315 method : "get_users_groups"
316 args : { }
316 args : { }
317
317
318 OUTPUT::
318 OUTPUT::
319
319
320 result : [
320 result : [
321 {
321 {
322 "id" : "<id>",
322 "id" : "<id>",
323 "group_name" : "<groupname>",
323 "group_name" : "<groupname>",
324 "active": "<bool>",
324 "active": "<bool>",
325 "members" : [
325 "members" : [
326 {
326 {
327 "id" : "<userid>",
327 "id" : "<userid>",
328 "username" : "<username>",
328 "username" : "<username>",
329 "firstname": "<firstname>",
329 "firstname": "<firstname>",
330 "lastname" : "<lastname>",
330 "lastname" : "<lastname>",
331 "email" : "<email>",
331 "email" : "<email>",
332 "active" : "<bool>",
332 "active" : "<bool>",
333 "admin" :Β  "<bool>",
333 "admin" :Β  "<bool>",
334 "ldap" : "<ldap_dn>"
334 "ldap" : "<ldap_dn>"
335 },
335 },
336 …
336 …
337 ]
337 ]
338 }
338 }
339 ]
339 ]
340 error : null
340 error : null
341
341
342
342
343 create_users_group
343 create_users_group
344 ------------------
344 ------------------
345
345
346 Creates new users group. This command can be executed only using api_key
346 Creates new users group. This command can be executed only using api_key
347 belonging to user with admin rights
347 belonging to user with admin rights
348
348
349
349
350 INPUT::
350 INPUT::
351
351
352 id : <id_for_response>
352 id : <id_for_response>
353 api_key : "<api_key>"
353 api_key : "<api_key>"
354 method : "create_users_group"
354 method : "create_users_group"
355 args: {
355 args: {
356 "group_name": "<groupname>",
356 "group_name": "<groupname>",
357 "active":"<bool> = True"
357 "active":"<bool> = True"
358 }
358 }
359
359
360 OUTPUT::
360 OUTPUT::
361
361
362 result: {
362 result: {
363 "id": "<newusersgroupid>",
363 "id": "<newusersgroupid>",
364 "msg": "created new users group <groupname>"
364 "msg": "created new users group <groupname>"
365 }
365 }
366 error: null
366 error: null
367
367
368
368
369 add_user_to_users_group
369 add_user_to_users_group
370 -----------------------
370 -----------------------
371
371
372 Adds a user to a users group. If user exists in that group success will be
372 Adds a user to a users group. If user exists in that group success will be
373 `false`. This command can be executed only using api_key
373 `false`. This command can be executed only using api_key
374 belonging to user with admin rights
374 belonging to user with admin rights
375
375
376
376
377 INPUT::
377 INPUT::
378
378
379 id : <id_for_response>
379 id : <id_for_response>
380 api_key : "<api_key>"
380 api_key : "<api_key>"
381 method : "add_user_users_group"
381 method : "add_user_users_group"
382 args: {
382 args: {
383 "group_name" : "<groupname>",
383 "group_name" : "<groupname>",
384 "username" : "<username>"
384 "username" : "<username>"
385 }
385 }
386
386
387 OUTPUT::
387 OUTPUT::
388
388
389 result: {
389 result: {
390 "id": "<newusersgroupmemberid>",
390 "id": "<newusersgroupmemberid>",
391 "success": True|False # depends on if member is in group
391 "success": True|False # depends on if member is in group
392 "msg": "added member <username> to users group <groupname> |
392 "msg": "added member <username> to users group <groupname> |
393 User is already in that group"
393 User is already in that group"
394 }
394 }
395 error: null
395 error: null
396
396
397
397
398 remove_user_from_users_group
398 remove_user_from_users_group
399 ----------------------------
399 ----------------------------
400
400
401 Removes a user from a users group. If user is not in given group success will
401 Removes a user from a users group. If user is not in given group success will
402 be `false`. This command can be executed only
402 be `false`. This command can be executed only
403 using api_key belonging to user with admin rights
403 using api_key belonging to user with admin rights
404
404
405
405
406 INPUT::
406 INPUT::
407
407
408 id : <id_for_response>
408 id : <id_for_response>
409 api_key : "<api_key>"
409 api_key : "<api_key>"
410 method : "remove_user_from_users_group"
410 method : "remove_user_from_users_group"
411 args: {
411 args: {
412 "group_name" : "<groupname>",
412 "group_name" : "<groupname>",
413 "username" : "<username>"
413 "username" : "<username>"
414 }
414 }
415
415
416 OUTPUT::
416 OUTPUT::
417
417
418 result: {
418 result: {
419 "success": True|False, # depends on if member is in group
419 "success": True|False, # depends on if member is in group
420 "msg": "removed member <username> from users group <groupname> |
420 "msg": "removed member <username> from users group <groupname> |
421 User wasn't in group"
421 User wasn't in group"
422 }
422 }
423 error: null
423 error: null
424
424
425
425
426 get_repo
426 get_repo
427 --------
427 --------
428
428
429 Gets an existing repository by it's name or repository_id. Members will return
429 Gets an existing repository by it's name or repository_id. Members will return
430 either users_group or user associated to that repository. This command can
430 either users_group or user associated to that repository. This command can
431 be executed only using api_key belonging to user with admin rights.
431 be executed only using api_key belonging to user with admin rights.
432
432
433
433
434 INPUT::
434 INPUT::
435
435
436 id : <id_for_response>
436 id : <id_for_response>
437 api_key : "<api_key>"
437 api_key : "<api_key>"
438 method : "get_repo"
438 method : "get_repo"
439 args: {
439 args: {
440 "repoid" : "<reponame or repo_id>"
440 "repoid" : "<reponame or repo_id>"
441 }
441 }
442
442
443 OUTPUT::
443 OUTPUT::
444
444
445 result: None if repository does not exist or
445 result: None if repository does not exist or
446 {
446 {
447 "id" : "<id>",
447 "id" : "<id>",
448 "repo_name" : "<reponame>"
448 "repo_name" : "<reponame>"
449 "type" : "<type>",
449 "type" : "<type>",
450 "description" : "<description>",
450 "description" : "<description>",
451 "clone_uri" : "<clone_uri>",
451 "clone_uri" : "<clone_uri>",
452 "private": : "<bool>",
452 "private": : "<bool>",
453 "created_on" : "<datetimecreated>",
453 "created_on" : "<datetimecreated>",
454 "members" : [
454 "members" : [
455 {
455 {
456 "type": "user",
456 "type": "user",
457 "id" : "<userid>",
457 "id" : "<userid>",
458 "username" : "<username>",
458 "username" : "<username>",
459 "firstname": "<firstname>",
459 "firstname": "<firstname>",
460 "lastname" : "<lastname>",
460 "lastname" : "<lastname>",
461 "email" : "<email>",
461 "email" : "<email>",
462 "active" : "<bool>",
462 "active" : "<bool>",
463 "admin" :Β  "<bool>",
463 "admin" :Β  "<bool>",
464 "ldap" : "<ldap_dn>",
464 "ldap" : "<ldap_dn>",
465 "permission" : "repository.(read|write|admin)"
465 "permission" : "repository.(read|write|admin)"
466 },
466 },
467 …
467 …
468 {
468 {
469 "type": "users_group",
469 "type": "users_group",
470 "id" : "<usersgroupid>",
470 "id" : "<usersgroupid>",
471 "name" : "<usersgroupname>",
471 "name" : "<usersgroupname>",
472 "active": "<bool>",
472 "active": "<bool>",
473 "permission" : "repository.(read|write|admin)"
473 "permission" : "repository.(read|write|admin)"
474 },
474 },
475 …
475 …
476 ]
476 ]
477 }
477 }
478 error: null
478 error: null
479
479
480
480
481 get_repos
481 get_repos
482 ---------
482 ---------
483
483
484 Lists all existing repositories. This command can be executed only using api_key
484 Lists all existing repositories. This command can be executed only using api_key
485 belonging to user with admin rights
485 belonging to user with admin rights
486
486
487
487
488 INPUT::
488 INPUT::
489
489
490 id : <id_for_response>
490 id : <id_for_response>
491 api_key : "<api_key>"
491 api_key : "<api_key>"
492 method : "get_repos"
492 method : "get_repos"
493 args: { }
493 args: { }
494
494
495 OUTPUT::
495 OUTPUT::
496
496
497 result: [
497 result: [
498 {
498 {
499 "id" : "<id>",
499 "id" : "<id>",
500 "repo_name" : "<reponame>"
500 "repo_name" : "<reponame>"
501 "type" : "<type>",
501 "type" : "<type>",
502 "description" : "<description>",
502 "description" : "<description>",
503 "clone_uri" : "<clone_uri>",
503 "clone_uri" : "<clone_uri>",
504 "private": : "<bool>",
504 "private": : "<bool>",
505 "created_on" : "<datetimecreated>",
505 "created_on" : "<datetimecreated>",
506 },
506 },
507 …
507 …
508 ]
508 ]
509 error: null
509 error: null
510
510
511
511
512 get_repo_nodes
512 get_repo_nodes
513 --------------
513 --------------
514
514
515 returns a list of nodes and it's children in a flat list for a given path
515 returns a list of nodes and it's children in a flat list for a given path
516 at given revision. It's possible to specify ret_type to show only `files` or
516 at given revision. It's possible to specify ret_type to show only `files` or
517 `dirs`. This command can be executed only using api_key belonging to user
517 `dirs`. This command can be executed only using api_key belonging to user
518 with admin rights
518 with admin rights
519
519
520
520
521 INPUT::
521 INPUT::
522
522
523 id : <id_for_response>
523 id : <id_for_response>
524 api_key : "<api_key>"
524 api_key : "<api_key>"
525 method : "get_repo_nodes"
525 method : "get_repo_nodes"
526 args: {
526 args: {
527 "repo_name" : "<reponame>",
527 "repo_name" : "<reponame>",
528 "revision" : "<revision>",
528 "revision" : "<revision>",
529 "root_path" : "<root_path>",
529 "root_path" : "<root_path>",
530 "ret_type" : "<ret_type>" = 'all'
530 "ret_type" : "<ret_type>" = 'all'
531 }
531 }
532
532
533 OUTPUT::
533 OUTPUT::
534
534
535 result: [
535 result: [
536 {
536 {
537 "name" : "<name>"
537 "name" : "<name>"
538 "type" : "<type>",
538 "type" : "<type>",
539 },
539 },
540 …
540 …
541 ]
541 ]
542 error: null
542 error: null
543
543
544
544
545 create_repo
545 create_repo
546 -----------
546 -----------
547
547
548 Creates a repository. This command can be executed only using api_key
548 Creates a repository. This command can be executed only using api_key
549 belonging to user with admin rights.
549 belonging to user with admin rights.
550 If repository name contains "/", all needed repository groups will be created.
550 If repository name contains "/", all needed repository groups will be created.
551 For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent),
551 For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent),
552 and create "baz" repository with "bar" as group.
552 and create "baz" repository with "bar" as group.
553
553
554
554
555 INPUT::
555 INPUT::
556
556
557 id : <id_for_response>
557 id : <id_for_response>
558 api_key : "<api_key>"
558 api_key : "<api_key>"
559 method : "create_repo"
559 method : "create_repo"
560 args: {
560 args: {
561 "repo_name" : "<reponame>",
561 "repo_name" : "<reponame>",
562 "owner_name" : "<ownername>",
562 "owner_name" : "<ownername>",
563 "description" : "<description> = ''",
563 "description" : "<description> = ''",
564 "repo_type" : "<type> = 'hg'",
564 "repo_type" : "<type> = 'hg'",
565 "private" : "<bool> = False",
565 "private" : "<bool> = False",
566 "clone_uri" : "<clone_uri> = None",
566 "clone_uri" : "<clone_uri> = None",
567 }
567 }
568
568
569 OUTPUT::
569 OUTPUT::
570
570
571 result: {
571 result: {
572 "id": "<newrepoid>",
572 "id": "<newrepoid>",
573 "msg": "Created new repository <reponame>",
573 "msg": "Created new repository <reponame>",
574 "repo": {
575 "id" : "<id>",
576 "repo_name" : "<reponame>"
577 "type" : "<type>",
578 "description" : "<description>",
579 "clone_uri" : "<clone_uri>",
580 "private": : "<bool>",
581 "created_on" : "<datetimecreated>",
582 },
574 }
583 }
575 error: null
584 error: null
576
585
577
586
578 delete_repo
587 delete_repo
579 -----------
588 -----------
580
589
581 Deletes a repository. This command can be executed only using api_key
590 Deletes a repository. This command can be executed only using api_key
582 belonging to user with admin rights.
591 belonging to user with admin rights.
583
592
584
593
585 INPUT::
594 INPUT::
586
595
587 id : <id_for_response>
596 id : <id_for_response>
588 api_key : "<api_key>"
597 api_key : "<api_key>"
589 method : "delete_repo"
598 method : "delete_repo"
590 args: {
599 args: {
591 "repo_name" : "<reponame>",
600 "repo_name" : "<reponame>",
592 }
601 }
593
602
594 OUTPUT::
603 OUTPUT::
595
604
596 result: {
605 result: {
597 "msg": "Deleted repository <reponame>",
606 "msg": "Deleted repository <reponame>",
598 }
607 }
599 error: null
608 error: null
600
609
601
610
602 grant_user_permission
611 grant_user_permission
603 ---------------------
612 ---------------------
604
613
605 Grant permission for user on given repository, or update existing one
614 Grant permission for user on given repository, or update existing one
606 if found. This command can be executed only using api_key belonging to user
615 if found. This command can be executed only using api_key belonging to user
607 with admin rights.
616 with admin rights.
608
617
609
618
610 INPUT::
619 INPUT::
611
620
612 id : <id_for_response>
621 id : <id_for_response>
613 api_key : "<api_key>"
622 api_key : "<api_key>"
614 method : "grant_user_permission"
623 method : "grant_user_permission"
615 args: {
624 args: {
616 "repo_name" : "<reponame>",
625 "repo_name" : "<reponame>",
617 "username" : "<username>",
626 "username" : "<username>",
618 "perm" : "(repository.(none|read|write|admin))",
627 "perm" : "(repository.(none|read|write|admin))",
619 }
628 }
620
629
621 OUTPUT::
630 OUTPUT::
622
631
623 result: {
632 result: {
624 "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>"
633 "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>"
625 }
634 }
626 error: null
635 error: null
627
636
628
637
629 revoke_user_permission
638 revoke_user_permission
630 ----------------------
639 ----------------------
631
640
632 Revoke permission for user on given repository. This command can be executed
641 Revoke permission for user on given repository. This command can be executed
633 only using api_key belonging to user with admin rights.
642 only using api_key belonging to user with admin rights.
634
643
635
644
636 INPUT::
645 INPUT::
637
646
638 id : <id_for_response>
647 id : <id_for_response>
639 api_key : "<api_key>"
648 api_key : "<api_key>"
640 method : "revoke_user_permission"
649 method : "revoke_user_permission"
641 args: {
650 args: {
642 "repo_name" : "<reponame>",
651 "repo_name" : "<reponame>",
643 "username" : "<username>",
652 "username" : "<username>",
644 }
653 }
645
654
646 OUTPUT::
655 OUTPUT::
647
656
648 result: {
657 result: {
649 "msg" : "Revoked perm for user: <suername> in repo: <reponame>"
658 "msg" : "Revoked perm for user: <suername> in repo: <reponame>"
650 }
659 }
651 error: null
660 error: null
652
661
653
662
654 grant_users_group_permission
663 grant_users_group_permission
655 ----------------------------
664 ----------------------------
656
665
657 Grant permission for users group on given repository, or update
666 Grant permission for users group on given repository, or update
658 existing one if found. This command can be executed only using
667 existing one if found. This command can be executed only using
659 api_key belonging to user with admin rights.
668 api_key belonging to user with admin rights.
660
669
661
670
662 INPUT::
671 INPUT::
663
672
664 id : <id_for_response>
673 id : <id_for_response>
665 api_key : "<api_key>"
674 api_key : "<api_key>"
666 method : "grant_users_group_permission"
675 method : "grant_users_group_permission"
667 args: {
676 args: {
668 "repo_name" : "<reponame>",
677 "repo_name" : "<reponame>",
669 "group_name" : "<usersgroupname>",
678 "group_name" : "<usersgroupname>",
670 "perm" : "(repository.(none|read|write|admin))",
679 "perm" : "(repository.(none|read|write|admin))",
671 }
680 }
672
681
673 OUTPUT::
682 OUTPUT::
674
683
675 result: {
684 result: {
676 "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>"
685 "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>"
677 }
686 }
678 error: null
687 error: null
679
688
680
689
681 revoke_users_group_permission
690 revoke_users_group_permission
682 -----------------------------
691 -----------------------------
683
692
684 Revoke permission for users group on given repository.This command can be
693 Revoke permission for users group on given repository.This command can be
685 executed only using api_key belonging to user with admin rights.
694 executed only using api_key belonging to user with admin rights.
686
695
687 INPUT::
696 INPUT::
688
697
689 id : <id_for_response>
698 id : <id_for_response>
690 api_key : "<api_key>"
699 api_key : "<api_key>"
691 method : "revoke_users_group_permission"
700 method : "revoke_users_group_permission"
692 args: {
701 args: {
693 "repo_name" : "<reponame>",
702 "repo_name" : "<reponame>",
694 "users_group" : "<usersgroupname>",
703 "users_group" : "<usersgroupname>",
695 }
704 }
696
705
697 OUTPUT::
706 OUTPUT::
698
707
699 result: {
708 result: {
700 "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>"
709 "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>"
701 }
710 }
702 error: null No newline at end of file
711 error: null
@@ -1,698 +1,706 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.controllers.api
3 rhodecode.controllers.api
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
5
6 API controller for RhodeCode
6 API controller for RhodeCode
7
7
8 :created_on: Aug 20, 2011
8 :created_on: Aug 20, 2011
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software; you can redistribute it and/or
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; version 2
15 # as published by the Free Software Foundation; version 2
16 # of the License or (at your opinion) any later version of the license.
16 # of the License or (at your opinion) any later version of the license.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # MA 02110-1301, USA.
26 # MA 02110-1301, USA.
27
27
28 import traceback
28 import traceback
29 import logging
29 import logging
30
30
31 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
31 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
32 from rhodecode.lib.auth import HasPermissionAllDecorator, \
32 from rhodecode.lib.auth import HasPermissionAllDecorator, \
33 HasPermissionAnyDecorator, PasswordGenerator, AuthUser
33 HasPermissionAnyDecorator, PasswordGenerator, AuthUser
34
34
35 from rhodecode.model.meta import Session
35 from rhodecode.model.meta import Session
36 from rhodecode.model.scm import ScmModel
36 from rhodecode.model.scm import ScmModel
37 from rhodecode.model.db import User, UsersGroup, Repository
37 from rhodecode.model.db import User, UsersGroup, Repository
38 from rhodecode.model.repo import RepoModel
38 from rhodecode.model.repo import RepoModel
39 from rhodecode.model.user import UserModel
39 from rhodecode.model.user import UserModel
40 from rhodecode.model.users_group import UsersGroupModel
40 from rhodecode.model.users_group import UsersGroupModel
41 from rhodecode.lib.utils import map_groups
41 from rhodecode.lib.utils import map_groups
42
42
43 log = logging.getLogger(__name__)
43 log = logging.getLogger(__name__)
44
44
45
45
46 class ApiController(JSONRPCController):
46 class ApiController(JSONRPCController):
47 """
47 """
48 API Controller
48 API Controller
49
49
50
50
51 Each method needs to have USER as argument this is then based on given
51 Each method needs to have USER as argument this is then based on given
52 API_KEY propagated as instance of user object
52 API_KEY propagated as instance of user object
53
53
54 Preferably this should be first argument also
54 Preferably this should be first argument also
55
55
56
56
57 Each function should also **raise** JSONRPCError for any
57 Each function should also **raise** JSONRPCError for any
58 errors that happens
58 errors that happens
59
59
60 """
60 """
61
61
62 @HasPermissionAllDecorator('hg.admin')
62 @HasPermissionAllDecorator('hg.admin')
63 def pull(self, apiuser, repo_name):
63 def pull(self, apiuser, repo_name):
64 """
64 """
65 Dispatch pull action on given repo
65 Dispatch pull action on given repo
66
66
67
67
68 :param user:
68 :param user:
69 :param repo_name:
69 :param repo_name:
70 """
70 """
71
71
72 if Repository.is_valid(repo_name) is False:
72 if Repository.is_valid(repo_name) is False:
73 raise JSONRPCError('Unknown repo "%s"' % repo_name)
73 raise JSONRPCError('Unknown repo "%s"' % repo_name)
74
74
75 try:
75 try:
76 ScmModel().pull_changes(repo_name, self.rhodecode_user.username)
76 ScmModel().pull_changes(repo_name, self.rhodecode_user.username)
77 return 'Pulled from %s' % repo_name
77 return 'Pulled from %s' % repo_name
78 except Exception:
78 except Exception:
79 raise JSONRPCError('Unable to pull changes from "%s"' % repo_name)
79 raise JSONRPCError('Unable to pull changes from "%s"' % repo_name)
80
80
81 @HasPermissionAllDecorator('hg.admin')
81 @HasPermissionAllDecorator('hg.admin')
82 def get_user(self, apiuser, userid):
82 def get_user(self, apiuser, userid):
83 """"
83 """"
84 Get a user by username
84 Get a user by username
85
85
86 :param apiuser:
86 :param apiuser:
87 :param username:
87 :param username:
88 """
88 """
89
89
90 user = UserModel().get_user(userid)
90 user = UserModel().get_user(userid)
91 if user is None:
91 if user is None:
92 return user
92 return user
93
93
94 return dict(
94 return dict(
95 id=user.user_id,
95 id=user.user_id,
96 username=user.username,
96 username=user.username,
97 firstname=user.name,
97 firstname=user.name,
98 lastname=user.lastname,
98 lastname=user.lastname,
99 email=user.email,
99 email=user.email,
100 active=user.active,
100 active=user.active,
101 admin=user.admin,
101 admin=user.admin,
102 ldap_dn=user.ldap_dn,
102 ldap_dn=user.ldap_dn,
103 last_login=user.last_login,
103 last_login=user.last_login,
104 permissions=AuthUser(user_id=user.user_id).permissions
104 permissions=AuthUser(user_id=user.user_id).permissions
105 )
105 )
106
106
107 @HasPermissionAllDecorator('hg.admin')
107 @HasPermissionAllDecorator('hg.admin')
108 def get_users(self, apiuser):
108 def get_users(self, apiuser):
109 """"
109 """"
110 Get all users
110 Get all users
111
111
112 :param apiuser:
112 :param apiuser:
113 """
113 """
114
114
115 result = []
115 result = []
116 for user in User.getAll():
116 for user in User.getAll():
117 result.append(
117 result.append(
118 dict(
118 dict(
119 id=user.user_id,
119 id=user.user_id,
120 username=user.username,
120 username=user.username,
121 firstname=user.name,
121 firstname=user.name,
122 lastname=user.lastname,
122 lastname=user.lastname,
123 email=user.email,
123 email=user.email,
124 active=user.active,
124 active=user.active,
125 admin=user.admin,
125 admin=user.admin,
126 ldap_dn=user.ldap_dn,
126 ldap_dn=user.ldap_dn,
127 last_login=user.last_login,
127 last_login=user.last_login,
128 )
128 )
129 )
129 )
130 return result
130 return result
131
131
132 @HasPermissionAllDecorator('hg.admin')
132 @HasPermissionAllDecorator('hg.admin')
133 def create_user(self, apiuser, username, email, password, firstname=None,
133 def create_user(self, apiuser, username, email, password, firstname=None,
134 lastname=None, active=True, admin=False, ldap_dn=None):
134 lastname=None, active=True, admin=False, ldap_dn=None):
135 """
135 """
136 Create new user
136 Create new user
137
137
138 :param apiuser:
138 :param apiuser:
139 :param username:
139 :param username:
140 :param password:
140 :param password:
141 :param email:
141 :param email:
142 :param name:
142 :param name:
143 :param lastname:
143 :param lastname:
144 :param active:
144 :param active:
145 :param admin:
145 :param admin:
146 :param ldap_dn:
146 :param ldap_dn:
147 """
147 """
148 if User.get_by_username(username):
148 if User.get_by_username(username):
149 raise JSONRPCError("user %s already exist" % username)
149 raise JSONRPCError("user %s already exist" % username)
150
150
151 if User.get_by_email(email, case_insensitive=True):
151 if User.get_by_email(email, case_insensitive=True):
152 raise JSONRPCError("email %s already exist" % email)
152 raise JSONRPCError("email %s already exist" % email)
153
153
154 if ldap_dn:
154 if ldap_dn:
155 # generate temporary password if ldap_dn
155 # generate temporary password if ldap_dn
156 password = PasswordGenerator().gen_password(length=8)
156 password = PasswordGenerator().gen_password(length=8)
157
157
158 try:
158 try:
159 user = UserModel().create_or_update(
159 user = UserModel().create_or_update(
160 username, password, email, firstname,
160 username, password, email, firstname,
161 lastname, active, admin, ldap_dn
161 lastname, active, admin, ldap_dn
162 )
162 )
163 Session.commit()
163 Session.commit()
164 return dict(
164 return dict(
165 id=user.user_id,
165 id=user.user_id,
166 msg='created new user %s' % username,
166 msg='created new user %s' % username,
167 user=dict(
167 user=dict(
168 id=user.user_id,
168 id=user.user_id,
169 username=user.username,
169 username=user.username,
170 firstname=user.name,
170 firstname=user.name,
171 lastname=user.lastname,
171 lastname=user.lastname,
172 email=user.email,
172 email=user.email,
173 active=user.active,
173 active=user.active,
174 admin=user.admin,
174 admin=user.admin,
175 ldap_dn=user.ldap_dn,
175 ldap_dn=user.ldap_dn,
176 last_login=user.last_login,
176 last_login=user.last_login,
177 )
177 )
178 )
178 )
179 except Exception:
179 except Exception:
180 log.error(traceback.format_exc())
180 log.error(traceback.format_exc())
181 raise JSONRPCError('failed to create user %s' % username)
181 raise JSONRPCError('failed to create user %s' % username)
182
182
183 @HasPermissionAllDecorator('hg.admin')
183 @HasPermissionAllDecorator('hg.admin')
184 def update_user(self, apiuser, userid, username, password, email,
184 def update_user(self, apiuser, userid, username, password, email,
185 firstname, lastname, active, admin, ldap_dn):
185 firstname, lastname, active, admin, ldap_dn):
186 """
186 """
187 Updates given user
187 Updates given user
188
188
189 :param apiuser:
189 :param apiuser:
190 :param username:
190 :param username:
191 :param password:
191 :param password:
192 :param email:
192 :param email:
193 :param name:
193 :param name:
194 :param lastname:
194 :param lastname:
195 :param active:
195 :param active:
196 :param admin:
196 :param admin:
197 :param ldap_dn:
197 :param ldap_dn:
198 """
198 """
199 usr = UserModel().get_user(userid)
199 usr = UserModel().get_user(userid)
200 if not usr:
200 if not usr:
201 raise JSONRPCError("user ID:%s does not exist" % userid)
201 raise JSONRPCError("user ID:%s does not exist" % userid)
202
202
203 try:
203 try:
204 usr = UserModel().create_or_update(
204 usr = UserModel().create_or_update(
205 username, password, email, firstname,
205 username, password, email, firstname,
206 lastname, active, admin, ldap_dn
206 lastname, active, admin, ldap_dn
207 )
207 )
208 Session.commit()
208 Session.commit()
209 return dict(
209 return dict(
210 id=usr.user_id,
210 id=usr.user_id,
211 msg='updated user ID:%s %s' % (usr.user_id, usr.username)
211 msg='updated user ID:%s %s' % (usr.user_id, usr.username)
212 )
212 )
213 except Exception:
213 except Exception:
214 log.error(traceback.format_exc())
214 log.error(traceback.format_exc())
215 raise JSONRPCError('failed to update user %s' % userid)
215 raise JSONRPCError('failed to update user %s' % userid)
216
216
217 @HasPermissionAllDecorator('hg.admin')
217 @HasPermissionAllDecorator('hg.admin')
218 def delete_user(self, apiuser, userid):
218 def delete_user(self, apiuser, userid):
219 """"
219 """"
220 Deletes an user
220 Deletes an user
221
221
222 :param apiuser:
222 :param apiuser:
223 """
223 """
224 usr = UserModel().get_user(userid)
224 usr = UserModel().get_user(userid)
225 if not usr:
225 if not usr:
226 raise JSONRPCError("user ID:%s does not exist" % userid)
226 raise JSONRPCError("user ID:%s does not exist" % userid)
227
227
228 try:
228 try:
229 UserModel().delete(userid)
229 UserModel().delete(userid)
230 Session.commit()
230 Session.commit()
231 return dict(
231 return dict(
232 id=usr.user_id,
232 id=usr.user_id,
233 msg='deleted user ID:%s %s' % (usr.user_id, usr.username)
233 msg='deleted user ID:%s %s' % (usr.user_id, usr.username)
234 )
234 )
235 except Exception:
235 except Exception:
236 log.error(traceback.format_exc())
236 log.error(traceback.format_exc())
237 raise JSONRPCError('failed to delete ID:%s %s' % (usr.user_id,
237 raise JSONRPCError('failed to delete ID:%s %s' % (usr.user_id,
238 usr.username))
238 usr.username))
239
239
240 @HasPermissionAllDecorator('hg.admin')
240 @HasPermissionAllDecorator('hg.admin')
241 def get_users_group(self, apiuser, group_name):
241 def get_users_group(self, apiuser, group_name):
242 """"
242 """"
243 Get users group by name
243 Get users group by name
244
244
245 :param apiuser:
245 :param apiuser:
246 :param group_name:
246 :param group_name:
247 """
247 """
248
248
249 users_group = UsersGroup.get_by_group_name(group_name)
249 users_group = UsersGroup.get_by_group_name(group_name)
250 if not users_group:
250 if not users_group:
251 return None
251 return None
252
252
253 members = []
253 members = []
254 for user in users_group.members:
254 for user in users_group.members:
255 user = user.user
255 user = user.user
256 members.append(dict(id=user.user_id,
256 members.append(dict(id=user.user_id,
257 username=user.username,
257 username=user.username,
258 firstname=user.name,
258 firstname=user.name,
259 lastname=user.lastname,
259 lastname=user.lastname,
260 email=user.email,
260 email=user.email,
261 active=user.active,
261 active=user.active,
262 admin=user.admin,
262 admin=user.admin,
263 ldap=user.ldap_dn))
263 ldap=user.ldap_dn))
264
264
265 return dict(id=users_group.users_group_id,
265 return dict(id=users_group.users_group_id,
266 group_name=users_group.users_group_name,
266 group_name=users_group.users_group_name,
267 active=users_group.users_group_active,
267 active=users_group.users_group_active,
268 members=members)
268 members=members)
269
269
270 @HasPermissionAllDecorator('hg.admin')
270 @HasPermissionAllDecorator('hg.admin')
271 def get_users_groups(self, apiuser):
271 def get_users_groups(self, apiuser):
272 """"
272 """"
273 Get all users groups
273 Get all users groups
274
274
275 :param apiuser:
275 :param apiuser:
276 """
276 """
277
277
278 result = []
278 result = []
279 for users_group in UsersGroup.getAll():
279 for users_group in UsersGroup.getAll():
280 members = []
280 members = []
281 for user in users_group.members:
281 for user in users_group.members:
282 user = user.user
282 user = user.user
283 members.append(dict(id=user.user_id,
283 members.append(dict(id=user.user_id,
284 username=user.username,
284 username=user.username,
285 firstname=user.name,
285 firstname=user.name,
286 lastname=user.lastname,
286 lastname=user.lastname,
287 email=user.email,
287 email=user.email,
288 active=user.active,
288 active=user.active,
289 admin=user.admin,
289 admin=user.admin,
290 ldap=user.ldap_dn))
290 ldap=user.ldap_dn))
291
291
292 result.append(dict(id=users_group.users_group_id,
292 result.append(dict(id=users_group.users_group_id,
293 group_name=users_group.users_group_name,
293 group_name=users_group.users_group_name,
294 active=users_group.users_group_active,
294 active=users_group.users_group_active,
295 members=members))
295 members=members))
296 return result
296 return result
297
297
298 @HasPermissionAllDecorator('hg.admin')
298 @HasPermissionAllDecorator('hg.admin')
299 def create_users_group(self, apiuser, group_name, active=True):
299 def create_users_group(self, apiuser, group_name, active=True):
300 """
300 """
301 Creates an new usergroup
301 Creates an new usergroup
302
302
303 :param group_name:
303 :param group_name:
304 :param active:
304 :param active:
305 """
305 """
306
306
307 if self.get_users_group(apiuser, group_name):
307 if self.get_users_group(apiuser, group_name):
308 raise JSONRPCError("users group %s already exist" % group_name)
308 raise JSONRPCError("users group %s already exist" % group_name)
309
309
310 try:
310 try:
311 ug = UsersGroupModel().create(name=group_name, active=active)
311 ug = UsersGroupModel().create(name=group_name, active=active)
312 Session.commit()
312 Session.commit()
313 return dict(id=ug.users_group_id,
313 return dict(id=ug.users_group_id,
314 msg='created new users group %s' % group_name)
314 msg='created new users group %s' % group_name)
315 except Exception:
315 except Exception:
316 log.error(traceback.format_exc())
316 log.error(traceback.format_exc())
317 raise JSONRPCError('failed to create group %s' % group_name)
317 raise JSONRPCError('failed to create group %s' % group_name)
318
318
319 @HasPermissionAllDecorator('hg.admin')
319 @HasPermissionAllDecorator('hg.admin')
320 def add_user_to_users_group(self, apiuser, group_name, username):
320 def add_user_to_users_group(self, apiuser, group_name, username):
321 """"
321 """"
322 Add a user to a users group
322 Add a user to a users group
323
323
324 :param apiuser:
324 :param apiuser:
325 :param group_name:
325 :param group_name:
326 :param username:
326 :param username:
327 """
327 """
328
328
329 try:
329 try:
330 users_group = UsersGroup.get_by_group_name(group_name)
330 users_group = UsersGroup.get_by_group_name(group_name)
331 if not users_group:
331 if not users_group:
332 raise JSONRPCError('unknown users group %s' % group_name)
332 raise JSONRPCError('unknown users group %s' % group_name)
333
333
334 user = User.get_by_username(username)
334 user = User.get_by_username(username)
335 if user is None:
335 if user is None:
336 raise JSONRPCError('unknown user %s' % username)
336 raise JSONRPCError('unknown user %s' % username)
337
337
338 ugm = UsersGroupModel().add_user_to_group(users_group, user)
338 ugm = UsersGroupModel().add_user_to_group(users_group, user)
339 success = True if ugm != True else False
339 success = True if ugm != True else False
340 msg = 'added member %s to users group %s' % (username, group_name)
340 msg = 'added member %s to users group %s' % (username, group_name)
341 msg = msg if success else 'User is already in that group'
341 msg = msg if success else 'User is already in that group'
342 Session.commit()
342 Session.commit()
343
343
344 return dict(
344 return dict(
345 id=ugm.users_group_member_id if ugm != True else None,
345 id=ugm.users_group_member_id if ugm != True else None,
346 success=success,
346 success=success,
347 msg=msg
347 msg=msg
348 )
348 )
349 except Exception:
349 except Exception:
350 log.error(traceback.format_exc())
350 log.error(traceback.format_exc())
351 raise JSONRPCError('failed to add users group member')
351 raise JSONRPCError('failed to add users group member')
352
352
353 @HasPermissionAllDecorator('hg.admin')
353 @HasPermissionAllDecorator('hg.admin')
354 def remove_user_from_users_group(self, apiuser, group_name, username):
354 def remove_user_from_users_group(self, apiuser, group_name, username):
355 """
355 """
356 Remove user from a group
356 Remove user from a group
357
357
358 :param apiuser
358 :param apiuser
359 :param group_name
359 :param group_name
360 :param username
360 :param username
361 """
361 """
362
362
363 try:
363 try:
364 users_group = UsersGroup.get_by_group_name(group_name)
364 users_group = UsersGroup.get_by_group_name(group_name)
365 if not users_group:
365 if not users_group:
366 raise JSONRPCError('unknown users group %s' % group_name)
366 raise JSONRPCError('unknown users group %s' % group_name)
367
367
368 user = User.get_by_username(username)
368 user = User.get_by_username(username)
369 if user is None:
369 if user is None:
370 raise JSONRPCError('unknown user %s' % username)
370 raise JSONRPCError('unknown user %s' % username)
371
371
372 success = UsersGroupModel().remove_user_from_group(users_group, user)
372 success = UsersGroupModel().remove_user_from_group(users_group, user)
373 msg = 'removed member %s from users group %s' % (username, group_name)
373 msg = 'removed member %s from users group %s' % (username, group_name)
374 msg = msg if success else "User wasn't in group"
374 msg = msg if success else "User wasn't in group"
375 Session.commit()
375 Session.commit()
376 return dict(success=success, msg=msg)
376 return dict(success=success, msg=msg)
377 except Exception:
377 except Exception:
378 log.error(traceback.format_exc())
378 log.error(traceback.format_exc())
379 raise JSONRPCError('failed to remove user from group')
379 raise JSONRPCError('failed to remove user from group')
380
380
381 @HasPermissionAnyDecorator('hg.admin')
381 @HasPermissionAnyDecorator('hg.admin')
382 def get_repo(self, apiuser, repoid):
382 def get_repo(self, apiuser, repoid):
383 """"
383 """"
384 Get repository by name
384 Get repository by name
385
385
386 :param apiuser:
386 :param apiuser:
387 :param repo_name:
387 :param repo_name:
388 """
388 """
389
389
390 repo = RepoModel().get_repo(repoid)
390 repo = RepoModel().get_repo(repoid)
391 if repo is None:
391 if repo is None:
392 raise JSONRPCError('unknown repository %s' % repo)
392 raise JSONRPCError('unknown repository %s' % repo)
393
393
394 members = []
394 members = []
395 for user in repo.repo_to_perm:
395 for user in repo.repo_to_perm:
396 perm = user.permission.permission_name
396 perm = user.permission.permission_name
397 user = user.user
397 user = user.user
398 members.append(
398 members.append(
399 dict(
399 dict(
400 type="user",
400 type="user",
401 id=user.user_id,
401 id=user.user_id,
402 username=user.username,
402 username=user.username,
403 firstname=user.name,
403 firstname=user.name,
404 lastname=user.lastname,
404 lastname=user.lastname,
405 email=user.email,
405 email=user.email,
406 active=user.active,
406 active=user.active,
407 admin=user.admin,
407 admin=user.admin,
408 ldap=user.ldap_dn,
408 ldap=user.ldap_dn,
409 permission=perm
409 permission=perm
410 )
410 )
411 )
411 )
412 for users_group in repo.users_group_to_perm:
412 for users_group in repo.users_group_to_perm:
413 perm = users_group.permission.permission_name
413 perm = users_group.permission.permission_name
414 users_group = users_group.users_group
414 users_group = users_group.users_group
415 members.append(
415 members.append(
416 dict(
416 dict(
417 type="users_group",
417 type="users_group",
418 id=users_group.users_group_id,
418 id=users_group.users_group_id,
419 name=users_group.users_group_name,
419 name=users_group.users_group_name,
420 active=users_group.users_group_active,
420 active=users_group.users_group_active,
421 permission=perm
421 permission=perm
422 )
422 )
423 )
423 )
424
424
425 return dict(
425 return dict(
426 id=repo.repo_id,
426 id=repo.repo_id,
427 repo_name=repo.repo_name,
427 repo_name=repo.repo_name,
428 type=repo.repo_type,
428 type=repo.repo_type,
429 clone_uri=repo.clone_uri,
429 clone_uri=repo.clone_uri,
430 private=repo.private,
430 private=repo.private,
431 created_on=repo.created_on,
431 created_on=repo.created_on,
432 description=repo.description,
432 description=repo.description,
433 members=members
433 members=members
434 )
434 )
435
435
436 @HasPermissionAnyDecorator('hg.admin')
436 @HasPermissionAnyDecorator('hg.admin')
437 def get_repos(self, apiuser):
437 def get_repos(self, apiuser):
438 """"
438 """"
439 Get all repositories
439 Get all repositories
440
440
441 :param apiuser:
441 :param apiuser:
442 """
442 """
443
443
444 result = []
444 result = []
445 for repo in Repository.getAll():
445 for repo in Repository.getAll():
446 result.append(
446 result.append(
447 dict(
447 dict(
448 id=repo.repo_id,
448 id=repo.repo_id,
449 repo_name=repo.repo_name,
449 repo_name=repo.repo_name,
450 type=repo.repo_type,
450 type=repo.repo_type,
451 clone_uri=repo.clone_uri,
451 clone_uri=repo.clone_uri,
452 private=repo.private,
452 private=repo.private,
453 created_on=repo.created_on,
453 created_on=repo.created_on,
454 description=repo.description,
454 description=repo.description,
455 )
455 )
456 )
456 )
457 return result
457 return result
458
458
459 @HasPermissionAnyDecorator('hg.admin')
459 @HasPermissionAnyDecorator('hg.admin')
460 def get_repo_nodes(self, apiuser, repo_name, revision, root_path,
460 def get_repo_nodes(self, apiuser, repo_name, revision, root_path,
461 ret_type='all'):
461 ret_type='all'):
462 """
462 """
463 returns a list of nodes and it's children
463 returns a list of nodes and it's children
464 for a given path at given revision. It's possible to specify ret_type
464 for a given path at given revision. It's possible to specify ret_type
465 to show only files or dirs
465 to show only files or dirs
466
466
467 :param apiuser:
467 :param apiuser:
468 :param repo_name: name of repository
468 :param repo_name: name of repository
469 :param revision: revision for which listing should be done
469 :param revision: revision for which listing should be done
470 :param root_path: path from which start displaying
470 :param root_path: path from which start displaying
471 :param ret_type: return type 'all|files|dirs' nodes
471 :param ret_type: return type 'all|files|dirs' nodes
472 """
472 """
473 try:
473 try:
474 _d, _f = ScmModel().get_nodes(repo_name, revision, root_path,
474 _d, _f = ScmModel().get_nodes(repo_name, revision, root_path,
475 flat=False)
475 flat=False)
476 _map = {
476 _map = {
477 'all': _d + _f,
477 'all': _d + _f,
478 'files': _f,
478 'files': _f,
479 'dirs': _d,
479 'dirs': _d,
480 }
480 }
481 return _map[ret_type]
481 return _map[ret_type]
482 except KeyError:
482 except KeyError:
483 raise JSONRPCError('ret_type must be one of %s' % _map.keys())
483 raise JSONRPCError('ret_type must be one of %s' % _map.keys())
484 except Exception, e:
484 except Exception, e:
485 raise JSONRPCError(e)
485 raise JSONRPCError(e)
486
486
487 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
487 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
488 def create_repo(self, apiuser, repo_name, owner_name, description='',
488 def create_repo(self, apiuser, repo_name, owner_name, description='',
489 repo_type='hg', private=False, clone_uri=None):
489 repo_type='hg', private=False, clone_uri=None):
490 """
490 """
491 Create repository, if clone_url is given it makes a remote clone
491 Create repository, if clone_url is given it makes a remote clone
492
492
493 :param apiuser:
493 :param apiuser:
494 :param repo_name:
494 :param repo_name:
495 :param owner_name:
495 :param owner_name:
496 :param description:
496 :param description:
497 :param repo_type:
497 :param repo_type:
498 :param private:
498 :param private:
499 :param clone_uri:
499 :param clone_uri:
500 """
500 """
501
501
502 try:
502 try:
503 owner = User.get_by_username(owner_name)
503 owner = User.get_by_username(owner_name)
504 if owner is None:
504 if owner is None:
505 raise JSONRPCError('unknown user %s' % owner_name)
505 raise JSONRPCError('unknown user %s' % owner_name)
506
506
507 if Repository.get_by_repo_name(repo_name):
507 if Repository.get_by_repo_name(repo_name):
508 raise JSONRPCError("repo %s already exist" % repo_name)
508 raise JSONRPCError("repo %s already exist" % repo_name)
509
509
510 groups = repo_name.split(Repository.url_sep())
510 groups = repo_name.split(Repository.url_sep())
511 real_name = groups[-1]
511 real_name = groups[-1]
512 # create structure of groups
512 # create structure of groups
513 group = map_groups(repo_name)
513 group = map_groups(repo_name)
514
514
515 repo = RepoModel().create(
515 repo = RepoModel().create(
516 dict(
516 dict(
517 repo_name=real_name,
517 repo_name=real_name,
518 repo_name_full=repo_name,
518 repo_name_full=repo_name,
519 description=description,
519 description=description,
520 private=private,
520 private=private,
521 repo_type=repo_type,
521 repo_type=repo_type,
522 repo_group=group.group_id if group else None,
522 repo_group=group.group_id if group else None,
523 clone_uri=clone_uri
523 clone_uri=clone_uri
524 ),
524 )
525 owner
526 )
525 )
527 Session.commit()
526 Session.commit()
528
527
529 return dict(
528 return dict(
530 id=repo.repo_id,
529 id=repo.repo_id,
531 msg="Created new repository %s" % repo.repo_name
530 msg="Created new repository %s" % (repo.repo_name),
531 repo=dict(
532 id=repo.repo_id,
533 repo_name=repo.repo_name,
534 type=repo.repo_type,
535 clone_uri=repo.clone_uri,
536 private=repo.private,
537 created_on=repo.created_on,
538 description=repo.description,
539 )
532 )
540 )
533
541
534 except Exception:
542 except Exception:
535 log.error(traceback.format_exc())
543 log.error(traceback.format_exc())
536 raise JSONRPCError('failed to create repository %s' % repo_name)
544 raise JSONRPCError('failed to create repository %s' % repo_name)
537
545
538 @HasPermissionAnyDecorator('hg.admin')
546 @HasPermissionAnyDecorator('hg.admin')
539 def delete_repo(self, apiuser, repo_name):
547 def delete_repo(self, apiuser, repo_name):
540 """
548 """
541 Deletes a given repository
549 Deletes a given repository
542
550
543 :param repo_name:
551 :param repo_name:
544 """
552 """
545 if not Repository.get_by_repo_name(repo_name):
553 if not Repository.get_by_repo_name(repo_name):
546 raise JSONRPCError("repo %s does not exist" % repo_name)
554 raise JSONRPCError("repo %s does not exist" % repo_name)
547 try:
555 try:
548 RepoModel().delete(repo_name)
556 RepoModel().delete(repo_name)
549 Session.commit()
557 Session.commit()
550 return dict(
558 return dict(
551 msg='Deleted repository %s' % repo_name
559 msg='Deleted repository %s' % repo_name
552 )
560 )
553 except Exception:
561 except Exception:
554 log.error(traceback.format_exc())
562 log.error(traceback.format_exc())
555 raise JSONRPCError('failed to delete repository %s' % repo_name)
563 raise JSONRPCError('failed to delete repository %s' % repo_name)
556
564
557 @HasPermissionAnyDecorator('hg.admin')
565 @HasPermissionAnyDecorator('hg.admin')
558 def grant_user_permission(self, apiuser, repo_name, username, perm):
566 def grant_user_permission(self, apiuser, repo_name, username, perm):
559 """
567 """
560 Grant permission for user on given repository, or update existing one
568 Grant permission for user on given repository, or update existing one
561 if found
569 if found
562
570
563 :param repo_name:
571 :param repo_name:
564 :param username:
572 :param username:
565 :param perm:
573 :param perm:
566 """
574 """
567
575
568 try:
576 try:
569 repo = Repository.get_by_repo_name(repo_name)
577 repo = Repository.get_by_repo_name(repo_name)
570 if repo is None:
578 if repo is None:
571 raise JSONRPCError('unknown repository %s' % repo)
579 raise JSONRPCError('unknown repository %s' % repo)
572
580
573 user = User.get_by_username(username)
581 user = User.get_by_username(username)
574 if user is None:
582 if user is None:
575 raise JSONRPCError('unknown user %s' % username)
583 raise JSONRPCError('unknown user %s' % username)
576
584
577 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
585 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
578
586
579 Session.commit()
587 Session.commit()
580 return dict(
588 return dict(
581 msg='Granted perm: %s for user: %s in repo: %s' % (
589 msg='Granted perm: %s for user: %s in repo: %s' % (
582 perm, username, repo_name
590 perm, username, repo_name
583 )
591 )
584 )
592 )
585 except Exception:
593 except Exception:
586 log.error(traceback.format_exc())
594 log.error(traceback.format_exc())
587 raise JSONRPCError(
595 raise JSONRPCError(
588 'failed to edit permission %(repo)s for %(user)s' % dict(
596 'failed to edit permission %(repo)s for %(user)s' % dict(
589 user=username, repo=repo_name
597 user=username, repo=repo_name
590 )
598 )
591 )
599 )
592
600
593 @HasPermissionAnyDecorator('hg.admin')
601 @HasPermissionAnyDecorator('hg.admin')
594 def revoke_user_permission(self, apiuser, repo_name, username):
602 def revoke_user_permission(self, apiuser, repo_name, username):
595 """
603 """
596 Revoke permission for user on given repository
604 Revoke permission for user on given repository
597
605
598 :param repo_name:
606 :param repo_name:
599 :param username:
607 :param username:
600 """
608 """
601
609
602 try:
610 try:
603 repo = Repository.get_by_repo_name(repo_name)
611 repo = Repository.get_by_repo_name(repo_name)
604 if repo is None:
612 if repo is None:
605 raise JSONRPCError('unknown repository %s' % repo)
613 raise JSONRPCError('unknown repository %s' % repo)
606
614
607 user = User.get_by_username(username)
615 user = User.get_by_username(username)
608 if user is None:
616 if user is None:
609 raise JSONRPCError('unknown user %s' % username)
617 raise JSONRPCError('unknown user %s' % username)
610
618
611 RepoModel().revoke_user_permission(repo=repo_name, user=username)
619 RepoModel().revoke_user_permission(repo=repo_name, user=username)
612
620
613 Session.commit()
621 Session.commit()
614 return dict(
622 return dict(
615 msg='Revoked perm for user: %s in repo: %s' % (
623 msg='Revoked perm for user: %s in repo: %s' % (
616 username, repo_name
624 username, repo_name
617 )
625 )
618 )
626 )
619 except Exception:
627 except Exception:
620 log.error(traceback.format_exc())
628 log.error(traceback.format_exc())
621 raise JSONRPCError(
629 raise JSONRPCError(
622 'failed to edit permission %(repo)s for %(user)s' % dict(
630 'failed to edit permission %(repo)s for %(user)s' % dict(
623 user=username, repo=repo_name
631 user=username, repo=repo_name
624 )
632 )
625 )
633 )
626
634
627 @HasPermissionAnyDecorator('hg.admin')
635 @HasPermissionAnyDecorator('hg.admin')
628 def grant_users_group_permission(self, apiuser, repo_name, group_name, perm):
636 def grant_users_group_permission(self, apiuser, repo_name, group_name, perm):
629 """
637 """
630 Grant permission for users group on given repository, or update
638 Grant permission for users group on given repository, or update
631 existing one if found
639 existing one if found
632
640
633 :param repo_name:
641 :param repo_name:
634 :param group_name:
642 :param group_name:
635 :param perm:
643 :param perm:
636 """
644 """
637
645
638 try:
646 try:
639 repo = Repository.get_by_repo_name(repo_name)
647 repo = Repository.get_by_repo_name(repo_name)
640 if repo is None:
648 if repo is None:
641 raise JSONRPCError('unknown repository %s' % repo)
649 raise JSONRPCError('unknown repository %s' % repo)
642
650
643 user_group = UsersGroup.get_by_group_name(group_name)
651 user_group = UsersGroup.get_by_group_name(group_name)
644 if user_group is None:
652 if user_group is None:
645 raise JSONRPCError('unknown users group %s' % user_group)
653 raise JSONRPCError('unknown users group %s' % user_group)
646
654
647 RepoModel().grant_users_group_permission(repo=repo_name,
655 RepoModel().grant_users_group_permission(repo=repo_name,
648 group_name=group_name,
656 group_name=group_name,
649 perm=perm)
657 perm=perm)
650
658
651 Session.commit()
659 Session.commit()
652 return dict(
660 return dict(
653 msg='Granted perm: %s for group: %s in repo: %s' % (
661 msg='Granted perm: %s for group: %s in repo: %s' % (
654 perm, group_name, repo_name
662 perm, group_name, repo_name
655 )
663 )
656 )
664 )
657 except Exception:
665 except Exception:
658 log.error(traceback.format_exc())
666 log.error(traceback.format_exc())
659 raise JSONRPCError(
667 raise JSONRPCError(
660 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
668 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
661 usersgr=group_name, repo=repo_name
669 usersgr=group_name, repo=repo_name
662 )
670 )
663 )
671 )
664
672
665 @HasPermissionAnyDecorator('hg.admin')
673 @HasPermissionAnyDecorator('hg.admin')
666 def revoke_users_group_permission(self, apiuser, repo_name, group_name):
674 def revoke_users_group_permission(self, apiuser, repo_name, group_name):
667 """
675 """
668 Revoke permission for users group on given repository
676 Revoke permission for users group on given repository
669
677
670 :param repo_name:
678 :param repo_name:
671 :param group_name:
679 :param group_name:
672 """
680 """
673
681
674 try:
682 try:
675 repo = Repository.get_by_repo_name(repo_name)
683 repo = Repository.get_by_repo_name(repo_name)
676 if repo is None:
684 if repo is None:
677 raise JSONRPCError('unknown repository %s' % repo)
685 raise JSONRPCError('unknown repository %s' % repo)
678
686
679 user_group = UsersGroup.get_by_group_name(group_name)
687 user_group = UsersGroup.get_by_group_name(group_name)
680 if user_group is None:
688 if user_group is None:
681 raise JSONRPCError('unknown users group %s' % user_group)
689 raise JSONRPCError('unknown users group %s' % user_group)
682
690
683 RepoModel().revoke_users_group_permission(repo=repo_name,
691 RepoModel().revoke_users_group_permission(repo=repo_name,
684 group_name=group_name)
692 group_name=group_name)
685
693
686 Session.commit()
694 Session.commit()
687 return dict(
695 return dict(
688 msg='Revoked perm for group: %s in repo: %s' % (
696 msg='Revoked perm for group: %s in repo: %s' % (
689 group_name, repo_name
697 group_name, repo_name
690 )
698 )
691 )
699 )
692 except Exception:
700 except Exception:
693 log.error(traceback.format_exc())
701 log.error(traceback.format_exc())
694 raise JSONRPCError(
702 raise JSONRPCError(
695 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
703 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
696 usersgr=group_name, repo=repo_name
704 usersgr=group_name, repo=repo_name
697 )
705 )
698 )
706 )
General Comments 0
You need to be logged in to leave comments. Login now