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