annotate rhodecode/controllers/api/api.py @ 3730:e42e1d4e1c47 beta

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