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