Show More
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
This diff has been collapsed as it changes many lines, (902 lines changed) Show them Hide them | |||||
@@ -0,0 +1,902 b'' | |||||
|
1 | import random | |||
|
2 | import mock | |||
|
3 | ||||
|
4 | from rhodecode.tests import * | |||
|
5 | from rhodecode.model.meta import Session | |||
|
6 | from rhodecode.lib.compat import json | |||
|
7 | from rhodecode.lib.auth import AuthUser | |||
|
8 | from rhodecode.model.user import UserModel | |||
|
9 | from rhodecode.model.users_group import UsersGroupModel | |||
|
10 | from rhodecode.model.repo import RepoModel | |||
|
11 | from nose.tools import with_setup | |||
|
12 | ||||
|
13 | API_URL = '/_admin/api' | |||
|
14 | ||||
|
15 | ||||
|
16 | def _build_data(apikey, method, **kw): | |||
|
17 | """ | |||
|
18 | Builds API data with given random ID | |||
|
19 | ||||
|
20 | :param random_id: | |||
|
21 | :type random_id: | |||
|
22 | """ | |||
|
23 | random_id = random.randrange(1, 9999) | |||
|
24 | return random_id, json.dumps({ | |||
|
25 | "id": random_id, | |||
|
26 | "api_key": apikey, | |||
|
27 | "method": method, | |||
|
28 | "args": kw | |||
|
29 | }) | |||
|
30 | ||||
|
31 | jsonify = lambda obj: json.loads(json.dumps(obj)) | |||
|
32 | ||||
|
33 | ||||
|
34 | def crash(*args, **kwargs): | |||
|
35 | raise Exception('Total Crash !') | |||
|
36 | ||||
|
37 | ||||
|
38 | TEST_USERS_GROUP = 'test_users_group' | |||
|
39 | ||||
|
40 | ||||
|
41 | def make_users_group(name=TEST_USERS_GROUP): | |||
|
42 | gr = UsersGroupModel().create(name=name) | |||
|
43 | UsersGroupModel().add_user_to_group(users_group=gr, | |||
|
44 | user=TEST_USER_ADMIN_LOGIN) | |||
|
45 | Session().commit() | |||
|
46 | return gr | |||
|
47 | ||||
|
48 | ||||
|
49 | def destroy_users_group(name=TEST_USERS_GROUP): | |||
|
50 | UsersGroupModel().delete(users_group=name, force=True) | |||
|
51 | Session().commit() | |||
|
52 | ||||
|
53 | ||||
|
54 | def create_repo(repo_name, repo_type): | |||
|
55 | # create new repo | |||
|
56 | form_data = dict(repo_name=repo_name, | |||
|
57 | repo_name_full=repo_name, | |||
|
58 | fork_name=None, | |||
|
59 | description='description %s' % repo_name, | |||
|
60 | repo_group=None, | |||
|
61 | private=False, | |||
|
62 | repo_type=repo_type, | |||
|
63 | clone_uri=None, | |||
|
64 | landing_rev='tip') | |||
|
65 | cur_user = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
66 | r = RepoModel().create(form_data, cur_user) | |||
|
67 | Session().commit() | |||
|
68 | return r | |||
|
69 | ||||
|
70 | ||||
|
71 | def destroy_repo(repo_name): | |||
|
72 | RepoModel().delete(repo_name) | |||
|
73 | Session().commit() | |||
|
74 | ||||
|
75 | ||||
|
76 | class BaseTestApi(object): | |||
|
77 | REPO = None | |||
|
78 | REPO_TYPE = None | |||
|
79 | ||||
|
80 | @classmethod | |||
|
81 | def setUpClass(self): | |||
|
82 | self.usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
83 | self.apikey = self.usr.api_key | |||
|
84 | self.TEST_USER = UserModel().create_or_update( | |||
|
85 | username='test-api', | |||
|
86 | password='test', | |||
|
87 | email='test@api.rhodecode.org', | |||
|
88 | firstname='first', | |||
|
89 | lastname='last' | |||
|
90 | ) | |||
|
91 | Session().commit() | |||
|
92 | self.TEST_USER_LOGIN = self.TEST_USER.username | |||
|
93 | ||||
|
94 | @classmethod | |||
|
95 | def teardownClass(self): | |||
|
96 | pass | |||
|
97 | ||||
|
98 | def setUp(self): | |||
|
99 | self.maxDiff = None | |||
|
100 | make_users_group() | |||
|
101 | ||||
|
102 | def tearDown(self): | |||
|
103 | destroy_users_group() | |||
|
104 | ||||
|
105 | def _compare_ok(self, id_, expected, given): | |||
|
106 | expected = jsonify({ | |||
|
107 | 'id': id_, | |||
|
108 | 'error': None, | |||
|
109 | 'result': expected | |||
|
110 | }) | |||
|
111 | given = json.loads(given) | |||
|
112 | self.assertEqual(expected, given) | |||
|
113 | ||||
|
114 | def _compare_error(self, id_, expected, given): | |||
|
115 | expected = jsonify({ | |||
|
116 | 'id': id_, | |||
|
117 | 'error': expected, | |||
|
118 | 'result': None | |||
|
119 | }) | |||
|
120 | given = json.loads(given) | |||
|
121 | self.assertEqual(expected, given) | |||
|
122 | ||||
|
123 | # def test_Optional(self): | |||
|
124 | # from rhodecode.controllers.api.api import Optional | |||
|
125 | # option1 = Optional(None) | |||
|
126 | # self.assertEqual('<Optional:%s>' % None, repr(option1)) | |||
|
127 | # | |||
|
128 | # self.assertEqual(1, Optional.extract(Optional(1))) | |||
|
129 | # self.assertEqual('trololo', Optional.extract('trololo')) | |||
|
130 | ||||
|
131 | def test_api_wrong_key(self): | |||
|
132 | id_, params = _build_data('trololo', 'get_user') | |||
|
133 | response = self.app.post(API_URL, content_type='application/json', | |||
|
134 | params=params) | |||
|
135 | ||||
|
136 | expected = 'Invalid API KEY' | |||
|
137 | self._compare_error(id_, expected, given=response.body) | |||
|
138 | ||||
|
139 | def test_api_missing_non_optional_param(self): | |||
|
140 | id_, params = _build_data(self.apikey, 'get_user') | |||
|
141 | response = self.app.post(API_URL, content_type='application/json', | |||
|
142 | params=params) | |||
|
143 | ||||
|
144 | expected = 'Missing non optional `userid` arg in JSON DATA' | |||
|
145 | self._compare_error(id_, expected, given=response.body) | |||
|
146 | ||||
|
147 | def test_api_get_users(self): | |||
|
148 | id_, params = _build_data(self.apikey, 'get_users',) | |||
|
149 | response = self.app.post(API_URL, content_type='application/json', | |||
|
150 | params=params) | |||
|
151 | ret_all = [] | |||
|
152 | for usr in UserModel().get_all(): | |||
|
153 | ret = usr.get_api_data() | |||
|
154 | ret_all.append(jsonify(ret)) | |||
|
155 | expected = ret_all | |||
|
156 | self._compare_ok(id_, expected, given=response.body) | |||
|
157 | ||||
|
158 | def test_api_get_user(self): | |||
|
159 | id_, params = _build_data(self.apikey, 'get_user', | |||
|
160 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
161 | response = self.app.post(API_URL, content_type='application/json', | |||
|
162 | params=params) | |||
|
163 | ||||
|
164 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
165 | ret = usr.get_api_data() | |||
|
166 | ret['permissions'] = AuthUser(usr.user_id).permissions | |||
|
167 | ||||
|
168 | expected = ret | |||
|
169 | self._compare_ok(id_, expected, given=response.body) | |||
|
170 | ||||
|
171 | def test_api_get_user_that_does_not_exist(self): | |||
|
172 | id_, params = _build_data(self.apikey, 'get_user', | |||
|
173 | userid='trololo') | |||
|
174 | response = self.app.post(API_URL, content_type='application/json', | |||
|
175 | params=params) | |||
|
176 | ||||
|
177 | expected = "user `%s` does not exist" % 'trololo' | |||
|
178 | self._compare_error(id_, expected, given=response.body) | |||
|
179 | ||||
|
180 | def test_api_pull(self): | |||
|
181 | #TODO: issues with rhodecode_extras here.. not sure why ! | |||
|
182 | pass | |||
|
183 | ||||
|
184 | # repo_name = 'test_pull' | |||
|
185 | # r = create_repo(repo_name, self.REPO_TYPE) | |||
|
186 | # r.clone_uri = TEST_self.REPO | |||
|
187 | # Session.add(r) | |||
|
188 | # Session.commit() | |||
|
189 | # | |||
|
190 | # id_, params = _build_data(self.apikey, 'pull', | |||
|
191 | # repoid=repo_name,) | |||
|
192 | # response = self.app.post(API_URL, content_type='application/json', | |||
|
193 | # params=params) | |||
|
194 | # | |||
|
195 | # expected = 'Pulled from `%s`' % repo_name | |||
|
196 | # self._compare_ok(id_, expected, given=response.body) | |||
|
197 | # | |||
|
198 | # destroy_repo(repo_name) | |||
|
199 | ||||
|
200 | def test_api_pull_error(self): | |||
|
201 | id_, params = _build_data(self.apikey, 'pull', | |||
|
202 | repoid=self.REPO,) | |||
|
203 | response = self.app.post(API_URL, content_type='application/json', | |||
|
204 | params=params) | |||
|
205 | ||||
|
206 | expected = 'Unable to pull changes from `%s`' % self.REPO | |||
|
207 | self._compare_error(id_, expected, given=response.body) | |||
|
208 | ||||
|
209 | def test_api_create_existing_user(self): | |||
|
210 | id_, params = _build_data(self.apikey, 'create_user', | |||
|
211 | username=TEST_USER_ADMIN_LOGIN, | |||
|
212 | email='test@foo.com', | |||
|
213 | password='trololo') | |||
|
214 | response = self.app.post(API_URL, content_type='application/json', | |||
|
215 | params=params) | |||
|
216 | ||||
|
217 | expected = "user `%s` already exist" % TEST_USER_ADMIN_LOGIN | |||
|
218 | self._compare_error(id_, expected, given=response.body) | |||
|
219 | ||||
|
220 | def test_api_create_user_with_existing_email(self): | |||
|
221 | id_, params = _build_data(self.apikey, 'create_user', | |||
|
222 | username=TEST_USER_ADMIN_LOGIN + 'new', | |||
|
223 | email=TEST_USER_REGULAR_EMAIL, | |||
|
224 | password='trololo') | |||
|
225 | response = self.app.post(API_URL, content_type='application/json', | |||
|
226 | params=params) | |||
|
227 | ||||
|
228 | expected = "email `%s` already exist" % TEST_USER_REGULAR_EMAIL | |||
|
229 | self._compare_error(id_, expected, given=response.body) | |||
|
230 | ||||
|
231 | def test_api_create_user(self): | |||
|
232 | username = 'test_new_api_user' | |||
|
233 | email = username + "@foo.com" | |||
|
234 | ||||
|
235 | id_, params = _build_data(self.apikey, 'create_user', | |||
|
236 | username=username, | |||
|
237 | email=email, | |||
|
238 | password='trololo') | |||
|
239 | response = self.app.post(API_URL, content_type='application/json', | |||
|
240 | params=params) | |||
|
241 | ||||
|
242 | usr = UserModel().get_by_username(username) | |||
|
243 | ret = dict( | |||
|
244 | msg='created new user `%s`' % username, | |||
|
245 | user=jsonify(usr.get_api_data()) | |||
|
246 | ) | |||
|
247 | ||||
|
248 | expected = ret | |||
|
249 | self._compare_ok(id_, expected, given=response.body) | |||
|
250 | ||||
|
251 | UserModel().delete(usr.user_id) | |||
|
252 | Session().commit() | |||
|
253 | ||||
|
254 | @mock.patch.object(UserModel, 'create_or_update', crash) | |||
|
255 | def test_api_create_user_when_exception_happened(self): | |||
|
256 | ||||
|
257 | username = 'test_new_api_user' | |||
|
258 | email = username + "@foo.com" | |||
|
259 | ||||
|
260 | id_, params = _build_data(self.apikey, 'create_user', | |||
|
261 | username=username, | |||
|
262 | email=email, | |||
|
263 | password='trololo') | |||
|
264 | response = self.app.post(API_URL, content_type='application/json', | |||
|
265 | params=params) | |||
|
266 | expected = 'failed to create user `%s`' % username | |||
|
267 | self._compare_error(id_, expected, given=response.body) | |||
|
268 | ||||
|
269 | def test_api_delete_user(self): | |||
|
270 | usr = UserModel().create_or_update(username=u'test_user', | |||
|
271 | password=u'qweqwe', | |||
|
272 | email=u'u232@rhodecode.org', | |||
|
273 | firstname=u'u1', lastname=u'u1') | |||
|
274 | Session().commit() | |||
|
275 | username = usr.username | |||
|
276 | email = usr.email | |||
|
277 | usr_id = usr.user_id | |||
|
278 | ## DELETE THIS USER NOW | |||
|
279 | ||||
|
280 | id_, params = _build_data(self.apikey, 'delete_user', | |||
|
281 | userid=username,) | |||
|
282 | response = self.app.post(API_URL, content_type='application/json', | |||
|
283 | params=params) | |||
|
284 | ||||
|
285 | ret = {'msg': 'deleted user ID:%s %s' % (usr_id, username), | |||
|
286 | 'user': None} | |||
|
287 | expected = ret | |||
|
288 | self._compare_ok(id_, expected, given=response.body) | |||
|
289 | ||||
|
290 | @mock.patch.object(UserModel, 'delete', crash) | |||
|
291 | def test_api_delete_user_when_exception_happened(self): | |||
|
292 | usr = UserModel().create_or_update(username=u'test_user', | |||
|
293 | password=u'qweqwe', | |||
|
294 | email=u'u232@rhodecode.org', | |||
|
295 | firstname=u'u1', lastname=u'u1') | |||
|
296 | Session().commit() | |||
|
297 | username = usr.username | |||
|
298 | ||||
|
299 | id_, params = _build_data(self.apikey, 'delete_user', | |||
|
300 | userid=username,) | |||
|
301 | response = self.app.post(API_URL, content_type='application/json', | |||
|
302 | params=params) | |||
|
303 | ret = 'failed to delete ID:%s %s' % (usr.user_id, | |||
|
304 | usr.username) | |||
|
305 | expected = ret | |||
|
306 | self._compare_error(id_, expected, given=response.body) | |||
|
307 | ||||
|
308 | @parameterized.expand([('firstname', 'new_username'), | |||
|
309 | ('lastname', 'new_username'), | |||
|
310 | ('email', 'new_username'), | |||
|
311 | ('admin', True), | |||
|
312 | ('admin', False), | |||
|
313 | ('ldap_dn', 'test'), | |||
|
314 | ('ldap_dn', None), | |||
|
315 | ('active', False), | |||
|
316 | ('active', True), | |||
|
317 | ('password', 'newpass') | |||
|
318 | ]) | |||
|
319 | def test_api_update_user(self, name, expected): | |||
|
320 | usr = UserModel().get_by_username(self.TEST_USER_LOGIN) | |||
|
321 | kw = {name: expected, | |||
|
322 | 'userid': usr.user_id} | |||
|
323 | id_, params = _build_data(self.apikey, 'update_user', **kw) | |||
|
324 | response = self.app.post(API_URL, content_type='application/json', | |||
|
325 | params=params) | |||
|
326 | ||||
|
327 | ret = { | |||
|
328 | 'msg': 'updated user ID:%s %s' % (usr.user_id, self.TEST_USER_LOGIN), | |||
|
329 | 'user': jsonify(UserModel()\ | |||
|
330 | .get_by_username(self.TEST_USER_LOGIN)\ | |||
|
331 | .get_api_data()) | |||
|
332 | } | |||
|
333 | ||||
|
334 | expected = ret | |||
|
335 | self._compare_ok(id_, expected, given=response.body) | |||
|
336 | ||||
|
337 | def test_api_update_user_no_changed_params(self): | |||
|
338 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
339 | ret = jsonify(usr.get_api_data()) | |||
|
340 | id_, params = _build_data(self.apikey, 'update_user', | |||
|
341 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
342 | ||||
|
343 | response = self.app.post(API_URL, content_type='application/json', | |||
|
344 | params=params) | |||
|
345 | ret = { | |||
|
346 | 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), | |||
|
347 | 'user': ret | |||
|
348 | } | |||
|
349 | expected = ret | |||
|
350 | self._compare_ok(id_, expected, given=response.body) | |||
|
351 | ||||
|
352 | def test_api_update_user_by_user_id(self): | |||
|
353 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
354 | ret = jsonify(usr.get_api_data()) | |||
|
355 | id_, params = _build_data(self.apikey, 'update_user', | |||
|
356 | userid=usr.user_id) | |||
|
357 | ||||
|
358 | response = self.app.post(API_URL, content_type='application/json', | |||
|
359 | params=params) | |||
|
360 | ret = { | |||
|
361 | 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), | |||
|
362 | 'user': ret | |||
|
363 | } | |||
|
364 | expected = ret | |||
|
365 | self._compare_ok(id_, expected, given=response.body) | |||
|
366 | ||||
|
367 | @mock.patch.object(UserModel, 'create_or_update', crash) | |||
|
368 | def test_api_update_user_when_exception_happens(self): | |||
|
369 | usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) | |||
|
370 | ret = jsonify(usr.get_api_data()) | |||
|
371 | id_, params = _build_data(self.apikey, 'update_user', | |||
|
372 | userid=usr.user_id) | |||
|
373 | ||||
|
374 | response = self.app.post(API_URL, content_type='application/json', | |||
|
375 | params=params) | |||
|
376 | ret = 'failed to update user `%s`' % usr.user_id | |||
|
377 | ||||
|
378 | expected = ret | |||
|
379 | self._compare_error(id_, expected, given=response.body) | |||
|
380 | ||||
|
381 | def test_api_get_repo(self): | |||
|
382 | new_group = 'some_new_group' | |||
|
383 | make_users_group(new_group) | |||
|
384 | RepoModel().grant_users_group_permission(repo=self.REPO, | |||
|
385 | group_name=new_group, | |||
|
386 | perm='repository.read') | |||
|
387 | Session().commit() | |||
|
388 | id_, params = _build_data(self.apikey, 'get_repo', | |||
|
389 | repoid=self.REPO) | |||
|
390 | response = self.app.post(API_URL, content_type='application/json', | |||
|
391 | params=params) | |||
|
392 | ||||
|
393 | repo = RepoModel().get_by_repo_name(self.REPO) | |||
|
394 | ret = repo.get_api_data() | |||
|
395 | ||||
|
396 | members = [] | |||
|
397 | for user in repo.repo_to_perm: | |||
|
398 | perm = user.permission.permission_name | |||
|
399 | user = user.user | |||
|
400 | user_data = user.get_api_data() | |||
|
401 | user_data['type'] = "user" | |||
|
402 | user_data['permission'] = perm | |||
|
403 | members.append(user_data) | |||
|
404 | ||||
|
405 | for users_group in repo.users_group_to_perm: | |||
|
406 | perm = users_group.permission.permission_name | |||
|
407 | users_group = users_group.users_group | |||
|
408 | users_group_data = users_group.get_api_data() | |||
|
409 | users_group_data['type'] = "users_group" | |||
|
410 | users_group_data['permission'] = perm | |||
|
411 | members.append(users_group_data) | |||
|
412 | ||||
|
413 | ret['members'] = members | |||
|
414 | ||||
|
415 | expected = ret | |||
|
416 | self._compare_ok(id_, expected, given=response.body) | |||
|
417 | destroy_users_group(new_group) | |||
|
418 | ||||
|
419 | def test_api_get_repo_that_doesn_not_exist(self): | |||
|
420 | id_, params = _build_data(self.apikey, 'get_repo', | |||
|
421 | repoid='no-such-repo') | |||
|
422 | response = self.app.post(API_URL, content_type='application/json', | |||
|
423 | params=params) | |||
|
424 | ||||
|
425 | ret = 'repository `%s` does not exist' % 'no-such-repo' | |||
|
426 | expected = ret | |||
|
427 | self._compare_error(id_, expected, given=response.body) | |||
|
428 | ||||
|
429 | def test_api_get_repos(self): | |||
|
430 | id_, params = _build_data(self.apikey, 'get_repos') | |||
|
431 | response = self.app.post(API_URL, content_type='application/json', | |||
|
432 | params=params) | |||
|
433 | ||||
|
434 | result = [] | |||
|
435 | for repo in RepoModel().get_all(): | |||
|
436 | result.append(repo.get_api_data()) | |||
|
437 | ret = jsonify(result) | |||
|
438 | ||||
|
439 | expected = ret | |||
|
440 | self._compare_ok(id_, expected, given=response.body) | |||
|
441 | ||||
|
442 | @parameterized.expand([('all', 'all'), | |||
|
443 | ('dirs', 'dirs'), | |||
|
444 | ('files', 'files'), ]) | |||
|
445 | def test_api_get_repo_nodes(self, name, ret_type): | |||
|
446 | rev = 'tip' | |||
|
447 | path = '/' | |||
|
448 | id_, params = _build_data(self.apikey, 'get_repo_nodes', | |||
|
449 | repoid=self.REPO, revision=rev, | |||
|
450 | root_path=path, | |||
|
451 | ret_type=ret_type) | |||
|
452 | response = self.app.post(API_URL, content_type='application/json', | |||
|
453 | params=params) | |||
|
454 | ||||
|
455 | # we don't the actual return types here since it's tested somewhere | |||
|
456 | # else | |||
|
457 | expected = json.loads(response.body)['result'] | |||
|
458 | self._compare_ok(id_, expected, given=response.body) | |||
|
459 | ||||
|
460 | def test_api_get_repo_nodes_bad_revisions(self): | |||
|
461 | rev = 'i-dont-exist' | |||
|
462 | path = '/' | |||
|
463 | id_, params = _build_data(self.apikey, 'get_repo_nodes', | |||
|
464 | repoid=self.REPO, revision=rev, | |||
|
465 | root_path=path,) | |||
|
466 | response = self.app.post(API_URL, content_type='application/json', | |||
|
467 | params=params) | |||
|
468 | ||||
|
469 | expected = 'failed to get repo: `%s` nodes' % self.REPO | |||
|
470 | self._compare_error(id_, expected, given=response.body) | |||
|
471 | ||||
|
472 | def test_api_get_repo_nodes_bad_path(self): | |||
|
473 | rev = 'tip' | |||
|
474 | path = '/idontexits' | |||
|
475 | id_, params = _build_data(self.apikey, 'get_repo_nodes', | |||
|
476 | repoid=self.REPO, revision=rev, | |||
|
477 | root_path=path,) | |||
|
478 | response = self.app.post(API_URL, content_type='application/json', | |||
|
479 | params=params) | |||
|
480 | ||||
|
481 | expected = 'failed to get repo: `%s` nodes' % self.REPO | |||
|
482 | self._compare_error(id_, expected, given=response.body) | |||
|
483 | ||||
|
484 | def test_api_get_repo_nodes_bad_ret_type(self): | |||
|
485 | rev = 'tip' | |||
|
486 | path = '/' | |||
|
487 | ret_type = 'error' | |||
|
488 | id_, params = _build_data(self.apikey, 'get_repo_nodes', | |||
|
489 | repoid=self.REPO, revision=rev, | |||
|
490 | root_path=path, | |||
|
491 | ret_type=ret_type) | |||
|
492 | response = self.app.post(API_URL, content_type='application/json', | |||
|
493 | params=params) | |||
|
494 | ||||
|
495 | expected = 'ret_type must be one of %s' % (['files', 'dirs', 'all']) | |||
|
496 | self._compare_error(id_, expected, given=response.body) | |||
|
497 | ||||
|
498 | def test_api_create_repo(self): | |||
|
499 | repo_name = 'api-repo' | |||
|
500 | id_, params = _build_data(self.apikey, 'create_repo', | |||
|
501 | repo_name=repo_name, | |||
|
502 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
503 | repo_type='hg', | |||
|
504 | ) | |||
|
505 | response = self.app.post(API_URL, content_type='application/json', | |||
|
506 | params=params) | |||
|
507 | ||||
|
508 | repo = RepoModel().get_by_repo_name(repo_name) | |||
|
509 | ret = { | |||
|
510 | 'msg': 'Created new repository `%s`' % repo_name, | |||
|
511 | 'repo': jsonify(repo.get_api_data()) | |||
|
512 | } | |||
|
513 | expected = ret | |||
|
514 | self._compare_ok(id_, expected, given=response.body) | |||
|
515 | destroy_repo(repo_name) | |||
|
516 | ||||
|
517 | def test_api_create_repo_unknown_owner(self): | |||
|
518 | repo_name = 'api-repo' | |||
|
519 | owner = 'i-dont-exist' | |||
|
520 | id_, params = _build_data(self.apikey, 'create_repo', | |||
|
521 | repo_name=repo_name, | |||
|
522 | owner=owner, | |||
|
523 | repo_type='hg', | |||
|
524 | ) | |||
|
525 | response = self.app.post(API_URL, content_type='application/json', | |||
|
526 | params=params) | |||
|
527 | expected = 'user `%s` does not exist' % owner | |||
|
528 | self._compare_error(id_, expected, given=response.body) | |||
|
529 | ||||
|
530 | def test_api_create_repo_exists(self): | |||
|
531 | repo_name = self.REPO | |||
|
532 | id_, params = _build_data(self.apikey, 'create_repo', | |||
|
533 | repo_name=repo_name, | |||
|
534 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
535 | repo_type='hg', | |||
|
536 | ) | |||
|
537 | response = self.app.post(API_URL, content_type='application/json', | |||
|
538 | params=params) | |||
|
539 | expected = "repo `%s` already exist" % repo_name | |||
|
540 | self._compare_error(id_, expected, given=response.body) | |||
|
541 | ||||
|
542 | @mock.patch.object(RepoModel, 'create_repo', crash) | |||
|
543 | def test_api_create_repo_exception_occurred(self): | |||
|
544 | repo_name = 'api-repo' | |||
|
545 | id_, params = _build_data(self.apikey, 'create_repo', | |||
|
546 | repo_name=repo_name, | |||
|
547 | owner=TEST_USER_ADMIN_LOGIN, | |||
|
548 | repo_type='hg', | |||
|
549 | ) | |||
|
550 | response = self.app.post(API_URL, content_type='application/json', | |||
|
551 | params=params) | |||
|
552 | expected = 'failed to create repository `%s`' % repo_name | |||
|
553 | self._compare_error(id_, expected, given=response.body) | |||
|
554 | ||||
|
555 | def test_api_delete_repo(self): | |||
|
556 | repo_name = 'api_delete_me' | |||
|
557 | create_repo(repo_name, self.REPO_TYPE) | |||
|
558 | ||||
|
559 | id_, params = _build_data(self.apikey, 'delete_repo', | |||
|
560 | repoid=repo_name,) | |||
|
561 | response = self.app.post(API_URL, content_type='application/json', | |||
|
562 | params=params) | |||
|
563 | ||||
|
564 | ret = { | |||
|
565 | 'msg': 'Deleted repository `%s`' % repo_name, | |||
|
566 | 'success': True | |||
|
567 | } | |||
|
568 | expected = ret | |||
|
569 | self._compare_ok(id_, expected, given=response.body) | |||
|
570 | ||||
|
571 | def test_api_delete_repo_exception_occurred(self): | |||
|
572 | repo_name = 'api_delete_me' | |||
|
573 | create_repo(repo_name, self.REPO_TYPE) | |||
|
574 | with mock.patch.object(RepoModel, 'delete', crash): | |||
|
575 | id_, params = _build_data(self.apikey, 'delete_repo', | |||
|
576 | repoid=repo_name,) | |||
|
577 | response = self.app.post(API_URL, content_type='application/json', | |||
|
578 | params=params) | |||
|
579 | ||||
|
580 | expected = 'failed to delete repository `%s`' % repo_name | |||
|
581 | self._compare_error(id_, expected, given=response.body) | |||
|
582 | destroy_repo(repo_name) | |||
|
583 | ||||
|
584 | def test_api_fork_repo(self): | |||
|
585 | self.failIf(False, 'TODO:') | |||
|
586 | ||||
|
587 | def test_api_get_users_group(self): | |||
|
588 | id_, params = _build_data(self.apikey, 'get_users_group', | |||
|
589 | usersgroupid=TEST_USERS_GROUP) | |||
|
590 | response = self.app.post(API_URL, content_type='application/json', | |||
|
591 | params=params) | |||
|
592 | ||||
|
593 | users_group = UsersGroupModel().get_group(TEST_USERS_GROUP) | |||
|
594 | members = [] | |||
|
595 | for user in users_group.members: | |||
|
596 | user = user.user | |||
|
597 | members.append(user.get_api_data()) | |||
|
598 | ||||
|
599 | ret = users_group.get_api_data() | |||
|
600 | ret['members'] = members | |||
|
601 | expected = ret | |||
|
602 | self._compare_ok(id_, expected, given=response.body) | |||
|
603 | ||||
|
604 | def test_api_get_users_groups(self): | |||
|
605 | ||||
|
606 | make_users_group('test_users_group2') | |||
|
607 | ||||
|
608 | id_, params = _build_data(self.apikey, 'get_users_groups',) | |||
|
609 | response = self.app.post(API_URL, content_type='application/json', | |||
|
610 | params=params) | |||
|
611 | ||||
|
612 | expected = [] | |||
|
613 | for gr_name in [TEST_USERS_GROUP, 'test_users_group2']: | |||
|
614 | users_group = UsersGroupModel().get_group(gr_name) | |||
|
615 | ret = users_group.get_api_data() | |||
|
616 | expected.append(ret) | |||
|
617 | self._compare_ok(id_, expected, given=response.body) | |||
|
618 | ||||
|
619 | UsersGroupModel().delete(users_group='test_users_group2') | |||
|
620 | Session().commit() | |||
|
621 | ||||
|
622 | def test_api_create_users_group(self): | |||
|
623 | group_name = 'some_new_group' | |||
|
624 | id_, params = _build_data(self.apikey, 'create_users_group', | |||
|
625 | group_name=group_name) | |||
|
626 | response = self.app.post(API_URL, content_type='application/json', | |||
|
627 | params=params) | |||
|
628 | ||||
|
629 | ret = { | |||
|
630 | 'msg': 'created new users group `%s`' % group_name, | |||
|
631 | 'users_group': jsonify(UsersGroupModel()\ | |||
|
632 | .get_by_name(group_name)\ | |||
|
633 | .get_api_data()) | |||
|
634 | } | |||
|
635 | expected = ret | |||
|
636 | self._compare_ok(id_, expected, given=response.body) | |||
|
637 | ||||
|
638 | destroy_users_group(group_name) | |||
|
639 | ||||
|
640 | def test_api_get_users_group_that_exist(self): | |||
|
641 | id_, params = _build_data(self.apikey, 'create_users_group', | |||
|
642 | group_name=TEST_USERS_GROUP) | |||
|
643 | response = self.app.post(API_URL, content_type='application/json', | |||
|
644 | params=params) | |||
|
645 | ||||
|
646 | expected = "users group `%s` already exist" % TEST_USERS_GROUP | |||
|
647 | self._compare_error(id_, expected, given=response.body) | |||
|
648 | ||||
|
649 | @mock.patch.object(UsersGroupModel, 'create', crash) | |||
|
650 | def test_api_get_users_group_exception_occurred(self): | |||
|
651 | group_name = 'exception_happens' | |||
|
652 | id_, params = _build_data(self.apikey, 'create_users_group', | |||
|
653 | group_name=group_name) | |||
|
654 | response = self.app.post(API_URL, content_type='application/json', | |||
|
655 | params=params) | |||
|
656 | ||||
|
657 | expected = 'failed to create group `%s`' % group_name | |||
|
658 | self._compare_error(id_, expected, given=response.body) | |||
|
659 | ||||
|
660 | def test_api_add_user_to_users_group(self): | |||
|
661 | gr_name = 'test_group' | |||
|
662 | UsersGroupModel().create(gr_name) | |||
|
663 | Session().commit() | |||
|
664 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', | |||
|
665 | usersgroupid=gr_name, | |||
|
666 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
667 | response = self.app.post(API_URL, content_type='application/json', | |||
|
668 | params=params) | |||
|
669 | ||||
|
670 | expected = { | |||
|
671 | 'msg': 'added member `%s` to users group `%s`' % ( | |||
|
672 | TEST_USER_ADMIN_LOGIN, gr_name | |||
|
673 | ), | |||
|
674 | 'success': True} | |||
|
675 | self._compare_ok(id_, expected, given=response.body) | |||
|
676 | ||||
|
677 | UsersGroupModel().delete(users_group=gr_name) | |||
|
678 | Session().commit() | |||
|
679 | ||||
|
680 | def test_api_add_user_to_users_group_that_doesnt_exist(self): | |||
|
681 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', | |||
|
682 | usersgroupid='false-group', | |||
|
683 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
684 | response = self.app.post(API_URL, content_type='application/json', | |||
|
685 | params=params) | |||
|
686 | ||||
|
687 | expected = 'users group `%s` does not exist' % 'false-group' | |||
|
688 | self._compare_error(id_, expected, given=response.body) | |||
|
689 | ||||
|
690 | @mock.patch.object(UsersGroupModel, 'add_user_to_group', crash) | |||
|
691 | def test_api_add_user_to_users_group_exception_occurred(self): | |||
|
692 | gr_name = 'test_group' | |||
|
693 | UsersGroupModel().create(gr_name) | |||
|
694 | Session().commit() | |||
|
695 | id_, params = _build_data(self.apikey, 'add_user_to_users_group', | |||
|
696 | usersgroupid=gr_name, | |||
|
697 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
698 | response = self.app.post(API_URL, content_type='application/json', | |||
|
699 | params=params) | |||
|
700 | ||||
|
701 | expected = 'failed to add member to users group `%s`' % gr_name | |||
|
702 | self._compare_error(id_, expected, given=response.body) | |||
|
703 | ||||
|
704 | UsersGroupModel().delete(users_group=gr_name) | |||
|
705 | Session().commit() | |||
|
706 | ||||
|
707 | def test_api_remove_user_from_users_group(self): | |||
|
708 | gr_name = 'test_group_3' | |||
|
709 | gr = UsersGroupModel().create(gr_name) | |||
|
710 | UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) | |||
|
711 | Session().commit() | |||
|
712 | id_, params = _build_data(self.apikey, 'remove_user_from_users_group', | |||
|
713 | usersgroupid=gr_name, | |||
|
714 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
715 | response = self.app.post(API_URL, content_type='application/json', | |||
|
716 | params=params) | |||
|
717 | ||||
|
718 | expected = { | |||
|
719 | 'msg': 'removed member `%s` from users group `%s`' % ( | |||
|
720 | TEST_USER_ADMIN_LOGIN, gr_name | |||
|
721 | ), | |||
|
722 | 'success': True} | |||
|
723 | self._compare_ok(id_, expected, given=response.body) | |||
|
724 | ||||
|
725 | UsersGroupModel().delete(users_group=gr_name) | |||
|
726 | Session().commit() | |||
|
727 | ||||
|
728 | @mock.patch.object(UsersGroupModel, 'remove_user_from_group', crash) | |||
|
729 | def test_api_remove_user_from_users_group_exception_occurred(self): | |||
|
730 | gr_name = 'test_group_3' | |||
|
731 | gr = UsersGroupModel().create(gr_name) | |||
|
732 | UsersGroupModel().add_user_to_group(gr, user=TEST_USER_ADMIN_LOGIN) | |||
|
733 | Session().commit() | |||
|
734 | id_, params = _build_data(self.apikey, 'remove_user_from_users_group', | |||
|
735 | usersgroupid=gr_name, | |||
|
736 | userid=TEST_USER_ADMIN_LOGIN) | |||
|
737 | response = self.app.post(API_URL, content_type='application/json', | |||
|
738 | params=params) | |||
|
739 | ||||
|
740 | expected = 'failed to remove member from users group `%s`' % gr_name | |||
|
741 | self._compare_error(id_, expected, given=response.body) | |||
|
742 | ||||
|
743 | UsersGroupModel().delete(users_group=gr_name) | |||
|
744 | Session().commit() | |||
|
745 | ||||
|
746 | @parameterized.expand([('none', 'repository.none'), | |||
|
747 | ('read', 'repository.read'), | |||
|
748 | ('write', 'repository.write'), | |||
|
749 | ('admin', 'repository.admin')]) | |||
|
750 | def test_api_grant_user_permission(self, name, perm): | |||
|
751 | id_, params = _build_data(self.apikey, 'grant_user_permission', | |||
|
752 | repoid=self.REPO, | |||
|
753 | userid=TEST_USER_ADMIN_LOGIN, | |||
|
754 | perm=perm) | |||
|
755 | response = self.app.post(API_URL, content_type='application/json', | |||
|
756 | params=params) | |||
|
757 | ||||
|
758 | ret = { | |||
|
759 | 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( | |||
|
760 | perm, TEST_USER_ADMIN_LOGIN, self.REPO | |||
|
761 | ), | |||
|
762 | 'success': True | |||
|
763 | } | |||
|
764 | expected = ret | |||
|
765 | self._compare_ok(id_, expected, given=response.body) | |||
|
766 | ||||
|
767 | def test_api_grant_user_permission_wrong_permission(self): | |||
|
768 | perm = 'haha.no.permission' | |||
|
769 | id_, params = _build_data(self.apikey, 'grant_user_permission', | |||
|
770 | repoid=self.REPO, | |||
|
771 | userid=TEST_USER_ADMIN_LOGIN, | |||
|
772 | perm=perm) | |||
|
773 | response = self.app.post(API_URL, content_type='application/json', | |||
|
774 | params=params) | |||
|
775 | ||||
|
776 | expected = 'permission `%s` does not exist' % perm | |||
|
777 | self._compare_error(id_, expected, given=response.body) | |||
|
778 | ||||
|
779 | @mock.patch.object(RepoModel, 'grant_user_permission', crash) | |||
|
780 | def test_api_grant_user_permission_exception_when_adding(self): | |||
|
781 | perm = 'repository.read' | |||
|
782 | id_, params = _build_data(self.apikey, 'grant_user_permission', | |||
|
783 | repoid=self.REPO, | |||
|
784 | userid=TEST_USER_ADMIN_LOGIN, | |||
|
785 | perm=perm) | |||
|
786 | response = self.app.post(API_URL, content_type='application/json', | |||
|
787 | params=params) | |||
|
788 | ||||
|
789 | expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( | |||
|
790 | TEST_USER_ADMIN_LOGIN, self.REPO | |||
|
791 | ) | |||
|
792 | self._compare_error(id_, expected, given=response.body) | |||
|
793 | ||||
|
794 | def test_api_revoke_user_permission(self): | |||
|
795 | id_, params = _build_data(self.apikey, 'revoke_user_permission', | |||
|
796 | repoid=self.REPO, | |||
|
797 | userid=TEST_USER_ADMIN_LOGIN,) | |||
|
798 | response = self.app.post(API_URL, content_type='application/json', | |||
|
799 | params=params) | |||
|
800 | ||||
|
801 | expected = { | |||
|
802 | 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % ( | |||
|
803 | TEST_USER_ADMIN_LOGIN, self.REPO | |||
|
804 | ), | |||
|
805 | 'success': True | |||
|
806 | } | |||
|
807 | self._compare_ok(id_, expected, given=response.body) | |||
|
808 | ||||
|
809 | @mock.patch.object(RepoModel, 'revoke_user_permission', crash) | |||
|
810 | def test_api_revoke_user_permission_exception_when_adding(self): | |||
|
811 | id_, params = _build_data(self.apikey, 'revoke_user_permission', | |||
|
812 | repoid=self.REPO, | |||
|
813 | userid=TEST_USER_ADMIN_LOGIN,) | |||
|
814 | response = self.app.post(API_URL, content_type='application/json', | |||
|
815 | params=params) | |||
|
816 | ||||
|
817 | expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( | |||
|
818 | TEST_USER_ADMIN_LOGIN, self.REPO | |||
|
819 | ) | |||
|
820 | self._compare_error(id_, expected, given=response.body) | |||
|
821 | ||||
|
822 | @parameterized.expand([('none', 'repository.none'), | |||
|
823 | ('read', 'repository.read'), | |||
|
824 | ('write', 'repository.write'), | |||
|
825 | ('admin', 'repository.admin')]) | |||
|
826 | def test_api_grant_users_group_permission(self, name, perm): | |||
|
827 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', | |||
|
828 | repoid=self.REPO, | |||
|
829 | usersgroupid=TEST_USERS_GROUP, | |||
|
830 | perm=perm) | |||
|
831 | response = self.app.post(API_URL, content_type='application/json', | |||
|
832 | params=params) | |||
|
833 | ||||
|
834 | ret = { | |||
|
835 | 'msg': 'Granted perm: `%s` for users group: `%s` in repo: `%s`' % ( | |||
|
836 | perm, TEST_USERS_GROUP, self.REPO | |||
|
837 | ), | |||
|
838 | 'success': True | |||
|
839 | } | |||
|
840 | expected = ret | |||
|
841 | self._compare_ok(id_, expected, given=response.body) | |||
|
842 | ||||
|
843 | def test_api_grant_users_group_permission_wrong_permission(self): | |||
|
844 | perm = 'haha.no.permission' | |||
|
845 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', | |||
|
846 | repoid=self.REPO, | |||
|
847 | usersgroupid=TEST_USERS_GROUP, | |||
|
848 | perm=perm) | |||
|
849 | response = self.app.post(API_URL, content_type='application/json', | |||
|
850 | params=params) | |||
|
851 | ||||
|
852 | expected = 'permission `%s` does not exist' % perm | |||
|
853 | self._compare_error(id_, expected, given=response.body) | |||
|
854 | ||||
|
855 | @mock.patch.object(RepoModel, 'grant_users_group_permission', crash) | |||
|
856 | def test_api_grant_users_group_permission_exception_when_adding(self): | |||
|
857 | perm = 'repository.read' | |||
|
858 | id_, params = _build_data(self.apikey, 'grant_users_group_permission', | |||
|
859 | repoid=self.REPO, | |||
|
860 | usersgroupid=TEST_USERS_GROUP, | |||
|
861 | perm=perm) | |||
|
862 | response = self.app.post(API_URL, content_type='application/json', | |||
|
863 | params=params) | |||
|
864 | ||||
|
865 | expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % ( | |||
|
866 | TEST_USERS_GROUP, self.REPO | |||
|
867 | ) | |||
|
868 | self._compare_error(id_, expected, given=response.body) | |||
|
869 | ||||
|
870 | def test_api_revoke_users_group_permission(self): | |||
|
871 | RepoModel().grant_users_group_permission(repo=self.REPO, | |||
|
872 | group_name=TEST_USERS_GROUP, | |||
|
873 | perm='repository.read') | |||
|
874 | Session().commit() | |||
|
875 | id_, params = _build_data(self.apikey, 'revoke_users_group_permission', | |||
|
876 | repoid=self.REPO, | |||
|
877 | usersgroupid=TEST_USERS_GROUP,) | |||
|
878 | response = self.app.post(API_URL, content_type='application/json', | |||
|
879 | params=params) | |||
|
880 | ||||
|
881 | expected = { | |||
|
882 | 'msg': 'Revoked perm for users group: `%s` in repo: `%s`' % ( | |||
|
883 | TEST_USERS_GROUP, self.REPO | |||
|
884 | ), | |||
|
885 | 'success': True | |||
|
886 | } | |||
|
887 | self._compare_ok(id_, expected, given=response.body) | |||
|
888 | ||||
|
889 | @mock.patch.object(RepoModel, 'revoke_users_group_permission', crash) | |||
|
890 | def test_api_revoke_users_group_permission_exception_when_adding(self): | |||
|
891 | ||||
|
892 | id_, params = _build_data(self.apikey, 'revoke_users_group_permission', | |||
|
893 | repoid=self.REPO, | |||
|
894 | usersgroupid=TEST_USERS_GROUP,) | |||
|
895 | response = self.app.post(API_URL, content_type='application/json', | |||
|
896 | params=params) | |||
|
897 | ||||
|
898 | expected = 'failed to edit permission for users group: `%s` in repo: `%s`' % ( | |||
|
899 | TEST_USERS_GROUP, self.REPO | |||
|
900 | ) | |||
|
901 | self._compare_error(id_, expected, given=response.body) | |||
|
902 |
@@ -0,0 +1,7 b'' | |||||
|
1 | from rhodecode.tests import * | |||
|
2 | from rhodecode.tests.api.api_base import BaseTestApi | |||
|
3 | ||||
|
4 | ||||
|
5 | class TestGitApi(BaseTestApi, TestController): | |||
|
6 | REPO = GIT_REPO | |||
|
7 | REPO_TYPE = 'git' |
@@ -0,0 +1,7 b'' | |||||
|
1 | from rhodecode.tests import * | |||
|
2 | from rhodecode.tests.api.api_base import BaseTestApi | |||
|
3 | ||||
|
4 | ||||
|
5 | class TestHgApi(BaseTestApi, TestController): | |||
|
6 | REPO = HG_REPO | |||
|
7 | REPO_TYPE = 'hg' No newline at end of file |
This diff has been collapsed as it changes many lines, (752 lines changed) Show them Hide them | |||||
@@ -1,727 +1,739 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.controllers.api |
|
3 | rhodecode.controllers.api | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | API controller for RhodeCode |
|
6 | API controller for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Aug 20, 2011 |
|
8 | :created_on: Aug 20, 2011 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 |
|
27 | |||
28 | import traceback |
|
28 | import traceback | |
29 | import logging |
|
29 | import logging | |
30 |
|
30 | |||
31 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError |
|
31 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError | |
32 | from rhodecode.lib.auth import HasPermissionAllDecorator, \ |
|
32 | from rhodecode.lib.auth import HasPermissionAllDecorator, \ | |
33 | HasPermissionAnyDecorator, PasswordGenerator, AuthUser |
|
33 | HasPermissionAnyDecorator, PasswordGenerator, AuthUser | |
34 |
|
34 | from rhodecode.lib.utils import map_groups | ||
35 | from rhodecode.model.meta import Session |
|
35 | from rhodecode.model.meta import Session | |
36 | from rhodecode.model.scm import ScmModel |
|
36 | from rhodecode.model.scm import ScmModel | |
37 | from rhodecode.model.db import User, UsersGroup, Repository |
|
|||
38 | from rhodecode.model.repo import RepoModel |
|
37 | from rhodecode.model.repo import RepoModel | |
39 | from rhodecode.model.user import UserModel |
|
38 | from rhodecode.model.user import UserModel | |
40 | from rhodecode.model.users_group import UsersGroupModel |
|
39 | from rhodecode.model.users_group import UsersGroupModel | |
41 | from rhodecode.lib.utils import map_groups |
|
40 | from rhodecode.model.permission import PermissionModel | |
42 |
|
41 | |||
43 | log = logging.getLogger(__name__) |
|
42 | log = logging.getLogger(__name__) | |
44 |
|
43 | |||
45 |
|
44 | |||
|
45 | class Optional(object): | |||
|
46 | """ | |||
|
47 | Defines an optional parameter:: | |||
|
48 | ||||
|
49 | param = param.getval() if isinstance(param, Optional) else param | |||
|
50 | param = param() if isinstance(param, Optional) else param | |||
|
51 | ||||
|
52 | is equivalent of:: | |||
|
53 | ||||
|
54 | param = Optional.extract(param) | |||
|
55 | ||||
|
56 | """ | |||
|
57 | def __init__(self, type_): | |||
|
58 | self.type_ = type_ | |||
|
59 | ||||
|
60 | def __repr__(self): | |||
|
61 | return '<Optional:%s>' % self.type_.__repr__() | |||
|
62 | ||||
|
63 | def __call__(self): | |||
|
64 | return self.getval() | |||
|
65 | ||||
|
66 | def getval(self): | |||
|
67 | """ | |||
|
68 | returns value from this Optional instance | |||
|
69 | """ | |||
|
70 | return self.type_ | |||
|
71 | ||||
|
72 | @classmethod | |||
|
73 | def extract(cls, val): | |||
|
74 | if isinstance(val, cls): | |||
|
75 | return val.getval() | |||
|
76 | return val | |||
|
77 | ||||
|
78 | ||||
|
79 | def get_user_or_error(userid): | |||
|
80 | """ | |||
|
81 | Get user by id or name or return JsonRPCError if not found | |||
|
82 | ||||
|
83 | :param userid: | |||
|
84 | """ | |||
|
85 | user = UserModel().get_user(userid) | |||
|
86 | if user is None: | |||
|
87 | raise JSONRPCError("user `%s` does not exist" % userid) | |||
|
88 | return user | |||
|
89 | ||||
|
90 | ||||
|
91 | def get_repo_or_error(repoid): | |||
|
92 | """ | |||
|
93 | Get repo by id or name or return JsonRPCError if not found | |||
|
94 | ||||
|
95 | :param userid: | |||
|
96 | """ | |||
|
97 | repo = RepoModel().get_repo(repoid) | |||
|
98 | if repo is None: | |||
|
99 | raise JSONRPCError('repository `%s` does not exist' % (repoid)) | |||
|
100 | return repo | |||
|
101 | ||||
|
102 | ||||
|
103 | def get_users_group_or_error(usersgroupid): | |||
|
104 | """ | |||
|
105 | Get users group by id or name or return JsonRPCError if not found | |||
|
106 | ||||
|
107 | :param userid: | |||
|
108 | """ | |||
|
109 | users_group = UsersGroupModel().get_group(usersgroupid) | |||
|
110 | if users_group is None: | |||
|
111 | raise JSONRPCError('users group `%s` does not exist' % usersgroupid) | |||
|
112 | return users_group | |||
|
113 | ||||
|
114 | ||||
|
115 | def get_perm_or_error(permid): | |||
|
116 | """ | |||
|
117 | Get permission by id or name or return JsonRPCError if not found | |||
|
118 | ||||
|
119 | :param userid: | |||
|
120 | """ | |||
|
121 | perm = PermissionModel().get_permission_by_name(permid) | |||
|
122 | if perm is None: | |||
|
123 | raise JSONRPCError('permission `%s` does not exist' % (permid)) | |||
|
124 | return perm | |||
|
125 | ||||
|
126 | ||||
46 | class ApiController(JSONRPCController): |
|
127 | class ApiController(JSONRPCController): | |
47 | """ |
|
128 | """ | |
48 | API Controller |
|
129 | API Controller | |
49 |
|
130 | |||
50 |
|
131 | |||
51 | Each method needs to have USER as argument this is then based on given |
|
132 | Each method needs to have USER as argument this is then based on given | |
52 | API_KEY propagated as instance of user object |
|
133 | API_KEY propagated as instance of user object | |
53 |
|
134 | |||
54 | Preferably this should be first argument also |
|
135 | Preferably this should be first argument also | |
55 |
|
136 | |||
56 |
|
137 | |||
57 | Each function should also **raise** JSONRPCError for any |
|
138 | Each function should also **raise** JSONRPCError for any | |
58 | errors that happens |
|
139 | errors that happens | |
59 |
|
140 | |||
60 | """ |
|
141 | """ | |
61 |
|
142 | |||
62 | @HasPermissionAllDecorator('hg.admin') |
|
143 | @HasPermissionAllDecorator('hg.admin') | |
63 |
def pull(self, apiuser, repo |
|
144 | def pull(self, apiuser, repoid): | |
64 | """ |
|
145 | """ | |
65 | Dispatch pull action on given repo |
|
146 | Dispatch pull action on given repo | |
66 |
|
147 | |||
67 |
|
148 | :param apiuser: | ||
68 |
:param |
|
149 | :param repoid: | |
69 | :param repo_name: |
|
|||
70 | """ |
|
150 | """ | |
71 |
|
151 | |||
72 | if Repository.is_valid(repo_name) is False: |
|
152 | repo = get_repo_or_error(repoid) | |
73 | raise JSONRPCError('Unknown repo "%s"' % repo_name) |
|
|||
74 |
|
153 | |||
75 | try: |
|
154 | try: | |
76 |
ScmModel().pull_changes(repo_name, |
|
155 | ScmModel().pull_changes(repo.repo_name, | |
77 | return 'Pulled from %s' % repo_name |
|
156 | self.rhodecode_user.username) | |
|
157 | return 'Pulled from `%s`' % repo.repo_name | |||
78 | except Exception: |
|
158 | except Exception: | |
79 | raise JSONRPCError('Unable to pull changes from "%s"' % repo_name) |
|
159 | log.error(traceback.format_exc()) | |
|
160 | raise JSONRPCError( | |||
|
161 | 'Unable to pull changes from `%s`' % repo.repo_name | |||
|
162 | ) | |||
80 |
|
163 | |||
81 | @HasPermissionAllDecorator('hg.admin') |
|
164 | @HasPermissionAllDecorator('hg.admin') | |
82 | def get_user(self, apiuser, userid): |
|
165 | def get_user(self, apiuser, userid): | |
83 | """" |
|
166 | """" | |
84 | Get a user by username |
|
167 | Get a user by username | |
85 |
|
168 | |||
86 | :param apiuser: |
|
169 | :param apiuser: | |
87 |
:param user |
|
170 | :param userid: | |
88 | """ |
|
171 | """ | |
89 |
|
172 | |||
90 |
user = |
|
173 | user = get_user_or_error(userid) | |
91 | if user is None: |
|
174 | data = user.get_api_data() | |
92 | return user |
|
175 | data['permissions'] = AuthUser(user_id=user.user_id).permissions | |
93 |
|
176 | return data | ||
94 | return dict( |
|
|||
95 | id=user.user_id, |
|
|||
96 | username=user.username, |
|
|||
97 | firstname=user.name, |
|
|||
98 | lastname=user.lastname, |
|
|||
99 | email=user.email, |
|
|||
100 | emails=user.emails, |
|
|||
101 | active=user.active, |
|
|||
102 | admin=user.admin, |
|
|||
103 | ldap_dn=user.ldap_dn, |
|
|||
104 | last_login=user.last_login, |
|
|||
105 | permissions=AuthUser(user_id=user.user_id).permissions |
|
|||
106 | ) |
|
|||
107 |
|
177 | |||
108 | @HasPermissionAllDecorator('hg.admin') |
|
178 | @HasPermissionAllDecorator('hg.admin') | |
109 | def get_users(self, apiuser): |
|
179 | def get_users(self, apiuser): | |
110 | """" |
|
180 | """" | |
111 | Get all users |
|
181 | Get all users | |
112 |
|
182 | |||
113 | :param apiuser: |
|
183 | :param apiuser: | |
114 | """ |
|
184 | """ | |
115 |
|
185 | |||
116 | result = [] |
|
186 | result = [] | |
117 |
for user in User.get |
|
187 | for user in UserModel().get_all(): | |
118 | result.append( |
|
188 | result.append(user.get_api_data()) | |
119 | dict( |
|
|||
120 | id=user.user_id, |
|
|||
121 | username=user.username, |
|
|||
122 | firstname=user.name, |
|
|||
123 | lastname=user.lastname, |
|
|||
124 | email=user.email, |
|
|||
125 | active=user.active, |
|
|||
126 | admin=user.admin, |
|
|||
127 | ldap_dn=user.ldap_dn, |
|
|||
128 | last_login=user.last_login, |
|
|||
129 | ) |
|
|||
130 | ) |
|
|||
131 | return result |
|
189 | return result | |
132 |
|
190 | |||
133 | @HasPermissionAllDecorator('hg.admin') |
|
191 | @HasPermissionAllDecorator('hg.admin') | |
134 |
def create_user(self, apiuser, username, email, password, |
|
192 | def create_user(self, apiuser, username, email, password, | |
135 | lastname=None, active=True, admin=False, ldap_dn=None): |
|
193 | firstname=Optional(None), lastname=Optional(None), | |
|
194 | active=Optional(True), admin=Optional(False), | |||
|
195 | ldap_dn=Optional(None)): | |||
136 | """ |
|
196 | """ | |
137 | Create new user |
|
197 | Create new user | |
138 |
|
198 | |||
139 | :param apiuser: |
|
199 | :param apiuser: | |
140 | :param username: |
|
200 | :param username: | |
|
201 | :param email: | |||
141 | :param password: |
|
202 | :param password: | |
142 |
:param |
|
203 | :param firstname: | |
143 | :param name: |
|
|||
144 | :param lastname: |
|
204 | :param lastname: | |
145 | :param active: |
|
205 | :param active: | |
146 | :param admin: |
|
206 | :param admin: | |
147 | :param ldap_dn: |
|
207 | :param ldap_dn: | |
148 | """ |
|
208 | """ | |
149 | if User.get_by_username(username): |
|
|||
150 | raise JSONRPCError("user %s already exist" % username) |
|
|||
151 |
|
209 | |||
152 | if User.get_by_email(email, case_insensitive=True): |
|
210 | if UserModel().get_by_username(username): | |
153 |
raise JSONRPCError(" |
|
211 | raise JSONRPCError("user `%s` already exist" % username) | |
|
212 | ||||
|
213 | if UserModel().get_by_email(email, case_insensitive=True): | |||
|
214 | raise JSONRPCError("email `%s` already exist" % email) | |||
154 |
|
215 | |||
155 | if ldap_dn: |
|
216 | if ldap_dn: | |
156 | # generate temporary password if ldap_dn |
|
217 | # generate temporary password if ldap_dn | |
157 | password = PasswordGenerator().gen_password(length=8) |
|
218 | password = PasswordGenerator().gen_password(length=8) | |
158 |
|
219 | |||
159 | try: |
|
220 | try: | |
160 | user = UserModel().create_or_update( |
|
221 | user = UserModel().create_or_update( | |
161 |
username |
|
222 | username=Optional.extract(username), | |
162 | lastname, active, admin, ldap_dn |
|
223 | password=Optional.extract(password), | |
|
224 | email=Optional.extract(email), | |||
|
225 | firstname=Optional.extract(firstname), | |||
|
226 | lastname=Optional.extract(lastname), | |||
|
227 | active=Optional.extract(active), | |||
|
228 | admin=Optional.extract(admin), | |||
|
229 | ldap_dn=Optional.extract(ldap_dn) | |||
163 | ) |
|
230 | ) | |
164 | Session.commit() |
|
231 | Session().commit() | |
165 | return dict( |
|
232 | return dict( | |
166 | id=user.user_id, |
|
233 | msg='created new user `%s`' % username, | |
167 | msg='created new user %s' % username, |
|
234 | user=user.get_api_data() | |
168 | user=dict( |
|
|||
169 | id=user.user_id, |
|
|||
170 | username=user.username, |
|
|||
171 | firstname=user.name, |
|
|||
172 | lastname=user.lastname, |
|
|||
173 | email=user.email, |
|
|||
174 | active=user.active, |
|
|||
175 | admin=user.admin, |
|
|||
176 | ldap_dn=user.ldap_dn, |
|
|||
177 | last_login=user.last_login, |
|
|||
178 | ) |
|
|||
179 | ) |
|
235 | ) | |
180 | except Exception: |
|
236 | except Exception: | |
181 | log.error(traceback.format_exc()) |
|
237 | log.error(traceback.format_exc()) | |
182 | raise JSONRPCError('failed to create user %s' % username) |
|
238 | raise JSONRPCError('failed to create user `%s`' % username) | |
183 |
|
239 | |||
184 | @HasPermissionAllDecorator('hg.admin') |
|
240 | @HasPermissionAllDecorator('hg.admin') | |
185 |
def update_user(self, apiuser, userid, username |
|
241 | def update_user(self, apiuser, userid, username=Optional(None), | |
186 | firstname, lastname, active, admin, ldap_dn): |
|
242 | email=Optional(None), firstname=Optional(None), | |
|
243 | lastname=Optional(None), active=Optional(None), | |||
|
244 | admin=Optional(None), ldap_dn=Optional(None), | |||
|
245 | password=Optional(None)): | |||
187 | """ |
|
246 | """ | |
188 | Updates given user |
|
247 | Updates given user | |
189 |
|
248 | |||
190 | :param apiuser: |
|
249 | :param apiuser: | |
|
250 | :param userid: | |||
191 | :param username: |
|
251 | :param username: | |
192 | :param password: |
|
|||
193 | :param email: |
|
252 | :param email: | |
194 | :param name: |
|
253 | :param firstname: | |
195 | :param lastname: |
|
254 | :param lastname: | |
196 | :param active: |
|
255 | :param active: | |
197 | :param admin: |
|
256 | :param admin: | |
198 | :param ldap_dn: |
|
257 | :param ldap_dn: | |
|
258 | :param password: | |||
199 | """ |
|
259 | """ | |
200 | usr = UserModel().get_user(userid) |
|
260 | ||
201 | if not usr: |
|
261 | user = get_user_or_error(userid) | |
202 | raise JSONRPCError("user ID:%s does not exist" % userid) |
|
262 | ||
|
263 | #return old attribute if Optional is passed. We don't change parameter | |||
|
264 | # so user doesn't get updated parameters | |||
|
265 | get = lambda attr, name: ( | |||
|
266 | getattr(user, name) if isinstance(attr, Optional) else attr | |||
|
267 | ) | |||
203 |
|
268 | |||
204 | try: |
|
269 | try: | |
|
270 | ||||
205 | user = UserModel().create_or_update( |
|
271 | user = UserModel().create_or_update( | |
206 | username, password, email, firstname, |
|
272 | username=get(username, 'username'), | |
207 | lastname, active, admin, ldap_dn |
|
273 | password=get(password, 'password'), | |
|
274 | email=get(email, 'email'), | |||
|
275 | firstname=get(firstname, 'name'), | |||
|
276 | lastname=get(lastname, 'lastname'), | |||
|
277 | active=get(active, 'active'), | |||
|
278 | admin=get(admin, 'admin'), | |||
|
279 | ldap_dn=get(ldap_dn, 'ldap_dn') | |||
208 | ) |
|
280 | ) | |
209 | Session.commit() |
|
281 | Session().commit() | |
210 | return dict( |
|
282 | return dict( | |
211 | id=usr.user_id, |
|
|||
212 | msg='updated user ID:%s %s' % (user.user_id, user.username), |
|
283 | msg='updated user ID:%s %s' % (user.user_id, user.username), | |
213 |
user= |
|
284 | user=user.get_api_data() | |
214 | id=user.user_id, |
|
|||
215 | username=user.username, |
|
|||
216 | firstname=user.name, |
|
|||
217 | lastname=user.lastname, |
|
|||
218 | email=user.email, |
|
|||
219 | active=user.active, |
|
|||
220 | admin=user.admin, |
|
|||
221 | ldap_dn=user.ldap_dn, |
|
|||
222 | last_login=user.last_login, |
|
|||
223 | ) |
|
|||
224 | ) |
|
285 | ) | |
225 | except Exception: |
|
286 | except Exception: | |
226 | log.error(traceback.format_exc()) |
|
287 | log.error(traceback.format_exc()) | |
227 | raise JSONRPCError('failed to update user %s' % userid) |
|
288 | raise JSONRPCError('failed to update user `%s`' % userid) | |
228 |
|
289 | |||
229 | @HasPermissionAllDecorator('hg.admin') |
|
290 | @HasPermissionAllDecorator('hg.admin') | |
230 | def delete_user(self, apiuser, userid): |
|
291 | def delete_user(self, apiuser, userid): | |
231 | """" |
|
292 | """" | |
232 | Deletes an user |
|
293 | Deletes an user | |
233 |
|
294 | |||
234 | :param apiuser: |
|
295 | :param apiuser: | |
|
296 | :param userid: | |||
235 | """ |
|
297 | """ | |
236 |
usr = |
|
298 | user = get_user_or_error(userid) | |
237 | if not usr: |
|
|||
238 | raise JSONRPCError("user ID:%s does not exist" % userid) |
|
|||
239 |
|
299 | |||
240 | try: |
|
300 | try: | |
241 | UserModel().delete(userid) |
|
301 | UserModel().delete(userid) | |
242 | Session.commit() |
|
302 | Session().commit() | |
243 | return dict( |
|
303 | return dict( | |
244 | id=usr.user_id, |
|
304 | msg='deleted user ID:%s %s' % (user.user_id, user.username), | |
245 | msg='deleted user ID:%s %s' % (usr.user_id, usr.username) |
|
305 | user=None | |
246 | ) |
|
306 | ) | |
247 | except Exception: |
|
307 | except Exception: | |
248 | log.error(traceback.format_exc()) |
|
308 | log.error(traceback.format_exc()) | |
249 | raise JSONRPCError('failed to delete ID:%s %s' % (usr.user_id, |
|
309 | raise JSONRPCError('failed to delete ID:%s %s' % (user.user_id, | |
250 | usr.username)) |
|
310 | user.username)) | |
251 |
|
311 | |||
252 | @HasPermissionAllDecorator('hg.admin') |
|
312 | @HasPermissionAllDecorator('hg.admin') | |
253 |
def get_users_group(self, apiuser, |
|
313 | def get_users_group(self, apiuser, usersgroupid): | |
254 | """" |
|
314 | """" | |
255 | Get users group by name |
|
315 | Get users group by name or id | |
256 |
|
316 | |||
257 | :param apiuser: |
|
317 | :param apiuser: | |
258 |
:param |
|
318 | :param usersgroupid: | |
259 | """ |
|
319 | """ | |
|
320 | users_group = get_users_group_or_error(usersgroupid) | |||
260 |
|
321 | |||
261 | users_group = UsersGroup.get_by_group_name(group_name) |
|
322 | data = users_group.get_api_data() | |
262 | if not users_group: |
|
|||
263 | return None |
|
|||
264 |
|
323 | |||
265 | members = [] |
|
324 | members = [] | |
266 | for user in users_group.members: |
|
325 | for user in users_group.members: | |
267 | user = user.user |
|
326 | user = user.user | |
268 |
members.append( |
|
327 | members.append(user.get_api_data()) | |
269 | username=user.username, |
|
328 | data['members'] = members | |
270 | firstname=user.name, |
|
329 | return data | |
271 | lastname=user.lastname, |
|
|||
272 | email=user.email, |
|
|||
273 | active=user.active, |
|
|||
274 | admin=user.admin, |
|
|||
275 | ldap=user.ldap_dn)) |
|
|||
276 |
|
||||
277 | return dict(id=users_group.users_group_id, |
|
|||
278 | group_name=users_group.users_group_name, |
|
|||
279 | active=users_group.users_group_active, |
|
|||
280 | members=members) |
|
|||
281 |
|
330 | |||
282 | @HasPermissionAllDecorator('hg.admin') |
|
331 | @HasPermissionAllDecorator('hg.admin') | |
283 | def get_users_groups(self, apiuser): |
|
332 | def get_users_groups(self, apiuser): | |
284 | """" |
|
333 | """" | |
285 | Get all users groups |
|
334 | Get all users groups | |
286 |
|
335 | |||
287 | :param apiuser: |
|
336 | :param apiuser: | |
288 | """ |
|
337 | """ | |
289 |
|
338 | |||
290 | result = [] |
|
339 | result = [] | |
291 |
for users_group in UsersGroup.get |
|
340 | for users_group in UsersGroupModel().get_all(): | |
292 | members = [] |
|
341 | result.append(users_group.get_api_data()) | |
293 | for user in users_group.members: |
|
|||
294 | user = user.user |
|
|||
295 | members.append(dict(id=user.user_id, |
|
|||
296 | username=user.username, |
|
|||
297 | firstname=user.name, |
|
|||
298 | lastname=user.lastname, |
|
|||
299 | email=user.email, |
|
|||
300 | active=user.active, |
|
|||
301 | admin=user.admin, |
|
|||
302 | ldap=user.ldap_dn)) |
|
|||
303 |
|
||||
304 | result.append(dict(id=users_group.users_group_id, |
|
|||
305 | group_name=users_group.users_group_name, |
|
|||
306 | active=users_group.users_group_active, |
|
|||
307 | members=members)) |
|
|||
308 | return result |
|
342 | return result | |
309 |
|
343 | |||
310 | @HasPermissionAllDecorator('hg.admin') |
|
344 | @HasPermissionAllDecorator('hg.admin') | |
311 | def create_users_group(self, apiuser, group_name, active=True): |
|
345 | def create_users_group(self, apiuser, group_name, active=Optional(True)): | |
312 | """ |
|
346 | """ | |
313 | Creates an new usergroup |
|
347 | Creates an new usergroup | |
314 |
|
348 | |||
|
349 | :param apiuser: | |||
315 | :param group_name: |
|
350 | :param group_name: | |
316 | :param active: |
|
351 | :param active: | |
317 | """ |
|
352 | """ | |
318 |
|
353 | |||
319 |
if |
|
354 | if UsersGroupModel().get_by_name(group_name): | |
320 | raise JSONRPCError("users group %s already exist" % group_name) |
|
355 | raise JSONRPCError("users group `%s` already exist" % group_name) | |
321 |
|
356 | |||
322 | try: |
|
357 | try: | |
|
358 | active = Optional.extract(active) | |||
323 | ug = UsersGroupModel().create(name=group_name, active=active) |
|
359 | ug = UsersGroupModel().create(name=group_name, active=active) | |
324 | Session.commit() |
|
360 | Session().commit() | |
325 |
return dict( |
|
361 | return dict( | |
326 |
|
|
362 | msg='created new users group `%s`' % group_name, | |
|
363 | users_group=ug.get_api_data() | |||
|
364 | ) | |||
327 | except Exception: |
|
365 | except Exception: | |
328 | log.error(traceback.format_exc()) |
|
366 | log.error(traceback.format_exc()) | |
329 | raise JSONRPCError('failed to create group %s' % group_name) |
|
367 | raise JSONRPCError('failed to create group `%s`' % group_name) | |
330 |
|
368 | |||
331 | @HasPermissionAllDecorator('hg.admin') |
|
369 | @HasPermissionAllDecorator('hg.admin') | |
332 |
def add_user_to_users_group(self, apiuser, |
|
370 | def add_user_to_users_group(self, apiuser, usersgroupid, userid): | |
333 | """" |
|
371 | """" | |
334 | Add a user to a users group |
|
372 | Add a user to a users group | |
335 |
|
373 | |||
336 | :param apiuser: |
|
374 | :param apiuser: | |
337 |
:param |
|
375 | :param usersgroupid: | |
338 |
:param user |
|
376 | :param userid: | |
339 | """ |
|
377 | """ | |
|
378 | user = get_user_or_error(userid) | |||
|
379 | users_group = get_users_group_or_error(usersgroupid) | |||
340 |
|
380 | |||
341 | try: |
|
381 | try: | |
342 | users_group = UsersGroup.get_by_group_name(group_name) |
|
|||
343 | if not users_group: |
|
|||
344 | raise JSONRPCError('unknown users group %s' % group_name) |
|
|||
345 |
|
||||
346 | user = User.get_by_username(username) |
|
|||
347 | if user is None: |
|
|||
348 | raise JSONRPCError('unknown user %s' % username) |
|
|||
349 |
|
||||
350 | ugm = UsersGroupModel().add_user_to_group(users_group, user) |
|
382 | ugm = UsersGroupModel().add_user_to_group(users_group, user) | |
351 | success = True if ugm != True else False |
|
383 | success = True if ugm != True else False | |
352 |
msg = 'added member %s to users group %s' % ( |
|
384 | msg = 'added member `%s` to users group `%s`' % ( | |
|
385 | user.username, users_group.users_group_name | |||
|
386 | ) | |||
353 | msg = msg if success else 'User is already in that group' |
|
387 | msg = msg if success else 'User is already in that group' | |
354 | Session.commit() |
|
388 | Session().commit() | |
355 |
|
389 | |||
356 | return dict( |
|
390 | return dict( | |
357 | id=ugm.users_group_member_id if ugm != True else None, |
|
|||
358 | success=success, |
|
391 | success=success, | |
359 | msg=msg |
|
392 | msg=msg | |
360 | ) |
|
393 | ) | |
361 | except Exception: |
|
394 | except Exception: | |
362 | log.error(traceback.format_exc()) |
|
395 | log.error(traceback.format_exc()) | |
363 |
raise JSONRPCError( |
|
396 | raise JSONRPCError( | |
|
397 | 'failed to add member to users group `%s`' % ( | |||
|
398 | users_group.users_group_name | |||
|
399 | ) | |||
|
400 | ) | |||
364 |
|
401 | |||
365 | @HasPermissionAllDecorator('hg.admin') |
|
402 | @HasPermissionAllDecorator('hg.admin') | |
366 |
def remove_user_from_users_group(self, apiuser, |
|
403 | def remove_user_from_users_group(self, apiuser, usersgroupid, userid): | |
367 | """ |
|
404 | """ | |
368 | Remove user from a group |
|
405 | Remove user from a group | |
369 |
|
406 | |||
370 | :param apiuser |
|
407 | :param apiuser: | |
371 |
:param |
|
408 | :param usersgroupid: | |
372 |
:param user |
|
409 | :param userid: | |
373 | """ |
|
410 | """ | |
|
411 | user = get_user_or_error(userid) | |||
|
412 | users_group = get_users_group_or_error(usersgroupid) | |||
374 |
|
413 | |||
375 | try: |
|
414 | try: | |
376 |
|
|
415 | success = UsersGroupModel().remove_user_from_group(users_group, | |
377 | if not users_group: |
|
416 | user) | |
378 | raise JSONRPCError('unknown users group %s' % group_name) |
|
417 | msg = 'removed member `%s` from users group `%s`' % ( | |
379 |
|
418 | user.username, users_group.users_group_name | ||
380 | user = User.get_by_username(username) |
|
419 | ) | |
381 | if user is None: |
|
|||
382 | raise JSONRPCError('unknown user %s' % username) |
|
|||
383 |
|
||||
384 | success = UsersGroupModel().remove_user_from_group(users_group, user) |
|
|||
385 | msg = 'removed member %s from users group %s' % (username, group_name) |
|
|||
386 | msg = msg if success else "User wasn't in group" |
|
420 | msg = msg if success else "User wasn't in group" | |
387 | Session.commit() |
|
421 | Session().commit() | |
388 | return dict(success=success, msg=msg) |
|
422 | return dict(success=success, msg=msg) | |
389 | except Exception: |
|
423 | except Exception: | |
390 | log.error(traceback.format_exc()) |
|
424 | log.error(traceback.format_exc()) | |
391 |
raise JSONRPCError( |
|
425 | raise JSONRPCError( | |
|
426 | 'failed to remove member from users group `%s`' % ( | |||
|
427 | users_group.users_group_name | |||
|
428 | ) | |||
|
429 | ) | |||
392 |
|
430 | |||
393 | @HasPermissionAnyDecorator('hg.admin') |
|
431 | @HasPermissionAnyDecorator('hg.admin') | |
394 | def get_repo(self, apiuser, repoid): |
|
432 | def get_repo(self, apiuser, repoid): | |
395 | """" |
|
433 | """" | |
396 | Get repository by name |
|
434 | Get repository by name | |
397 |
|
435 | |||
398 | :param apiuser: |
|
436 | :param apiuser: | |
399 |
:param repo |
|
437 | :param repoid: | |
400 | """ |
|
438 | """ | |
401 |
|
439 | repo = get_repo_or_error(repoid) | ||
402 | repo = RepoModel().get_repo(repoid) |
|
|||
403 | if repo is None: |
|
|||
404 | raise JSONRPCError('unknown repository "%s"' % (repo or repoid)) |
|
|||
405 |
|
440 | |||
406 | members = [] |
|
441 | members = [] | |
407 | for user in repo.repo_to_perm: |
|
442 | for user in repo.repo_to_perm: | |
408 | perm = user.permission.permission_name |
|
443 | perm = user.permission.permission_name | |
409 | user = user.user |
|
444 | user = user.user | |
410 | members.append( |
|
445 | user_data = user.get_api_data() | |
411 | dict( |
|
446 | user_data['type'] = "user" | |
412 | type="user", |
|
447 | user_data['permission'] = perm | |
413 | id=user.user_id, |
|
448 | members.append(user_data) | |
414 | username=user.username, |
|
449 | ||
415 | firstname=user.name, |
|
|||
416 | lastname=user.lastname, |
|
|||
417 | email=user.email, |
|
|||
418 | active=user.active, |
|
|||
419 | admin=user.admin, |
|
|||
420 | ldap=user.ldap_dn, |
|
|||
421 | permission=perm |
|
|||
422 | ) |
|
|||
423 | ) |
|
|||
424 | for users_group in repo.users_group_to_perm: |
|
450 | for users_group in repo.users_group_to_perm: | |
425 | perm = users_group.permission.permission_name |
|
451 | perm = users_group.permission.permission_name | |
426 | users_group = users_group.users_group |
|
452 | users_group = users_group.users_group | |
427 | members.append( |
|
453 | users_group_data = users_group.get_api_data() | |
428 | dict( |
|
454 | users_group_data['type'] = "users_group" | |
429 | type="users_group", |
|
455 | users_group_data['permission'] = perm | |
430 |
|
|
456 | members.append(users_group_data) | |
431 | name=users_group.users_group_name, |
|
|||
432 | active=users_group.users_group_active, |
|
|||
433 | permission=perm |
|
|||
434 | ) |
|
|||
435 | ) |
|
|||
436 |
|
457 | |||
437 | return dict( |
|
458 | data = repo.get_api_data() | |
438 | id=repo.repo_id, |
|
459 | data['members'] = members | |
439 | repo_name=repo.repo_name, |
|
460 | return data | |
440 | type=repo.repo_type, |
|
|||
441 | clone_uri=repo.clone_uri, |
|
|||
442 | private=repo.private, |
|
|||
443 | created_on=repo.created_on, |
|
|||
444 | description=repo.description, |
|
|||
445 | members=members |
|
|||
446 | ) |
|
|||
447 |
|
461 | |||
448 | @HasPermissionAnyDecorator('hg.admin') |
|
462 | @HasPermissionAnyDecorator('hg.admin') | |
449 | def get_repos(self, apiuser): |
|
463 | def get_repos(self, apiuser): | |
450 | """" |
|
464 | """" | |
451 | Get all repositories |
|
465 | Get all repositories | |
452 |
|
466 | |||
453 | :param apiuser: |
|
467 | :param apiuser: | |
454 | """ |
|
468 | """ | |
455 |
|
469 | |||
456 | result = [] |
|
470 | result = [] | |
457 |
for repo in Repo |
|
471 | for repo in RepoModel().get_all(): | |
458 | result.append( |
|
472 | result.append(repo.get_api_data()) | |
459 | dict( |
|
|||
460 | id=repo.repo_id, |
|
|||
461 | repo_name=repo.repo_name, |
|
|||
462 | type=repo.repo_type, |
|
|||
463 | clone_uri=repo.clone_uri, |
|
|||
464 | private=repo.private, |
|
|||
465 | created_on=repo.created_on, |
|
|||
466 | description=repo.description, |
|
|||
467 | ) |
|
|||
468 | ) |
|
|||
469 | return result |
|
473 | return result | |
470 |
|
474 | |||
471 | @HasPermissionAnyDecorator('hg.admin') |
|
475 | @HasPermissionAnyDecorator('hg.admin') | |
472 |
def get_repo_nodes(self, apiuser, repo |
|
476 | def get_repo_nodes(self, apiuser, repoid, revision, root_path, | |
473 | ret_type='all'): |
|
477 | ret_type='all'): | |
474 | """ |
|
478 | """ | |
475 | returns a list of nodes and it's children |
|
479 | returns a list of nodes and it's children | |
476 | for a given path at given revision. It's possible to specify ret_type |
|
480 | for a given path at given revision. It's possible to specify ret_type | |
477 | to show only files or dirs |
|
481 | to show only files or dirs | |
478 |
|
482 | |||
479 | :param apiuser: |
|
483 | :param apiuser: | |
480 |
:param repo |
|
484 | :param repoid: name or id of repository | |
481 | :param revision: revision for which listing should be done |
|
485 | :param revision: revision for which listing should be done | |
482 | :param root_path: path from which start displaying |
|
486 | :param root_path: path from which start displaying | |
483 | :param ret_type: return type 'all|files|dirs' nodes |
|
487 | :param ret_type: return type 'all|files|dirs' nodes | |
484 | """ |
|
488 | """ | |
|
489 | repo = get_repo_or_error(repoid) | |||
485 | try: |
|
490 | try: | |
486 |
_d, _f = ScmModel().get_nodes(repo |
|
491 | _d, _f = ScmModel().get_nodes(repo, revision, root_path, | |
487 | flat=False) |
|
492 | flat=False) | |
488 | _map = { |
|
493 | _map = { | |
489 | 'all': _d + _f, |
|
494 | 'all': _d + _f, | |
490 | 'files': _f, |
|
495 | 'files': _f, | |
491 | 'dirs': _d, |
|
496 | 'dirs': _d, | |
492 | } |
|
497 | } | |
493 | return _map[ret_type] |
|
498 | return _map[ret_type] | |
494 | except KeyError: |
|
499 | except KeyError: | |
495 | raise JSONRPCError('ret_type must be one of %s' % _map.keys()) |
|
500 | raise JSONRPCError('ret_type must be one of %s' % _map.keys()) | |
496 |
except Exception |
|
501 | except Exception: | |
497 | raise JSONRPCError(e) |
|
502 | log.error(traceback.format_exc()) | |
|
503 | raise JSONRPCError( | |||
|
504 | 'failed to get repo: `%s` nodes' % repo.repo_name | |||
|
505 | ) | |||
498 |
|
506 | |||
499 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
507 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
500 |
def create_repo(self, apiuser, repo_name, owner |
|
508 | def create_repo(self, apiuser, repo_name, owner, repo_type, | |
501 |
|
|
509 | description=Optional(''), private=Optional(False), | |
|
510 | clone_uri=Optional(None), landing_rev=Optional('tip')): | |||
502 | """ |
|
511 | """ | |
503 | Create repository, if clone_url is given it makes a remote clone |
|
512 | Create repository, if clone_url is given it makes a remote clone | |
|
513 | if repo_name is withina group name the groups will be created | |||
|
514 | automatically if they aren't present | |||
504 |
|
515 | |||
505 | :param apiuser: |
|
516 | :param apiuser: | |
506 | :param repo_name: |
|
517 | :param repo_name: | |
507 |
:param o |
|
518 | :param onwer: | |
|
519 | :param repo_type: | |||
508 | :param description: |
|
520 | :param description: | |
509 | :param repo_type: |
|
|||
510 | :param private: |
|
521 | :param private: | |
511 | :param clone_uri: |
|
522 | :param clone_uri: | |
|
523 | :param landing_rev: | |||
512 | """ |
|
524 | """ | |
|
525 | owner = get_user_or_error(owner) | |||
|
526 | ||||
|
527 | if RepoModel().get_by_repo_name(repo_name): | |||
|
528 | raise JSONRPCError("repo `%s` already exist" % repo_name) | |||
|
529 | ||||
|
530 | private = Optional.extract(private) | |||
|
531 | clone_uri = Optional.extract(clone_uri) | |||
|
532 | description = Optional.extract(description) | |||
|
533 | landing_rev = Optional.extract(landing_rev) | |||
513 |
|
534 | |||
514 | try: |
|
535 | try: | |
515 | owner = User.get_by_username(owner_name) |
|
536 | # create structure of groups and return the last group | |
516 | if owner is None: |
|
|||
517 | raise JSONRPCError('unknown user %s' % owner_name) |
|
|||
518 |
|
||||
519 | if Repository.get_by_repo_name(repo_name): |
|
|||
520 | raise JSONRPCError("repo %s already exist" % repo_name) |
|
|||
521 |
|
||||
522 | groups = repo_name.split(Repository.url_sep()) |
|
|||
523 | real_name = groups[-1] |
|
|||
524 | # create structure of groups |
|
|||
525 | group = map_groups(repo_name) |
|
537 | group = map_groups(repo_name) | |
526 |
|
538 | |||
527 | repo = RepoModel().create( |
|
539 | repo = RepoModel().create_repo( | |
528 | dict( |
|
540 | repo_name=repo_name, | |
529 |
|
|
541 | repo_type=repo_type, | |
530 | repo_name_full=repo_name, |
|
542 | description=description, | |
531 |
|
|
543 | owner=owner, | |
532 |
|
|
544 | private=private, | |
533 | repo_type=repo_type, |
|
545 | clone_uri=clone_uri, | |
534 |
|
|
546 | repos_group=group, | |
535 | clone_uri=clone_uri |
|
547 | landing_rev=landing_rev, | |
536 | ) |
|
|||
537 | ) |
|
548 | ) | |
538 | Session.commit() |
|
549 | ||
|
550 | Session().commit() | |||
539 |
|
551 | |||
540 | return dict( |
|
552 | return dict( | |
541 | id=repo.repo_id, |
|
553 | msg="Created new repository `%s`" % (repo.repo_name), | |
542 | msg="Created new repository %s" % (repo.repo_name), |
|
554 | repo=repo.get_api_data() | |
543 | repo=dict( |
|
|||
544 | id=repo.repo_id, |
|
|||
545 | repo_name=repo.repo_name, |
|
|||
546 | type=repo.repo_type, |
|
|||
547 | clone_uri=repo.clone_uri, |
|
|||
548 | private=repo.private, |
|
|||
549 | created_on=repo.created_on, |
|
|||
550 | description=repo.description, |
|
|||
551 | ) |
|
|||
552 | ) |
|
555 | ) | |
553 |
|
556 | |||
554 | except Exception: |
|
557 | except Exception: | |
555 | log.error(traceback.format_exc()) |
|
558 | log.error(traceback.format_exc()) | |
556 | raise JSONRPCError('failed to create repository %s' % repo_name) |
|
559 | raise JSONRPCError('failed to create repository `%s`' % repo_name) | |
|
560 | ||||
|
561 | # @HasPermissionAnyDecorator('hg.admin') | |||
|
562 | # def fork_repo(self, apiuser, repoid, fork_name): | |||
|
563 | # repo = get_repo_or_error(repoid) | |||
|
564 | # | |||
|
565 | # try: | |||
|
566 | # form_data = dict( | |||
|
567 | # | |||
|
568 | # ) | |||
|
569 | # RepoModel().create_fork(form_data, cur_user=apiuser) | |||
|
570 | # return dict( | |||
|
571 | # msg='Created fork of `%s` as `%s`' % (repo.repo_name, | |||
|
572 | # fork_name), | |||
|
573 | # success=True | |||
|
574 | # ) | |||
|
575 | # except Exception: | |||
|
576 | # log.error(traceback.format_exc()) | |||
|
577 | # raise JSONRPCError( | |||
|
578 | # 'failed to fork repository `%s` as `%s`' % (repo.repo_name, | |||
|
579 | # fork_name) | |||
|
580 | # ) | |||
557 |
|
581 | |||
558 | @HasPermissionAnyDecorator('hg.admin') |
|
582 | @HasPermissionAnyDecorator('hg.admin') | |
559 |
def |
|
583 | def delete_repo(self, apiuser, repoid): | |
560 | repo = RepoModel().get_repo(repoid) |
|
|||
561 | if repo is None: |
|
|||
562 | raise JSONRPCError('unknown repository "%s"' % (repo or repoid)) |
|
|||
563 |
|
||||
564 | RepoModel().create_fork(form_data, cur_user) |
|
|||
565 |
|
||||
566 |
|
||||
567 | @HasPermissionAnyDecorator('hg.admin') |
|
|||
568 | def delete_repo(self, apiuser, repo_name): |
|
|||
569 | """ |
|
584 | """ | |
570 | Deletes a given repository |
|
585 | Deletes a given repository | |
571 |
|
586 | |||
572 |
:param |
|
587 | :param apiuser: | |
|
588 | :param repoid: | |||
573 | """ |
|
589 | """ | |
574 | if not Repository.get_by_repo_name(repo_name): |
|
590 | repo = get_repo_or_error(repoid) | |
575 | raise JSONRPCError("repo %s does not exist" % repo_name) |
|
591 | ||
576 | try: |
|
592 | try: | |
577 |
RepoModel().delete(repo |
|
593 | RepoModel().delete(repo) | |
578 | Session.commit() |
|
594 | Session().commit() | |
579 | return dict( |
|
595 | return dict( | |
580 | msg='Deleted repository %s' % repo_name |
|
596 | msg='Deleted repository `%s`' % repo.repo_name, | |
|
597 | success=True | |||
581 | ) |
|
598 | ) | |
582 | except Exception: |
|
599 | except Exception: | |
583 | log.error(traceback.format_exc()) |
|
600 | log.error(traceback.format_exc()) | |
584 | raise JSONRPCError('failed to delete repository %s' % repo_name) |
|
601 | raise JSONRPCError( | |
|
602 | 'failed to delete repository `%s`' % repo.repo_name | |||
|
603 | ) | |||
585 |
|
604 | |||
586 | @HasPermissionAnyDecorator('hg.admin') |
|
605 | @HasPermissionAnyDecorator('hg.admin') | |
587 |
def grant_user_permission(self, apiuser, repo |
|
606 | def grant_user_permission(self, apiuser, repoid, userid, perm): | |
588 | """ |
|
607 | """ | |
589 | Grant permission for user on given repository, or update existing one |
|
608 | Grant permission for user on given repository, or update existing one | |
590 | if found |
|
609 | if found | |
591 |
|
610 | |||
592 |
:param repo |
|
611 | :param repoid: | |
593 |
:param user |
|
612 | :param userid: | |
594 | :param perm: |
|
613 | :param perm: | |
595 | """ |
|
614 | """ | |
|
615 | repo = get_repo_or_error(repoid) | |||
|
616 | user = get_user_or_error(userid) | |||
|
617 | perm = get_perm_or_error(perm) | |||
596 |
|
618 | |||
597 | try: |
|
619 | try: | |
598 | repo = Repository.get_by_repo_name(repo_name) |
|
|||
599 | if repo is None: |
|
|||
600 | raise JSONRPCError('unknown repository %s' % repo) |
|
|||
601 |
|
||||
602 | user = User.get_by_username(username) |
|
|||
603 | if user is None: |
|
|||
604 | raise JSONRPCError('unknown user %s' % username) |
|
|||
605 |
|
620 | |||
606 | RepoModel().grant_user_permission(repo=repo, user=user, perm=perm) |
|
621 | RepoModel().grant_user_permission(repo=repo, user=user, perm=perm) | |
607 |
|
622 | |||
608 | Session.commit() |
|
623 | Session().commit() | |
609 | return dict( |
|
624 | return dict( | |
610 | msg='Granted perm: %s for user: %s in repo: %s' % ( |
|
625 | msg='Granted perm: `%s` for user: `%s` in repo: `%s`' % ( | |
611 | perm, username, repo_name |
|
626 | perm.permission_name, user.username, repo.repo_name | |
612 | ) |
|
627 | ), | |
|
628 | success=True | |||
613 | ) |
|
629 | ) | |
614 | except Exception: |
|
630 | except Exception: | |
615 | log.error(traceback.format_exc()) |
|
631 | log.error(traceback.format_exc()) | |
616 | raise JSONRPCError( |
|
632 | raise JSONRPCError( | |
617 |
'failed to edit permission |
|
633 | 'failed to edit permission for user: `%s` in repo: `%s`' % ( | |
618 |
user |
|
634 | userid, repoid | |
619 | ) |
|
635 | ) | |
620 | ) |
|
636 | ) | |
621 |
|
637 | |||
622 | @HasPermissionAnyDecorator('hg.admin') |
|
638 | @HasPermissionAnyDecorator('hg.admin') | |
623 |
def revoke_user_permission(self, apiuser, repo |
|
639 | def revoke_user_permission(self, apiuser, repoid, userid): | |
624 | """ |
|
640 | """ | |
625 | Revoke permission for user on given repository |
|
641 | Revoke permission for user on given repository | |
626 |
|
642 | |||
627 |
:param |
|
643 | :param apiuser: | |
628 |
:param |
|
644 | :param repoid: | |
|
645 | :param userid: | |||
629 | """ |
|
646 | """ | |
630 |
|
647 | |||
|
648 | repo = get_repo_or_error(repoid) | |||
|
649 | user = get_user_or_error(userid) | |||
631 | try: |
|
650 | try: | |
632 | repo = Repository.get_by_repo_name(repo_name) |
|
651 | ||
633 | if repo is None: |
|
652 | RepoModel().revoke_user_permission(repo=repo, user=user) | |
634 | raise JSONRPCError('unknown repository %s' % repo) |
|
|||
635 |
|
653 | |||
636 | user = User.get_by_username(username) |
|
654 | Session().commit() | |
637 | if user is None: |
|
|||
638 | raise JSONRPCError('unknown user %s' % username) |
|
|||
639 |
|
||||
640 | RepoModel().revoke_user_permission(repo=repo_name, user=username) |
|
|||
641 |
|
||||
642 | Session.commit() |
|
|||
643 | return dict( |
|
655 | return dict( | |
644 | msg='Revoked perm for user: %s in repo: %s' % ( |
|
656 | msg='Revoked perm for user: `%s` in repo: `%s`' % ( | |
645 | username, repo_name |
|
657 | user.username, repo.repo_name | |
646 | ) |
|
658 | ), | |
|
659 | success=True | |||
647 | ) |
|
660 | ) | |
648 | except Exception: |
|
661 | except Exception: | |
649 | log.error(traceback.format_exc()) |
|
662 | log.error(traceback.format_exc()) | |
650 | raise JSONRPCError( |
|
663 | raise JSONRPCError( | |
651 |
'failed to edit permission |
|
664 | 'failed to edit permission for user: `%s` in repo: `%s`' % ( | |
652 |
user |
|
665 | userid, repoid | |
653 | ) |
|
666 | ) | |
654 | ) |
|
667 | ) | |
655 |
|
668 | |||
656 | @HasPermissionAnyDecorator('hg.admin') |
|
669 | @HasPermissionAnyDecorator('hg.admin') | |
657 |
def grant_users_group_permission(self, apiuser, repo |
|
670 | def grant_users_group_permission(self, apiuser, repoid, usersgroupid, | |
|
671 | perm): | |||
658 | """ |
|
672 | """ | |
659 | Grant permission for users group on given repository, or update |
|
673 | Grant permission for users group on given repository, or update | |
660 | existing one if found |
|
674 | existing one if found | |
661 |
|
675 | |||
662 |
:param |
|
676 | :param apiuser: | |
663 |
:param |
|
677 | :param repoid: | |
|
678 | :param usersgroupid: | |||
664 | :param perm: |
|
679 | :param perm: | |
665 | """ |
|
680 | """ | |
|
681 | repo = get_repo_or_error(repoid) | |||
|
682 | perm = get_perm_or_error(perm) | |||
|
683 | users_group = get_users_group_or_error(usersgroupid) | |||
666 |
|
684 | |||
667 | try: |
|
685 | try: | |
668 | repo = Repository.get_by_repo_name(repo_name) |
|
686 | RepoModel().grant_users_group_permission(repo=repo, | |
669 | if repo is None: |
|
687 | group_name=users_group, | |
670 | raise JSONRPCError('unknown repository %s' % repo) |
|
|||
671 |
|
||||
672 | user_group = UsersGroup.get_by_group_name(group_name) |
|
|||
673 | if user_group is None: |
|
|||
674 | raise JSONRPCError('unknown users group %s' % user_group) |
|
|||
675 |
|
||||
676 | RepoModel().grant_users_group_permission(repo=repo_name, |
|
|||
677 | group_name=group_name, |
|
|||
678 | perm=perm) |
|
688 | perm=perm) | |
679 |
|
689 | |||
680 | Session.commit() |
|
690 | Session().commit() | |
681 | return dict( |
|
691 | return dict( | |
682 |
msg='Granted perm: %s for group: %s in |
|
692 | msg='Granted perm: `%s` for users group: `%s` in ' | |
683 | perm, group_name, repo_name |
|
693 | 'repo: `%s`' % ( | |
|
694 | perm.permission_name, users_group.users_group_name, | |||
|
695 | repo.repo_name | |||
|
696 | ), | |||
|
697 | success=True | |||
|
698 | ) | |||
|
699 | except Exception: | |||
|
700 | print traceback.format_exc() | |||
|
701 | log.error(traceback.format_exc()) | |||
|
702 | raise JSONRPCError( | |||
|
703 | 'failed to edit permission for users group: `%s` in ' | |||
|
704 | 'repo: `%s`' % ( | |||
|
705 | usersgroupid, repo.repo_name | |||
684 | ) |
|
706 | ) | |
685 | ) |
|
707 | ) | |
|
708 | ||||
|
709 | @HasPermissionAnyDecorator('hg.admin') | |||
|
710 | def revoke_users_group_permission(self, apiuser, repoid, usersgroupid): | |||
|
711 | """ | |||
|
712 | Revoke permission for users group on given repository | |||
|
713 | ||||
|
714 | :param apiuser: | |||
|
715 | :param repoid: | |||
|
716 | :param usersgroupid: | |||
|
717 | """ | |||
|
718 | repo = get_repo_or_error(repoid) | |||
|
719 | users_group = get_users_group_or_error(usersgroupid) | |||
|
720 | ||||
|
721 | try: | |||
|
722 | RepoModel().revoke_users_group_permission(repo=repo, | |||
|
723 | group_name=users_group) | |||
|
724 | ||||
|
725 | Session().commit() | |||
|
726 | return dict( | |||
|
727 | msg='Revoked perm for users group: `%s` in repo: `%s`' % ( | |||
|
728 | users_group.users_group_name, repo.repo_name | |||
|
729 | ), | |||
|
730 | success=True | |||
|
731 | ) | |||
686 | except Exception: |
|
732 | except Exception: | |
687 | log.error(traceback.format_exc()) |
|
733 | log.error(traceback.format_exc()) | |
688 | raise JSONRPCError( |
|
734 | raise JSONRPCError( | |
689 |
'failed to edit permission |
|
735 | 'failed to edit permission for users group: `%s` in ' | |
690 | usersgr=group_name, repo=repo_name |
|
736 | 'repo: `%s`' % ( | |
|
737 | users_group.users_group_name, repo.repo_name | |||
691 | ) |
|
738 | ) | |
692 | ) |
|
739 | ) | |
693 |
|
||||
694 | @HasPermissionAnyDecorator('hg.admin') |
|
|||
695 | def revoke_users_group_permission(self, apiuser, repo_name, group_name): |
|
|||
696 | """ |
|
|||
697 | Revoke permission for users group on given repository |
|
|||
698 |
|
||||
699 | :param repo_name: |
|
|||
700 | :param group_name: |
|
|||
701 | """ |
|
|||
702 |
|
||||
703 | try: |
|
|||
704 | repo = Repository.get_by_repo_name(repo_name) |
|
|||
705 | if repo is None: |
|
|||
706 | raise JSONRPCError('unknown repository %s' % repo) |
|
|||
707 |
|
||||
708 | user_group = UsersGroup.get_by_group_name(group_name) |
|
|||
709 | if user_group is None: |
|
|||
710 | raise JSONRPCError('unknown users group %s' % user_group) |
|
|||
711 |
|
||||
712 | RepoModel().revoke_users_group_permission(repo=repo_name, |
|
|||
713 | group_name=group_name) |
|
|||
714 |
|
||||
715 | Session.commit() |
|
|||
716 | return dict( |
|
|||
717 | msg='Revoked perm for group: %s in repo: %s' % ( |
|
|||
718 | group_name, repo_name |
|
|||
719 | ) |
|
|||
720 | ) |
|
|||
721 | except Exception: |
|
|||
722 | log.error(traceback.format_exc()) |
|
|||
723 | raise JSONRPCError( |
|
|||
724 | 'failed to edit permission %(repo)s for %(usersgr)s' % dict( |
|
|||
725 | usersgr=group_name, repo=repo_name |
|
|||
726 | ) |
|
|||
727 | ) |
|
General Comments 0
You need to be logged in to leave comments.
Login now