Mercurial > kallithea
annotate rhodecode/controllers/api/api.py @ 3700:3563bb7b4b82
merge with rc1
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 07 Apr 2013 18:37:22 +0200 |
parents | cd50d1b5f35b b44979649915 |
children | e3857cbb6d10 |
rev | line source |
---|---|
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
1 # -*- coding: utf-8 -*- |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
2 """ |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
3 rhodecode.controllers.api |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
5 |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
6 API controller for RhodeCode |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
7 |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
8 :created_on: Aug 20, 2011 |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
9 :author: marcink |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
12 """ |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
13 # This program is free software; you can redistribute it and/or |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
14 # modify it under the terms of the GNU General Public License |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
15 # as published by the Free Software Foundation; version 2 |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
16 # of the License or (at your opinion) any later version of the license. |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
17 # |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
18 # This program is distributed in the hope that it will be useful, |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
21 # GNU General Public License for more details. |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
22 # |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
24 # along with this program; if not, write to the Free Software |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
26 # MA 02110-1301, USA. |
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1810
diff
changeset
|
27 |
1500 | 28 import traceback |
29 import logging | |
30 | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 from rhodecode.controllers.api import JSONRPCController, JSONRPCError |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
32 from rhodecode.lib.auth import PasswordGenerator, AuthUser, \ |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
33 HasPermissionAllDecorator, HasPermissionAnyDecorator, \ |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
34 HasPermissionAnyApi, HasRepoPermissionAnyApi |
2697
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
35 from rhodecode.lib.utils import map_groups, repo2db_mapper |
3502
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_int |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
37 from rhodecode.lib import helpers as h |
1734
48d4fcf04a29
another major refactoring with session management
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
38 from rhodecode.model.meta import Session |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 from rhodecode.model.scm import ScmModel |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
40 from rhodecode.model.repo import RepoModel |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
41 from rhodecode.model.user import UserModel |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
42 from rhodecode.model.users_group import UserGroupModel |
2526 | 43 from rhodecode.model.permission import PermissionModel |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
44 from rhodecode.model.db import Repository, RhodeCodeSetting, UserIpMap |
3502
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
45 from rhodecode.lib.compat import json |
1734
48d4fcf04a29
another major refactoring with session management
Marcin Kuzminski <marcin@python-works.com>
parents:
1716
diff
changeset
|
46 |
1593 | 47 log = logging.getLogger(__name__) |
1500 | 48 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
50 class OptionalAttr(object): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
51 """ |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
52 Special Optional Option that defines other attribute |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
53 """ |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
54 def __init__(self, attr_name): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
55 self.attr_name = attr_name |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
56 |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
57 def __repr__(self): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
58 return '<OptionalAttr:%s>' % self.attr_name |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
59 |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
60 def __call__(self): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
61 return self |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
62 #alias |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
63 OAttr = OptionalAttr |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
64 |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
65 |
2526 | 66 class Optional(object): |
67 """ | |
68 Defines an optional parameter:: | |
69 | |
70 param = param.getval() if isinstance(param, Optional) else param | |
71 param = param() if isinstance(param, Optional) else param | |
72 | |
73 is equivalent of:: | |
74 | |
75 param = Optional.extract(param) | |
76 | |
77 """ | |
78 def __init__(self, type_): | |
79 self.type_ = type_ | |
80 | |
81 def __repr__(self): | |
82 return '<Optional:%s>' % self.type_.__repr__() | |
83 | |
84 def __call__(self): | |
85 return self.getval() | |
86 | |
87 def getval(self): | |
88 """ | |
89 returns value from this Optional instance | |
90 """ | |
91 return self.type_ | |
92 | |
93 @classmethod | |
94 def extract(cls, val): | |
95 if isinstance(val, cls): | |
96 return val.getval() | |
97 return val | |
98 | |
99 | |
100 def get_user_or_error(userid): | |
101 """ | |
102 Get user by id or name or return JsonRPCError if not found | |
103 | |
104 :param userid: | |
105 """ | |
106 user = UserModel().get_user(userid) | |
107 if user is None: | |
108 raise JSONRPCError("user `%s` does not exist" % userid) | |
109 return user | |
110 | |
111 | |
112 def get_repo_or_error(repoid): | |
113 """ | |
114 Get repo by id or name or return JsonRPCError if not found | |
115 | |
116 :param userid: | |
117 """ | |
118 repo = RepoModel().get_repo(repoid) | |
119 if repo is None: | |
120 raise JSONRPCError('repository `%s` does not exist' % (repoid)) | |
121 return repo | |
122 | |
123 | |
124 def get_users_group_or_error(usersgroupid): | |
125 """ | |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
126 Get user group by id or name or return JsonRPCError if not found |
2526 | 127 |
128 :param userid: | |
129 """ | |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
130 users_group = UserGroupModel().get_group(usersgroupid) |
2526 | 131 if users_group is None: |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
132 raise JSONRPCError('user group `%s` does not exist' % usersgroupid) |
2526 | 133 return users_group |
134 | |
135 | |
136 def get_perm_or_error(permid): | |
137 """ | |
138 Get permission by id or name or return JsonRPCError if not found | |
139 | |
140 :param userid: | |
141 """ | |
142 perm = PermissionModel().get_permission_by_name(permid) | |
143 if perm is None: | |
144 raise JSONRPCError('permission `%s` does not exist' % (permid)) | |
145 return perm | |
146 | |
147 | |
1593 | 148 class ApiController(JSONRPCController): |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 API Controller |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
151 |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
152 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 Each method needs to have USER as argument this is then based on given |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 API_KEY propagated as instance of user object |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
155 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 Preferably this should be first argument also |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
157 |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
158 |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
159 Each function should also **raise** JSONRPCError for any |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 errors that happens |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
161 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
162 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 |
1593 | 164 @HasPermissionAllDecorator('hg.admin') |
2526 | 165 def pull(self, apiuser, repoid): |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
167 Dispatch pull action on given repo |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
168 |
2526 | 169 :param apiuser: |
170 :param repoid: | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 """ |
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 |
2526 | 173 repo = get_repo_or_error(repoid) |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
174 |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 try: |
2526 | 176 ScmModel().pull_changes(repo.repo_name, |
177 self.rhodecode_user.username) | |
178 return 'Pulled from `%s`' % repo.repo_name | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 except Exception: |
2526 | 180 log.error(traceback.format_exc()) |
181 raise JSONRPCError( | |
182 'Unable to pull changes from `%s`' % repo.repo_name | |
183 ) | |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 |
1593 | 185 @HasPermissionAllDecorator('hg.admin') |
2697
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
186 def rescan_repos(self, apiuser, remove_obsolete=Optional(False)): |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
187 """ |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
188 Dispatch rescan repositories action. If remove_obsolete is set |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
189 than also delete repos that are in database but not in the filesystem. |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
190 aka "clean zombies" |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
191 |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
192 :param apiuser: |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
193 :param remove_obsolete: |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
194 """ |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
195 |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
196 try: |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
197 rm_obsolete = Optional.extract(remove_obsolete) |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
198 added, removed = repo2db_mapper(ScmModel().repo_scan(), |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
199 remove_obsolete=rm_obsolete) |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
200 return {'added': added, 'removed': removed} |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
201 except Exception: |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
202 log.error(traceback.format_exc()) |
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
203 raise JSONRPCError( |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
204 'Error occurred during rescan repositories action' |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
205 ) |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
206 |
3235
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
207 def invalidate_cache(self, apiuser, repoid): |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
208 """ |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
209 Dispatch cache invalidation action on given repo |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
210 |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
211 :param apiuser: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
212 :param repoid: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
213 """ |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
214 repo = get_repo_or_error(repoid) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
215 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
216 # check if we have admin permission for this repo ! |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
217 if HasRepoPermissionAnyApi('repository.admin', |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
218 'repository.write')(user=apiuser, |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
219 repo_name=repo.repo_name) is False: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
220 raise JSONRPCError('repository `%s` does not exist' % (repoid)) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
221 |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
222 try: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
223 invalidated_keys = ScmModel().mark_for_invalidation(repo.repo_name) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
224 Session().commit() |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
225 return ('Cache for repository `%s` was invalidated: ' |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
226 'invalidated cache keys: %s' % (repoid, invalidated_keys)) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
227 except Exception: |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
228 log.error(traceback.format_exc()) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
229 raise JSONRPCError( |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
230 'Error occurred during cache invalidation action' |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
231 ) |
d6029dacbcc4
API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents:
3195
diff
changeset
|
232 |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
233 def lock(self, apiuser, repoid, locked=Optional(None), |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
234 userid=Optional(OAttr('apiuser'))): |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
235 """ |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
236 Set locking state on particular repository by given user, if |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
237 this command is runned by non-admin account userid is set to user |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
238 who is calling this method |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
239 |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
240 :param apiuser: |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
241 :param repoid: |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
242 :param userid: |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
243 :param locked: |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
244 """ |
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
245 repo = get_repo_or_error(repoid) |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
246 if HasPermissionAnyApi('hg.admin')(user=apiuser): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
247 pass |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
248 elif HasRepoPermissionAnyApi('repository.admin', |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
249 'repository.write')(user=apiuser, |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
250 repo_name=repo.repo_name): |
3168
2fb94c52e20e
whitespace cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
251 #make sure normal user does not pass someone else userid, |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
252 #he is not allowed to do that |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
253 if not isinstance(userid, Optional) and userid != apiuser.user_id: |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
254 raise JSONRPCError( |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
255 'userid is not the same as your user' |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
256 ) |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
257 else: |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
258 raise JSONRPCError('repository `%s` does not exist' % (repoid)) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
259 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
260 if isinstance(userid, Optional): |
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
261 userid = apiuser.user_id |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
262 |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
263 user = get_user_or_error(userid) |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
264 |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
265 if isinstance(locked, Optional): |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
266 lockobj = Repository.getlock(repo) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
267 |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
268 if lockobj[0] is None: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
269 return ('Repo `%s` not locked. Locked=`False`.' |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
270 % (repo.repo_name)) |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
271 else: |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
272 userid, time_ = lockobj |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
273 user = get_user_or_error(userid) |
2737
e21cb7b1a4a5
added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2697
diff
changeset
|
274 |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
275 return ('Repo `%s` locked by `%s`. Locked=`True`. ' |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
276 'Locked since: `%s`' |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
277 % (repo.repo_name, user.username, |
3502
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
278 json.dumps(time_to_datetime(time_)))) |
3457
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
279 |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
280 else: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
281 locked = str2bool(locked) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
282 try: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
283 if locked: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
284 Repository.lock(repo, user.user_id) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
285 else: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
286 Repository.unlock(repo) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
287 |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
288 return ('User `%s` set lock state for repo `%s` to `%s`' |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
289 % (user.username, repo.repo_name, locked)) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
290 except Exception: |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
291 log.error(traceback.format_exc()) |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
292 raise JSONRPCError( |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
293 'Error occurred locking repository `%s`' % repo.repo_name |
08e8115585bd
calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents:
3450
diff
changeset
|
294 ) |
2697
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
295 |
3502
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
296 def get_locks(self, apiuser, userid=Optional(OAttr('apiuser'))): |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
297 """ |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
298 Get all locks for given userid, if |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
299 this command is runned by non-admin account userid is set to user |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
300 who is calling this method, thus returning locks for himself |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
301 |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
302 :param apiuser: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
303 :param userid: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
304 """ |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
305 if HasPermissionAnyApi('hg.admin')(user=apiuser): |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
306 pass |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
307 else: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
308 #make sure normal user does not pass someone else userid, |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
309 #he is not allowed to do that |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
310 if not isinstance(userid, Optional) and userid != apiuser.user_id: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
311 raise JSONRPCError( |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
312 'userid is not the same as your user' |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
313 ) |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
314 ret = [] |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
315 if isinstance(userid, Optional): |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
316 user = None |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
317 else: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
318 user = get_user_or_error(userid) |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
319 |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
320 #show all locks |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
321 for r in Repository.getAll(): |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
322 userid, time_ = r.locked |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
323 if time_: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
324 _api_data = r.get_api_data() |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
325 # if we use userfilter just show the locks for this user |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
326 if user: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
327 if safe_int(userid) == user.user_id: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
328 ret.append(_api_data) |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
329 else: |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
330 ret.append(_api_data) |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
331 |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
332 return ret |
7cde75eac0fe
get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents:
3457
diff
changeset
|
333 |
2697
4565e655ea2a
API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents:
2657
diff
changeset
|
334 @HasPermissionAllDecorator('hg.admin') |
3126
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
335 def show_ip(self, apiuser, userid): |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
336 """ |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
337 Shows IP address as seen from RhodeCode server, together with all |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
338 defined IP addresses for given user |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
339 |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
340 :param apiuser: |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
341 :param userid: |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
342 """ |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
343 user = get_user_or_error(userid) |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
344 ips = UserIpMap.query().filter(UserIpMap.user == user).all() |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
345 return dict( |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
346 ip_addr_server=self.ip_addr, |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
347 user_ips=ips |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
348 ) |
703070153bc1
added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents:
3125
diff
changeset
|
349 |
3162
a0a8f38e8fb8
API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents:
3161
diff
changeset
|
350 def get_user(self, apiuser, userid=Optional(OAttr('apiuser'))): |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
351 """" |
3162
a0a8f38e8fb8
API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents:
3161
diff
changeset
|
352 Get a user by username, or userid, if userid is given |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
353 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
354 :param apiuser: |
2526 | 355 :param userid: |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
356 """ |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
357 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
3168
2fb94c52e20e
whitespace cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
3163
diff
changeset
|
358 #make sure normal user does not pass someone else userid, |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
359 #he is not allowed to do that |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
360 if not isinstance(userid, Optional) and userid != apiuser.user_id: |
3162
a0a8f38e8fb8
API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents:
3161
diff
changeset
|
361 raise JSONRPCError( |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
362 'userid is not the same as your user' |
3162
a0a8f38e8fb8
API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents:
3161
diff
changeset
|
363 ) |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
364 |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
365 if isinstance(userid, Optional): |
3162
a0a8f38e8fb8
API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents:
3161
diff
changeset
|
366 userid = apiuser.user_id |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
367 |
2526 | 368 user = get_user_or_error(userid) |
369 data = user.get_api_data() | |
370 data['permissions'] = AuthUser(user_id=user.user_id).permissions | |
371 return data | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
372 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
373 @HasPermissionAllDecorator('hg.admin') |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
374 def get_users(self, apiuser): |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
375 """" |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
376 Get all users |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
377 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
378 :param apiuser: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
379 """ |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
380 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
381 result = [] |
2526 | 382 for user in UserModel().get_all(): |
383 result.append(user.get_api_data()) | |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
384 return result |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
385 |
1593 | 386 @HasPermissionAllDecorator('hg.admin') |
2526 | 387 def create_user(self, apiuser, username, email, password, |
388 firstname=Optional(None), lastname=Optional(None), | |
389 active=Optional(True), admin=Optional(False), | |
390 ldap_dn=Optional(None)): | |
1500 | 391 """ |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
392 Create new user |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
393 |
1500 | 394 :param apiuser: |
395 :param username: | |
2526 | 396 :param email: |
1500 | 397 :param password: |
2526 | 398 :param firstname: |
1500 | 399 :param lastname: |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
400 :param active: |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
401 :param admin: |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
402 :param ldap_dn: |
1500 | 403 """ |
1589
307ec693bdf2
[API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1587
diff
changeset
|
404 |
2526 | 405 if UserModel().get_by_username(username): |
406 raise JSONRPCError("user `%s` already exist" % username) | |
407 | |
408 if UserModel().get_by_email(email, case_insensitive=True): | |
409 raise JSONRPCError("email `%s` already exist" % email) | |
2008
9ddbfaeefb73
API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents:
2006
diff
changeset
|
410 |
2758
dc4709e7da51
fixed passing password via API create_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2737
diff
changeset
|
411 if Optional.extract(ldap_dn): |
2008
9ddbfaeefb73
API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents:
2006
diff
changeset
|
412 # generate temporary password if ldap_dn |
9ddbfaeefb73
API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents:
2006
diff
changeset
|
413 password = PasswordGenerator().gen_password(length=8) |
9ddbfaeefb73
API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents:
2006
diff
changeset
|
414 |
1500 | 415 try: |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
416 user = UserModel().create_or_update( |
2526 | 417 username=Optional.extract(username), |
418 password=Optional.extract(password), | |
419 email=Optional.extract(email), | |
420 firstname=Optional.extract(firstname), | |
421 lastname=Optional.extract(lastname), | |
422 active=Optional.extract(active), | |
423 admin=Optional.extract(admin), | |
424 ldap_dn=Optional.extract(ldap_dn) | |
1843 | 425 ) |
2526 | 426 Session().commit() |
1843 | 427 return dict( |
2526 | 428 msg='created new user `%s`' % username, |
429 user=user.get_api_data() | |
1843 | 430 ) |
1500 | 431 except Exception: |
1593 | 432 log.error(traceback.format_exc()) |
2526 | 433 raise JSONRPCError('failed to create user `%s`' % username) |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
434 |
1593 | 435 @HasPermissionAllDecorator('hg.admin') |
2526 | 436 def update_user(self, apiuser, userid, username=Optional(None), |
437 email=Optional(None), firstname=Optional(None), | |
438 lastname=Optional(None), active=Optional(None), | |
439 admin=Optional(None), ldap_dn=Optional(None), | |
440 password=Optional(None)): | |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
441 """ |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
442 Updates given user |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
443 |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
444 :param apiuser: |
2526 | 445 :param userid: |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
446 :param username: |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
447 :param email: |
2526 | 448 :param firstname: |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
449 :param lastname: |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
450 :param active: |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
451 :param admin: |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
452 :param ldap_dn: |
2526 | 453 :param password: |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
454 """ |
2526 | 455 |
456 user = get_user_or_error(userid) | |
457 | |
2657
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
458 # call function and store only updated arguments |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
459 updates = {} |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
460 |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
461 def store_update(attr, name): |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
462 if not isinstance(attr, Optional): |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
463 updates[name] = attr |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
464 |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
465 try: |
2526 | 466 |
2657
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
467 store_update(username, 'username') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
468 store_update(password, 'password') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
469 store_update(email, 'email') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
470 store_update(firstname, 'name') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
471 store_update(lastname, 'lastname') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
472 store_update(active, 'active') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
473 store_update(admin, 'admin') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
474 store_update(ldap_dn, 'ldap_dn') |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
475 |
001c7e2ae986
fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents:
2653
diff
changeset
|
476 user = UserModel().update_user(user, **updates) |
2526 | 477 Session().commit() |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
478 return dict( |
2507
374693af2849
API: update_user returns new updated user data
Marcin Kuzminski <marcin@python-works.com>
parents:
2506
diff
changeset
|
479 msg='updated user ID:%s %s' % (user.user_id, user.username), |
2526 | 480 user=user.get_api_data() |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
481 ) |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
482 except Exception: |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
483 log.error(traceback.format_exc()) |
2526 | 484 raise JSONRPCError('failed to update user `%s`' % userid) |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
485 |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
486 @HasPermissionAllDecorator('hg.admin') |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
487 def delete_user(self, apiuser, userid): |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
488 """" |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
489 Deletes an user |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
490 |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
491 :param apiuser: |
2526 | 492 :param userid: |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
493 """ |
2526 | 494 user = get_user_or_error(userid) |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
495 |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
496 try: |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
497 UserModel().delete(userid) |
2526 | 498 Session().commit() |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
499 return dict( |
2526 | 500 msg='deleted user ID:%s %s' % (user.user_id, user.username), |
501 user=None | |
2365
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
502 ) |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
503 except Exception: |
b902baeaa494
API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents:
2338
diff
changeset
|
504 log.error(traceback.format_exc()) |
2526 | 505 raise JSONRPCError('failed to delete ID:%s %s' % (user.user_id, |
506 user.username)) | |
2002
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
507 |
bdc0ad168006
API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents:
1989
diff
changeset
|
508 @HasPermissionAllDecorator('hg.admin') |
2526 | 509 def get_users_group(self, apiuser, usersgroupid): |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
510 """" |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
511 Get user group by name or id |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
512 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
513 :param apiuser: |
2526 | 514 :param usersgroupid: |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
515 """ |
2526 | 516 users_group = get_users_group_or_error(usersgroupid) |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
517 |
2526 | 518 data = users_group.get_api_data() |
1445
c78f6bf52e9c
Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
519 |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
520 members = [] |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
521 for user in users_group.members: |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
522 user = user.user |
2526 | 523 members.append(user.get_api_data()) |
524 data['members'] = members | |
525 return data | |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
526 |
1593 | 527 @HasPermissionAllDecorator('hg.admin') |
528 def get_users_groups(self, apiuser): | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
529 """" |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
530 Get all user groups |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
531 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
532 :param apiuser: |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
533 """ |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
534 |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
535 result = [] |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
536 for users_group in UserGroupModel().get_all(): |
2526 | 537 result.append(users_group.get_api_data()) |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
538 return result |
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
539 |
1593 | 540 @HasPermissionAllDecorator('hg.admin') |
2526 | 541 def create_users_group(self, apiuser, group_name, active=Optional(True)): |
1500 | 542 """ |
543 Creates an new usergroup | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
544 |
2526 | 545 :param apiuser: |
1843 | 546 :param group_name: |
1500 | 547 :param active: |
548 """ | |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
549 |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
550 if UserGroupModel().get_by_name(group_name): |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
551 raise JSONRPCError("user group `%s` already exist" % group_name) |
1589
307ec693bdf2
[API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1587
diff
changeset
|
552 |
1500 | 553 try: |
2526 | 554 active = Optional.extract(active) |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
555 ug = UserGroupModel().create(name=group_name, active=active) |
2526 | 556 Session().commit() |
557 return dict( | |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
558 msg='created new user group `%s`' % group_name, |
2526 | 559 users_group=ug.get_api_data() |
560 ) | |
1500 | 561 except Exception: |
1593 | 562 log.error(traceback.format_exc()) |
2526 | 563 raise JSONRPCError('failed to create group `%s`' % group_name) |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
564 |
1593 | 565 @HasPermissionAllDecorator('hg.admin') |
2526 | 566 def add_user_to_users_group(self, apiuser, usersgroupid, userid): |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
567 """" |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
568 Add a user to a user group |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
569 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
570 :param apiuser: |
2526 | 571 :param usersgroupid: |
572 :param userid: | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
573 """ |
2526 | 574 user = get_user_or_error(userid) |
575 users_group = get_users_group_or_error(usersgroupid) | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
576 |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
577 try: |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
578 ugm = UserGroupModel().add_user_to_group(users_group, user) |
1989 | 579 success = True if ugm != True else False |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
580 msg = 'added member `%s` to user group `%s`' % ( |
2526 | 581 user.username, users_group.users_group_name |
582 ) | |
1989 | 583 msg = msg if success else 'User is already in that group' |
2526 | 584 Session().commit() |
1989 | 585 |
586 return dict( | |
587 success=success, | |
588 msg=msg | |
589 ) | |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
590 except Exception: |
1593 | 591 log.error(traceback.format_exc()) |
2526 | 592 raise JSONRPCError( |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
593 'failed to add member to user group `%s`' % ( |
2526 | 594 users_group.users_group_name |
595 ) | |
596 ) | |
1989 | 597 |
598 @HasPermissionAllDecorator('hg.admin') | |
2526 | 599 def remove_user_from_users_group(self, apiuser, usersgroupid, userid): |
1989 | 600 """ |
601 Remove user from a group | |
602 | |
2526 | 603 :param apiuser: |
604 :param usersgroupid: | |
605 :param userid: | |
1989 | 606 """ |
2526 | 607 user = get_user_or_error(userid) |
608 users_group = get_users_group_or_error(usersgroupid) | |
1989 | 609 |
610 try: | |
3417
fa6ba6727475
further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents:
3410
diff
changeset
|
611 success = UserGroupModel().remove_user_from_group(users_group, |
2526 | 612 user) |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
613 msg = 'removed member `%s` from user group `%s`' % ( |
2526 | 614 user.username, users_group.users_group_name |
615 ) | |
1989 | 616 msg = msg if success else "User wasn't in group" |
2526 | 617 Session().commit() |
1989 | 618 return dict(success=success, msg=msg) |
619 except Exception: | |
620 log.error(traceback.format_exc()) | |
2526 | 621 raise JSONRPCError( |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
622 'failed to remove member from user group `%s`' % ( |
2526 | 623 users_group.users_group_name |
624 ) | |
625 ) | |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
626 |
2010
14dffcfebb02
API get_user and get_repo methods can fetch by id or names
Marcin Kuzminski <marcin@python-works.com>
parents:
2009
diff
changeset
|
627 def get_repo(self, apiuser, repoid): |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
628 """" |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
629 Get repository by name |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
630 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
631 :param apiuser: |
2526 | 632 :param repoid: |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
633 """ |
2526 | 634 repo = get_repo_or_error(repoid) |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
635 |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
636 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
637 # check if we have admin permission for this repo ! |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
638 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser, |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
639 repo_name=repo.repo_name) is False: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
640 raise JSONRPCError('repository `%s` does not exist' % (repoid)) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
641 |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
642 members = [] |
3195
a50901f2108d
Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents:
3168
diff
changeset
|
643 followers = [] |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
644 for user in repo.repo_to_perm: |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
645 perm = user.permission.permission_name |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
646 user = user.user |
2526 | 647 user_data = user.get_api_data() |
648 user_data['type'] = "user" | |
649 user_data['permission'] = perm | |
650 members.append(user_data) | |
651 | |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
652 for users_group in repo.users_group_to_perm: |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
653 perm = users_group.permission.permission_name |
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
654 users_group = users_group.users_group |
2526 | 655 users_group_data = users_group.get_api_data() |
656 users_group_data['type'] = "users_group" | |
657 users_group_data['permission'] = perm | |
658 members.append(users_group_data) | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
659 |
3195
a50901f2108d
Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents:
3168
diff
changeset
|
660 for user in repo.followers: |
a50901f2108d
Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents:
3168
diff
changeset
|
661 followers.append(user.user.get_api_data()) |
a50901f2108d
Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents:
3168
diff
changeset
|
662 |
2526 | 663 data = repo.get_api_data() |
664 data['members'] = members | |
3195
a50901f2108d
Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents:
3168
diff
changeset
|
665 data['followers'] = followers |
2526 | 666 return data |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
667 |
1593 | 668 def get_repos(self, apiuser): |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
669 """" |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
670 Get all repositories |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
671 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
672 :param apiuser: |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
673 """ |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
674 result = [] |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
675 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
676 repos = RepoModel().get_all_user_repos(user=apiuser) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
677 else: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
678 repos = RepoModel().get_all() |
1587
8898a79ac628
Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1586
diff
changeset
|
679 |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
680 for repo in repos: |
2526 | 681 result.append(repo.get_api_data()) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
682 return result |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
683 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
684 @HasPermissionAllDecorator('hg.admin') |
2526 | 685 def get_repo_nodes(self, apiuser, repoid, revision, root_path, |
1810
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
686 ret_type='all'): |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
687 """ |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
688 returns a list of nodes and it's children |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
689 for a given path at given revision. It's possible to specify ret_type |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
690 to show only files or dirs |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
691 |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
692 :param apiuser: |
2526 | 693 :param repoid: name or id of repository |
1810
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
694 :param revision: revision for which listing should be done |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
695 :param root_path: path from which start displaying |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
696 :param ret_type: return type 'all|files|dirs' nodes |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
697 """ |
2526 | 698 repo = get_repo_or_error(repoid) |
1810
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
699 try: |
2526 | 700 _d, _f = ScmModel().get_nodes(repo, revision, root_path, |
1810
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
701 flat=False) |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
702 _map = { |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
703 'all': _d + _f, |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
704 'files': _f, |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
705 'dirs': _d, |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
706 } |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
707 return _map[ret_type] |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
708 except KeyError: |
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
709 raise JSONRPCError('ret_type must be one of %s' % _map.keys()) |
2526 | 710 except Exception: |
711 log.error(traceback.format_exc()) | |
712 raise JSONRPCError( | |
713 'failed to get repo: `%s` nodes' % repo.repo_name | |
714 ) | |
1810
203af05539e0
implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents:
1793
diff
changeset
|
715 |
1593 | 716 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
717 def create_repo(self, apiuser, repo_name, owner=Optional(OAttr('apiuser')), |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
718 repo_type=Optional('hg'), |
2526 | 719 description=Optional(''), private=Optional(False), |
3115
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
720 clone_uri=Optional(None), landing_rev=Optional('tip'), |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
721 enable_statistics=Optional(False), |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
722 enable_locking=Optional(False), |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
723 enable_downloads=Optional(False)): |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
724 """ |
2006
34d009e5147a
added clone_uri to API method for creating users
Marcin Kuzminski <marcin@python-works.com>
parents:
2004
diff
changeset
|
725 Create repository, if clone_url is given it makes a remote clone |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
726 if repo_name is within a group name the groups will be created |
2526 | 727 automatically if they aren't present |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
728 |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
729 :param apiuser: |
1843 | 730 :param repo_name: |
2526 | 731 :param onwer: |
732 :param repo_type: | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
733 :param description: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
734 :param private: |
2006
34d009e5147a
added clone_uri to API method for creating users
Marcin Kuzminski <marcin@python-works.com>
parents:
2004
diff
changeset
|
735 :param clone_uri: |
2526 | 736 :param landing_rev: |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
737 """ |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
738 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
739 if not isinstance(owner, Optional): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
740 #forbid setting owner for non-admins |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
741 raise JSONRPCError( |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
742 'Only RhodeCode admin can specify `owner` param' |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
743 ) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
744 if isinstance(owner, Optional): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
745 owner = apiuser.user_id |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
746 |
2526 | 747 owner = get_user_or_error(owner) |
748 | |
749 if RepoModel().get_by_repo_name(repo_name): | |
750 raise JSONRPCError("repo `%s` already exist" % repo_name) | |
751 | |
3115
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
752 defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
753 if isinstance(private, Optional): |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
754 private = defs.get('repo_private') or Optional.extract(private) |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
755 if isinstance(repo_type, Optional): |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
756 repo_type = defs.get('repo_type') |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
757 if isinstance(enable_statistics, Optional): |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
758 enable_statistics = defs.get('repo_enable_statistics') |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
759 if isinstance(enable_locking, Optional): |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
760 enable_locking = defs.get('repo_enable_locking') |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
761 if isinstance(enable_downloads, Optional): |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
762 enable_downloads = defs.get('repo_enable_downloads') |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
763 |
2526 | 764 clone_uri = Optional.extract(clone_uri) |
765 description = Optional.extract(description) | |
766 landing_rev = Optional.extract(landing_rev) | |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
767 |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
768 try: |
2526 | 769 # create structure of groups and return the last group |
2120
d5527cebf76a
Resolve error occurring during recursive group creation in API create-repo function
Marcin Kuzminski <marcin@python-works.com>
parents:
2010
diff
changeset
|
770 group = map_groups(repo_name) |
1589
307ec693bdf2
[API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents:
1587
diff
changeset
|
771 |
2526 | 772 repo = RepoModel().create_repo( |
773 repo_name=repo_name, | |
774 repo_type=repo_type, | |
775 description=description, | |
776 owner=owner, | |
777 private=private, | |
778 clone_uri=clone_uri, | |
779 repos_group=group, | |
780 landing_rev=landing_rev, | |
3115
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
781 enable_statistics=enable_statistics, |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
782 enable_downloads=enable_downloads, |
ebd76deee70d
Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents:
3024
diff
changeset
|
783 enable_locking=enable_locking |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
784 ) |
2526 | 785 |
786 Session().commit() | |
1843 | 787 return dict( |
2526 | 788 msg="Created new repository `%s`" % (repo.repo_name), |
789 repo=repo.get_api_data() | |
1843 | 790 ) |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
791 except Exception: |
1593 | 792 log.error(traceback.format_exc()) |
2526 | 793 raise JSONRPCError('failed to create repository `%s`' % repo_name) |
794 | |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
795 @HasPermissionAnyDecorator('hg.admin', 'hg.fork.repository') |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
796 def fork_repo(self, apiuser, repoid, fork_name, owner=Optional(OAttr('apiuser')), |
2653
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
797 description=Optional(''), copy_permissions=Optional(False), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
798 private=Optional(False), landing_rev=Optional('tip')): |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
799 repo = get_repo_or_error(repoid) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
800 repo_name = repo.repo_name |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
801 |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
802 _repo = RepoModel().get_by_repo_name(fork_name) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
803 if _repo: |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
804 type_ = 'fork' if _repo.fork else 'repo' |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
805 raise JSONRPCError("%s `%s` already exist" % (type_, fork_name)) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
806 |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
807 if HasPermissionAnyApi('hg.admin')(user=apiuser): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
808 pass |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
809 elif HasRepoPermissionAnyApi('repository.admin', |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
810 'repository.write', |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
811 'repository.read')(user=apiuser, |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
812 repo_name=repo.repo_name): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
813 if not isinstance(owner, Optional): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
814 #forbid setting owner for non-admins |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
815 raise JSONRPCError( |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
816 'Only RhodeCode admin can specify `owner` param' |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
817 ) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
818 else: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
819 raise JSONRPCError('repository `%s` does not exist' % (repoid)) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
820 |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
821 if isinstance(owner, Optional): |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
822 owner = apiuser.user_id |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
823 |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
824 owner = get_user_or_error(owner) |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
825 |
2653
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
826 try: |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
827 # create structure of groups and return the last group |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
828 group = map_groups(fork_name) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
829 |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
830 form_data = dict( |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
831 repo_name=fork_name, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
832 repo_name_full=fork_name, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
833 repo_group=group, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
834 repo_type=repo.repo_type, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
835 description=Optional.extract(description), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
836 private=Optional.extract(private), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
837 copy_permissions=Optional.extract(copy_permissions), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
838 landing_rev=Optional.extract(landing_rev), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
839 update_after_clone=False, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
840 fork_parent_id=repo.repo_id, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
841 ) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
842 RepoModel().create_fork(form_data, cur_user=owner) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
843 return dict( |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
844 msg='Created fork of `%s` as `%s`' % (repo.repo_name, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
845 fork_name), |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
846 success=True # cannot return the repo data here since fork |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
847 # cann be done async |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
848 ) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
849 except Exception: |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
850 log.error(traceback.format_exc()) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
851 raise JSONRPCError( |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
852 'failed to fork repository `%s` as `%s`' % (repo_name, |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
853 fork_name) |
8be70a4d72c7
Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents:
2526
diff
changeset
|
854 ) |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
855 |
3641
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
856 def delete_repo(self, apiuser, repoid, forks=Optional(None)): |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
857 """ |
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
858 Deletes a given repository |
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
859 |
2526 | 860 :param apiuser: |
861 :param repoid: | |
3641
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
862 :param forks: detach or delete, what do do with attached forks for repo |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
863 """ |
2526 | 864 repo = get_repo_or_error(repoid) |
865 | |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
866 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False: |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
867 # check if we have admin permission for this repo ! |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
868 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser, |
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
869 repo_name=repo.repo_name) is False: |
3641
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
870 raise JSONRPCError('repository `%s` does not exist' % (repoid)) |
3163
28571535dd61
API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents:
3162
diff
changeset
|
871 |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
872 try: |
3641
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
873 handle_forks = Optional.extract(forks) |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
874 _forks_msg = '' |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
875 _forks = [f for f in repo.forks] |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
876 if handle_forks == 'detach': |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
877 _forks_msg = ' ' + _('Detached %s forks') % len(_forks) |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
878 elif handle_forks == 'delete': |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
879 _forks_msg = ' ' + _('Deleted %s forks') % len(_forks) |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
880 elif _forks: |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
881 raise JSONRPCError( |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
882 'Cannot delete `%s` it still contains attached forks' |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
883 % repo.repo_name |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
884 ) |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
885 |
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
886 RepoModel().delete(repo, forks=forks) |
2526 | 887 Session().commit() |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
888 return dict( |
3641
b44979649915
recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents:
3502
diff
changeset
|
889 msg='Deleted repository `%s`%s' % (repo.repo_name, _forks_msg), |
2526 | 890 success=True |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
891 ) |
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
892 except Exception: |
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
893 log.error(traceback.format_exc()) |
2526 | 894 raise JSONRPCError( |
895 'failed to delete repository `%s`' % repo.repo_name | |
896 ) | |
2003
7dfcdf4c7dd2
implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2002
diff
changeset
|
897 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
898 @HasPermissionAllDecorator('hg.admin') |
2526 | 899 def grant_user_permission(self, apiuser, repoid, userid, perm): |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
900 """ |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
901 Grant permission for user on given repository, or update existing one |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
902 if found |
1584
3338a0994472
Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents:
1508
diff
changeset
|
903 |
2526 | 904 :param repoid: |
905 :param userid: | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
906 :param perm: |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
907 """ |
2526 | 908 repo = get_repo_or_error(repoid) |
909 user = get_user_or_error(userid) | |
910 perm = get_perm_or_error(perm) | |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
911 |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
912 try: |
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
913 |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
914 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
915 |
2526 | 916 Session().commit() |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
917 return dict( |
2526 | 918 msg='Granted perm: `%s` for user: `%s` in repo: `%s`' % ( |
919 perm.permission_name, user.username, repo.repo_name | |
920 ), | |
921 success=True | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
922 ) |
1586
2ccb32ddcfd7
Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents:
1584
diff
changeset
|
923 except Exception: |
1593 | 924 log.error(traceback.format_exc()) |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
925 raise JSONRPCError( |
2526 | 926 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
927 userid, repoid | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
928 ) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
929 ) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
930 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
931 @HasPermissionAllDecorator('hg.admin') |
2526 | 932 def revoke_user_permission(self, apiuser, repoid, userid): |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
933 """ |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
934 Revoke permission for user on given repository |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
935 |
2526 | 936 :param apiuser: |
937 :param repoid: | |
938 :param userid: | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
939 """ |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
940 |
2526 | 941 repo = get_repo_or_error(repoid) |
942 user = get_user_or_error(userid) | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
943 try: |
2526 | 944 |
945 RepoModel().revoke_user_permission(repo=repo, user=user) | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
946 |
2526 | 947 Session().commit() |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
948 return dict( |
2526 | 949 msg='Revoked perm for user: `%s` in repo: `%s`' % ( |
950 user.username, repo.repo_name | |
951 ), | |
952 success=True | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
953 ) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
954 except Exception: |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
955 log.error(traceback.format_exc()) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
956 raise JSONRPCError( |
2526 | 957 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
958 userid, repoid | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
959 ) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
960 ) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
961 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
962 @HasPermissionAllDecorator('hg.admin') |
2526 | 963 def grant_users_group_permission(self, apiuser, repoid, usersgroupid, |
964 perm): | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
965 """ |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
966 Grant permission for user group on given repository, or update |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
967 existing one if found |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
968 |
2526 | 969 :param apiuser: |
970 :param repoid: | |
971 :param usersgroupid: | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
972 :param perm: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
973 """ |
2526 | 974 repo = get_repo_or_error(repoid) |
975 perm = get_perm_or_error(perm) | |
976 users_group = get_users_group_or_error(usersgroupid) | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
977 |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
978 try: |
2526 | 979 RepoModel().grant_users_group_permission(repo=repo, |
980 group_name=users_group, | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
981 perm=perm) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1950
diff
changeset
|
982 |
2526 | 983 Session().commit() |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
984 return dict( |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
985 msg='Granted perm: `%s` for user group: `%s` in ' |
2526 | 986 'repo: `%s`' % ( |
987 perm.permission_name, users_group.users_group_name, | |
988 repo.repo_name | |
989 ), | |
990 success=True | |
991 ) | |
992 except Exception: | |
993 log.error(traceback.format_exc()) | |
994 raise JSONRPCError( | |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
995 'failed to edit permission for user group: `%s` in ' |
2526 | 996 'repo: `%s`' % ( |
997 usersgroupid, repo.repo_name | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
998 ) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
999 ) |
2526 | 1000 |
3161
3563c47e52fd
Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3126
diff
changeset
|
1001 @HasPermissionAllDecorator('hg.admin') |
2526 | 1002 def revoke_users_group_permission(self, apiuser, repoid, usersgroupid): |
1003 """ | |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
1004 Revoke permission for user group on given repository |
2526 | 1005 |
1006 :param apiuser: | |
1007 :param repoid: | |
1008 :param usersgroupid: | |
1009 """ | |
1010 repo = get_repo_or_error(repoid) | |
1011 users_group = get_users_group_or_error(usersgroupid) | |
1012 | |
1013 try: | |
1014 RepoModel().revoke_users_group_permission(repo=repo, | |
1015 group_name=users_group) | |
1016 | |
1017 Session().commit() | |
1018 return dict( | |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
1019 msg='Revoked perm for user group: `%s` in repo: `%s`' % ( |
2526 | 1020 users_group.users_group_name, repo.repo_name |
1021 ), | |
1022 success=True | |
1023 ) | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
1024 except Exception: |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
1025 log.error(traceback.format_exc()) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
1026 raise JSONRPCError( |
3410
5f1850e4712a
"Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents:
3235
diff
changeset
|
1027 'failed to edit permission for user group: `%s` in ' |
2526 | 1028 'repo: `%s`' % ( |
1029 users_group.users_group_name, repo.repo_name | |
1793
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
1030 ) |
631caf880b87
implements #329
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
1031 ) |