##// END OF EJS Templates
get_locks API function draft
marcink -
r3502:7cde75ea beta
parent child Browse files
Show More
@@ -33,7 +33,7 b' from rhodecode.lib.auth import PasswordG'
33 33 HasPermissionAllDecorator, HasPermissionAnyDecorator, \
34 34 HasPermissionAnyApi, HasRepoPermissionAnyApi
35 35 from rhodecode.lib.utils import map_groups, repo2db_mapper
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_int
37 37 from rhodecode.lib import helpers as h
38 38 from rhodecode.model.meta import Session
39 39 from rhodecode.model.scm import ScmModel
@@ -42,6 +42,7 b' from rhodecode.model.user import UserMod'
42 42 from rhodecode.model.users_group import UserGroupModel
43 43 from rhodecode.model.permission import PermissionModel
44 44 from rhodecode.model.db import Repository, RhodeCodeSetting, UserIpMap
45 from rhodecode.lib.compat import json
45 46
46 47 log = logging.getLogger(__name__)
47 48
@@ -274,7 +275,7 b' class ApiController(JSONRPCController):'
274 275 return ('Repo `%s` locked by `%s`. Locked=`True`. '
275 276 'Locked since: `%s`'
276 277 % (repo.repo_name, user.username,
277 h.fmt_date(time_to_datetime(time_))))
278 json.dumps(time_to_datetime(time_))))
278 279
279 280 else:
280 281 locked = str2bool(locked)
@@ -292,6 +293,44 b' class ApiController(JSONRPCController):'
292 293 'Error occurred locking repository `%s`' % repo.repo_name
293 294 )
294 295
296 def get_locks(self, apiuser, userid=Optional(OAttr('apiuser'))):
297 """
298 Get all locks for given userid, if
299 this command is runned by non-admin account userid is set to user
300 who is calling this method, thus returning locks for himself
301
302 :param apiuser:
303 :param userid:
304 """
305 if HasPermissionAnyApi('hg.admin')(user=apiuser):
306 pass
307 else:
308 #make sure normal user does not pass someone else userid,
309 #he is not allowed to do that
310 if not isinstance(userid, Optional) and userid != apiuser.user_id:
311 raise JSONRPCError(
312 'userid is not the same as your user'
313 )
314 ret = []
315 if isinstance(userid, Optional):
316 user = None
317 else:
318 user = get_user_or_error(userid)
319
320 #show all locks
321 for r in Repository.getAll():
322 userid, time_ = r.locked
323 if time_:
324 _api_data = r.get_api_data()
325 # if we use userfilter just show the locks for this user
326 if user:
327 if safe_int(userid) == user.user_id:
328 ret.append(_api_data)
329 else:
330 ret.append(_api_data)
331
332 return ret
333
295 334 @HasPermissionAllDecorator('hg.admin')
296 335 def show_ip(self, apiuser, userid):
297 336 """
@@ -47,7 +47,7 b' from rhodecode.lib.vcs.utils.lazy import'
47 47 from rhodecode.lib.vcs.backends.base import EmptyChangeset
48 48
49 49 from rhodecode.lib.utils2 import str2bool, safe_str, get_changeset_safe, \
50 safe_unicode, remove_suffix, remove_prefix
50 safe_unicode, remove_suffix, remove_prefix, time_to_datetime
51 51 from rhodecode.lib.compat import json
52 52 from rhodecode.lib.caching_query import FromCache
53 53
@@ -972,7 +972,11 b' class Repository(Base, BaseModel):'
972 972 enable_statistics=repo.enable_statistics,
973 973 enable_locking=repo.enable_locking,
974 974 enable_downloads=repo.enable_downloads,
975 last_changeset=repo.changeset_cache
975 last_changeset=repo.changeset_cache,
976 locked_by=User.get(self.locked[0]).get_api_data() \
977 if self.locked[0] else None,
978 locked_date=time_to_datetime(self.locked[1]) \
979 if self.locked[1] else None
976 980 )
977 981 rc_config = RhodeCodeSetting.get_app_settings()
978 982 repository_fields = str2bool(rc_config.get('rhodecode_repository_fields'))
@@ -371,9 +371,8 b' class BaseTestApi(object):'
371 371 self._compare_ok(id_, expected, given=response.body)
372 372
373 373 def test_api_lock_repo_lock_optional_locked(self):
374 from rhodecode.lib import helpers
375 374 from rhodecode.lib.utils2 import time_to_datetime
376 _locked_since = helpers.fmt_date(time_to_datetime(Repository\
375 _locked_since = json.dumps(time_to_datetime(Repository\
377 376 .get_by_repo_name(self.REPO).locked[1]))
378 377 id_, params = _build_data(self.apikey, 'lock',
379 378 repoid=self.REPO)
@@ -393,6 +392,32 b' class BaseTestApi(object):'
393 392 expected = 'Error occurred locking repository `%s`' % self.REPO
394 393 self._compare_error(id_, expected, given=response.body)
395 394
395 def test_api_get_locks_regular_user(self):
396 id_, params = _build_data(self.apikey_regular, 'get_locks')
397 response = api_call(self, params)
398 expected = []
399 self._compare_ok(id_, expected, given=response.body)
400
401 def test_api_get_locks_with_userid_regular_user(self):
402 id_, params = _build_data(self.apikey_regular, 'get_locks',
403 userid=TEST_USER_ADMIN_LOGIN)
404 response = api_call(self, params)
405 expected = 'userid is not the same as your user'
406 self._compare_error(id_, expected, given=response.body)
407
408 def test_api_get_locks(self):
409 id_, params = _build_data(self.apikey, 'get_locks')
410 response = api_call(self, params)
411 expected = []
412 self._compare_ok(id_, expected, given=response.body)
413
414 def test_api_get_locks_with_userid(self):
415 id_, params = _build_data(self.apikey, 'get_locks',
416 userid=TEST_USER_REGULAR_LOGIN)
417 response = api_call(self, params)
418 expected = []
419 self._compare_ok(id_, expected, given=response.body)
420
396 421 def test_api_create_existing_user(self):
397 422 id_, params = _build_data(self.apikey, 'create_user',
398 423 username=TEST_USER_ADMIN_LOGIN,
General Comments 0
You need to be logged in to leave comments. Login now