##// END OF EJS Templates
Updated API to return clone_uri, private, created_on
marcink -
r2338:347b0054 beta
parent child Browse files
Show More
@@ -1,659 +1,665 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 }
191 }
192 error: null
192 error: null
193
193
194
194
195 update_user
195 update_user
196 -----------
196 -----------
197
197
198 updates current one if such user exists. This command can
198 updates current one if such user exists. This command can
199 be executed only using api_key belonging to user with admin rights.
199 be executed only using api_key belonging to user with admin rights.
200
200
201
201
202 INPUT::
202 INPUT::
203
203
204 id : <id_for_response>
204 id : <id_for_response>
205 api_key : "<api_key>"
205 api_key : "<api_key>"
206 method : "update_user"
206 method : "update_user"
207 args : {
207 args : {
208 "userid" : "<user_id or username>",
208 "userid" : "<user_id or username>",
209 "username" : "<username>",
209 "username" : "<username>",
210 "password" : "<password>",
210 "password" : "<password>",
211 "email" : "<useremail>",
211 "email" : "<useremail>",
212 "firstname" : "<firstname>",
212 "firstname" : "<firstname>",
213 "lastname" : "<lastname>",
213 "lastname" : "<lastname>",
214 "active" : "<bool>",
214 "active" : "<bool>",
215 "admin" : "<bool>",
215 "admin" : "<bool>",
216 "ldap_dn" : "<ldap_dn>"
216 "ldap_dn" : "<ldap_dn>"
217 }
217 }
218
218
219 OUTPUT::
219 OUTPUT::
220
220
221 result: {
221 result: {
222 "id" : "<edited_user_id>",
222 "id" : "<edited_user_id>",
223 "msg" : "updated user <username>"
223 "msg" : "updated user <username>"
224 }
224 }
225 error: null
225 error: null
226
226
227
227
228 get_users_group
228 get_users_group
229 ---------------
229 ---------------
230
230
231 Gets an existing users group. This command can be executed only using api_key
231 Gets an existing users group. This command can be executed only using api_key
232 belonging to user with admin rights.
232 belonging to user with admin rights.
233
233
234
234
235 INPUT::
235 INPUT::
236
236
237 id : <id_for_response>
237 id : <id_for_response>
238 api_key : "<api_key>"
238 api_key : "<api_key>"
239 method : "get_users_group"
239 method : "get_users_group"
240 args : {
240 args : {
241 "group_name" : "<name>"
241 "group_name" : "<name>"
242 }
242 }
243
243
244 OUTPUT::
244 OUTPUT::
245
245
246 result : None if group not exist
246 result : None if group not exist
247 {
247 {
248 "id" : "<id>",
248 "id" : "<id>",
249 "group_name" : "<groupname>",
249 "group_name" : "<groupname>",
250 "active": "<bool>",
250 "active": "<bool>",
251 "members" : [
251 "members" : [
252 { "id" : "<userid>",
252 { "id" : "<userid>",
253 "username" : "<username>",
253 "username" : "<username>",
254 "firstname": "<firstname>",
254 "firstname": "<firstname>",
255 "lastname" : "<lastname>",
255 "lastname" : "<lastname>",
256 "email" : "<email>",
256 "email" : "<email>",
257 "active" : "<bool>",
257 "active" : "<bool>",
258 "admin" :Β  "<bool>",
258 "admin" :Β  "<bool>",
259 "ldap" : "<ldap_dn>"
259 "ldap" : "<ldap_dn>"
260 },
260 },
261 …
261 …
262 ]
262 ]
263 }
263 }
264 error : null
264 error : null
265
265
266
266
267 get_users_groups
267 get_users_groups
268 ----------------
268 ----------------
269
269
270 Lists all existing users groups. This command can be executed only using
270 Lists all existing users groups. This command can be executed only using
271 api_key belonging to user with admin rights.
271 api_key belonging to user with admin rights.
272
272
273
273
274 INPUT::
274 INPUT::
275
275
276 id : <id_for_response>
276 id : <id_for_response>
277 api_key : "<api_key>"
277 api_key : "<api_key>"
278 method : "get_users_groups"
278 method : "get_users_groups"
279 args : { }
279 args : { }
280
280
281 OUTPUT::
281 OUTPUT::
282
282
283 result : [
283 result : [
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 {
289 {
290 "id" : "<userid>",
290 "id" : "<userid>",
291 "username" : "<username>",
291 "username" : "<username>",
292 "firstname": "<firstname>",
292 "firstname": "<firstname>",
293 "lastname" : "<lastname>",
293 "lastname" : "<lastname>",
294 "email" : "<email>",
294 "email" : "<email>",
295 "active" : "<bool>",
295 "active" : "<bool>",
296 "admin" :Β  "<bool>",
296 "admin" :Β  "<bool>",
297 "ldap" : "<ldap_dn>"
297 "ldap" : "<ldap_dn>"
298 },
298 },
299 …
299 …
300 ]
300 ]
301 }
301 }
302 ]
302 ]
303 error : null
303 error : null
304
304
305
305
306 create_users_group
306 create_users_group
307 ------------------
307 ------------------
308
308
309 Creates new users group. This command can be executed only using api_key
309 Creates new users group. This command can be executed only using api_key
310 belonging to user with admin rights
310 belonging to user with admin rights
311
311
312
312
313 INPUT::
313 INPUT::
314
314
315 id : <id_for_response>
315 id : <id_for_response>
316 api_key : "<api_key>"
316 api_key : "<api_key>"
317 method : "create_users_group"
317 method : "create_users_group"
318 args: {
318 args: {
319 "group_name": "<groupname>",
319 "group_name": "<groupname>",
320 "active":"<bool> = True"
320 "active":"<bool> = True"
321 }
321 }
322
322
323 OUTPUT::
323 OUTPUT::
324
324
325 result: {
325 result: {
326 "id": "<newusersgroupid>",
326 "id": "<newusersgroupid>",
327 "msg": "created new users group <groupname>"
327 "msg": "created new users group <groupname>"
328 }
328 }
329 error: null
329 error: null
330
330
331
331
332 add_user_to_users_group
332 add_user_to_users_group
333 -----------------------
333 -----------------------
334
334
335 Adds a user to a users group. If user exists in that group success will be
335 Adds a user to a users group. If user exists in that group success will be
336 `false`. This command can be executed only using api_key
336 `false`. This command can be executed only using api_key
337 belonging to user with admin rights
337 belonging to user with admin rights
338
338
339
339
340 INPUT::
340 INPUT::
341
341
342 id : <id_for_response>
342 id : <id_for_response>
343 api_key : "<api_key>"
343 api_key : "<api_key>"
344 method : "add_user_users_group"
344 method : "add_user_users_group"
345 args: {
345 args: {
346 "group_name" : "<groupname>",
346 "group_name" : "<groupname>",
347 "username" : "<username>"
347 "username" : "<username>"
348 }
348 }
349
349
350 OUTPUT::
350 OUTPUT::
351
351
352 result: {
352 result: {
353 "id": "<newusersgroupmemberid>",
353 "id": "<newusersgroupmemberid>",
354 "success": True|False # depends on if member is in group
354 "success": True|False # depends on if member is in group
355 "msg": "added member <username> to users group <groupname> |
355 "msg": "added member <username> to users group <groupname> |
356 User is already in that group"
356 User is already in that group"
357 }
357 }
358 error: null
358 error: null
359
359
360
360
361 remove_user_from_users_group
361 remove_user_from_users_group
362 ----------------------------
362 ----------------------------
363
363
364 Removes a user from a users group. If user is not in given group success will
364 Removes a user from a users group. If user is not in given group success will
365 be `false`. This command can be executed only
365 be `false`. This command can be executed only
366 using api_key belonging to user with admin rights
366 using api_key belonging to user with admin rights
367
367
368
368
369 INPUT::
369 INPUT::
370
370
371 id : <id_for_response>
371 id : <id_for_response>
372 api_key : "<api_key>"
372 api_key : "<api_key>"
373 method : "remove_user_from_users_group"
373 method : "remove_user_from_users_group"
374 args: {
374 args: {
375 "group_name" : "<groupname>",
375 "group_name" : "<groupname>",
376 "username" : "<username>"
376 "username" : "<username>"
377 }
377 }
378
378
379 OUTPUT::
379 OUTPUT::
380
380
381 result: {
381 result: {
382 "success": True|False, # depends on if member is in group
382 "success": True|False, # depends on if member is in group
383 "msg": "removed member <username> from users group <groupname> |
383 "msg": "removed member <username> from users group <groupname> |
384 User wasn't in group"
384 User wasn't in group"
385 }
385 }
386 error: null
386 error: null
387
387
388
388
389 get_repo
389 get_repo
390 --------
390 --------
391
391
392 Gets an existing repository by it's name or repository_id. Members will return
392 Gets an existing repository by it's name or repository_id. Members will return
393 either users_group or user associated to that repository. This command can
393 either users_group or user associated to that repository. This command can
394 be executed only using api_key belonging to user with admin rights.
394 be executed only using api_key belonging to user with admin rights.
395
395
396
396
397 INPUT::
397 INPUT::
398
398
399 id : <id_for_response>
399 id : <id_for_response>
400 api_key : "<api_key>"
400 api_key : "<api_key>"
401 method : "get_repo"
401 method : "get_repo"
402 args: {
402 args: {
403 "repoid" : "<reponame or repo_id>"
403 "repoid" : "<reponame or repo_id>"
404 }
404 }
405
405
406 OUTPUT::
406 OUTPUT::
407
407
408 result: None if repository does not exist or
408 result: None if repository does not exist or
409 {
409 {
410 "id" : "<id>",
410 "id" : "<id>",
411 "repo_name" : "<reponame>"
411 "repo_name" : "<reponame>"
412 "type" : "<type>",
412 "type" : "<type>",
413 "description" : "<description>",
413 "description" : "<description>",
414 "clone_uri" : "<clone_uri>",
415 "private": : "<bool>",
416 "created_on" : "<datetimecreated>",
414 "members" : [
417 "members" : [
415 {
418 {
416 "type": "user",
419 "type": "user",
417 "id" : "<userid>",
420 "id" : "<userid>",
418 "username" : "<username>",
421 "username" : "<username>",
419 "firstname": "<firstname>",
422 "firstname": "<firstname>",
420 "lastname" : "<lastname>",
423 "lastname" : "<lastname>",
421 "email" : "<email>",
424 "email" : "<email>",
422 "active" : "<bool>",
425 "active" : "<bool>",
423 "admin" :Β  "<bool>",
426 "admin" :Β  "<bool>",
424 "ldap" : "<ldap_dn>",
427 "ldap" : "<ldap_dn>",
425 "permission" : "repository.(read|write|admin)"
428 "permission" : "repository.(read|write|admin)"
426 },
429 },
427 …
430 …
428 {
431 {
429 "type": "users_group",
432 "type": "users_group",
430 "id" : "<usersgroupid>",
433 "id" : "<usersgroupid>",
431 "name" : "<usersgroupname>",
434 "name" : "<usersgroupname>",
432 "active": "<bool>",
435 "active": "<bool>",
433 "permission" : "repository.(read|write|admin)"
436 "permission" : "repository.(read|write|admin)"
434 },
437 },
435 …
438 …
436 ]
439 ]
437 }
440 }
438 error: null
441 error: null
439
442
440
443
441 get_repos
444 get_repos
442 ---------
445 ---------
443
446
444 Lists all existing repositories. This command can be executed only using api_key
447 Lists all existing repositories. This command can be executed only using api_key
445 belonging to user with admin rights
448 belonging to user with admin rights
446
449
447
450
448 INPUT::
451 INPUT::
449
452
450 id : <id_for_response>
453 id : <id_for_response>
451 api_key : "<api_key>"
454 api_key : "<api_key>"
452 method : "get_repos"
455 method : "get_repos"
453 args: { }
456 args: { }
454
457
455 OUTPUT::
458 OUTPUT::
456
459
457 result: [
460 result: [
458 {
461 {
459 "id" : "<id>",
462 "id" : "<id>",
460 "repo_name" : "<reponame>"
463 "repo_name" : "<reponame>"
461 "type" : "<type>",
464 "type" : "<type>",
462 "description" : "<description>"
465 "description" : "<description>",
466 "clone_uri" : "<clone_uri>",
467 "private": : "<bool>",
468 "created_on" : "<datetimecreated>",
463 },
469 },
464 …
470 …
465 ]
471 ]
466 error: null
472 error: null
467
473
468
474
469 get_repo_nodes
475 get_repo_nodes
470 --------------
476 --------------
471
477
472 returns a list of nodes and it's children in a flat list for a given path
478 returns a list of nodes and it's children in a flat list for a given path
473 at given revision. It's possible to specify ret_type to show only `files` or
479 at given revision. It's possible to specify ret_type to show only `files` or
474 `dirs`. This command can be executed only using api_key belonging to user
480 `dirs`. This command can be executed only using api_key belonging to user
475 with admin rights
481 with admin rights
476
482
477
483
478 INPUT::
484 INPUT::
479
485
480 id : <id_for_response>
486 id : <id_for_response>
481 api_key : "<api_key>"
487 api_key : "<api_key>"
482 method : "get_repo_nodes"
488 method : "get_repo_nodes"
483 args: {
489 args: {
484 "repo_name" : "<reponame>",
490 "repo_name" : "<reponame>",
485 "revision" : "<revision>",
491 "revision" : "<revision>",
486 "root_path" : "<root_path>",
492 "root_path" : "<root_path>",
487 "ret_type" : "<ret_type>" = 'all'
493 "ret_type" : "<ret_type>" = 'all'
488 }
494 }
489
495
490 OUTPUT::
496 OUTPUT::
491
497
492 result: [
498 result: [
493 {
499 {
494 "name" : "<name>"
500 "name" : "<name>"
495 "type" : "<type>",
501 "type" : "<type>",
496 },
502 },
497 …
503 …
498 ]
504 ]
499 error: null
505 error: null
500
506
501
507
502 create_repo
508 create_repo
503 -----------
509 -----------
504
510
505 Creates a repository. This command can be executed only using api_key
511 Creates a repository. This command can be executed only using api_key
506 belonging to user with admin rights.
512 belonging to user with admin rights.
507 If repository name contains "/", all needed repository groups will be created.
513 If repository name contains "/", all needed repository groups will be created.
508 For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent),
514 For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent),
509 and create "baz" repository with "bar" as group.
515 and create "baz" repository with "bar" as group.
510
516
511
517
512 INPUT::
518 INPUT::
513
519
514 id : <id_for_response>
520 id : <id_for_response>
515 api_key : "<api_key>"
521 api_key : "<api_key>"
516 method : "create_repo"
522 method : "create_repo"
517 args: {
523 args: {
518 "repo_name" : "<reponame>",
524 "repo_name" : "<reponame>",
519 "owner_name" : "<ownername>",
525 "owner_name" : "<ownername>",
520 "description" : "<description> = ''",
526 "description" : "<description> = ''",
521 "repo_type" : "<type> = 'hg'",
527 "repo_type" : "<type> = 'hg'",
522 "private" : "<bool> = False",
528 "private" : "<bool> = False",
523 "clone_uri" : "<clone_uri> = None",
529 "clone_uri" : "<clone_uri> = None",
524 }
530 }
525
531
526 OUTPUT::
532 OUTPUT::
527
533
528 result: {
534 result: {
529 "id": "<newrepoid>",
535 "id": "<newrepoid>",
530 "msg": "Created new repository <reponame>",
536 "msg": "Created new repository <reponame>",
531 }
537 }
532 error: null
538 error: null
533
539
534
540
535 delete_repo
541 delete_repo
536 -----------
542 -----------
537
543
538 Deletes a repository. This command can be executed only using api_key
544 Deletes a repository. This command can be executed only using api_key
539 belonging to user with admin rights.
545 belonging to user with admin rights.
540
546
541
547
542 INPUT::
548 INPUT::
543
549
544 id : <id_for_response>
550 id : <id_for_response>
545 api_key : "<api_key>"
551 api_key : "<api_key>"
546 method : "delete_repo"
552 method : "delete_repo"
547 args: {
553 args: {
548 "repo_name" : "<reponame>",
554 "repo_name" : "<reponame>",
549 }
555 }
550
556
551 OUTPUT::
557 OUTPUT::
552
558
553 result: {
559 result: {
554 "msg": "Deleted repository <reponame>",
560 "msg": "Deleted repository <reponame>",
555 }
561 }
556 error: null
562 error: null
557
563
558
564
559 grant_user_permission
565 grant_user_permission
560 ---------------------
566 ---------------------
561
567
562 Grant permission for user on given repository, or update existing one
568 Grant permission for user on given repository, or update existing one
563 if found. This command can be executed only using api_key belonging to user
569 if found. This command can be executed only using api_key belonging to user
564 with admin rights.
570 with admin rights.
565
571
566
572
567 INPUT::
573 INPUT::
568
574
569 id : <id_for_response>
575 id : <id_for_response>
570 api_key : "<api_key>"
576 api_key : "<api_key>"
571 method : "grant_user_permission"
577 method : "grant_user_permission"
572 args: {
578 args: {
573 "repo_name" : "<reponame>",
579 "repo_name" : "<reponame>",
574 "username" : "<username>",
580 "username" : "<username>",
575 "perm" : "(repository.(none|read|write|admin))",
581 "perm" : "(repository.(none|read|write|admin))",
576 }
582 }
577
583
578 OUTPUT::
584 OUTPUT::
579
585
580 result: {
586 result: {
581 "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>"
587 "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>"
582 }
588 }
583 error: null
589 error: null
584
590
585
591
586 revoke_user_permission
592 revoke_user_permission
587 ----------------------
593 ----------------------
588
594
589 Revoke permission for user on given repository. This command can be executed
595 Revoke permission for user on given repository. This command can be executed
590 only using api_key belonging to user with admin rights.
596 only using api_key belonging to user with admin rights.
591
597
592
598
593 INPUT::
599 INPUT::
594
600
595 id : <id_for_response>
601 id : <id_for_response>
596 api_key : "<api_key>"
602 api_key : "<api_key>"
597 method : "revoke_user_permission"
603 method : "revoke_user_permission"
598 args: {
604 args: {
599 "repo_name" : "<reponame>",
605 "repo_name" : "<reponame>",
600 "username" : "<username>",
606 "username" : "<username>",
601 }
607 }
602
608
603 OUTPUT::
609 OUTPUT::
604
610
605 result: {
611 result: {
606 "msg" : "Revoked perm for user: <suername> in repo: <reponame>"
612 "msg" : "Revoked perm for user: <suername> in repo: <reponame>"
607 }
613 }
608 error: null
614 error: null
609
615
610
616
611 grant_users_group_permission
617 grant_users_group_permission
612 ----------------------------
618 ----------------------------
613
619
614 Grant permission for users group on given repository, or update
620 Grant permission for users group on given repository, or update
615 existing one if found. This command can be executed only using
621 existing one if found. This command can be executed only using
616 api_key belonging to user with admin rights.
622 api_key belonging to user with admin rights.
617
623
618
624
619 INPUT::
625 INPUT::
620
626
621 id : <id_for_response>
627 id : <id_for_response>
622 api_key : "<api_key>"
628 api_key : "<api_key>"
623 method : "grant_users_group_permission"
629 method : "grant_users_group_permission"
624 args: {
630 args: {
625 "repo_name" : "<reponame>",
631 "repo_name" : "<reponame>",
626 "group_name" : "<usersgroupname>",
632 "group_name" : "<usersgroupname>",
627 "perm" : "(repository.(none|read|write|admin))",
633 "perm" : "(repository.(none|read|write|admin))",
628 }
634 }
629
635
630 OUTPUT::
636 OUTPUT::
631
637
632 result: {
638 result: {
633 "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>"
639 "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>"
634 }
640 }
635 error: null
641 error: null
636
642
637
643
638 revoke_users_group_permission
644 revoke_users_group_permission
639 -----------------------------
645 -----------------------------
640
646
641 Revoke permission for users group on given repository.This command can be
647 Revoke permission for users group on given repository.This command can be
642 executed only using api_key belonging to user with admin rights.
648 executed only using api_key belonging to user with admin rights.
643
649
644 INPUT::
650 INPUT::
645
651
646 id : <id_for_response>
652 id : <id_for_response>
647 api_key : "<api_key>"
653 api_key : "<api_key>"
648 method : "revoke_users_group_permission"
654 method : "revoke_users_group_permission"
649 args: {
655 args: {
650 "repo_name" : "<reponame>",
656 "repo_name" : "<reponame>",
651 "users_group" : "<usersgroupname>",
657 "users_group" : "<usersgroupname>",
652 }
658 }
653
659
654 OUTPUT::
660 OUTPUT::
655
661
656 result: {
662 result: {
657 "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>"
663 "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>"
658 }
664 }
659 error: null No newline at end of file
665 error: null
@@ -1,657 +1,663 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 usr = UserModel().create_or_update(
159 usr = 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=usr.user_id,
165 id=usr.user_id,
166 msg='created new user %s' % username
166 msg='created new user %s' % username
167 )
167 )
168 except Exception:
168 except Exception:
169 log.error(traceback.format_exc())
169 log.error(traceback.format_exc())
170 raise JSONRPCError('failed to create user %s' % username)
170 raise JSONRPCError('failed to create user %s' % username)
171
171
172 @HasPermissionAllDecorator('hg.admin')
172 @HasPermissionAllDecorator('hg.admin')
173 def update_user(self, apiuser, userid, username, password, email,
173 def update_user(self, apiuser, userid, username, password, email,
174 firstname, lastname, active, admin, ldap_dn):
174 firstname, lastname, active, admin, ldap_dn):
175 """
175 """
176 Updates given user
176 Updates given user
177
177
178 :param apiuser:
178 :param apiuser:
179 :param username:
179 :param username:
180 :param password:
180 :param password:
181 :param email:
181 :param email:
182 :param name:
182 :param name:
183 :param lastname:
183 :param lastname:
184 :param active:
184 :param active:
185 :param admin:
185 :param admin:
186 :param ldap_dn:
186 :param ldap_dn:
187 """
187 """
188 if not UserModel().get_user(userid):
188 if not UserModel().get_user(userid):
189 raise JSONRPCError("user %s does not exist" % username)
189 raise JSONRPCError("user %s does not exist" % username)
190
190
191 try:
191 try:
192 usr = UserModel().create_or_update(
192 usr = UserModel().create_or_update(
193 username, password, email, firstname,
193 username, password, email, firstname,
194 lastname, active, admin, ldap_dn
194 lastname, active, admin, ldap_dn
195 )
195 )
196 Session.commit()
196 Session.commit()
197 return dict(
197 return dict(
198 id=usr.user_id,
198 id=usr.user_id,
199 msg='updated user %s' % username
199 msg='updated user %s' % username
200 )
200 )
201 except Exception:
201 except Exception:
202 log.error(traceback.format_exc())
202 log.error(traceback.format_exc())
203 raise JSONRPCError('failed to update user %s' % username)
203 raise JSONRPCError('failed to update user %s' % username)
204
204
205 @HasPermissionAllDecorator('hg.admin')
205 @HasPermissionAllDecorator('hg.admin')
206 def get_users_group(self, apiuser, group_name):
206 def get_users_group(self, apiuser, group_name):
207 """"
207 """"
208 Get users group by name
208 Get users group by name
209
209
210 :param apiuser:
210 :param apiuser:
211 :param group_name:
211 :param group_name:
212 """
212 """
213
213
214 users_group = UsersGroup.get_by_group_name(group_name)
214 users_group = UsersGroup.get_by_group_name(group_name)
215 if not users_group:
215 if not users_group:
216 return None
216 return None
217
217
218 members = []
218 members = []
219 for user in users_group.members:
219 for user in users_group.members:
220 user = user.user
220 user = user.user
221 members.append(dict(id=user.user_id,
221 members.append(dict(id=user.user_id,
222 username=user.username,
222 username=user.username,
223 firstname=user.name,
223 firstname=user.name,
224 lastname=user.lastname,
224 lastname=user.lastname,
225 email=user.email,
225 email=user.email,
226 active=user.active,
226 active=user.active,
227 admin=user.admin,
227 admin=user.admin,
228 ldap=user.ldap_dn))
228 ldap=user.ldap_dn))
229
229
230 return dict(id=users_group.users_group_id,
230 return dict(id=users_group.users_group_id,
231 group_name=users_group.users_group_name,
231 group_name=users_group.users_group_name,
232 active=users_group.users_group_active,
232 active=users_group.users_group_active,
233 members=members)
233 members=members)
234
234
235 @HasPermissionAllDecorator('hg.admin')
235 @HasPermissionAllDecorator('hg.admin')
236 def get_users_groups(self, apiuser):
236 def get_users_groups(self, apiuser):
237 """"
237 """"
238 Get all users groups
238 Get all users groups
239
239
240 :param apiuser:
240 :param apiuser:
241 """
241 """
242
242
243 result = []
243 result = []
244 for users_group in UsersGroup.getAll():
244 for users_group in UsersGroup.getAll():
245 members = []
245 members = []
246 for user in users_group.members:
246 for user in users_group.members:
247 user = user.user
247 user = user.user
248 members.append(dict(id=user.user_id,
248 members.append(dict(id=user.user_id,
249 username=user.username,
249 username=user.username,
250 firstname=user.name,
250 firstname=user.name,
251 lastname=user.lastname,
251 lastname=user.lastname,
252 email=user.email,
252 email=user.email,
253 active=user.active,
253 active=user.active,
254 admin=user.admin,
254 admin=user.admin,
255 ldap=user.ldap_dn))
255 ldap=user.ldap_dn))
256
256
257 result.append(dict(id=users_group.users_group_id,
257 result.append(dict(id=users_group.users_group_id,
258 group_name=users_group.users_group_name,
258 group_name=users_group.users_group_name,
259 active=users_group.users_group_active,
259 active=users_group.users_group_active,
260 members=members))
260 members=members))
261 return result
261 return result
262
262
263 @HasPermissionAllDecorator('hg.admin')
263 @HasPermissionAllDecorator('hg.admin')
264 def create_users_group(self, apiuser, group_name, active=True):
264 def create_users_group(self, apiuser, group_name, active=True):
265 """
265 """
266 Creates an new usergroup
266 Creates an new usergroup
267
267
268 :param group_name:
268 :param group_name:
269 :param active:
269 :param active:
270 """
270 """
271
271
272 if self.get_users_group(apiuser, group_name):
272 if self.get_users_group(apiuser, group_name):
273 raise JSONRPCError("users group %s already exist" % group_name)
273 raise JSONRPCError("users group %s already exist" % group_name)
274
274
275 try:
275 try:
276 ug = UsersGroupModel().create(name=group_name, active=active)
276 ug = UsersGroupModel().create(name=group_name, active=active)
277 Session.commit()
277 Session.commit()
278 return dict(id=ug.users_group_id,
278 return dict(id=ug.users_group_id,
279 msg='created new users group %s' % group_name)
279 msg='created new users group %s' % group_name)
280 except Exception:
280 except Exception:
281 log.error(traceback.format_exc())
281 log.error(traceback.format_exc())
282 raise JSONRPCError('failed to create group %s' % group_name)
282 raise JSONRPCError('failed to create group %s' % group_name)
283
283
284 @HasPermissionAllDecorator('hg.admin')
284 @HasPermissionAllDecorator('hg.admin')
285 def add_user_to_users_group(self, apiuser, group_name, username):
285 def add_user_to_users_group(self, apiuser, group_name, username):
286 """"
286 """"
287 Add a user to a users group
287 Add a user to a users group
288
288
289 :param apiuser:
289 :param apiuser:
290 :param group_name:
290 :param group_name:
291 :param username:
291 :param username:
292 """
292 """
293
293
294 try:
294 try:
295 users_group = UsersGroup.get_by_group_name(group_name)
295 users_group = UsersGroup.get_by_group_name(group_name)
296 if not users_group:
296 if not users_group:
297 raise JSONRPCError('unknown users group %s' % group_name)
297 raise JSONRPCError('unknown users group %s' % group_name)
298
298
299 user = User.get_by_username(username)
299 user = User.get_by_username(username)
300 if user is None:
300 if user is None:
301 raise JSONRPCError('unknown user %s' % username)
301 raise JSONRPCError('unknown user %s' % username)
302
302
303 ugm = UsersGroupModel().add_user_to_group(users_group, user)
303 ugm = UsersGroupModel().add_user_to_group(users_group, user)
304 success = True if ugm != True else False
304 success = True if ugm != True else False
305 msg = 'added member %s to users group %s' % (username, group_name)
305 msg = 'added member %s to users group %s' % (username, group_name)
306 msg = msg if success else 'User is already in that group'
306 msg = msg if success else 'User is already in that group'
307 Session.commit()
307 Session.commit()
308
308
309 return dict(
309 return dict(
310 id=ugm.users_group_member_id if ugm != True else None,
310 id=ugm.users_group_member_id if ugm != True else None,
311 success=success,
311 success=success,
312 msg=msg
312 msg=msg
313 )
313 )
314 except Exception:
314 except Exception:
315 log.error(traceback.format_exc())
315 log.error(traceback.format_exc())
316 raise JSONRPCError('failed to add users group member')
316 raise JSONRPCError('failed to add users group member')
317
317
318 @HasPermissionAllDecorator('hg.admin')
318 @HasPermissionAllDecorator('hg.admin')
319 def remove_user_from_users_group(self, apiuser, group_name, username):
319 def remove_user_from_users_group(self, apiuser, group_name, username):
320 """
320 """
321 Remove user from a group
321 Remove user from a group
322
322
323 :param apiuser
323 :param apiuser
324 :param group_name
324 :param group_name
325 :param username
325 :param username
326 """
326 """
327
327
328 try:
328 try:
329 users_group = UsersGroup.get_by_group_name(group_name)
329 users_group = UsersGroup.get_by_group_name(group_name)
330 if not users_group:
330 if not users_group:
331 raise JSONRPCError('unknown users group %s' % group_name)
331 raise JSONRPCError('unknown users group %s' % group_name)
332
332
333 user = User.get_by_username(username)
333 user = User.get_by_username(username)
334 if user is None:
334 if user is None:
335 raise JSONRPCError('unknown user %s' % username)
335 raise JSONRPCError('unknown user %s' % username)
336
336
337 success = UsersGroupModel().remove_user_from_group(users_group, user)
337 success = UsersGroupModel().remove_user_from_group(users_group, user)
338 msg = 'removed member %s from users group %s' % (username, group_name)
338 msg = 'removed member %s from users group %s' % (username, group_name)
339 msg = msg if success else "User wasn't in group"
339 msg = msg if success else "User wasn't in group"
340 Session.commit()
340 Session.commit()
341 return dict(success=success, msg=msg)
341 return dict(success=success, msg=msg)
342 except Exception:
342 except Exception:
343 log.error(traceback.format_exc())
343 log.error(traceback.format_exc())
344 raise JSONRPCError('failed to remove user from group')
344 raise JSONRPCError('failed to remove user from group')
345
345
346 @HasPermissionAnyDecorator('hg.admin')
346 @HasPermissionAnyDecorator('hg.admin')
347 def get_repo(self, apiuser, repoid):
347 def get_repo(self, apiuser, repoid):
348 """"
348 """"
349 Get repository by name
349 Get repository by name
350
350
351 :param apiuser:
351 :param apiuser:
352 :param repo_name:
352 :param repo_name:
353 """
353 """
354
354
355 repo = RepoModel().get_repo(repoid)
355 repo = RepoModel().get_repo(repoid)
356 if repo is None:
356 if repo is None:
357 raise JSONRPCError('unknown repository %s' % repo)
357 raise JSONRPCError('unknown repository %s' % repo)
358
358
359 members = []
359 members = []
360 for user in repo.repo_to_perm:
360 for user in repo.repo_to_perm:
361 perm = user.permission.permission_name
361 perm = user.permission.permission_name
362 user = user.user
362 user = user.user
363 members.append(
363 members.append(
364 dict(
364 dict(
365 type="user",
365 type="user",
366 id=user.user_id,
366 id=user.user_id,
367 username=user.username,
367 username=user.username,
368 firstname=user.name,
368 firstname=user.name,
369 lastname=user.lastname,
369 lastname=user.lastname,
370 email=user.email,
370 email=user.email,
371 active=user.active,
371 active=user.active,
372 admin=user.admin,
372 admin=user.admin,
373 ldap=user.ldap_dn,
373 ldap=user.ldap_dn,
374 permission=perm
374 permission=perm
375 )
375 )
376 )
376 )
377 for users_group in repo.users_group_to_perm:
377 for users_group in repo.users_group_to_perm:
378 perm = users_group.permission.permission_name
378 perm = users_group.permission.permission_name
379 users_group = users_group.users_group
379 users_group = users_group.users_group
380 members.append(
380 members.append(
381 dict(
381 dict(
382 type="users_group",
382 type="users_group",
383 id=users_group.users_group_id,
383 id=users_group.users_group_id,
384 name=users_group.users_group_name,
384 name=users_group.users_group_name,
385 active=users_group.users_group_active,
385 active=users_group.users_group_active,
386 permission=perm
386 permission=perm
387 )
387 )
388 )
388 )
389
389
390 return dict(
390 return dict(
391 id=repo.repo_id,
391 id=repo.repo_id,
392 repo_name=repo.repo_name,
392 repo_name=repo.repo_name,
393 type=repo.repo_type,
393 type=repo.repo_type,
394 clone_uri=repo.clone_uri,
395 private=repo.private,
396 created_on=repo.created_on,
394 description=repo.description,
397 description=repo.description,
395 members=members
398 members=members
396 )
399 )
397
400
398 @HasPermissionAnyDecorator('hg.admin')
401 @HasPermissionAnyDecorator('hg.admin')
399 def get_repos(self, apiuser):
402 def get_repos(self, apiuser):
400 """"
403 """"
401 Get all repositories
404 Get all repositories
402
405
403 :param apiuser:
406 :param apiuser:
404 """
407 """
405
408
406 result = []
409 result = []
407 for repository in Repository.getAll():
410 for repo in Repository.getAll():
408 result.append(
411 result.append(
409 dict(
412 dict(
410 id=repository.repo_id,
413 id=repo.repo_id,
411 repo_name=repository.repo_name,
414 repo_name=repo.repo_name,
412 type=repository.repo_type,
415 type=repo.repo_type,
413 description=repository.description
416 clone_uri=repo.clone_uri,
417 private=repo.private,
418 created_on=repo.created_on,
419 description=repo.description,
414 )
420 )
415 )
421 )
416 return result
422 return result
417
423
418 @HasPermissionAnyDecorator('hg.admin')
424 @HasPermissionAnyDecorator('hg.admin')
419 def get_repo_nodes(self, apiuser, repo_name, revision, root_path,
425 def get_repo_nodes(self, apiuser, repo_name, revision, root_path,
420 ret_type='all'):
426 ret_type='all'):
421 """
427 """
422 returns a list of nodes and it's children
428 returns a list of nodes and it's children
423 for a given path at given revision. It's possible to specify ret_type
429 for a given path at given revision. It's possible to specify ret_type
424 to show only files or dirs
430 to show only files or dirs
425
431
426 :param apiuser:
432 :param apiuser:
427 :param repo_name: name of repository
433 :param repo_name: name of repository
428 :param revision: revision for which listing should be done
434 :param revision: revision for which listing should be done
429 :param root_path: path from which start displaying
435 :param root_path: path from which start displaying
430 :param ret_type: return type 'all|files|dirs' nodes
436 :param ret_type: return type 'all|files|dirs' nodes
431 """
437 """
432 try:
438 try:
433 _d, _f = ScmModel().get_nodes(repo_name, revision, root_path,
439 _d, _f = ScmModel().get_nodes(repo_name, revision, root_path,
434 flat=False)
440 flat=False)
435 _map = {
441 _map = {
436 'all': _d + _f,
442 'all': _d + _f,
437 'files': _f,
443 'files': _f,
438 'dirs': _d,
444 'dirs': _d,
439 }
445 }
440 return _map[ret_type]
446 return _map[ret_type]
441 except KeyError:
447 except KeyError:
442 raise JSONRPCError('ret_type must be one of %s' % _map.keys())
448 raise JSONRPCError('ret_type must be one of %s' % _map.keys())
443 except Exception, e:
449 except Exception, e:
444 raise JSONRPCError(e)
450 raise JSONRPCError(e)
445
451
446 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
452 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
447 def create_repo(self, apiuser, repo_name, owner_name, description='',
453 def create_repo(self, apiuser, repo_name, owner_name, description='',
448 repo_type='hg', private=False, clone_uri=None):
454 repo_type='hg', private=False, clone_uri=None):
449 """
455 """
450 Create repository, if clone_url is given it makes a remote clone
456 Create repository, if clone_url is given it makes a remote clone
451
457
452 :param apiuser:
458 :param apiuser:
453 :param repo_name:
459 :param repo_name:
454 :param owner_name:
460 :param owner_name:
455 :param description:
461 :param description:
456 :param repo_type:
462 :param repo_type:
457 :param private:
463 :param private:
458 :param clone_uri:
464 :param clone_uri:
459 """
465 """
460
466
461 try:
467 try:
462 owner = User.get_by_username(owner_name)
468 owner = User.get_by_username(owner_name)
463 if owner is None:
469 if owner is None:
464 raise JSONRPCError('unknown user %s' % owner_name)
470 raise JSONRPCError('unknown user %s' % owner_name)
465
471
466 if Repository.get_by_repo_name(repo_name):
472 if Repository.get_by_repo_name(repo_name):
467 raise JSONRPCError("repo %s already exist" % repo_name)
473 raise JSONRPCError("repo %s already exist" % repo_name)
468
474
469 groups = repo_name.split(Repository.url_sep())
475 groups = repo_name.split(Repository.url_sep())
470 real_name = groups[-1]
476 real_name = groups[-1]
471 # create structure of groups
477 # create structure of groups
472 group = map_groups(repo_name)
478 group = map_groups(repo_name)
473
479
474 repo = RepoModel().create(
480 repo = RepoModel().create(
475 dict(
481 dict(
476 repo_name=real_name,
482 repo_name=real_name,
477 repo_name_full=repo_name,
483 repo_name_full=repo_name,
478 description=description,
484 description=description,
479 private=private,
485 private=private,
480 repo_type=repo_type,
486 repo_type=repo_type,
481 repo_group=group.group_id if group else None,
487 repo_group=group.group_id if group else None,
482 clone_uri=clone_uri
488 clone_uri=clone_uri
483 ),
489 ),
484 owner
490 owner
485 )
491 )
486 Session.commit()
492 Session.commit()
487
493
488 return dict(
494 return dict(
489 id=repo.repo_id,
495 id=repo.repo_id,
490 msg="Created new repository %s" % repo.repo_name
496 msg="Created new repository %s" % repo.repo_name
491 )
497 )
492
498
493 except Exception:
499 except Exception:
494 log.error(traceback.format_exc())
500 log.error(traceback.format_exc())
495 raise JSONRPCError('failed to create repository %s' % repo_name)
501 raise JSONRPCError('failed to create repository %s' % repo_name)
496
502
497 @HasPermissionAnyDecorator('hg.admin')
503 @HasPermissionAnyDecorator('hg.admin')
498 def delete_repo(self, apiuser, repo_name):
504 def delete_repo(self, apiuser, repo_name):
499 """
505 """
500 Deletes a given repository
506 Deletes a given repository
501
507
502 :param repo_name:
508 :param repo_name:
503 """
509 """
504 if not Repository.get_by_repo_name(repo_name):
510 if not Repository.get_by_repo_name(repo_name):
505 raise JSONRPCError("repo %s does not exist" % repo_name)
511 raise JSONRPCError("repo %s does not exist" % repo_name)
506 try:
512 try:
507 RepoModel().delete(repo_name)
513 RepoModel().delete(repo_name)
508 Session.commit()
514 Session.commit()
509 return dict(
515 return dict(
510 msg='Deleted repository %s' % repo_name
516 msg='Deleted repository %s' % repo_name
511 )
517 )
512 except Exception:
518 except Exception:
513 log.error(traceback.format_exc())
519 log.error(traceback.format_exc())
514 raise JSONRPCError('failed to delete repository %s' % repo_name)
520 raise JSONRPCError('failed to delete repository %s' % repo_name)
515
521
516 @HasPermissionAnyDecorator('hg.admin')
522 @HasPermissionAnyDecorator('hg.admin')
517 def grant_user_permission(self, apiuser, repo_name, username, perm):
523 def grant_user_permission(self, apiuser, repo_name, username, perm):
518 """
524 """
519 Grant permission for user on given repository, or update existing one
525 Grant permission for user on given repository, or update existing one
520 if found
526 if found
521
527
522 :param repo_name:
528 :param repo_name:
523 :param username:
529 :param username:
524 :param perm:
530 :param perm:
525 """
531 """
526
532
527 try:
533 try:
528 repo = Repository.get_by_repo_name(repo_name)
534 repo = Repository.get_by_repo_name(repo_name)
529 if repo is None:
535 if repo is None:
530 raise JSONRPCError('unknown repository %s' % repo)
536 raise JSONRPCError('unknown repository %s' % repo)
531
537
532 user = User.get_by_username(username)
538 user = User.get_by_username(username)
533 if user is None:
539 if user is None:
534 raise JSONRPCError('unknown user %s' % username)
540 raise JSONRPCError('unknown user %s' % username)
535
541
536 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
542 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
537
543
538 Session.commit()
544 Session.commit()
539 return dict(
545 return dict(
540 msg='Granted perm: %s for user: %s in repo: %s' % (
546 msg='Granted perm: %s for user: %s in repo: %s' % (
541 perm, username, repo_name
547 perm, username, repo_name
542 )
548 )
543 )
549 )
544 except Exception:
550 except Exception:
545 log.error(traceback.format_exc())
551 log.error(traceback.format_exc())
546 raise JSONRPCError(
552 raise JSONRPCError(
547 'failed to edit permission %(repo)s for %(user)s' % dict(
553 'failed to edit permission %(repo)s for %(user)s' % dict(
548 user=username, repo=repo_name
554 user=username, repo=repo_name
549 )
555 )
550 )
556 )
551
557
552 @HasPermissionAnyDecorator('hg.admin')
558 @HasPermissionAnyDecorator('hg.admin')
553 def revoke_user_permission(self, apiuser, repo_name, username):
559 def revoke_user_permission(self, apiuser, repo_name, username):
554 """
560 """
555 Revoke permission for user on given repository
561 Revoke permission for user on given repository
556
562
557 :param repo_name:
563 :param repo_name:
558 :param username:
564 :param username:
559 """
565 """
560
566
561 try:
567 try:
562 repo = Repository.get_by_repo_name(repo_name)
568 repo = Repository.get_by_repo_name(repo_name)
563 if repo is None:
569 if repo is None:
564 raise JSONRPCError('unknown repository %s' % repo)
570 raise JSONRPCError('unknown repository %s' % repo)
565
571
566 user = User.get_by_username(username)
572 user = User.get_by_username(username)
567 if user is None:
573 if user is None:
568 raise JSONRPCError('unknown user %s' % username)
574 raise JSONRPCError('unknown user %s' % username)
569
575
570 RepoModel().revoke_user_permission(repo=repo_name, user=username)
576 RepoModel().revoke_user_permission(repo=repo_name, user=username)
571
577
572 Session.commit()
578 Session.commit()
573 return dict(
579 return dict(
574 msg='Revoked perm for user: %s in repo: %s' % (
580 msg='Revoked perm for user: %s in repo: %s' % (
575 username, repo_name
581 username, repo_name
576 )
582 )
577 )
583 )
578 except Exception:
584 except Exception:
579 log.error(traceback.format_exc())
585 log.error(traceback.format_exc())
580 raise JSONRPCError(
586 raise JSONRPCError(
581 'failed to edit permission %(repo)s for %(user)s' % dict(
587 'failed to edit permission %(repo)s for %(user)s' % dict(
582 user=username, repo=repo_name
588 user=username, repo=repo_name
583 )
589 )
584 )
590 )
585
591
586 @HasPermissionAnyDecorator('hg.admin')
592 @HasPermissionAnyDecorator('hg.admin')
587 def grant_users_group_permission(self, apiuser, repo_name, group_name, perm):
593 def grant_users_group_permission(self, apiuser, repo_name, group_name, perm):
588 """
594 """
589 Grant permission for users group on given repository, or update
595 Grant permission for users group on given repository, or update
590 existing one if found
596 existing one if found
591
597
592 :param repo_name:
598 :param repo_name:
593 :param group_name:
599 :param group_name:
594 :param perm:
600 :param perm:
595 """
601 """
596
602
597 try:
603 try:
598 repo = Repository.get_by_repo_name(repo_name)
604 repo = Repository.get_by_repo_name(repo_name)
599 if repo is None:
605 if repo is None:
600 raise JSONRPCError('unknown repository %s' % repo)
606 raise JSONRPCError('unknown repository %s' % repo)
601
607
602 user_group = UsersGroup.get_by_group_name(group_name)
608 user_group = UsersGroup.get_by_group_name(group_name)
603 if user_group is None:
609 if user_group is None:
604 raise JSONRPCError('unknown users group %s' % user_group)
610 raise JSONRPCError('unknown users group %s' % user_group)
605
611
606 RepoModel().grant_users_group_permission(repo=repo_name,
612 RepoModel().grant_users_group_permission(repo=repo_name,
607 group_name=group_name,
613 group_name=group_name,
608 perm=perm)
614 perm=perm)
609
615
610 Session.commit()
616 Session.commit()
611 return dict(
617 return dict(
612 msg='Granted perm: %s for group: %s in repo: %s' % (
618 msg='Granted perm: %s for group: %s in repo: %s' % (
613 perm, group_name, repo_name
619 perm, group_name, repo_name
614 )
620 )
615 )
621 )
616 except Exception:
622 except Exception:
617 log.error(traceback.format_exc())
623 log.error(traceback.format_exc())
618 raise JSONRPCError(
624 raise JSONRPCError(
619 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
625 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
620 usersgr=group_name, repo=repo_name
626 usersgr=group_name, repo=repo_name
621 )
627 )
622 )
628 )
623
629
624 @HasPermissionAnyDecorator('hg.admin')
630 @HasPermissionAnyDecorator('hg.admin')
625 def revoke_users_group_permission(self, apiuser, repo_name, group_name):
631 def revoke_users_group_permission(self, apiuser, repo_name, group_name):
626 """
632 """
627 Revoke permission for users group on given repository
633 Revoke permission for users group on given repository
628
634
629 :param repo_name:
635 :param repo_name:
630 :param group_name:
636 :param group_name:
631 """
637 """
632
638
633 try:
639 try:
634 repo = Repository.get_by_repo_name(repo_name)
640 repo = Repository.get_by_repo_name(repo_name)
635 if repo is None:
641 if repo is None:
636 raise JSONRPCError('unknown repository %s' % repo)
642 raise JSONRPCError('unknown repository %s' % repo)
637
643
638 user_group = UsersGroup.get_by_group_name(group_name)
644 user_group = UsersGroup.get_by_group_name(group_name)
639 if user_group is None:
645 if user_group is None:
640 raise JSONRPCError('unknown users group %s' % user_group)
646 raise JSONRPCError('unknown users group %s' % user_group)
641
647
642 RepoModel().revoke_users_group_permission(repo=repo_name,
648 RepoModel().revoke_users_group_permission(repo=repo_name,
643 group_name=group_name)
649 group_name=group_name)
644
650
645 Session.commit()
651 Session.commit()
646 return dict(
652 return dict(
647 msg='Revoked perm for group: %s in repo: %s' % (
653 msg='Revoked perm for group: %s in repo: %s' % (
648 group_name, repo_name
654 group_name, repo_name
649 )
655 )
650 )
656 )
651 except Exception:
657 except Exception:
652 log.error(traceback.format_exc())
658 log.error(traceback.format_exc())
653 raise JSONRPCError(
659 raise JSONRPCError(
654 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
660 'failed to edit permission %(repo)s for %(usersgr)s' % dict(
655 usersgr=group_name, repo=repo_name
661 usersgr=group_name, repo=repo_name
656 )
662 )
657 )
663 )
General Comments 0
You need to be logged in to leave comments. Login now