Show More
@@ -1,374 +1,422 | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | """ | |
|
3 | rhodecode.controllers.api | |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
|
5 | ||
|
6 | API controller for RhodeCode | |
|
7 | ||
|
8 | :created_on: Aug 20, 2011 | |
|
9 | :author: marcink | |
|
10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |
|
11 | :license: GPLv3, see COPYING for more details. | |
|
12 | """ | |
|
13 | # This program is free software; you can redistribute it and/or | |
|
14 | # modify it under the terms of the GNU General Public License | |
|
15 | # as published by the Free Software Foundation; version 2 | |
|
16 | # of the License or (at your opinion) any later version of the license. | |
|
17 | # | |
|
18 | # This program is distributed in the hope that it will be useful, | |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
21 | # GNU General Public License for more details. | |
|
22 | # | |
|
23 | # You should have received a copy of the GNU General Public License | |
|
24 | # along with this program; if not, write to the Free Software | |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
|
26 | # MA 02110-1301, USA. | |
|
27 | ||
|
1 | 28 | import traceback |
|
2 | 29 | import logging |
|
3 | 30 | |
|
4 | 31 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError |
|
5 | 32 | from rhodecode.lib.auth import HasPermissionAllDecorator, \ |
|
6 | 33 | HasPermissionAnyDecorator |
|
7 | 34 | from rhodecode.model.scm import ScmModel |
|
8 | 35 | |
|
9 | 36 | from rhodecode.model.db import User, UsersGroup, Group, Repository |
|
10 | 37 | from rhodecode.model.repo import RepoModel |
|
11 | 38 | from rhodecode.model.user import UserModel |
|
12 | 39 | from rhodecode.model.repo_permission import RepositoryPermissionModel |
|
13 | 40 | from rhodecode.model.users_group import UsersGroupModel |
|
14 | 41 | from rhodecode.model import users_group |
|
15 | 42 | from rhodecode.model.repos_group import ReposGroupModel |
|
16 | 43 | from sqlalchemy.orm.exc import NoResultFound |
|
17 | 44 | |
|
45 | ||
|
18 | 46 | log = logging.getLogger(__name__) |
|
19 | 47 | |
|
20 | 48 | |
|
21 | 49 | class ApiController(JSONRPCController): |
|
22 | 50 | """ |
|
23 | 51 | API Controller |
|
24 | 52 | |
|
25 | 53 | |
|
26 | 54 | Each method needs to have USER as argument this is then based on given |
|
27 | 55 | API_KEY propagated as instance of user object |
|
28 | 56 | |
|
29 | 57 | Preferably this should be first argument also |
|
30 | 58 | |
|
31 | 59 | |
|
32 | 60 | Each function should also **raise** JSONRPCError for any |
|
33 | 61 | errors that happens |
|
34 | 62 | |
|
35 | 63 | """ |
|
36 | 64 | |
|
37 | 65 | @HasPermissionAllDecorator('hg.admin') |
|
38 | 66 | def pull(self, apiuser, repo): |
|
39 | 67 | """ |
|
40 | 68 | Dispatch pull action on given repo |
|
41 | 69 | |
|
42 | 70 | |
|
43 | 71 | :param user: |
|
44 | 72 | :param repo: |
|
45 | 73 | """ |
|
46 | 74 | |
|
47 | 75 | if Repository.is_valid(repo) is False: |
|
48 | 76 | raise JSONRPCError('Unknown repo "%s"' % repo) |
|
49 | 77 | |
|
50 | 78 | try: |
|
51 | 79 | ScmModel().pull_changes(repo, self.rhodecode_user.username) |
|
52 | 80 | return 'Pulled from %s' % repo |
|
53 | 81 | except Exception: |
|
54 | 82 | raise JSONRPCError('Unable to pull changes from "%s"' % repo) |
|
55 | 83 | |
|
56 | 84 | @HasPermissionAllDecorator('hg.admin') |
|
57 | 85 | def get_user(self, apiuser, username): |
|
58 | 86 | """" |
|
59 | 87 | Get a user by username |
|
60 | 88 | |
|
61 | :param apiuser | |
|
62 | :param username | |
|
89 | :param apiuser: | |
|
90 | :param username: | |
|
63 | 91 | """ |
|
64 | 92 | |
|
65 | 93 | user = User.get_by_username(username) |
|
66 | 94 | if not user: |
|
67 | 95 | return None |
|
68 | 96 | |
|
69 |
return dict( |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 | email=user.email, | |
|
74 |
|
|
|
75 |
|
|
|
76 | ldap=user.ldap_dn) | |
|
97 | return dict( | |
|
98 | id=user.user_id, | |
|
99 | username=user.username, | |
|
100 | firstname=user.name, | |
|
101 | lastname=user.lastname, | |
|
102 | email=user.email, | |
|
103 | active=user.active, | |
|
104 | admin=user.admin, | |
|
105 | ldap=user.ldap_dn | |
|
106 | ) | |
|
77 | 107 | |
|
78 | 108 | @HasPermissionAllDecorator('hg.admin') |
|
79 | 109 | def get_users(self, apiuser): |
|
80 | 110 | """" |
|
81 | 111 | Get all users |
|
82 | 112 | |
|
83 | :param apiuser | |
|
113 | :param apiuser: | |
|
84 | 114 | """ |
|
85 | 115 | |
|
86 | 116 | result = [] |
|
87 | 117 | for user in User.getAll(): |
|
88 |
result.append( |
|
|
89 | username=user.username, | |
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
118 | result.append( | |
|
119 | dict( | |
|
120 | id=user.user_id, | |
|
121 | username=user.username, | |
|
122 | firstname=user.name, | |
|
123 | lastname=user.lastname, | |
|
124 | email=user.email, | |
|
125 | active=user.active, | |
|
126 | admin=user.admin, | |
|
127 | ldap=user.ldap_dn | |
|
128 | ) | |
|
129 | ) | |
|
96 | 130 | return result |
|
97 | 131 | |
|
98 | 132 | @HasPermissionAllDecorator('hg.admin') |
|
99 | 133 | def create_user(self, apiuser, username, password, firstname, |
|
100 | 134 | lastname, email, active=True, admin=False, ldap_dn=None): |
|
101 | 135 | """ |
|
102 | 136 | Create new user |
|
103 | 137 | |
|
104 | 138 | :param apiuser: |
|
105 | 139 | :param username: |
|
106 | 140 | :param password: |
|
107 | 141 | :param name: |
|
108 | 142 | :param lastname: |
|
109 | 143 | :param email: |
|
110 | 144 | :param active: |
|
111 | 145 | :param admin: |
|
112 | 146 | :param ldap_dn: |
|
113 | 147 | """ |
|
114 | 148 | |
|
115 |
if |
|
|
149 | if User.get_by_username(username): | |
|
116 | 150 | raise JSONRPCError("user %s already exist" % username) |
|
117 | 151 | |
|
118 | 152 | try: |
|
119 | 153 | form_data = dict(username=username, |
|
120 | 154 | password=password, |
|
121 | 155 | active=active, |
|
122 | 156 | admin=admin, |
|
123 | 157 | name=firstname, |
|
124 | 158 | lastname=lastname, |
|
125 | 159 | email=email, |
|
126 | 160 | ldap_dn=ldap_dn) |
|
127 | 161 | UserModel().create_ldap(username, password, ldap_dn, form_data) |
|
128 | 162 | return dict(msg='created new user %s' % username) |
|
129 | 163 | except Exception: |
|
130 | 164 | log.error(traceback.format_exc()) |
|
131 | 165 | raise JSONRPCError('failed to create user %s' % username) |
|
132 | 166 | |
|
133 | 167 | @HasPermissionAllDecorator('hg.admin') |
|
134 | 168 | def get_users_group(self, apiuser, group_name): |
|
135 | 169 | """" |
|
136 | 170 | Get users group by name |
|
137 | 171 | |
|
138 | :param apiuser | |
|
139 | :param group_name | |
|
172 | :param apiuser: | |
|
173 | :param group_name: | |
|
140 | 174 | """ |
|
141 | 175 | |
|
142 | 176 | users_group = UsersGroup.get_by_group_name(group_name) |
|
143 | 177 | if not users_group: |
|
144 | 178 | return None |
|
145 | 179 | |
|
146 | 180 | members = [] |
|
147 | 181 | for user in users_group.members: |
|
148 | 182 | user = user.user |
|
149 | 183 | members.append(dict(id=user.user_id, |
|
150 | 184 | username=user.username, |
|
151 | 185 | firstname=user.name, |
|
152 | 186 | lastname=user.lastname, |
|
153 | 187 | email=user.email, |
|
154 | 188 | active=user.active, |
|
155 | 189 | admin=user.admin, |
|
156 | 190 | ldap=user.ldap_dn)) |
|
157 | 191 | |
|
158 | 192 | return dict(id=users_group.users_group_id, |
|
159 | 193 | name=users_group.users_group_name, |
|
160 | 194 | active=users_group.users_group_active, |
|
161 | 195 | members=members) |
|
162 | 196 | |
|
163 | 197 | @HasPermissionAllDecorator('hg.admin') |
|
164 | 198 | def get_users_groups(self, apiuser): |
|
165 | 199 | """" |
|
166 | 200 | Get all users groups |
|
167 | 201 | |
|
168 | :param apiuser | |
|
202 | :param apiuser: | |
|
169 | 203 | """ |
|
170 | 204 | |
|
171 | 205 | result = [] |
|
172 | 206 | for users_group in UsersGroup.getAll(): |
|
173 | 207 | members = [] |
|
174 | 208 | for user in users_group.members: |
|
175 | 209 | user = user.user |
|
176 | 210 | members.append(dict(id=user.user_id, |
|
177 | 211 | username=user.username, |
|
178 | 212 | firstname=user.name, |
|
179 | 213 | lastname=user.lastname, |
|
180 | 214 | email=user.email, |
|
181 | 215 | active=user.active, |
|
182 | 216 | admin=user.admin, |
|
183 | 217 | ldap=user.ldap_dn)) |
|
184 | 218 | |
|
185 | 219 | result.append(dict(id=users_group.users_group_id, |
|
186 | 220 | name=users_group.users_group_name, |
|
187 | 221 | active=users_group.users_group_active, |
|
188 | 222 | members=members)) |
|
189 | 223 | return result |
|
190 | 224 | |
|
191 | 225 | @HasPermissionAllDecorator('hg.admin') |
|
192 | 226 | def create_users_group(self, apiuser, name, active=True): |
|
193 | 227 | """ |
|
194 | 228 | Creates an new usergroup |
|
195 | 229 | |
|
196 | 230 | :param name: |
|
197 | 231 | :param active: |
|
198 | 232 | """ |
|
199 | 233 | |
|
200 | 234 | if self.get_users_group(apiuser, name): |
|
201 | 235 | raise JSONRPCError("users group %s already exist" % name) |
|
202 | 236 | |
|
203 | 237 | try: |
|
204 | 238 | form_data = dict(users_group_name=name, |
|
205 | 239 | users_group_active=active) |
|
206 | 240 | ug = UsersGroup.create(form_data) |
|
207 | 241 | return dict(id=ug.users_group_id, |
|
208 | 242 | msg='created new users group %s' % name) |
|
209 | 243 | except Exception: |
|
210 | 244 | log.error(traceback.format_exc()) |
|
211 | 245 | raise JSONRPCError('failed to create group %s' % name) |
|
212 | 246 | |
|
213 | 247 | @HasPermissionAllDecorator('hg.admin') |
|
214 | 248 | def add_user_to_users_group(self, apiuser, group_name, user_name): |
|
215 | 249 | """" |
|
216 | 250 | Add a user to a group |
|
217 | 251 | |
|
218 | 252 | :param apiuser |
|
219 | 253 | :param group_name |
|
220 | 254 | :param user_name |
|
221 | 255 | """ |
|
222 | 256 | |
|
223 | 257 | try: |
|
224 | 258 | users_group = UsersGroup.get_by_group_name(group_name) |
|
225 | 259 | if not users_group: |
|
226 | 260 | raise JSONRPCError('unknown users group %s' % group_name) |
|
227 | 261 | |
|
228 | 262 | try: |
|
229 | 263 | user = User.get_by_username(user_name) |
|
230 | 264 | except NoResultFound: |
|
231 | 265 | raise JSONRPCError('unknown user %s' % user_name) |
|
232 | 266 | |
|
233 | 267 | ugm = UsersGroupModel().add_user_to_group(users_group, user) |
|
234 | 268 | |
|
235 | 269 | return dict(id=ugm.users_group_member_id, |
|
236 | 270 | msg='created new users group member') |
|
237 | 271 | except Exception: |
|
238 | 272 | log.error(traceback.format_exc()) |
|
239 | 273 | raise JSONRPCError('failed to create users group member') |
|
240 | 274 | |
|
241 | 275 | @HasPermissionAnyDecorator('hg.admin') |
|
242 |
def get_repo(self, apiuser, |
|
|
276 | def get_repo(self, apiuser, name): | |
|
243 | 277 | """" |
|
244 | 278 | Get repository by name |
|
245 | 279 | |
|
246 | 280 | :param apiuser |
|
247 | 281 | :param repo_name |
|
248 | 282 | """ |
|
249 | 283 | |
|
250 | 284 | try: |
|
251 |
repo = Repository.get_by_repo_name( |
|
|
285 | repo = Repository.get_by_repo_name(name) | |
|
252 | 286 | except NoResultFound: |
|
253 | 287 | return None |
|
254 | 288 | |
|
255 | 289 | members = [] |
|
256 | 290 | for user in repo.repo_to_perm: |
|
257 | 291 | perm = user.permission.permission_name |
|
258 | 292 | user = user.user |
|
259 |
members.append( |
|
|
260 | id=user.user_id, | |
|
261 | username=user.username, | |
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 | permission=perm)) | |
|
293 | members.append( | |
|
294 | dict( | |
|
295 | type_="user", | |
|
296 | id=user.user_id, | |
|
297 | username=user.username, | |
|
298 | firstname=user.name, | |
|
299 | lastname=user.lastname, | |
|
300 | email=user.email, | |
|
301 | active=user.active, | |
|
302 | admin=user.admin, | |
|
303 | ldap=user.ldap_dn, | |
|
304 | permission=perm | |
|
305 | ) | |
|
306 | ) | |
|
269 | 307 | for users_group in repo.users_group_to_perm: |
|
270 | 308 | perm = users_group.permission.permission_name |
|
271 | 309 | users_group = users_group.users_group |
|
272 |
members.append( |
|
|
273 | id=users_group.users_group_id, | |
|
274 |
|
|
|
275 |
|
|
|
276 | permission=perm)) | |
|
310 | members.append( | |
|
311 | dict( | |
|
312 | type_="users_group", | |
|
313 | id=users_group.users_group_id, | |
|
314 | name=users_group.users_group_name, | |
|
315 | active=users_group.users_group_active, | |
|
316 | permission=perm | |
|
317 | ) | |
|
318 | ) | |
|
277 | 319 | |
|
278 |
return dict( |
|
|
279 |
|
|
|
280 |
|
|
|
281 | description=repo.description, | |
|
282 | members=members) | |
|
320 | return dict( | |
|
321 | id=repo.repo_id, | |
|
322 | name=repo.repo_name, | |
|
323 | type=repo.repo_type, | |
|
324 | description=repo.description, | |
|
325 | members=members | |
|
326 | ) | |
|
283 | 327 | |
|
284 | 328 | @HasPermissionAnyDecorator('hg.admin') |
|
285 | 329 | def get_repos(self, apiuser): |
|
286 | 330 | """" |
|
287 | 331 | Get all repositories |
|
288 | 332 | |
|
289 | 333 | :param apiuser |
|
290 | 334 | """ |
|
291 | 335 | |
|
292 | 336 | result = [] |
|
293 | 337 | for repository in Repository.getAll(): |
|
294 |
result.append( |
|
|
295 | name=repository.repo_name, | |
|
296 |
|
|
|
297 |
|
|
|
338 | result.append( | |
|
339 | dict( | |
|
340 | id=repository.repo_id, | |
|
341 | name=repository.repo_name, | |
|
342 | type=repository.repo_type, | |
|
343 | description=repository.description | |
|
344 | ) | |
|
345 | ) | |
|
298 | 346 | return result |
|
299 | 347 | |
|
300 | 348 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
301 | 349 | def create_repo(self, apiuser, name, owner_name, description='', |
|
302 | 350 | repo_type='hg', private=False): |
|
303 | 351 | """ |
|
304 | 352 | Create a repository |
|
305 | 353 | |
|
306 | 354 | :param apiuser |
|
307 | 355 | :param name |
|
308 | 356 | :param description |
|
309 | 357 | :param type |
|
310 | 358 | :param private |
|
311 | 359 | :param owner_name |
|
312 | 360 | """ |
|
313 | 361 | |
|
314 | 362 | try: |
|
315 | 363 | try: |
|
316 | 364 | owner = User.get_by_username(owner_name) |
|
317 | 365 | except NoResultFound: |
|
318 | 366 | raise JSONRPCError('unknown user %s' % owner) |
|
319 | 367 | |
|
320 | 368 | if self.get_repo(apiuser, name): |
|
321 | 369 | raise JSONRPCError("repo %s already exist" % name) |
|
322 | 370 | |
|
323 | 371 | groups = name.split('/') |
|
324 | 372 | real_name = groups[-1] |
|
325 | 373 | groups = groups[:-1] |
|
326 | 374 | parent_id = None |
|
327 | 375 | for g in groups: |
|
328 | 376 | group = Group.get_by_group_name(g) |
|
329 | 377 | if not group: |
|
330 | 378 | group = ReposGroupModel().create(dict(group_name=g, |
|
331 | 379 | group_description='', |
|
332 | 380 | group_parent_id=parent_id)) |
|
333 | 381 | parent_id = group.group_id |
|
334 | 382 | |
|
335 | 383 | RepoModel().create(dict(repo_name=real_name, |
|
336 | 384 | repo_name_full=name, |
|
337 | 385 | description=description, |
|
338 | 386 | private=private, |
|
339 | 387 | repo_type=repo_type, |
|
340 | 388 | repo_group=parent_id, |
|
341 | 389 | clone_uri=None), owner) |
|
342 | 390 | except Exception: |
|
343 | 391 | log.error(traceback.format_exc()) |
|
344 | 392 | raise JSONRPCError('failed to create repository %s' % name) |
|
345 | 393 | |
|
346 | 394 | @HasPermissionAnyDecorator('hg.admin') |
|
347 | 395 | def add_user_to_repo(self, apiuser, repo_name, user_name, perm): |
|
348 | 396 | """ |
|
349 | 397 | Add permission for a user to a repository |
|
350 | 398 | |
|
351 | 399 | :param apiuser |
|
352 | 400 | :param repo_name |
|
353 | 401 | :param user_name |
|
354 | 402 | :param perm |
|
355 | 403 | """ |
|
356 | 404 | |
|
357 | 405 | try: |
|
358 | 406 | try: |
|
359 | 407 | repo = Repository.get_by_repo_name(repo_name) |
|
360 | 408 | except NoResultFound: |
|
361 | 409 | raise JSONRPCError('unknown repository %s' % repo) |
|
362 | 410 | |
|
363 | 411 | try: |
|
364 | 412 | user = User.get_by_username(user_name) |
|
365 | 413 | except NoResultFound: |
|
366 | 414 | raise JSONRPCError('unknown user %s' % user) |
|
367 | 415 | |
|
368 | 416 | RepositoryPermissionModel()\ |
|
369 | 417 | .update_or_delete_user_permission(repo, user, perm) |
|
370 | 418 | except Exception: |
|
371 | 419 | log.error(traceback.format_exc()) |
|
372 | 420 | raise JSONRPCError('failed to edit permission %(repo)s for %(user)s' |
|
373 | 421 | % dict(user=user_name, repo=repo_name)) |
|
374 | 422 |
General Comments 0
You need to be logged in to leave comments.
Login now