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