##// END OF EJS Templates
caches: ensure we don't use non-ascii characters in cache keys....
marcink -
r1749:8c35f24d default
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,1973 +1,1973 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 authentication and permission libraries
23 23 """
24 24
25 25 import os
26 26 import inspect
27 27 import collections
28 28 import fnmatch
29 29 import hashlib
30 30 import itertools
31 31 import logging
32 32 import random
33 33 import traceback
34 34 from functools import wraps
35 35
36 36 import ipaddress
37 37 from pyramid.httpexceptions import HTTPForbidden, HTTPFound
38 38 from pylons import url, request
39 39 from pylons.controllers.util import abort, redirect
40 40 from pylons.i18n.translation import _
41 41 from sqlalchemy.orm.exc import ObjectDeletedError
42 42 from sqlalchemy.orm import joinedload
43 43 from zope.cachedescriptors.property import Lazy as LazyProperty
44 44
45 45 import rhodecode
46 46 from rhodecode.model import meta
47 47 from rhodecode.model.meta import Session
48 48 from rhodecode.model.user import UserModel
49 49 from rhodecode.model.db import (
50 50 User, Repository, Permission, UserToPerm, UserGroupToPerm, UserGroupMember,
51 51 UserIpMap, UserApiKeys, RepoGroup)
52 52 from rhodecode.lib import caches
53 53 from rhodecode.lib.utils2 import safe_unicode, aslist, safe_str, md5
54 54 from rhodecode.lib.utils import (
55 55 get_repo_slug, get_repo_group_slug, get_user_group_slug)
56 56 from rhodecode.lib.caching_query import FromCache
57 57
58 58
59 59 if rhodecode.is_unix:
60 60 import bcrypt
61 61
62 62 log = logging.getLogger(__name__)
63 63
64 64 csrf_token_key = "csrf_token"
65 65
66 66
67 67 class PasswordGenerator(object):
68 68 """
69 69 This is a simple class for generating password from different sets of
70 70 characters
71 71 usage::
72 72
73 73 passwd_gen = PasswordGenerator()
74 74 #print 8-letter password containing only big and small letters
75 75 of alphabet
76 76 passwd_gen.gen_password(8, passwd_gen.ALPHABETS_BIG_SMALL)
77 77 """
78 78 ALPHABETS_NUM = r'''1234567890'''
79 79 ALPHABETS_SMALL = r'''qwertyuiopasdfghjklzxcvbnm'''
80 80 ALPHABETS_BIG = r'''QWERTYUIOPASDFGHJKLZXCVBNM'''
81 81 ALPHABETS_SPECIAL = r'''`-=[]\;',./~!@#$%^&*()_+{}|:"<>?'''
82 82 ALPHABETS_FULL = ALPHABETS_BIG + ALPHABETS_SMALL \
83 83 + ALPHABETS_NUM + ALPHABETS_SPECIAL
84 84 ALPHABETS_ALPHANUM = ALPHABETS_BIG + ALPHABETS_SMALL + ALPHABETS_NUM
85 85 ALPHABETS_BIG_SMALL = ALPHABETS_BIG + ALPHABETS_SMALL
86 86 ALPHABETS_ALPHANUM_BIG = ALPHABETS_BIG + ALPHABETS_NUM
87 87 ALPHABETS_ALPHANUM_SMALL = ALPHABETS_SMALL + ALPHABETS_NUM
88 88
89 89 def __init__(self, passwd=''):
90 90 self.passwd = passwd
91 91
92 92 def gen_password(self, length, type_=None):
93 93 if type_ is None:
94 94 type_ = self.ALPHABETS_FULL
95 95 self.passwd = ''.join([random.choice(type_) for _ in xrange(length)])
96 96 return self.passwd
97 97
98 98
99 99 class _RhodeCodeCryptoBase(object):
100 100 ENC_PREF = None
101 101
102 102 def hash_create(self, str_):
103 103 """
104 104 hash the string using
105 105
106 106 :param str_: password to hash
107 107 """
108 108 raise NotImplementedError
109 109
110 110 def hash_check_with_upgrade(self, password, hashed):
111 111 """
112 112 Returns tuple in which first element is boolean that states that
113 113 given password matches it's hashed version, and the second is new hash
114 114 of the password, in case this password should be migrated to new
115 115 cipher.
116 116 """
117 117 checked_hash = self.hash_check(password, hashed)
118 118 return checked_hash, None
119 119
120 120 def hash_check(self, password, hashed):
121 121 """
122 122 Checks matching password with it's hashed value.
123 123
124 124 :param password: password
125 125 :param hashed: password in hashed form
126 126 """
127 127 raise NotImplementedError
128 128
129 129 def _assert_bytes(self, value):
130 130 """
131 131 Passing in an `unicode` object can lead to hard to detect issues
132 132 if passwords contain non-ascii characters. Doing a type check
133 133 during runtime, so that such mistakes are detected early on.
134 134 """
135 135 if not isinstance(value, str):
136 136 raise TypeError(
137 137 "Bytestring required as input, got %r." % (value, ))
138 138
139 139
140 140 class _RhodeCodeCryptoBCrypt(_RhodeCodeCryptoBase):
141 141 ENC_PREF = ('$2a$10', '$2b$10')
142 142
143 143 def hash_create(self, str_):
144 144 self._assert_bytes(str_)
145 145 return bcrypt.hashpw(str_, bcrypt.gensalt(10))
146 146
147 147 def hash_check_with_upgrade(self, password, hashed):
148 148 """
149 149 Returns tuple in which first element is boolean that states that
150 150 given password matches it's hashed version, and the second is new hash
151 151 of the password, in case this password should be migrated to new
152 152 cipher.
153 153
154 154 This implements special upgrade logic which works like that:
155 155 - check if the given password == bcrypted hash, if yes then we
156 156 properly used password and it was already in bcrypt. Proceed
157 157 without any changes
158 158 - if bcrypt hash check is not working try with sha256. If hash compare
159 159 is ok, it means we using correct but old hashed password. indicate
160 160 hash change and proceed
161 161 """
162 162
163 163 new_hash = None
164 164
165 165 # regular pw check
166 166 password_match_bcrypt = self.hash_check(password, hashed)
167 167
168 168 # now we want to know if the password was maybe from sha256
169 169 # basically calling _RhodeCodeCryptoSha256().hash_check()
170 170 if not password_match_bcrypt:
171 171 if _RhodeCodeCryptoSha256().hash_check(password, hashed):
172 172 new_hash = self.hash_create(password) # make new bcrypt hash
173 173 password_match_bcrypt = True
174 174
175 175 return password_match_bcrypt, new_hash
176 176
177 177 def hash_check(self, password, hashed):
178 178 """
179 179 Checks matching password with it's hashed value.
180 180
181 181 :param password: password
182 182 :param hashed: password in hashed form
183 183 """
184 184 self._assert_bytes(password)
185 185 try:
186 186 return bcrypt.hashpw(password, hashed) == hashed
187 187 except ValueError as e:
188 188 # we're having a invalid salt here probably, we should not crash
189 189 # just return with False as it would be a wrong password.
190 190 log.debug('Failed to check password hash using bcrypt %s',
191 191 safe_str(e))
192 192
193 193 return False
194 194
195 195
196 196 class _RhodeCodeCryptoSha256(_RhodeCodeCryptoBase):
197 197 ENC_PREF = '_'
198 198
199 199 def hash_create(self, str_):
200 200 self._assert_bytes(str_)
201 201 return hashlib.sha256(str_).hexdigest()
202 202
203 203 def hash_check(self, password, hashed):
204 204 """
205 205 Checks matching password with it's hashed value.
206 206
207 207 :param password: password
208 208 :param hashed: password in hashed form
209 209 """
210 210 self._assert_bytes(password)
211 211 return hashlib.sha256(password).hexdigest() == hashed
212 212
213 213
214 214 class _RhodeCodeCryptoMd5(_RhodeCodeCryptoBase):
215 215 ENC_PREF = '_'
216 216
217 217 def hash_create(self, str_):
218 218 self._assert_bytes(str_)
219 219 return hashlib.md5(str_).hexdigest()
220 220
221 221 def hash_check(self, password, hashed):
222 222 """
223 223 Checks matching password with it's hashed value.
224 224
225 225 :param password: password
226 226 :param hashed: password in hashed form
227 227 """
228 228 self._assert_bytes(password)
229 229 return hashlib.md5(password).hexdigest() == hashed
230 230
231 231
232 232 def crypto_backend():
233 233 """
234 234 Return the matching crypto backend.
235 235
236 236 Selection is based on if we run tests or not, we pick md5 backend to run
237 237 tests faster since BCRYPT is expensive to calculate
238 238 """
239 239 if rhodecode.is_test:
240 240 RhodeCodeCrypto = _RhodeCodeCryptoMd5()
241 241 else:
242 242 RhodeCodeCrypto = _RhodeCodeCryptoBCrypt()
243 243
244 244 return RhodeCodeCrypto
245 245
246 246
247 247 def get_crypt_password(password):
248 248 """
249 249 Create the hash of `password` with the active crypto backend.
250 250
251 251 :param password: The cleartext password.
252 252 :type password: unicode
253 253 """
254 254 password = safe_str(password)
255 255 return crypto_backend().hash_create(password)
256 256
257 257
258 258 def check_password(password, hashed):
259 259 """
260 260 Check if the value in `password` matches the hash in `hashed`.
261 261
262 262 :param password: The cleartext password.
263 263 :type password: unicode
264 264
265 265 :param hashed: The expected hashed version of the password.
266 266 :type hashed: The hash has to be passed in in text representation.
267 267 """
268 268 password = safe_str(password)
269 269 return crypto_backend().hash_check(password, hashed)
270 270
271 271
272 272 def generate_auth_token(data, salt=None):
273 273 """
274 274 Generates API KEY from given string
275 275 """
276 276
277 277 if salt is None:
278 278 salt = os.urandom(16)
279 279 return hashlib.sha1(safe_str(data) + salt).hexdigest()
280 280
281 281
282 282 class CookieStoreWrapper(object):
283 283
284 284 def __init__(self, cookie_store):
285 285 self.cookie_store = cookie_store
286 286
287 287 def __repr__(self):
288 288 return 'CookieStore<%s>' % (self.cookie_store)
289 289
290 290 def get(self, key, other=None):
291 291 if isinstance(self.cookie_store, dict):
292 292 return self.cookie_store.get(key, other)
293 293 elif isinstance(self.cookie_store, AuthUser):
294 294 return self.cookie_store.__dict__.get(key, other)
295 295
296 296
297 297 def _cached_perms_data(user_id, scope, user_is_admin,
298 298 user_inherit_default_permissions, explicit, algo):
299 299
300 300 permissions = PermissionCalculator(
301 301 user_id, scope, user_is_admin, user_inherit_default_permissions,
302 302 explicit, algo)
303 303 return permissions.calculate()
304 304
305 305 class PermOrigin:
306 306 ADMIN = 'superadmin'
307 307
308 308 REPO_USER = 'user:%s'
309 309 REPO_USERGROUP = 'usergroup:%s'
310 310 REPO_OWNER = 'repo.owner'
311 311 REPO_DEFAULT = 'repo.default'
312 312 REPO_PRIVATE = 'repo.private'
313 313
314 314 REPOGROUP_USER = 'user:%s'
315 315 REPOGROUP_USERGROUP = 'usergroup:%s'
316 316 REPOGROUP_OWNER = 'group.owner'
317 317 REPOGROUP_DEFAULT = 'group.default'
318 318
319 319 USERGROUP_USER = 'user:%s'
320 320 USERGROUP_USERGROUP = 'usergroup:%s'
321 321 USERGROUP_OWNER = 'usergroup.owner'
322 322 USERGROUP_DEFAULT = 'usergroup.default'
323 323
324 324
325 325 class PermOriginDict(dict):
326 326 """
327 327 A special dict used for tracking permissions along with their origins.
328 328
329 329 `__setitem__` has been overridden to expect a tuple(perm, origin)
330 330 `__getitem__` will return only the perm
331 331 `.perm_origin_stack` will return the stack of (perm, origin) set per key
332 332
333 333 >>> perms = PermOriginDict()
334 334 >>> perms['resource'] = 'read', 'default'
335 335 >>> perms['resource']
336 336 'read'
337 337 >>> perms['resource'] = 'write', 'admin'
338 338 >>> perms['resource']
339 339 'write'
340 340 >>> perms.perm_origin_stack
341 341 {'resource': [('read', 'default'), ('write', 'admin')]}
342 342 """
343 343
344 344
345 345 def __init__(self, *args, **kw):
346 346 dict.__init__(self, *args, **kw)
347 347 self.perm_origin_stack = {}
348 348
349 349 def __setitem__(self, key, (perm, origin)):
350 350 self.perm_origin_stack.setdefault(key, []).append((perm, origin))
351 351 dict.__setitem__(self, key, perm)
352 352
353 353
354 354 class PermissionCalculator(object):
355 355
356 356 def __init__(
357 357 self, user_id, scope, user_is_admin,
358 358 user_inherit_default_permissions, explicit, algo):
359 359 self.user_id = user_id
360 360 self.user_is_admin = user_is_admin
361 361 self.inherit_default_permissions = user_inherit_default_permissions
362 362 self.explicit = explicit
363 363 self.algo = algo
364 364
365 365 scope = scope or {}
366 366 self.scope_repo_id = scope.get('repo_id')
367 367 self.scope_repo_group_id = scope.get('repo_group_id')
368 368 self.scope_user_group_id = scope.get('user_group_id')
369 369
370 370 self.default_user_id = User.get_default_user(cache=True).user_id
371 371
372 372 self.permissions_repositories = PermOriginDict()
373 373 self.permissions_repository_groups = PermOriginDict()
374 374 self.permissions_user_groups = PermOriginDict()
375 375 self.permissions_global = set()
376 376
377 377 self.default_repo_perms = Permission.get_default_repo_perms(
378 378 self.default_user_id, self.scope_repo_id)
379 379 self.default_repo_groups_perms = Permission.get_default_group_perms(
380 380 self.default_user_id, self.scope_repo_group_id)
381 381 self.default_user_group_perms = \
382 382 Permission.get_default_user_group_perms(
383 383 self.default_user_id, self.scope_user_group_id)
384 384
385 385 def calculate(self):
386 386 if self.user_is_admin:
387 387 return self._admin_permissions()
388 388
389 389 self._calculate_global_default_permissions()
390 390 self._calculate_global_permissions()
391 391 self._calculate_default_permissions()
392 392 self._calculate_repository_permissions()
393 393 self._calculate_repository_group_permissions()
394 394 self._calculate_user_group_permissions()
395 395 return self._permission_structure()
396 396
397 397 def _admin_permissions(self):
398 398 """
399 399 admin user have all default rights for repositories
400 400 and groups set to admin
401 401 """
402 402 self.permissions_global.add('hg.admin')
403 403 self.permissions_global.add('hg.create.write_on_repogroup.true')
404 404
405 405 # repositories
406 406 for perm in self.default_repo_perms:
407 407 r_k = perm.UserRepoToPerm.repository.repo_name
408 408 p = 'repository.admin'
409 409 self.permissions_repositories[r_k] = p, PermOrigin.ADMIN
410 410
411 411 # repository groups
412 412 for perm in self.default_repo_groups_perms:
413 413 rg_k = perm.UserRepoGroupToPerm.group.group_name
414 414 p = 'group.admin'
415 415 self.permissions_repository_groups[rg_k] = p, PermOrigin.ADMIN
416 416
417 417 # user groups
418 418 for perm in self.default_user_group_perms:
419 419 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
420 420 p = 'usergroup.admin'
421 421 self.permissions_user_groups[u_k] = p, PermOrigin.ADMIN
422 422
423 423 return self._permission_structure()
424 424
425 425 def _calculate_global_default_permissions(self):
426 426 """
427 427 global permissions taken from the default user
428 428 """
429 429 default_global_perms = UserToPerm.query()\
430 430 .filter(UserToPerm.user_id == self.default_user_id)\
431 431 .options(joinedload(UserToPerm.permission))
432 432
433 433 for perm in default_global_perms:
434 434 self.permissions_global.add(perm.permission.permission_name)
435 435
436 436 def _calculate_global_permissions(self):
437 437 """
438 438 Set global system permissions with user permissions or permissions
439 439 taken from the user groups of the current user.
440 440
441 441 The permissions include repo creating, repo group creating, forking
442 442 etc.
443 443 """
444 444
445 445 # now we read the defined permissions and overwrite what we have set
446 446 # before those can be configured from groups or users explicitly.
447 447
448 448 # TODO: johbo: This seems to be out of sync, find out the reason
449 449 # for the comment below and update it.
450 450
451 451 # In case we want to extend this list we should be always in sync with
452 452 # User.DEFAULT_USER_PERMISSIONS definitions
453 453 _configurable = frozenset([
454 454 'hg.fork.none', 'hg.fork.repository',
455 455 'hg.create.none', 'hg.create.repository',
456 456 'hg.usergroup.create.false', 'hg.usergroup.create.true',
457 457 'hg.repogroup.create.false', 'hg.repogroup.create.true',
458 458 'hg.create.write_on_repogroup.false',
459 459 'hg.create.write_on_repogroup.true',
460 460 'hg.inherit_default_perms.false', 'hg.inherit_default_perms.true'
461 461 ])
462 462
463 463 # USER GROUPS comes first user group global permissions
464 464 user_perms_from_users_groups = Session().query(UserGroupToPerm)\
465 465 .options(joinedload(UserGroupToPerm.permission))\
466 466 .join((UserGroupMember, UserGroupToPerm.users_group_id ==
467 467 UserGroupMember.users_group_id))\
468 468 .filter(UserGroupMember.user_id == self.user_id)\
469 469 .order_by(UserGroupToPerm.users_group_id)\
470 470 .all()
471 471
472 472 # need to group here by groups since user can be in more than
473 473 # one group, so we get all groups
474 474 _explicit_grouped_perms = [
475 475 [x, list(y)] for x, y in
476 476 itertools.groupby(user_perms_from_users_groups,
477 477 lambda _x: _x.users_group)]
478 478
479 479 for gr, perms in _explicit_grouped_perms:
480 480 # since user can be in multiple groups iterate over them and
481 481 # select the lowest permissions first (more explicit)
482 482 # TODO: marcink: do this^^
483 483
484 484 # group doesn't inherit default permissions so we actually set them
485 485 if not gr.inherit_default_permissions:
486 486 # NEED TO IGNORE all previously set configurable permissions
487 487 # and replace them with explicitly set from this user
488 488 # group permissions
489 489 self.permissions_global = self.permissions_global.difference(
490 490 _configurable)
491 491 for perm in perms:
492 492 self.permissions_global.add(perm.permission.permission_name)
493 493
494 494 # user explicit global permissions
495 495 user_perms = Session().query(UserToPerm)\
496 496 .options(joinedload(UserToPerm.permission))\
497 497 .filter(UserToPerm.user_id == self.user_id).all()
498 498
499 499 if not self.inherit_default_permissions:
500 500 # NEED TO IGNORE all configurable permissions and
501 501 # replace them with explicitly set from this user permissions
502 502 self.permissions_global = self.permissions_global.difference(
503 503 _configurable)
504 504 for perm in user_perms:
505 505 self.permissions_global.add(perm.permission.permission_name)
506 506
507 507 def _calculate_default_permissions(self):
508 508 """
509 509 Set default user permissions for repositories, repository groups
510 510 taken from the default user.
511 511
512 512 Calculate inheritance of object permissions based on what we have now
513 513 in GLOBAL permissions. We check if .false is in GLOBAL since this is
514 514 explicitly set. Inherit is the opposite of .false being there.
515 515
516 516 .. note::
517 517
518 518 the syntax is little bit odd but what we need to check here is
519 519 the opposite of .false permission being in the list so even for
520 520 inconsistent state when both .true/.false is there
521 521 .false is more important
522 522
523 523 """
524 524 user_inherit_object_permissions = not ('hg.inherit_default_perms.false'
525 525 in self.permissions_global)
526 526
527 527 # defaults for repositories, taken from `default` user permissions
528 528 # on given repo
529 529 for perm in self.default_repo_perms:
530 530 r_k = perm.UserRepoToPerm.repository.repo_name
531 531 o = PermOrigin.REPO_DEFAULT
532 532 if perm.Repository.private and not (
533 533 perm.Repository.user_id == self.user_id):
534 534 # disable defaults for private repos,
535 535 p = 'repository.none'
536 536 o = PermOrigin.REPO_PRIVATE
537 537 elif perm.Repository.user_id == self.user_id:
538 538 # set admin if owner
539 539 p = 'repository.admin'
540 540 o = PermOrigin.REPO_OWNER
541 541 else:
542 542 p = perm.Permission.permission_name
543 543 # if we decide this user isn't inheriting permissions from
544 544 # default user we set him to .none so only explicit
545 545 # permissions work
546 546 if not user_inherit_object_permissions:
547 547 p = 'repository.none'
548 548 self.permissions_repositories[r_k] = p, o
549 549
550 550 # defaults for repository groups taken from `default` user permission
551 551 # on given group
552 552 for perm in self.default_repo_groups_perms:
553 553 rg_k = perm.UserRepoGroupToPerm.group.group_name
554 554 o = PermOrigin.REPOGROUP_DEFAULT
555 555 if perm.RepoGroup.user_id == self.user_id:
556 556 # set admin if owner
557 557 p = 'group.admin'
558 558 o = PermOrigin.REPOGROUP_OWNER
559 559 else:
560 560 p = perm.Permission.permission_name
561 561
562 562 # if we decide this user isn't inheriting permissions from default
563 563 # user we set him to .none so only explicit permissions work
564 564 if not user_inherit_object_permissions:
565 565 p = 'group.none'
566 566 self.permissions_repository_groups[rg_k] = p, o
567 567
568 568 # defaults for user groups taken from `default` user permission
569 569 # on given user group
570 570 for perm in self.default_user_group_perms:
571 571 u_k = perm.UserUserGroupToPerm.user_group.users_group_name
572 572 o = PermOrigin.USERGROUP_DEFAULT
573 573 if perm.UserGroup.user_id == self.user_id:
574 574 # set admin if owner
575 575 p = 'usergroup.admin'
576 576 o = PermOrigin.USERGROUP_OWNER
577 577 else:
578 578 p = perm.Permission.permission_name
579 579
580 580 # if we decide this user isn't inheriting permissions from default
581 581 # user we set him to .none so only explicit permissions work
582 582 if not user_inherit_object_permissions:
583 583 p = 'usergroup.none'
584 584 self.permissions_user_groups[u_k] = p, o
585 585
586 586 def _calculate_repository_permissions(self):
587 587 """
588 588 Repository permissions for the current user.
589 589
590 590 Check if the user is part of user groups for this repository and
591 591 fill in the permission from it. `_choose_permission` decides of which
592 592 permission should be selected based on selected method.
593 593 """
594 594
595 595 # user group for repositories permissions
596 596 user_repo_perms_from_user_group = Permission\
597 597 .get_default_repo_perms_from_user_group(
598 598 self.user_id, self.scope_repo_id)
599 599
600 600 multiple_counter = collections.defaultdict(int)
601 601 for perm in user_repo_perms_from_user_group:
602 602 r_k = perm.UserGroupRepoToPerm.repository.repo_name
603 603 ug_k = perm.UserGroupRepoToPerm.users_group.users_group_name
604 604 multiple_counter[r_k] += 1
605 605 p = perm.Permission.permission_name
606 606 o = PermOrigin.REPO_USERGROUP % ug_k
607 607
608 608 if perm.Repository.user_id == self.user_id:
609 609 # set admin if owner
610 610 p = 'repository.admin'
611 611 o = PermOrigin.REPO_OWNER
612 612 else:
613 613 if multiple_counter[r_k] > 1:
614 614 cur_perm = self.permissions_repositories[r_k]
615 615 p = self._choose_permission(p, cur_perm)
616 616 self.permissions_repositories[r_k] = p, o
617 617
618 618 # user explicit permissions for repositories, overrides any specified
619 619 # by the group permission
620 620 user_repo_perms = Permission.get_default_repo_perms(
621 621 self.user_id, self.scope_repo_id)
622 622 for perm in user_repo_perms:
623 623 r_k = perm.UserRepoToPerm.repository.repo_name
624 624 o = PermOrigin.REPO_USER % perm.UserRepoToPerm.user.username
625 625 # set admin if owner
626 626 if perm.Repository.user_id == self.user_id:
627 627 p = 'repository.admin'
628 628 o = PermOrigin.REPO_OWNER
629 629 else:
630 630 p = perm.Permission.permission_name
631 631 if not self.explicit:
632 632 cur_perm = self.permissions_repositories.get(
633 633 r_k, 'repository.none')
634 634 p = self._choose_permission(p, cur_perm)
635 635 self.permissions_repositories[r_k] = p, o
636 636
637 637 def _calculate_repository_group_permissions(self):
638 638 """
639 639 Repository group permissions for the current user.
640 640
641 641 Check if the user is part of user groups for repository groups and
642 642 fill in the permissions from it. `_choose_permmission` decides of which
643 643 permission should be selected based on selected method.
644 644 """
645 645 # user group for repo groups permissions
646 646 user_repo_group_perms_from_user_group = Permission\
647 647 .get_default_group_perms_from_user_group(
648 648 self.user_id, self.scope_repo_group_id)
649 649
650 650 multiple_counter = collections.defaultdict(int)
651 651 for perm in user_repo_group_perms_from_user_group:
652 652 g_k = perm.UserGroupRepoGroupToPerm.group.group_name
653 653 ug_k = perm.UserGroupRepoGroupToPerm.users_group.users_group_name
654 654 o = PermOrigin.REPOGROUP_USERGROUP % ug_k
655 655 multiple_counter[g_k] += 1
656 656 p = perm.Permission.permission_name
657 657 if perm.RepoGroup.user_id == self.user_id:
658 658 # set admin if owner, even for member of other user group
659 659 p = 'group.admin'
660 660 o = PermOrigin.REPOGROUP_OWNER
661 661 else:
662 662 if multiple_counter[g_k] > 1:
663 663 cur_perm = self.permissions_repository_groups[g_k]
664 664 p = self._choose_permission(p, cur_perm)
665 665 self.permissions_repository_groups[g_k] = p, o
666 666
667 667 # user explicit permissions for repository groups
668 668 user_repo_groups_perms = Permission.get_default_group_perms(
669 669 self.user_id, self.scope_repo_group_id)
670 670 for perm in user_repo_groups_perms:
671 671 rg_k = perm.UserRepoGroupToPerm.group.group_name
672 672 u_k = perm.UserRepoGroupToPerm.user.username
673 673 o = PermOrigin.REPOGROUP_USER % u_k
674 674
675 675 if perm.RepoGroup.user_id == self.user_id:
676 676 # set admin if owner
677 677 p = 'group.admin'
678 678 o = PermOrigin.REPOGROUP_OWNER
679 679 else:
680 680 p = perm.Permission.permission_name
681 681 if not self.explicit:
682 682 cur_perm = self.permissions_repository_groups.get(
683 683 rg_k, 'group.none')
684 684 p = self._choose_permission(p, cur_perm)
685 685 self.permissions_repository_groups[rg_k] = p, o
686 686
687 687 def _calculate_user_group_permissions(self):
688 688 """
689 689 User group permissions for the current user.
690 690 """
691 691 # user group for user group permissions
692 692 user_group_from_user_group = Permission\
693 693 .get_default_user_group_perms_from_user_group(
694 694 self.user_id, self.scope_user_group_id)
695 695
696 696 multiple_counter = collections.defaultdict(int)
697 697 for perm in user_group_from_user_group:
698 698 g_k = perm.UserGroupUserGroupToPerm\
699 699 .target_user_group.users_group_name
700 700 u_k = perm.UserGroupUserGroupToPerm\
701 701 .user_group.users_group_name
702 702 o = PermOrigin.USERGROUP_USERGROUP % u_k
703 703 multiple_counter[g_k] += 1
704 704 p = perm.Permission.permission_name
705 705
706 706 if perm.UserGroup.user_id == self.user_id:
707 707 # set admin if owner, even for member of other user group
708 708 p = 'usergroup.admin'
709 709 o = PermOrigin.USERGROUP_OWNER
710 710 else:
711 711 if multiple_counter[g_k] > 1:
712 712 cur_perm = self.permissions_user_groups[g_k]
713 713 p = self._choose_permission(p, cur_perm)
714 714 self.permissions_user_groups[g_k] = p, o
715 715
716 716 # user explicit permission for user groups
717 717 user_user_groups_perms = Permission.get_default_user_group_perms(
718 718 self.user_id, self.scope_user_group_id)
719 719 for perm in user_user_groups_perms:
720 720 ug_k = perm.UserUserGroupToPerm.user_group.users_group_name
721 721 u_k = perm.UserUserGroupToPerm.user.username
722 722 o = PermOrigin.USERGROUP_USER % u_k
723 723
724 724 if perm.UserGroup.user_id == self.user_id:
725 725 # set admin if owner
726 726 p = 'usergroup.admin'
727 727 o = PermOrigin.USERGROUP_OWNER
728 728 else:
729 729 p = perm.Permission.permission_name
730 730 if not self.explicit:
731 731 cur_perm = self.permissions_user_groups.get(
732 732 ug_k, 'usergroup.none')
733 733 p = self._choose_permission(p, cur_perm)
734 734 self.permissions_user_groups[ug_k] = p, o
735 735
736 736 def _choose_permission(self, new_perm, cur_perm):
737 737 new_perm_val = Permission.PERM_WEIGHTS[new_perm]
738 738 cur_perm_val = Permission.PERM_WEIGHTS[cur_perm]
739 739 if self.algo == 'higherwin':
740 740 if new_perm_val > cur_perm_val:
741 741 return new_perm
742 742 return cur_perm
743 743 elif self.algo == 'lowerwin':
744 744 if new_perm_val < cur_perm_val:
745 745 return new_perm
746 746 return cur_perm
747 747
748 748 def _permission_structure(self):
749 749 return {
750 750 'global': self.permissions_global,
751 751 'repositories': self.permissions_repositories,
752 752 'repositories_groups': self.permissions_repository_groups,
753 753 'user_groups': self.permissions_user_groups,
754 754 }
755 755
756 756
757 757 def allowed_auth_token_access(controller_name, whitelist=None, auth_token=None):
758 758 """
759 759 Check if given controller_name is in whitelist of auth token access
760 760 """
761 761 if not whitelist:
762 762 from rhodecode import CONFIG
763 763 whitelist = aslist(
764 764 CONFIG.get('api_access_controllers_whitelist'), sep=',')
765 765 log.debug(
766 766 'Allowed controllers for AUTH TOKEN access: %s' % (whitelist,))
767 767
768 768 auth_token_access_valid = False
769 769 for entry in whitelist:
770 770 if fnmatch.fnmatch(controller_name, entry):
771 771 auth_token_access_valid = True
772 772 break
773 773
774 774 if auth_token_access_valid:
775 775 log.debug('controller:%s matches entry in whitelist'
776 776 % (controller_name,))
777 777 else:
778 778 msg = ('controller: %s does *NOT* match any entry in whitelist'
779 779 % (controller_name,))
780 780 if auth_token:
781 781 # if we use auth token key and don't have access it's a warning
782 782 log.warning(msg)
783 783 else:
784 784 log.debug(msg)
785 785
786 786 return auth_token_access_valid
787 787
788 788
789 789 class AuthUser(object):
790 790 """
791 791 A simple object that handles all attributes of user in RhodeCode
792 792
793 793 It does lookup based on API key,given user, or user present in session
794 794 Then it fills all required information for such user. It also checks if
795 795 anonymous access is enabled and if so, it returns default user as logged in
796 796 """
797 797 GLOBAL_PERMS = [x[0] for x in Permission.PERMS]
798 798
799 799 def __init__(self, user_id=None, api_key=None, username=None, ip_addr=None):
800 800
801 801 self.user_id = user_id
802 802 self._api_key = api_key
803 803
804 804 self.api_key = None
805 805 self.feed_token = ''
806 806 self.username = username
807 807 self.ip_addr = ip_addr
808 808 self.name = ''
809 809 self.lastname = ''
810 810 self.email = ''
811 811 self.is_authenticated = False
812 812 self.admin = False
813 813 self.inherit_default_permissions = False
814 814 self.password = ''
815 815
816 816 self.anonymous_user = None # propagated on propagate_data
817 817 self.propagate_data()
818 818 self._instance = None
819 819 self._permissions_scoped_cache = {} # used to bind scoped calculation
820 820
821 821 @LazyProperty
822 822 def permissions(self):
823 823 return self.get_perms(user=self, cache=False)
824 824
825 825 def permissions_with_scope(self, scope):
826 826 """
827 827 Call the get_perms function with scoped data. The scope in that function
828 828 narrows the SQL calls to the given ID of objects resulting in fetching
829 829 Just particular permission we want to obtain. If scope is an empty dict
830 830 then it basically narrows the scope to GLOBAL permissions only.
831 831
832 832 :param scope: dict
833 833 """
834 834 if 'repo_name' in scope:
835 835 obj = Repository.get_by_repo_name(scope['repo_name'])
836 836 if obj:
837 837 scope['repo_id'] = obj.repo_id
838 838 _scope = {
839 839 'repo_id': -1,
840 840 'user_group_id': -1,
841 841 'repo_group_id': -1,
842 842 }
843 843 _scope.update(scope)
844 844 cache_key = "_".join(map(safe_str, reduce(lambda a, b: a+b,
845 845 _scope.items())))
846 846 if cache_key not in self._permissions_scoped_cache:
847 847 # store in cache to mimic how the @LazyProperty works,
848 848 # the difference here is that we use the unique key calculated
849 849 # from params and values
850 850 res = self.get_perms(user=self, cache=False, scope=_scope)
851 851 self._permissions_scoped_cache[cache_key] = res
852 852 return self._permissions_scoped_cache[cache_key]
853 853
854 854 def get_instance(self):
855 855 return User.get(self.user_id)
856 856
857 857 def update_lastactivity(self):
858 858 if self.user_id:
859 859 User.get(self.user_id).update_lastactivity()
860 860
861 861 def propagate_data(self):
862 862 """
863 863 Fills in user data and propagates values to this instance. Maps fetched
864 864 user attributes to this class instance attributes
865 865 """
866 866 log.debug('starting data propagation for new potential AuthUser')
867 867 user_model = UserModel()
868 868 anon_user = self.anonymous_user = User.get_default_user(cache=True)
869 869 is_user_loaded = False
870 870
871 871 # lookup by userid
872 872 if self.user_id is not None and self.user_id != anon_user.user_id:
873 873 log.debug('Trying Auth User lookup by USER ID: `%s`' % self.user_id)
874 874 is_user_loaded = user_model.fill_data(self, user_id=self.user_id)
875 875
876 876 # try go get user by api key
877 877 elif self._api_key and self._api_key != anon_user.api_key:
878 878 log.debug('Trying Auth User lookup by API KEY: `%s`' % self._api_key)
879 879 is_user_loaded = user_model.fill_data(self, api_key=self._api_key)
880 880
881 881 # lookup by username
882 882 elif self.username:
883 883 log.debug('Trying Auth User lookup by USER NAME: `%s`' % self.username)
884 884 is_user_loaded = user_model.fill_data(self, username=self.username)
885 885 else:
886 886 log.debug('No data in %s that could been used to log in' % self)
887 887
888 888 if not is_user_loaded:
889 889 log.debug('Failed to load user. Fallback to default user')
890 890 # if we cannot authenticate user try anonymous
891 891 if anon_user.active:
892 892 user_model.fill_data(self, user_id=anon_user.user_id)
893 893 # then we set this user is logged in
894 894 self.is_authenticated = True
895 895 else:
896 896 # in case of disabled anonymous user we reset some of the
897 897 # parameters so such user is "corrupted", skipping the fill_data
898 898 for attr in ['user_id', 'username', 'admin', 'active']:
899 899 setattr(self, attr, None)
900 900 self.is_authenticated = False
901 901
902 902 if not self.username:
903 903 self.username = 'None'
904 904
905 905 log.debug('Auth User is now %s' % self)
906 906
907 907 def get_perms(self, user, scope=None, explicit=True, algo='higherwin',
908 908 cache=False):
909 909 """
910 910 Fills user permission attribute with permissions taken from database
911 911 works for permissions given for repositories, and for permissions that
912 912 are granted to groups
913 913
914 914 :param user: instance of User object from database
915 915 :param explicit: In case there are permissions both for user and a group
916 916 that user is part of, explicit flag will defiine if user will
917 917 explicitly override permissions from group, if it's False it will
918 918 make decision based on the algo
919 919 :param algo: algorithm to decide what permission should be choose if
920 920 it's multiple defined, eg user in two different groups. It also
921 921 decides if explicit flag is turned off how to specify the permission
922 922 for case when user is in a group + have defined separate permission
923 923 """
924 924 user_id = user.user_id
925 925 user_is_admin = user.is_admin
926 926
927 927 # inheritance of global permissions like create repo/fork repo etc
928 928 user_inherit_default_permissions = user.inherit_default_permissions
929 929
930 930 log.debug('Computing PERMISSION tree for scope %s' % (scope, ))
931 931 compute = caches.conditional_cache(
932 932 'short_term', 'cache_desc',
933 933 condition=cache, func=_cached_perms_data)
934 934 result = compute(user_id, scope, user_is_admin,
935 935 user_inherit_default_permissions, explicit, algo)
936 936
937 937 result_repr = []
938 938 for k in result:
939 939 result_repr.append((k, len(result[k])))
940 940
941 941 log.debug('PERMISSION tree computed %s' % (result_repr,))
942 942 return result
943 943
944 944 @property
945 945 def is_default(self):
946 946 return self.username == User.DEFAULT_USER
947 947
948 948 @property
949 949 def is_admin(self):
950 950 return self.admin
951 951
952 952 @property
953 953 def is_user_object(self):
954 954 return self.user_id is not None
955 955
956 956 @property
957 957 def repositories_admin(self):
958 958 """
959 959 Returns list of repositories you're an admin of
960 960 """
961 961 return [
962 962 x[0] for x in self.permissions['repositories'].iteritems()
963 963 if x[1] == 'repository.admin']
964 964
965 965 @property
966 966 def repository_groups_admin(self):
967 967 """
968 968 Returns list of repository groups you're an admin of
969 969 """
970 970 return [
971 971 x[0] for x in self.permissions['repositories_groups'].iteritems()
972 972 if x[1] == 'group.admin']
973 973
974 974 @property
975 975 def user_groups_admin(self):
976 976 """
977 977 Returns list of user groups you're an admin of
978 978 """
979 979 return [
980 980 x[0] for x in self.permissions['user_groups'].iteritems()
981 981 if x[1] == 'usergroup.admin']
982 982
983 983 @property
984 984 def ip_allowed(self):
985 985 """
986 986 Checks if ip_addr used in constructor is allowed from defined list of
987 987 allowed ip_addresses for user
988 988
989 989 :returns: boolean, True if ip is in allowed ip range
990 990 """
991 991 # check IP
992 992 inherit = self.inherit_default_permissions
993 993 return AuthUser.check_ip_allowed(self.user_id, self.ip_addr,
994 994 inherit_from_default=inherit)
995 995 @property
996 996 def personal_repo_group(self):
997 997 return RepoGroup.get_user_personal_repo_group(self.user_id)
998 998
999 999 @classmethod
1000 1000 def check_ip_allowed(cls, user_id, ip_addr, inherit_from_default):
1001 1001 allowed_ips = AuthUser.get_allowed_ips(
1002 1002 user_id, cache=True, inherit_from_default=inherit_from_default)
1003 1003 if check_ip_access(source_ip=ip_addr, allowed_ips=allowed_ips):
1004 1004 log.debug('IP:%s is in range of %s' % (ip_addr, allowed_ips))
1005 1005 return True
1006 1006 else:
1007 1007 log.info('Access for IP:%s forbidden, '
1008 1008 'not in %s' % (ip_addr, allowed_ips))
1009 1009 return False
1010 1010
1011 1011 def __repr__(self):
1012 1012 return "<AuthUser('id:%s[%s] ip:%s auth:%s')>"\
1013 1013 % (self.user_id, self.username, self.ip_addr, self.is_authenticated)
1014 1014
1015 1015 def set_authenticated(self, authenticated=True):
1016 1016 if self.user_id != self.anonymous_user.user_id:
1017 1017 self.is_authenticated = authenticated
1018 1018
1019 1019 def get_cookie_store(self):
1020 1020 return {
1021 1021 'username': self.username,
1022 1022 'password': md5(self.password),
1023 1023 'user_id': self.user_id,
1024 1024 'is_authenticated': self.is_authenticated
1025 1025 }
1026 1026
1027 1027 @classmethod
1028 1028 def from_cookie_store(cls, cookie_store):
1029 1029 """
1030 1030 Creates AuthUser from a cookie store
1031 1031
1032 1032 :param cls:
1033 1033 :param cookie_store:
1034 1034 """
1035 1035 user_id = cookie_store.get('user_id')
1036 1036 username = cookie_store.get('username')
1037 1037 api_key = cookie_store.get('api_key')
1038 1038 return AuthUser(user_id, api_key, username)
1039 1039
1040 1040 @classmethod
1041 1041 def get_allowed_ips(cls, user_id, cache=False, inherit_from_default=False):
1042 1042 _set = set()
1043 1043
1044 1044 if inherit_from_default:
1045 1045 default_ips = UserIpMap.query().filter(
1046 1046 UserIpMap.user == User.get_default_user(cache=True))
1047 1047 if cache:
1048 default_ips = default_ips.options(FromCache("sql_cache_short",
1049 "get_user_ips_default"))
1048 default_ips = default_ips.options(
1049 FromCache("sql_cache_short", "get_user_ips_default"))
1050 1050
1051 1051 # populate from default user
1052 1052 for ip in default_ips:
1053 1053 try:
1054 1054 _set.add(ip.ip_addr)
1055 1055 except ObjectDeletedError:
1056 1056 # since we use heavy caching sometimes it happens that
1057 1057 # we get deleted objects here, we just skip them
1058 1058 pass
1059 1059
1060 1060 user_ips = UserIpMap.query().filter(UserIpMap.user_id == user_id)
1061 1061 if cache:
1062 user_ips = user_ips.options(FromCache("sql_cache_short",
1063 "get_user_ips_%s" % user_id))
1062 user_ips = user_ips.options(
1063 FromCache("sql_cache_short", "get_user_ips_%s" % user_id))
1064 1064
1065 1065 for ip in user_ips:
1066 1066 try:
1067 1067 _set.add(ip.ip_addr)
1068 1068 except ObjectDeletedError:
1069 1069 # since we use heavy caching sometimes it happens that we get
1070 1070 # deleted objects here, we just skip them
1071 1071 pass
1072 1072 return _set or set(['0.0.0.0/0', '::/0'])
1073 1073
1074 1074
1075 1075 def set_available_permissions(config):
1076 1076 """
1077 1077 This function will propagate pylons globals with all available defined
1078 1078 permission given in db. We don't want to check each time from db for new
1079 1079 permissions since adding a new permission also requires application restart
1080 1080 ie. to decorate new views with the newly created permission
1081 1081
1082 1082 :param config: current pylons config instance
1083 1083
1084 1084 """
1085 1085 log.info('getting information about all available permissions')
1086 1086 try:
1087 1087 sa = meta.Session
1088 1088 all_perms = sa.query(Permission).all()
1089 1089 config['available_permissions'] = [x.permission_name for x in all_perms]
1090 1090 except Exception:
1091 1091 log.error(traceback.format_exc())
1092 1092 finally:
1093 1093 meta.Session.remove()
1094 1094
1095 1095
1096 1096 def get_csrf_token(session=None, force_new=False, save_if_missing=True):
1097 1097 """
1098 1098 Return the current authentication token, creating one if one doesn't
1099 1099 already exist and the save_if_missing flag is present.
1100 1100
1101 1101 :param session: pass in the pylons session, else we use the global ones
1102 1102 :param force_new: force to re-generate the token and store it in session
1103 1103 :param save_if_missing: save the newly generated token if it's missing in
1104 1104 session
1105 1105 """
1106 1106 if not session:
1107 1107 from pylons import session
1108 1108
1109 1109 if (csrf_token_key not in session and save_if_missing) or force_new:
1110 1110 token = hashlib.sha1(str(random.getrandbits(128))).hexdigest()
1111 1111 session[csrf_token_key] = token
1112 1112 if hasattr(session, 'save'):
1113 1113 session.save()
1114 1114 return session.get(csrf_token_key)
1115 1115
1116 1116
1117 1117 # CHECK DECORATORS
1118 1118 class CSRFRequired(object):
1119 1119 """
1120 1120 Decorator for authenticating a form
1121 1121
1122 1122 This decorator uses an authorization token stored in the client's
1123 1123 session for prevention of certain Cross-site request forgery (CSRF)
1124 1124 attacks (See
1125 1125 http://en.wikipedia.org/wiki/Cross-site_request_forgery for more
1126 1126 information).
1127 1127
1128 1128 For use with the ``webhelpers.secure_form`` helper functions.
1129 1129
1130 1130 """
1131 1131 def __init__(self, token=csrf_token_key, header='X-CSRF-Token',
1132 1132 except_methods=None):
1133 1133 self.token = token
1134 1134 self.header = header
1135 1135 self.except_methods = except_methods or []
1136 1136
1137 1137 def __call__(self, func):
1138 1138 return get_cython_compat_decorator(self.__wrapper, func)
1139 1139
1140 1140 def _get_csrf(self, _request):
1141 1141 return _request.POST.get(self.token, _request.headers.get(self.header))
1142 1142
1143 1143 def check_csrf(self, _request, cur_token):
1144 1144 supplied_token = self._get_csrf(_request)
1145 1145 return supplied_token and supplied_token == cur_token
1146 1146
1147 1147 def __wrapper(self, func, *fargs, **fkwargs):
1148 1148 if request.method in self.except_methods:
1149 1149 return func(*fargs, **fkwargs)
1150 1150
1151 1151 cur_token = get_csrf_token(save_if_missing=False)
1152 1152 if self.check_csrf(request, cur_token):
1153 1153 if request.POST.get(self.token):
1154 1154 del request.POST[self.token]
1155 1155 return func(*fargs, **fkwargs)
1156 1156 else:
1157 1157 reason = 'token-missing'
1158 1158 supplied_token = self._get_csrf(request)
1159 1159 if supplied_token and cur_token != supplied_token:
1160 1160 reason = 'token-mismatch [%s:%s]' % (cur_token or ''[:6],
1161 1161 supplied_token or ''[:6])
1162 1162
1163 1163 csrf_message = \
1164 1164 ("Cross-site request forgery detected, request denied. See "
1165 1165 "http://en.wikipedia.org/wiki/Cross-site_request_forgery for "
1166 1166 "more information.")
1167 1167 log.warn('Cross-site request forgery detected, request %r DENIED: %s '
1168 1168 'REMOTE_ADDR:%s, HEADERS:%s' % (
1169 1169 request, reason, request.remote_addr, request.headers))
1170 1170
1171 1171 raise HTTPForbidden(explanation=csrf_message)
1172 1172
1173 1173
1174 1174 class LoginRequired(object):
1175 1175 """
1176 1176 Must be logged in to execute this function else
1177 1177 redirect to login page
1178 1178
1179 1179 :param api_access: if enabled this checks only for valid auth token
1180 1180 and grants access based on valid token
1181 1181 """
1182 1182 def __init__(self, auth_token_access=None):
1183 1183 self.auth_token_access = auth_token_access
1184 1184
1185 1185 def __call__(self, func):
1186 1186 return get_cython_compat_decorator(self.__wrapper, func)
1187 1187
1188 1188 def __wrapper(self, func, *fargs, **fkwargs):
1189 1189 from rhodecode.lib import helpers as h
1190 1190 cls = fargs[0]
1191 1191 user = cls._rhodecode_user
1192 1192 loc = "%s:%s" % (cls.__class__.__name__, func.__name__)
1193 1193 log.debug('Starting login restriction checks for user: %s' % (user,))
1194 1194 # check if our IP is allowed
1195 1195 ip_access_valid = True
1196 1196 if not user.ip_allowed:
1197 1197 h.flash(h.literal(_('IP %s not allowed' % (user.ip_addr,))),
1198 1198 category='warning')
1199 1199 ip_access_valid = False
1200 1200
1201 1201 # check if we used an APIKEY and it's a valid one
1202 1202 # defined white-list of controllers which API access will be enabled
1203 1203 _auth_token = request.GET.get(
1204 1204 'auth_token', '') or request.GET.get('api_key', '')
1205 1205 auth_token_access_valid = allowed_auth_token_access(
1206 1206 loc, auth_token=_auth_token)
1207 1207
1208 1208 # explicit controller is enabled or API is in our whitelist
1209 1209 if self.auth_token_access or auth_token_access_valid:
1210 1210 log.debug('Checking AUTH TOKEN access for %s' % (cls,))
1211 1211 db_user = user.get_instance()
1212 1212
1213 1213 if db_user:
1214 1214 if self.auth_token_access:
1215 1215 roles = self.auth_token_access
1216 1216 else:
1217 1217 roles = [UserApiKeys.ROLE_HTTP]
1218 1218 token_match = db_user.authenticate_by_token(
1219 1219 _auth_token, roles=roles)
1220 1220 else:
1221 1221 log.debug('Unable to fetch db instance for auth user: %s', user)
1222 1222 token_match = False
1223 1223
1224 1224 if _auth_token and token_match:
1225 1225 auth_token_access_valid = True
1226 1226 log.debug('AUTH TOKEN ****%s is VALID' % (_auth_token[-4:],))
1227 1227 else:
1228 1228 auth_token_access_valid = False
1229 1229 if not _auth_token:
1230 1230 log.debug("AUTH TOKEN *NOT* present in request")
1231 1231 else:
1232 1232 log.warning(
1233 1233 "AUTH TOKEN ****%s *NOT* valid" % _auth_token[-4:])
1234 1234
1235 1235 log.debug('Checking if %s is authenticated @ %s' % (user.username, loc))
1236 1236 reason = 'RHODECODE_AUTH' if user.is_authenticated \
1237 1237 else 'AUTH_TOKEN_AUTH'
1238 1238
1239 1239 if ip_access_valid and (
1240 1240 user.is_authenticated or auth_token_access_valid):
1241 1241 log.info(
1242 1242 'user %s authenticating with:%s IS authenticated on func %s'
1243 1243 % (user, reason, loc))
1244 1244
1245 1245 # update user data to check last activity
1246 1246 user.update_lastactivity()
1247 1247 Session().commit()
1248 1248 return func(*fargs, **fkwargs)
1249 1249 else:
1250 1250 log.warning(
1251 1251 'user %s authenticating with:%s NOT authenticated on '
1252 1252 'func: %s: IP_ACCESS:%s AUTH_TOKEN_ACCESS:%s'
1253 1253 % (user, reason, loc, ip_access_valid,
1254 1254 auth_token_access_valid))
1255 1255 # we preserve the get PARAM
1256 1256 came_from = request.path_qs
1257 1257 log.debug('redirecting to login page with %s' % (came_from,))
1258 1258 return redirect(
1259 1259 h.route_path('login', _query={'came_from': came_from}))
1260 1260
1261 1261
1262 1262 class NotAnonymous(object):
1263 1263 """
1264 1264 Must be logged in to execute this function else
1265 1265 redirect to login page"""
1266 1266
1267 1267 def __call__(self, func):
1268 1268 return get_cython_compat_decorator(self.__wrapper, func)
1269 1269
1270 1270 def __wrapper(self, func, *fargs, **fkwargs):
1271 1271 import rhodecode.lib.helpers as h
1272 1272 cls = fargs[0]
1273 1273 self.user = cls._rhodecode_user
1274 1274
1275 1275 log.debug('Checking if user is not anonymous @%s' % cls)
1276 1276
1277 1277 anonymous = self.user.username == User.DEFAULT_USER
1278 1278
1279 1279 if anonymous:
1280 1280 came_from = request.path_qs
1281 1281 h.flash(_('You need to be a registered user to '
1282 1282 'perform this action'),
1283 1283 category='warning')
1284 1284 return redirect(
1285 1285 h.route_path('login', _query={'came_from': came_from}))
1286 1286 else:
1287 1287 return func(*fargs, **fkwargs)
1288 1288
1289 1289
1290 1290 class XHRRequired(object):
1291 1291 def __call__(self, func):
1292 1292 return get_cython_compat_decorator(self.__wrapper, func)
1293 1293
1294 1294 def __wrapper(self, func, *fargs, **fkwargs):
1295 1295 log.debug('Checking if request is XMLHttpRequest (XHR)')
1296 1296 xhr_message = 'This is not a valid XMLHttpRequest (XHR) request'
1297 1297 if not request.is_xhr:
1298 1298 abort(400, detail=xhr_message)
1299 1299
1300 1300 return func(*fargs, **fkwargs)
1301 1301
1302 1302
1303 1303 class HasAcceptedRepoType(object):
1304 1304 """
1305 1305 Check if requested repo is within given repo type aliases
1306 1306
1307 1307 TODO: anderson: not sure where to put this decorator
1308 1308 """
1309 1309
1310 1310 def __init__(self, *repo_type_list):
1311 1311 self.repo_type_list = set(repo_type_list)
1312 1312
1313 1313 def __call__(self, func):
1314 1314 return get_cython_compat_decorator(self.__wrapper, func)
1315 1315
1316 1316 def __wrapper(self, func, *fargs, **fkwargs):
1317 1317 import rhodecode.lib.helpers as h
1318 1318 cls = fargs[0]
1319 1319 rhodecode_repo = cls.rhodecode_repo
1320 1320
1321 1321 log.debug('%s checking repo type for %s in %s',
1322 1322 self.__class__.__name__,
1323 1323 rhodecode_repo.alias, self.repo_type_list)
1324 1324
1325 1325 if rhodecode_repo.alias in self.repo_type_list:
1326 1326 return func(*fargs, **fkwargs)
1327 1327 else:
1328 1328 h.flash(h.literal(
1329 1329 _('Action not supported for %s.' % rhodecode_repo.alias)),
1330 1330 category='warning')
1331 1331 return redirect(
1332 1332 url('summary_home', repo_name=cls.rhodecode_db_repo.repo_name))
1333 1333
1334 1334
1335 1335 class PermsDecorator(object):
1336 1336 """
1337 1337 Base class for controller decorators, we extract the current user from
1338 1338 the class itself, which has it stored in base controllers
1339 1339 """
1340 1340
1341 1341 def __init__(self, *required_perms):
1342 1342 self.required_perms = set(required_perms)
1343 1343
1344 1344 def __call__(self, func):
1345 1345 return get_cython_compat_decorator(self.__wrapper, func)
1346 1346
1347 1347 def _get_request(self):
1348 1348 from pyramid.threadlocal import get_current_request
1349 1349 pyramid_request = get_current_request()
1350 1350 if not pyramid_request:
1351 1351 # return global request of pylons in case pyramid isn't available
1352 1352 return request
1353 1353 return pyramid_request
1354 1354
1355 1355 def _get_came_from(self):
1356 1356 _request = self._get_request()
1357 1357
1358 1358 # both pylons/pyramid has this attribute
1359 1359 return _request.path_qs
1360 1360
1361 1361 def __wrapper(self, func, *fargs, **fkwargs):
1362 1362 import rhodecode.lib.helpers as h
1363 1363 cls = fargs[0]
1364 1364 _user = cls._rhodecode_user
1365 1365
1366 1366 log.debug('checking %s permissions %s for %s %s',
1367 1367 self.__class__.__name__, self.required_perms, cls, _user)
1368 1368
1369 1369 if self.check_permissions(_user):
1370 1370 log.debug('Permission granted for %s %s', cls, _user)
1371 1371 return func(*fargs, **fkwargs)
1372 1372
1373 1373 else:
1374 1374 log.debug('Permission denied for %s %s', cls, _user)
1375 1375 anonymous = _user.username == User.DEFAULT_USER
1376 1376
1377 1377 if anonymous:
1378 1378 came_from = self._get_came_from()
1379 1379 h.flash(_('You need to be signed in to view this page'),
1380 1380 category='warning')
1381 1381 raise HTTPFound(
1382 1382 h.route_path('login', _query={'came_from': came_from}))
1383 1383
1384 1384 else:
1385 1385 # redirect with forbidden ret code
1386 1386 raise HTTPForbidden()
1387 1387
1388 1388 def check_permissions(self, user):
1389 1389 """Dummy function for overriding"""
1390 1390 raise NotImplementedError(
1391 1391 'You have to write this function in child class')
1392 1392
1393 1393
1394 1394 class HasPermissionAllDecorator(PermsDecorator):
1395 1395 """
1396 1396 Checks for access permission for all given predicates. All of them
1397 1397 have to be meet in order to fulfill the request
1398 1398 """
1399 1399
1400 1400 def check_permissions(self, user):
1401 1401 perms = user.permissions_with_scope({})
1402 1402 if self.required_perms.issubset(perms['global']):
1403 1403 return True
1404 1404 return False
1405 1405
1406 1406
1407 1407 class HasPermissionAnyDecorator(PermsDecorator):
1408 1408 """
1409 1409 Checks for access permission for any of given predicates. In order to
1410 1410 fulfill the request any of predicates must be meet
1411 1411 """
1412 1412
1413 1413 def check_permissions(self, user):
1414 1414 perms = user.permissions_with_scope({})
1415 1415 if self.required_perms.intersection(perms['global']):
1416 1416 return True
1417 1417 return False
1418 1418
1419 1419
1420 1420 class HasRepoPermissionAllDecorator(PermsDecorator):
1421 1421 """
1422 1422 Checks for access permission for all given predicates for specific
1423 1423 repository. All of them have to be meet in order to fulfill the request
1424 1424 """
1425 1425 def _get_repo_name(self):
1426 1426 _request = self._get_request()
1427 1427 return get_repo_slug(_request)
1428 1428
1429 1429 def check_permissions(self, user):
1430 1430 perms = user.permissions
1431 1431 repo_name = self._get_repo_name()
1432 1432 try:
1433 1433 user_perms = set([perms['repositories'][repo_name]])
1434 1434 except KeyError:
1435 1435 return False
1436 1436 if self.required_perms.issubset(user_perms):
1437 1437 return True
1438 1438 return False
1439 1439
1440 1440
1441 1441 class HasRepoPermissionAnyDecorator(PermsDecorator):
1442 1442 """
1443 1443 Checks for access permission for any of given predicates for specific
1444 1444 repository. In order to fulfill the request any of predicates must be meet
1445 1445 """
1446 1446 def _get_repo_name(self):
1447 1447 _request = self._get_request()
1448 1448 return get_repo_slug(_request)
1449 1449
1450 1450 def check_permissions(self, user):
1451 1451 perms = user.permissions
1452 1452 repo_name = self._get_repo_name()
1453 1453 try:
1454 1454 user_perms = set([perms['repositories'][repo_name]])
1455 1455 except KeyError:
1456 1456 return False
1457 1457
1458 1458 if self.required_perms.intersection(user_perms):
1459 1459 return True
1460 1460 return False
1461 1461
1462 1462
1463 1463 class HasRepoGroupPermissionAllDecorator(PermsDecorator):
1464 1464 """
1465 1465 Checks for access permission for all given predicates for specific
1466 1466 repository group. All of them have to be meet in order to
1467 1467 fulfill the request
1468 1468 """
1469 1469 def _get_repo_group_name(self):
1470 1470 _request = self._get_request()
1471 1471 return get_repo_group_slug(_request)
1472 1472
1473 1473 def check_permissions(self, user):
1474 1474 perms = user.permissions
1475 1475 group_name = self._get_repo_group_name()
1476 1476 try:
1477 1477 user_perms = set([perms['repositories_groups'][group_name]])
1478 1478 except KeyError:
1479 1479 return False
1480 1480
1481 1481 if self.required_perms.issubset(user_perms):
1482 1482 return True
1483 1483 return False
1484 1484
1485 1485
1486 1486 class HasRepoGroupPermissionAnyDecorator(PermsDecorator):
1487 1487 """
1488 1488 Checks for access permission for any of given predicates for specific
1489 1489 repository group. In order to fulfill the request any
1490 1490 of predicates must be met
1491 1491 """
1492 1492 def _get_repo_group_name(self):
1493 1493 _request = self._get_request()
1494 1494 return get_repo_group_slug(_request)
1495 1495
1496 1496 def check_permissions(self, user):
1497 1497 perms = user.permissions
1498 1498 group_name = self._get_repo_group_name()
1499 1499 try:
1500 1500 user_perms = set([perms['repositories_groups'][group_name]])
1501 1501 except KeyError:
1502 1502 return False
1503 1503
1504 1504 if self.required_perms.intersection(user_perms):
1505 1505 return True
1506 1506 return False
1507 1507
1508 1508
1509 1509 class HasUserGroupPermissionAllDecorator(PermsDecorator):
1510 1510 """
1511 1511 Checks for access permission for all given predicates for specific
1512 1512 user group. All of them have to be meet in order to fulfill the request
1513 1513 """
1514 1514 def _get_user_group_name(self):
1515 1515 _request = self._get_request()
1516 1516 return get_user_group_slug(_request)
1517 1517
1518 1518 def check_permissions(self, user):
1519 1519 perms = user.permissions
1520 1520 group_name = self._get_user_group_name()
1521 1521 try:
1522 1522 user_perms = set([perms['user_groups'][group_name]])
1523 1523 except KeyError:
1524 1524 return False
1525 1525
1526 1526 if self.required_perms.issubset(user_perms):
1527 1527 return True
1528 1528 return False
1529 1529
1530 1530
1531 1531 class HasUserGroupPermissionAnyDecorator(PermsDecorator):
1532 1532 """
1533 1533 Checks for access permission for any of given predicates for specific
1534 1534 user group. In order to fulfill the request any of predicates must be meet
1535 1535 """
1536 1536 def _get_user_group_name(self):
1537 1537 _request = self._get_request()
1538 1538 return get_user_group_slug(_request)
1539 1539
1540 1540 def check_permissions(self, user):
1541 1541 perms = user.permissions
1542 1542 group_name = self._get_user_group_name()
1543 1543 try:
1544 1544 user_perms = set([perms['user_groups'][group_name]])
1545 1545 except KeyError:
1546 1546 return False
1547 1547
1548 1548 if self.required_perms.intersection(user_perms):
1549 1549 return True
1550 1550 return False
1551 1551
1552 1552
1553 1553 # CHECK FUNCTIONS
1554 1554 class PermsFunction(object):
1555 1555 """Base function for other check functions"""
1556 1556
1557 1557 def __init__(self, *perms):
1558 1558 self.required_perms = set(perms)
1559 1559 self.repo_name = None
1560 1560 self.repo_group_name = None
1561 1561 self.user_group_name = None
1562 1562
1563 1563 def __bool__(self):
1564 1564 frame = inspect.currentframe()
1565 1565 stack_trace = traceback.format_stack(frame)
1566 1566 log.error('Checking bool value on a class instance of perm '
1567 1567 'function is not allowed: %s' % ''.join(stack_trace))
1568 1568 # rather than throwing errors, here we always return False so if by
1569 1569 # accident someone checks truth for just an instance it will always end
1570 1570 # up in returning False
1571 1571 return False
1572 1572 __nonzero__ = __bool__
1573 1573
1574 1574 def __call__(self, check_location='', user=None):
1575 1575 if not user:
1576 1576 log.debug('Using user attribute from global request')
1577 1577 # TODO: remove this someday,put as user as attribute here
1578 1578 user = request.user
1579 1579
1580 1580 # init auth user if not already given
1581 1581 if not isinstance(user, AuthUser):
1582 1582 log.debug('Wrapping user %s into AuthUser', user)
1583 1583 user = AuthUser(user.user_id)
1584 1584
1585 1585 cls_name = self.__class__.__name__
1586 1586 check_scope = self._get_check_scope(cls_name)
1587 1587 check_location = check_location or 'unspecified location'
1588 1588
1589 1589 log.debug('checking cls:%s %s usr:%s %s @ %s', cls_name,
1590 1590 self.required_perms, user, check_scope, check_location)
1591 1591 if not user:
1592 1592 log.warning('Empty user given for permission check')
1593 1593 return False
1594 1594
1595 1595 if self.check_permissions(user):
1596 1596 log.debug('Permission to repo:`%s` GRANTED for user:`%s` @ %s',
1597 1597 check_scope, user, check_location)
1598 1598 return True
1599 1599
1600 1600 else:
1601 1601 log.debug('Permission to repo:`%s` DENIED for user:`%s` @ %s',
1602 1602 check_scope, user, check_location)
1603 1603 return False
1604 1604
1605 1605 def _get_request(self):
1606 1606 from pyramid.threadlocal import get_current_request
1607 1607 pyramid_request = get_current_request()
1608 1608 if not pyramid_request:
1609 1609 # return global request of pylons incase pyramid one isn't available
1610 1610 return request
1611 1611 return pyramid_request
1612 1612
1613 1613 def _get_check_scope(self, cls_name):
1614 1614 return {
1615 1615 'HasPermissionAll': 'GLOBAL',
1616 1616 'HasPermissionAny': 'GLOBAL',
1617 1617 'HasRepoPermissionAll': 'repo:%s' % self.repo_name,
1618 1618 'HasRepoPermissionAny': 'repo:%s' % self.repo_name,
1619 1619 'HasRepoGroupPermissionAll': 'repo_group:%s' % self.repo_group_name,
1620 1620 'HasRepoGroupPermissionAny': 'repo_group:%s' % self.repo_group_name,
1621 1621 'HasUserGroupPermissionAll': 'user_group:%s' % self.user_group_name,
1622 1622 'HasUserGroupPermissionAny': 'user_group:%s' % self.user_group_name,
1623 1623 }.get(cls_name, '?:%s' % cls_name)
1624 1624
1625 1625 def check_permissions(self, user):
1626 1626 """Dummy function for overriding"""
1627 1627 raise Exception('You have to write this function in child class')
1628 1628
1629 1629
1630 1630 class HasPermissionAll(PermsFunction):
1631 1631 def check_permissions(self, user):
1632 1632 perms = user.permissions_with_scope({})
1633 1633 if self.required_perms.issubset(perms.get('global')):
1634 1634 return True
1635 1635 return False
1636 1636
1637 1637
1638 1638 class HasPermissionAny(PermsFunction):
1639 1639 def check_permissions(self, user):
1640 1640 perms = user.permissions_with_scope({})
1641 1641 if self.required_perms.intersection(perms.get('global')):
1642 1642 return True
1643 1643 return False
1644 1644
1645 1645
1646 1646 class HasRepoPermissionAll(PermsFunction):
1647 1647 def __call__(self, repo_name=None, check_location='', user=None):
1648 1648 self.repo_name = repo_name
1649 1649 return super(HasRepoPermissionAll, self).__call__(check_location, user)
1650 1650
1651 1651 def _get_repo_name(self):
1652 1652 if not self.repo_name:
1653 1653 _request = self._get_request()
1654 1654 self.repo_name = get_repo_slug(_request)
1655 1655 return self.repo_name
1656 1656
1657 1657 def check_permissions(self, user):
1658 1658 self.repo_name = self._get_repo_name()
1659 1659 perms = user.permissions
1660 1660 try:
1661 1661 user_perms = set([perms['repositories'][self.repo_name]])
1662 1662 except KeyError:
1663 1663 return False
1664 1664 if self.required_perms.issubset(user_perms):
1665 1665 return True
1666 1666 return False
1667 1667
1668 1668
1669 1669 class HasRepoPermissionAny(PermsFunction):
1670 1670 def __call__(self, repo_name=None, check_location='', user=None):
1671 1671 self.repo_name = repo_name
1672 1672 return super(HasRepoPermissionAny, self).__call__(check_location, user)
1673 1673
1674 1674 def _get_repo_name(self):
1675 1675 if not self.repo_name:
1676 1676 self.repo_name = get_repo_slug(request)
1677 1677 return self.repo_name
1678 1678
1679 1679 def check_permissions(self, user):
1680 1680 self.repo_name = self._get_repo_name()
1681 1681 perms = user.permissions
1682 1682 try:
1683 1683 user_perms = set([perms['repositories'][self.repo_name]])
1684 1684 except KeyError:
1685 1685 return False
1686 1686 if self.required_perms.intersection(user_perms):
1687 1687 return True
1688 1688 return False
1689 1689
1690 1690
1691 1691 class HasRepoGroupPermissionAny(PermsFunction):
1692 1692 def __call__(self, group_name=None, check_location='', user=None):
1693 1693 self.repo_group_name = group_name
1694 1694 return super(HasRepoGroupPermissionAny, self).__call__(
1695 1695 check_location, user)
1696 1696
1697 1697 def check_permissions(self, user):
1698 1698 perms = user.permissions
1699 1699 try:
1700 1700 user_perms = set(
1701 1701 [perms['repositories_groups'][self.repo_group_name]])
1702 1702 except KeyError:
1703 1703 return False
1704 1704 if self.required_perms.intersection(user_perms):
1705 1705 return True
1706 1706 return False
1707 1707
1708 1708
1709 1709 class HasRepoGroupPermissionAll(PermsFunction):
1710 1710 def __call__(self, group_name=None, check_location='', user=None):
1711 1711 self.repo_group_name = group_name
1712 1712 return super(HasRepoGroupPermissionAll, self).__call__(
1713 1713 check_location, user)
1714 1714
1715 1715 def check_permissions(self, user):
1716 1716 perms = user.permissions
1717 1717 try:
1718 1718 user_perms = set(
1719 1719 [perms['repositories_groups'][self.repo_group_name]])
1720 1720 except KeyError:
1721 1721 return False
1722 1722 if self.required_perms.issubset(user_perms):
1723 1723 return True
1724 1724 return False
1725 1725
1726 1726
1727 1727 class HasUserGroupPermissionAny(PermsFunction):
1728 1728 def __call__(self, user_group_name=None, check_location='', user=None):
1729 1729 self.user_group_name = user_group_name
1730 1730 return super(HasUserGroupPermissionAny, self).__call__(
1731 1731 check_location, user)
1732 1732
1733 1733 def check_permissions(self, user):
1734 1734 perms = user.permissions
1735 1735 try:
1736 1736 user_perms = set([perms['user_groups'][self.user_group_name]])
1737 1737 except KeyError:
1738 1738 return False
1739 1739 if self.required_perms.intersection(user_perms):
1740 1740 return True
1741 1741 return False
1742 1742
1743 1743
1744 1744 class HasUserGroupPermissionAll(PermsFunction):
1745 1745 def __call__(self, user_group_name=None, check_location='', user=None):
1746 1746 self.user_group_name = user_group_name
1747 1747 return super(HasUserGroupPermissionAll, self).__call__(
1748 1748 check_location, user)
1749 1749
1750 1750 def check_permissions(self, user):
1751 1751 perms = user.permissions
1752 1752 try:
1753 1753 user_perms = set([perms['user_groups'][self.user_group_name]])
1754 1754 except KeyError:
1755 1755 return False
1756 1756 if self.required_perms.issubset(user_perms):
1757 1757 return True
1758 1758 return False
1759 1759
1760 1760
1761 1761 # SPECIAL VERSION TO HANDLE MIDDLEWARE AUTH
1762 1762 class HasPermissionAnyMiddleware(object):
1763 1763 def __init__(self, *perms):
1764 1764 self.required_perms = set(perms)
1765 1765
1766 1766 def __call__(self, user, repo_name):
1767 1767 # repo_name MUST be unicode, since we handle keys in permission
1768 1768 # dict by unicode
1769 1769 repo_name = safe_unicode(repo_name)
1770 1770 user = AuthUser(user.user_id)
1771 1771 log.debug(
1772 1772 'Checking VCS protocol permissions %s for user:%s repo:`%s`',
1773 1773 self.required_perms, user, repo_name)
1774 1774
1775 1775 if self.check_permissions(user, repo_name):
1776 1776 log.debug('Permission to repo:`%s` GRANTED for user:%s @ %s',
1777 1777 repo_name, user, 'PermissionMiddleware')
1778 1778 return True
1779 1779
1780 1780 else:
1781 1781 log.debug('Permission to repo:`%s` DENIED for user:%s @ %s',
1782 1782 repo_name, user, 'PermissionMiddleware')
1783 1783 return False
1784 1784
1785 1785 def check_permissions(self, user, repo_name):
1786 1786 perms = user.permissions_with_scope({'repo_name': repo_name})
1787 1787
1788 1788 try:
1789 1789 user_perms = set([perms['repositories'][repo_name]])
1790 1790 except Exception:
1791 1791 log.exception('Error while accessing user permissions')
1792 1792 return False
1793 1793
1794 1794 if self.required_perms.intersection(user_perms):
1795 1795 return True
1796 1796 return False
1797 1797
1798 1798
1799 1799 # SPECIAL VERSION TO HANDLE API AUTH
1800 1800 class _BaseApiPerm(object):
1801 1801 def __init__(self, *perms):
1802 1802 self.required_perms = set(perms)
1803 1803
1804 1804 def __call__(self, check_location=None, user=None, repo_name=None,
1805 1805 group_name=None, user_group_name=None):
1806 1806 cls_name = self.__class__.__name__
1807 1807 check_scope = 'global:%s' % (self.required_perms,)
1808 1808 if repo_name:
1809 1809 check_scope += ', repo_name:%s' % (repo_name,)
1810 1810
1811 1811 if group_name:
1812 1812 check_scope += ', repo_group_name:%s' % (group_name,)
1813 1813
1814 1814 if user_group_name:
1815 1815 check_scope += ', user_group_name:%s' % (user_group_name,)
1816 1816
1817 1817 log.debug(
1818 1818 'checking cls:%s %s %s @ %s'
1819 1819 % (cls_name, self.required_perms, check_scope, check_location))
1820 1820 if not user:
1821 1821 log.debug('Empty User passed into arguments')
1822 1822 return False
1823 1823
1824 1824 # process user
1825 1825 if not isinstance(user, AuthUser):
1826 1826 user = AuthUser(user.user_id)
1827 1827 if not check_location:
1828 1828 check_location = 'unspecified'
1829 1829 if self.check_permissions(user.permissions, repo_name, group_name,
1830 1830 user_group_name):
1831 1831 log.debug('Permission to repo:`%s` GRANTED for user:`%s` @ %s',
1832 1832 check_scope, user, check_location)
1833 1833 return True
1834 1834
1835 1835 else:
1836 1836 log.debug('Permission to repo:`%s` DENIED for user:`%s` @ %s',
1837 1837 check_scope, user, check_location)
1838 1838 return False
1839 1839
1840 1840 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1841 1841 user_group_name=None):
1842 1842 """
1843 1843 implement in child class should return True if permissions are ok,
1844 1844 False otherwise
1845 1845
1846 1846 :param perm_defs: dict with permission definitions
1847 1847 :param repo_name: repo name
1848 1848 """
1849 1849 raise NotImplementedError()
1850 1850
1851 1851
1852 1852 class HasPermissionAllApi(_BaseApiPerm):
1853 1853 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1854 1854 user_group_name=None):
1855 1855 if self.required_perms.issubset(perm_defs.get('global')):
1856 1856 return True
1857 1857 return False
1858 1858
1859 1859
1860 1860 class HasPermissionAnyApi(_BaseApiPerm):
1861 1861 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1862 1862 user_group_name=None):
1863 1863 if self.required_perms.intersection(perm_defs.get('global')):
1864 1864 return True
1865 1865 return False
1866 1866
1867 1867
1868 1868 class HasRepoPermissionAllApi(_BaseApiPerm):
1869 1869 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1870 1870 user_group_name=None):
1871 1871 try:
1872 1872 _user_perms = set([perm_defs['repositories'][repo_name]])
1873 1873 except KeyError:
1874 1874 log.warning(traceback.format_exc())
1875 1875 return False
1876 1876 if self.required_perms.issubset(_user_perms):
1877 1877 return True
1878 1878 return False
1879 1879
1880 1880
1881 1881 class HasRepoPermissionAnyApi(_BaseApiPerm):
1882 1882 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1883 1883 user_group_name=None):
1884 1884 try:
1885 1885 _user_perms = set([perm_defs['repositories'][repo_name]])
1886 1886 except KeyError:
1887 1887 log.warning(traceback.format_exc())
1888 1888 return False
1889 1889 if self.required_perms.intersection(_user_perms):
1890 1890 return True
1891 1891 return False
1892 1892
1893 1893
1894 1894 class HasRepoGroupPermissionAnyApi(_BaseApiPerm):
1895 1895 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1896 1896 user_group_name=None):
1897 1897 try:
1898 1898 _user_perms = set([perm_defs['repositories_groups'][group_name]])
1899 1899 except KeyError:
1900 1900 log.warning(traceback.format_exc())
1901 1901 return False
1902 1902 if self.required_perms.intersection(_user_perms):
1903 1903 return True
1904 1904 return False
1905 1905
1906 1906
1907 1907 class HasRepoGroupPermissionAllApi(_BaseApiPerm):
1908 1908 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1909 1909 user_group_name=None):
1910 1910 try:
1911 1911 _user_perms = set([perm_defs['repositories_groups'][group_name]])
1912 1912 except KeyError:
1913 1913 log.warning(traceback.format_exc())
1914 1914 return False
1915 1915 if self.required_perms.issubset(_user_perms):
1916 1916 return True
1917 1917 return False
1918 1918
1919 1919
1920 1920 class HasUserGroupPermissionAnyApi(_BaseApiPerm):
1921 1921 def check_permissions(self, perm_defs, repo_name=None, group_name=None,
1922 1922 user_group_name=None):
1923 1923 try:
1924 1924 _user_perms = set([perm_defs['user_groups'][user_group_name]])
1925 1925 except KeyError:
1926 1926 log.warning(traceback.format_exc())
1927 1927 return False
1928 1928 if self.required_perms.intersection(_user_perms):
1929 1929 return True
1930 1930 return False
1931 1931
1932 1932
1933 1933 def check_ip_access(source_ip, allowed_ips=None):
1934 1934 """
1935 1935 Checks if source_ip is a subnet of any of allowed_ips.
1936 1936
1937 1937 :param source_ip:
1938 1938 :param allowed_ips: list of allowed ips together with mask
1939 1939 """
1940 1940 log.debug('checking if ip:%s is subnet of %s' % (source_ip, allowed_ips))
1941 1941 source_ip_address = ipaddress.ip_address(source_ip)
1942 1942 if isinstance(allowed_ips, (tuple, list, set)):
1943 1943 for ip in allowed_ips:
1944 1944 try:
1945 1945 network_address = ipaddress.ip_network(ip, strict=False)
1946 1946 if source_ip_address in network_address:
1947 1947 log.debug('IP %s is network %s' %
1948 1948 (source_ip_address, network_address))
1949 1949 return True
1950 1950 # for any case we cannot determine the IP, don't crash just
1951 1951 # skip it and log as error, we want to say forbidden still when
1952 1952 # sending bad IP
1953 1953 except Exception:
1954 1954 log.error(traceback.format_exc())
1955 1955 continue
1956 1956 return False
1957 1957
1958 1958
1959 1959 def get_cython_compat_decorator(wrapper, func):
1960 1960 """
1961 1961 Creates a cython compatible decorator. The previously used
1962 1962 decorator.decorator() function seems to be incompatible with cython.
1963 1963
1964 1964 :param wrapper: __wrapper method of the decorator class
1965 1965 :param func: decorated function
1966 1966 """
1967 1967 @wraps(func)
1968 1968 def local_wrapper(*args, **kwds):
1969 1969 return wrapper(func, *args, **kwds)
1970 1970 local_wrapper.__wrapped__ = func
1971 1971 return local_wrapper
1972 1972
1973 1973
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,213 +1,213 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 """
23 23 Model for integrations
24 24 """
25 25
26 26
27 27 import logging
28 28
29 29 from sqlalchemy import or_, and_
30 30
31 31 import rhodecode
32 32 from rhodecode import events
33 33 from rhodecode.lib.caching_query import FromCache
34 34 from rhodecode.model import BaseModel
35 35 from rhodecode.model.db import Integration, Repository, RepoGroup
36 36 from rhodecode.integrations import integration_type_registry
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40
41 41 class IntegrationModel(BaseModel):
42 42
43 43 cls = Integration
44 44
45 45 def __get_integration(self, integration):
46 46 if isinstance(integration, Integration):
47 47 return integration
48 48 elif isinstance(integration, (int, long)):
49 49 return self.sa.query(Integration).get(integration)
50 50 else:
51 51 if integration:
52 52 raise Exception('integration must be int, long or Instance'
53 53 ' of Integration got %s' % type(integration))
54 54
55 55 def create(self, IntegrationType, name, enabled, repo, repo_group,
56 56 child_repos_only, settings):
57 57 """ Create an IntegrationType integration """
58 58 integration = Integration()
59 59 integration.integration_type = IntegrationType.key
60 60 self.sa.add(integration)
61 61 self.update_integration(integration, name, enabled, repo, repo_group,
62 62 child_repos_only, settings)
63 63 self.sa.commit()
64 64 return integration
65 65
66 66 def update_integration(self, integration, name, enabled, repo, repo_group,
67 67 child_repos_only, settings):
68 68 integration = self.__get_integration(integration)
69 69
70 70 integration.repo = repo
71 71 integration.repo_group = repo_group
72 72 integration.child_repos_only = child_repos_only
73 73 integration.name = name
74 74 integration.enabled = enabled
75 75 integration.settings = settings
76 76
77 77 return integration
78 78
79 79 def delete(self, integration):
80 80 integration = self.__get_integration(integration)
81 81 if integration:
82 82 self.sa.delete(integration)
83 83 return True
84 84 return False
85 85
86 86 def get_integration_handler(self, integration):
87 87 TypeClass = integration_type_registry.get(integration.integration_type)
88 88 if not TypeClass:
89 89 log.error('No class could be found for integration type: {}'.format(
90 90 integration.integration_type))
91 91 return None
92 92
93 93 return TypeClass(integration.settings)
94 94
95 95 def send_event(self, integration, event):
96 96 """ Send an event to an integration """
97 97 handler = self.get_integration_handler(integration)
98 98 if handler:
99 99 handler.send_event(event)
100 100
101 101 def get_integrations(self, scope, IntegrationType=None):
102 102 """
103 103 Return integrations for a scope, which must be one of:
104 104
105 105 'all' - every integration, global/repogroup/repo
106 106 'global' - global integrations only
107 107 <Repository> instance - integrations for this repo only
108 108 <RepoGroup> instance - integrations for this repogroup only
109 109 """
110 110
111 111 if isinstance(scope, Repository):
112 112 query = self.sa.query(Integration).filter(
113 113 Integration.repo==scope)
114 114 elif isinstance(scope, RepoGroup):
115 115 query = self.sa.query(Integration).filter(
116 116 Integration.repo_group==scope)
117 117 elif scope == 'global':
118 118 # global integrations
119 119 query = self.sa.query(Integration).filter(
120 120 and_(Integration.repo_id==None, Integration.repo_group_id==None)
121 121 )
122 122 elif scope == 'root-repos':
123 123 query = self.sa.query(Integration).filter(
124 124 and_(Integration.repo_id==None,
125 125 Integration.repo_group_id==None,
126 126 Integration.child_repos_only==True)
127 127 )
128 128 elif scope == 'all':
129 129 query = self.sa.query(Integration)
130 130 else:
131 131 raise Exception(
132 132 "invalid `scope`, must be one of: "
133 133 "['global', 'all', <Repository>, <RepoGroup>]")
134 134
135 135 if IntegrationType is not None:
136 136 query = query.filter(
137 137 Integration.integration_type==IntegrationType.key)
138 138
139 139 result = []
140 140 for integration in query.all():
141 141 IntType = integration_type_registry.get(integration.integration_type)
142 142 result.append((IntType, integration))
143 143 return result
144 144
145 145 def get_for_event(self, event, cache=False):
146 146 """
147 147 Get integrations that match an event
148 148 """
149 149 query = self.sa.query(
150 150 Integration
151 151 ).filter(
152 152 Integration.enabled==True
153 153 )
154 154
155 155 global_integrations_filter = and_(
156 156 Integration.repo_id==None,
157 157 Integration.repo_group_id==None,
158 158 Integration.child_repos_only==False,
159 159 )
160 160
161 161 if isinstance(event, events.RepoEvent):
162 162 root_repos_integrations_filter = and_(
163 163 Integration.repo_id==None,
164 164 Integration.repo_group_id==None,
165 165 Integration.child_repos_only==True,
166 166 )
167 167
168 168 clauses = [
169 169 global_integrations_filter,
170 170 ]
171 171
172 172 # repo integrations
173 173 if event.repo.repo_id: # pre create events dont have a repo_id yet
174 174 clauses.append(
175 175 Integration.repo_id==event.repo.repo_id
176 176 )
177 177
178 178 if event.repo.group:
179 179 clauses.append(
180 180 and_(
181 181 Integration.repo_group_id==event.repo.group.group_id,
182 182 Integration.child_repos_only==True
183 183 )
184 184 )
185 185 # repo group cascade to kids
186 186 clauses.append(
187 187 and_(
188 188 Integration.repo_group_id.in_(
189 189 [group.group_id for group in
190 190 event.repo.groups_with_parents]
191 191 ),
192 192 Integration.child_repos_only==False
193 193 )
194 194 )
195 195
196 196
197 197 if not event.repo.group: # root repo
198 198 clauses.append(root_repos_integrations_filter)
199 199
200 200 query = query.filter(or_(*clauses))
201 201
202 202 if cache:
203 query = query.options(FromCache(
204 "sql_cache_short",
205 "get_enabled_repo_integrations_%i" % event.repo.repo_id))
203 cache_key = "get_enabled_repo_integrations_%i" % event.repo.repo_id
204 query = query.options(
205 FromCache("sql_cache_short", cache_key))
206 206 else: # only global integrations
207 207 query = query.filter(global_integrations_filter)
208 208 if cache:
209 query = query.options(FromCache(
210 "sql_cache_short", "get_enabled_global_integrations"))
209 query = query.options(
210 FromCache("sql_cache_short", "get_enabled_global_integrations"))
211 211
212 212 result = query.all()
213 213 return result No newline at end of file
@@ -1,999 +1,1000 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Repository model for rhodecode
23 23 """
24 24
25 25 import logging
26 26 import os
27 27 import re
28 28 import shutil
29 29 import time
30 30 import traceback
31 31 from datetime import datetime, timedelta
32 32
33 33 from zope.cachedescriptors.property import Lazy as LazyProperty
34 34
35 35 from rhodecode import events
36 36 from rhodecode.lib import helpers as h
37 37 from rhodecode.lib.auth import HasUserGroupPermissionAny
38 38 from rhodecode.lib.caching_query import FromCache
39 39 from rhodecode.lib.exceptions import AttachedForksError
40 40 from rhodecode.lib.hooks_base import log_delete_repository
41 41 from rhodecode.lib.utils import make_db_config
42 42 from rhodecode.lib.utils2 import (
43 43 safe_str, safe_unicode, remove_prefix, obfuscate_url_pw,
44 44 get_current_rhodecode_user, safe_int, datetime_to_time, action_logger_generic)
45 45 from rhodecode.lib.vcs.backends import get_backend
46 46 from rhodecode.model import BaseModel
47 from rhodecode.model.db import (
47 from rhodecode.model.db import (_hash_key,
48 48 Repository, UserRepoToPerm, UserGroupRepoToPerm, UserRepoGroupToPerm,
49 49 UserGroupRepoGroupToPerm, User, Permission, Statistics, UserGroup,
50 50 RepoGroup, RepositoryField)
51 51
52 52 from rhodecode.model.settings import VcsSettingsModel
53 53
54 54
55 55 log = logging.getLogger(__name__)
56 56
57 57
58 58 class RepoModel(BaseModel):
59 59
60 60 cls = Repository
61 61
62 62 def _get_user_group(self, users_group):
63 63 return self._get_instance(UserGroup, users_group,
64 64 callback=UserGroup.get_by_group_name)
65 65
66 66 def _get_repo_group(self, repo_group):
67 67 return self._get_instance(RepoGroup, repo_group,
68 68 callback=RepoGroup.get_by_group_name)
69 69
70 70 def _create_default_perms(self, repository, private):
71 71 # create default permission
72 72 default = 'repository.read'
73 73 def_user = User.get_default_user()
74 74 for p in def_user.user_perms:
75 75 if p.permission.permission_name.startswith('repository.'):
76 76 default = p.permission.permission_name
77 77 break
78 78
79 79 default_perm = 'repository.none' if private else default
80 80
81 81 repo_to_perm = UserRepoToPerm()
82 82 repo_to_perm.permission = Permission.get_by_key(default_perm)
83 83
84 84 repo_to_perm.repository = repository
85 85 repo_to_perm.user_id = def_user.user_id
86 86
87 87 return repo_to_perm
88 88
89 89 @LazyProperty
90 90 def repos_path(self):
91 91 """
92 92 Gets the repositories root path from database
93 93 """
94 94 settings_model = VcsSettingsModel(sa=self.sa)
95 95 return settings_model.get_repos_location()
96 96
97 97 def get(self, repo_id, cache=False):
98 98 repo = self.sa.query(Repository) \
99 99 .filter(Repository.repo_id == repo_id)
100 100
101 101 if cache:
102 repo = repo.options(FromCache("sql_cache_short",
103 "get_repo_%s" % repo_id))
102 repo = repo.options(
103 FromCache("sql_cache_short", "get_repo_%s" % repo_id))
104 104 return repo.scalar()
105 105
106 106 def get_repo(self, repository):
107 107 return self._get_repo(repository)
108 108
109 109 def get_by_repo_name(self, repo_name, cache=False):
110 110 repo = self.sa.query(Repository) \
111 111 .filter(Repository.repo_name == repo_name)
112 112
113 113 if cache:
114 repo = repo.options(FromCache("sql_cache_short",
115 "get_repo_%s" % repo_name))
114 name_key = _hash_key(repo_name)
115 repo = repo.options(
116 FromCache("sql_cache_short", "get_repo_%s" % name_key))
116 117 return repo.scalar()
117 118
118 119 def _extract_id_from_repo_name(self, repo_name):
119 120 if repo_name.startswith('/'):
120 121 repo_name = repo_name.lstrip('/')
121 122 by_id_match = re.match(r'^_(\d{1,})', repo_name)
122 123 if by_id_match:
123 124 return by_id_match.groups()[0]
124 125
125 126 def get_repo_by_id(self, repo_name):
126 127 """
127 128 Extracts repo_name by id from special urls.
128 129 Example url is _11/repo_name
129 130
130 131 :param repo_name:
131 132 :return: repo object if matched else None
132 133 """
133 134 try:
134 135 _repo_id = self._extract_id_from_repo_name(repo_name)
135 136 if _repo_id:
136 137 return self.get(_repo_id)
137 138 except Exception:
138 139 log.exception('Failed to extract repo_name from URL')
139 140
140 141 return None
141 142
142 143 def get_repos_for_root(self, root, traverse=False):
143 144 if traverse:
144 145 like_expression = u'{}%'.format(safe_unicode(root))
145 146 repos = Repository.query().filter(
146 147 Repository.repo_name.like(like_expression)).all()
147 148 else:
148 149 if root and not isinstance(root, RepoGroup):
149 150 raise ValueError(
150 151 'Root must be an instance '
151 152 'of RepoGroup, got:{} instead'.format(type(root)))
152 153 repos = Repository.query().filter(Repository.group == root).all()
153 154 return repos
154 155
155 156 def get_url(self, repo):
156 157 return h.url('summary_home', repo_name=safe_str(repo.repo_name),
157 158 qualified=True)
158 159
159 160 @classmethod
160 161 def update_repoinfo(cls, repositories=None):
161 162 if not repositories:
162 163 repositories = Repository.getAll()
163 164 for repo in repositories:
164 165 repo.update_commit_cache()
165 166
166 167 def get_repos_as_dict(self, repo_list=None, admin=False,
167 168 super_user_actions=False):
168 169
169 170 from rhodecode.lib.utils import PartialRenderer
170 171 _render = PartialRenderer('data_table/_dt_elements.mako')
171 172 c = _render.c
172 173
173 174 def quick_menu(repo_name):
174 175 return _render('quick_menu', repo_name)
175 176
176 177 def repo_lnk(name, rtype, rstate, private, fork_of):
177 178 return _render('repo_name', name, rtype, rstate, private, fork_of,
178 179 short_name=not admin, admin=False)
179 180
180 181 def last_change(last_change):
181 182 if admin and isinstance(last_change, datetime) and not last_change.tzinfo:
182 183 last_change = last_change + timedelta(seconds=
183 184 (datetime.now() - datetime.utcnow()).seconds)
184 185 return _render("last_change", last_change)
185 186
186 187 def rss_lnk(repo_name):
187 188 return _render("rss", repo_name)
188 189
189 190 def atom_lnk(repo_name):
190 191 return _render("atom", repo_name)
191 192
192 193 def last_rev(repo_name, cs_cache):
193 194 return _render('revision', repo_name, cs_cache.get('revision'),
194 195 cs_cache.get('raw_id'), cs_cache.get('author'),
195 196 cs_cache.get('message'))
196 197
197 198 def desc(desc):
198 199 if c.visual.stylify_metatags:
199 200 desc = h.urlify_text(h.escaped_stylize(desc))
200 201 else:
201 202 desc = h.urlify_text(h.html_escape(desc))
202 203
203 204 return _render('repo_desc', desc)
204 205
205 206 def state(repo_state):
206 207 return _render("repo_state", repo_state)
207 208
208 209 def repo_actions(repo_name):
209 210 return _render('repo_actions', repo_name, super_user_actions)
210 211
211 212 def user_profile(username):
212 213 return _render('user_profile', username)
213 214
214 215 repos_data = []
215 216 for repo in repo_list:
216 217 cs_cache = repo.changeset_cache
217 218 row = {
218 219 "menu": quick_menu(repo.repo_name),
219 220
220 221 "name": repo_lnk(repo.repo_name, repo.repo_type,
221 222 repo.repo_state, repo.private, repo.fork),
222 223 "name_raw": repo.repo_name.lower(),
223 224
224 225 "last_change": last_change(repo.last_db_change),
225 226 "last_change_raw": datetime_to_time(repo.last_db_change),
226 227
227 228 "last_changeset": last_rev(repo.repo_name, cs_cache),
228 229 "last_changeset_raw": cs_cache.get('revision'),
229 230
230 231 "desc": desc(repo.description),
231 232 "owner": user_profile(repo.user.username),
232 233
233 234 "state": state(repo.repo_state),
234 235 "rss": rss_lnk(repo.repo_name),
235 236
236 237 "atom": atom_lnk(repo.repo_name),
237 238 }
238 239 if admin:
239 240 row.update({
240 241 "action": repo_actions(repo.repo_name),
241 242 })
242 243 repos_data.append(row)
243 244
244 245 return repos_data
245 246
246 247 def _get_defaults(self, repo_name):
247 248 """
248 249 Gets information about repository, and returns a dict for
249 250 usage in forms
250 251
251 252 :param repo_name:
252 253 """
253 254
254 255 repo_info = Repository.get_by_repo_name(repo_name)
255 256
256 257 if repo_info is None:
257 258 return None
258 259
259 260 defaults = repo_info.get_dict()
260 261 defaults['repo_name'] = repo_info.just_name
261 262
262 263 groups = repo_info.groups_with_parents
263 264 parent_group = groups[-1] if groups else None
264 265
265 266 # we use -1 as this is how in HTML, we mark an empty group
266 267 defaults['repo_group'] = getattr(parent_group, 'group_id', -1)
267 268
268 269 keys_to_process = (
269 270 {'k': 'repo_type', 'strip': False},
270 271 {'k': 'repo_enable_downloads', 'strip': True},
271 272 {'k': 'repo_description', 'strip': True},
272 273 {'k': 'repo_enable_locking', 'strip': True},
273 274 {'k': 'repo_landing_rev', 'strip': True},
274 275 {'k': 'clone_uri', 'strip': False},
275 276 {'k': 'repo_private', 'strip': True},
276 277 {'k': 'repo_enable_statistics', 'strip': True}
277 278 )
278 279
279 280 for item in keys_to_process:
280 281 attr = item['k']
281 282 if item['strip']:
282 283 attr = remove_prefix(item['k'], 'repo_')
283 284
284 285 val = defaults[attr]
285 286 if item['k'] == 'repo_landing_rev':
286 287 val = ':'.join(defaults[attr])
287 288 defaults[item['k']] = val
288 289 if item['k'] == 'clone_uri':
289 290 defaults['clone_uri_hidden'] = repo_info.clone_uri_hidden
290 291
291 292 # fill owner
292 293 if repo_info.user:
293 294 defaults.update({'user': repo_info.user.username})
294 295 else:
295 296 replacement_user = User.get_first_super_admin().username
296 297 defaults.update({'user': replacement_user})
297 298
298 299 return defaults
299 300
300 301 def update(self, repo, **kwargs):
301 302 try:
302 303 cur_repo = self._get_repo(repo)
303 304 source_repo_name = cur_repo.repo_name
304 305 if 'user' in kwargs:
305 306 cur_repo.user = User.get_by_username(kwargs['user'])
306 307
307 308 if 'repo_group' in kwargs:
308 309 cur_repo.group = RepoGroup.get(kwargs['repo_group'])
309 310 log.debug('Updating repo %s with params:%s', cur_repo, kwargs)
310 311
311 312 update_keys = [
312 313 (1, 'repo_description'),
313 314 (1, 'repo_landing_rev'),
314 315 (1, 'repo_private'),
315 316 (1, 'repo_enable_downloads'),
316 317 (1, 'repo_enable_locking'),
317 318 (1, 'repo_enable_statistics'),
318 319 (0, 'clone_uri'),
319 320 (0, 'fork_id')
320 321 ]
321 322 for strip, k in update_keys:
322 323 if k in kwargs:
323 324 val = kwargs[k]
324 325 if strip:
325 326 k = remove_prefix(k, 'repo_')
326 327
327 328 setattr(cur_repo, k, val)
328 329
329 330 new_name = cur_repo.get_new_name(kwargs['repo_name'])
330 331 cur_repo.repo_name = new_name
331 332
332 333 # if private flag is set, reset default permission to NONE
333 334 if kwargs.get('repo_private'):
334 335 EMPTY_PERM = 'repository.none'
335 336 RepoModel().grant_user_permission(
336 337 repo=cur_repo, user=User.DEFAULT_USER, perm=EMPTY_PERM
337 338 )
338 339
339 340 # handle extra fields
340 341 for field in filter(lambda k: k.startswith(RepositoryField.PREFIX),
341 342 kwargs):
342 343 k = RepositoryField.un_prefix_key(field)
343 344 ex_field = RepositoryField.get_by_key_name(
344 345 key=k, repo=cur_repo)
345 346 if ex_field:
346 347 ex_field.field_value = kwargs[field]
347 348 self.sa.add(ex_field)
348 349 self.sa.add(cur_repo)
349 350
350 351 if source_repo_name != new_name:
351 352 # rename repository
352 353 self._rename_filesystem_repo(
353 354 old=source_repo_name, new=new_name)
354 355
355 356 return cur_repo
356 357 except Exception:
357 358 log.error(traceback.format_exc())
358 359 raise
359 360
360 361 def _create_repo(self, repo_name, repo_type, description, owner,
361 362 private=False, clone_uri=None, repo_group=None,
362 363 landing_rev='rev:tip', fork_of=None,
363 364 copy_fork_permissions=False, enable_statistics=False,
364 365 enable_locking=False, enable_downloads=False,
365 366 copy_group_permissions=False,
366 367 state=Repository.STATE_PENDING):
367 368 """
368 369 Create repository inside database with PENDING state, this should be
369 370 only executed by create() repo. With exception of importing existing
370 371 repos
371 372 """
372 373 from rhodecode.model.scm import ScmModel
373 374
374 375 owner = self._get_user(owner)
375 376 fork_of = self._get_repo(fork_of)
376 377 repo_group = self._get_repo_group(safe_int(repo_group))
377 378
378 379 try:
379 380 repo_name = safe_unicode(repo_name)
380 381 description = safe_unicode(description)
381 382 # repo name is just a name of repository
382 383 # while repo_name_full is a full qualified name that is combined
383 384 # with name and path of group
384 385 repo_name_full = repo_name
385 386 repo_name = repo_name.split(Repository.NAME_SEP)[-1]
386 387
387 388 new_repo = Repository()
388 389 new_repo.repo_state = state
389 390 new_repo.enable_statistics = False
390 391 new_repo.repo_name = repo_name_full
391 392 new_repo.repo_type = repo_type
392 393 new_repo.user = owner
393 394 new_repo.group = repo_group
394 395 new_repo.description = description or repo_name
395 396 new_repo.private = private
396 397 new_repo.clone_uri = clone_uri
397 398 new_repo.landing_rev = landing_rev
398 399
399 400 new_repo.enable_statistics = enable_statistics
400 401 new_repo.enable_locking = enable_locking
401 402 new_repo.enable_downloads = enable_downloads
402 403
403 404 if repo_group:
404 405 new_repo.enable_locking = repo_group.enable_locking
405 406
406 407 if fork_of:
407 408 parent_repo = fork_of
408 409 new_repo.fork = parent_repo
409 410
410 411 events.trigger(events.RepoPreCreateEvent(new_repo))
411 412
412 413 self.sa.add(new_repo)
413 414
414 415 EMPTY_PERM = 'repository.none'
415 416 if fork_of and copy_fork_permissions:
416 417 repo = fork_of
417 418 user_perms = UserRepoToPerm.query() \
418 419 .filter(UserRepoToPerm.repository == repo).all()
419 420 group_perms = UserGroupRepoToPerm.query() \
420 421 .filter(UserGroupRepoToPerm.repository == repo).all()
421 422
422 423 for perm in user_perms:
423 424 UserRepoToPerm.create(
424 425 perm.user, new_repo, perm.permission)
425 426
426 427 for perm in group_perms:
427 428 UserGroupRepoToPerm.create(
428 429 perm.users_group, new_repo, perm.permission)
429 430 # in case we copy permissions and also set this repo to private
430 431 # override the default user permission to make it a private
431 432 # repo
432 433 if private:
433 434 RepoModel(self.sa).grant_user_permission(
434 435 repo=new_repo, user=User.DEFAULT_USER, perm=EMPTY_PERM)
435 436
436 437 elif repo_group and copy_group_permissions:
437 438 user_perms = UserRepoGroupToPerm.query() \
438 439 .filter(UserRepoGroupToPerm.group == repo_group).all()
439 440
440 441 group_perms = UserGroupRepoGroupToPerm.query() \
441 442 .filter(UserGroupRepoGroupToPerm.group == repo_group).all()
442 443
443 444 for perm in user_perms:
444 445 perm_name = perm.permission.permission_name.replace(
445 446 'group.', 'repository.')
446 447 perm_obj = Permission.get_by_key(perm_name)
447 448 UserRepoToPerm.create(perm.user, new_repo, perm_obj)
448 449
449 450 for perm in group_perms:
450 451 perm_name = perm.permission.permission_name.replace(
451 452 'group.', 'repository.')
452 453 perm_obj = Permission.get_by_key(perm_name)
453 454 UserGroupRepoToPerm.create(
454 455 perm.users_group, new_repo, perm_obj)
455 456
456 457 if private:
457 458 RepoModel(self.sa).grant_user_permission(
458 459 repo=new_repo, user=User.DEFAULT_USER, perm=EMPTY_PERM)
459 460
460 461 else:
461 462 perm_obj = self._create_default_perms(new_repo, private)
462 463 self.sa.add(perm_obj)
463 464
464 465 # now automatically start following this repository as owner
465 466 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
466 467 owner.user_id)
467 468
468 469 # we need to flush here, in order to check if database won't
469 470 # throw any exceptions, create filesystem dirs at the very end
470 471 self.sa.flush()
471 472 events.trigger(events.RepoCreateEvent(new_repo))
472 473 return new_repo
473 474
474 475 except Exception:
475 476 log.error(traceback.format_exc())
476 477 raise
477 478
478 479 def create(self, form_data, cur_user):
479 480 """
480 481 Create repository using celery tasks
481 482
482 483 :param form_data:
483 484 :param cur_user:
484 485 """
485 486 from rhodecode.lib.celerylib import tasks, run_task
486 487 return run_task(tasks.create_repo, form_data, cur_user)
487 488
488 489 def update_permissions(self, repo, perm_additions=None, perm_updates=None,
489 490 perm_deletions=None, check_perms=True,
490 491 cur_user=None):
491 492 if not perm_additions:
492 493 perm_additions = []
493 494 if not perm_updates:
494 495 perm_updates = []
495 496 if not perm_deletions:
496 497 perm_deletions = []
497 498
498 499 req_perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin')
499 500
500 501 changes = {
501 502 'added': [],
502 503 'updated': [],
503 504 'deleted': []
504 505 }
505 506 # update permissions
506 507 for member_id, perm, member_type in perm_updates:
507 508 member_id = int(member_id)
508 509 if member_type == 'user':
509 510 member_name = User.get(member_id).username
510 511 # this updates also current one if found
511 512 self.grant_user_permission(
512 513 repo=repo, user=member_id, perm=perm)
513 514 else: # set for user group
514 515 # check if we have permissions to alter this usergroup
515 516 member_name = UserGroup.get(member_id).users_group_name
516 517 if not check_perms or HasUserGroupPermissionAny(
517 518 *req_perms)(member_name, user=cur_user):
518 519 self.grant_user_group_permission(
519 520 repo=repo, group_name=member_id, perm=perm)
520 521
521 522 changes['updated'].append({'type': member_type, 'id': member_id,
522 523 'name': member_name, 'new_perm': perm})
523 524
524 525 # set new permissions
525 526 for member_id, perm, member_type in perm_additions:
526 527 member_id = int(member_id)
527 528 if member_type == 'user':
528 529 member_name = User.get(member_id).username
529 530 self.grant_user_permission(
530 531 repo=repo, user=member_id, perm=perm)
531 532 else: # set for user group
532 533 # check if we have permissions to alter this usergroup
533 534 member_name = UserGroup.get(member_id).users_group_name
534 535 if not check_perms or HasUserGroupPermissionAny(
535 536 *req_perms)(member_name, user=cur_user):
536 537 self.grant_user_group_permission(
537 538 repo=repo, group_name=member_id, perm=perm)
538 539 changes['added'].append({'type': member_type, 'id': member_id,
539 540 'name': member_name, 'new_perm': perm})
540 541 # delete permissions
541 542 for member_id, perm, member_type in perm_deletions:
542 543 member_id = int(member_id)
543 544 if member_type == 'user':
544 545 member_name = User.get(member_id).username
545 546 self.revoke_user_permission(repo=repo, user=member_id)
546 547 else: # set for user group
547 548 # check if we have permissions to alter this usergroup
548 549 member_name = UserGroup.get(member_id).users_group_name
549 550 if not check_perms or HasUserGroupPermissionAny(
550 551 *req_perms)(member_name, user=cur_user):
551 552 self.revoke_user_group_permission(
552 553 repo=repo, group_name=member_id)
553 554
554 555 changes['deleted'].append({'type': member_type, 'id': member_id,
555 556 'name': member_name, 'new_perm': perm})
556 557 return changes
557 558
558 559 def create_fork(self, form_data, cur_user):
559 560 """
560 561 Simple wrapper into executing celery task for fork creation
561 562
562 563 :param form_data:
563 564 :param cur_user:
564 565 """
565 566 from rhodecode.lib.celerylib import tasks, run_task
566 567 return run_task(tasks.create_repo_fork, form_data, cur_user)
567 568
568 569 def delete(self, repo, forks=None, fs_remove=True, cur_user=None):
569 570 """
570 571 Delete given repository, forks parameter defines what do do with
571 572 attached forks. Throws AttachedForksError if deleted repo has attached
572 573 forks
573 574
574 575 :param repo:
575 576 :param forks: str 'delete' or 'detach'
576 577 :param fs_remove: remove(archive) repo from filesystem
577 578 """
578 579 if not cur_user:
579 580 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
580 581 repo = self._get_repo(repo)
581 582 if repo:
582 583 if forks == 'detach':
583 584 for r in repo.forks:
584 585 r.fork = None
585 586 self.sa.add(r)
586 587 elif forks == 'delete':
587 588 for r in repo.forks:
588 589 self.delete(r, forks='delete')
589 590 elif [f for f in repo.forks]:
590 591 raise AttachedForksError()
591 592
592 593 old_repo_dict = repo.get_dict()
593 594 events.trigger(events.RepoPreDeleteEvent(repo))
594 595 try:
595 596 self.sa.delete(repo)
596 597 if fs_remove:
597 598 self._delete_filesystem_repo(repo)
598 599 else:
599 600 log.debug('skipping removal from filesystem')
600 601 old_repo_dict.update({
601 602 'deleted_by': cur_user,
602 603 'deleted_on': time.time(),
603 604 })
604 605 log_delete_repository(**old_repo_dict)
605 606 events.trigger(events.RepoDeleteEvent(repo))
606 607 except Exception:
607 608 log.error(traceback.format_exc())
608 609 raise
609 610
610 611 def grant_user_permission(self, repo, user, perm):
611 612 """
612 613 Grant permission for user on given repository, or update existing one
613 614 if found
614 615
615 616 :param repo: Instance of Repository, repository_id, or repository name
616 617 :param user: Instance of User, user_id or username
617 618 :param perm: Instance of Permission, or permission_name
618 619 """
619 620 user = self._get_user(user)
620 621 repo = self._get_repo(repo)
621 622 permission = self._get_perm(perm)
622 623
623 624 # check if we have that permission already
624 625 obj = self.sa.query(UserRepoToPerm) \
625 626 .filter(UserRepoToPerm.user == user) \
626 627 .filter(UserRepoToPerm.repository == repo) \
627 628 .scalar()
628 629 if obj is None:
629 630 # create new !
630 631 obj = UserRepoToPerm()
631 632 obj.repository = repo
632 633 obj.user = user
633 634 obj.permission = permission
634 635 self.sa.add(obj)
635 636 log.debug('Granted perm %s to %s on %s', perm, user, repo)
636 637 action_logger_generic(
637 638 'granted permission: {} to user: {} on repo: {}'.format(
638 639 perm, user, repo), namespace='security.repo')
639 640 return obj
640 641
641 642 def revoke_user_permission(self, repo, user):
642 643 """
643 644 Revoke permission for user on given repository
644 645
645 646 :param repo: Instance of Repository, repository_id, or repository name
646 647 :param user: Instance of User, user_id or username
647 648 """
648 649
649 650 user = self._get_user(user)
650 651 repo = self._get_repo(repo)
651 652
652 653 obj = self.sa.query(UserRepoToPerm) \
653 654 .filter(UserRepoToPerm.repository == repo) \
654 655 .filter(UserRepoToPerm.user == user) \
655 656 .scalar()
656 657 if obj:
657 658 self.sa.delete(obj)
658 659 log.debug('Revoked perm on %s on %s', repo, user)
659 660 action_logger_generic(
660 661 'revoked permission from user: {} on repo: {}'.format(
661 662 user, repo), namespace='security.repo')
662 663
663 664 def grant_user_group_permission(self, repo, group_name, perm):
664 665 """
665 666 Grant permission for user group on given repository, or update
666 667 existing one if found
667 668
668 669 :param repo: Instance of Repository, repository_id, or repository name
669 670 :param group_name: Instance of UserGroup, users_group_id,
670 671 or user group name
671 672 :param perm: Instance of Permission, or permission_name
672 673 """
673 674 repo = self._get_repo(repo)
674 675 group_name = self._get_user_group(group_name)
675 676 permission = self._get_perm(perm)
676 677
677 678 # check if we have that permission already
678 679 obj = self.sa.query(UserGroupRepoToPerm) \
679 680 .filter(UserGroupRepoToPerm.users_group == group_name) \
680 681 .filter(UserGroupRepoToPerm.repository == repo) \
681 682 .scalar()
682 683
683 684 if obj is None:
684 685 # create new
685 686 obj = UserGroupRepoToPerm()
686 687
687 688 obj.repository = repo
688 689 obj.users_group = group_name
689 690 obj.permission = permission
690 691 self.sa.add(obj)
691 692 log.debug('Granted perm %s to %s on %s', perm, group_name, repo)
692 693 action_logger_generic(
693 694 'granted permission: {} to usergroup: {} on repo: {}'.format(
694 695 perm, group_name, repo), namespace='security.repo')
695 696
696 697 return obj
697 698
698 699 def revoke_user_group_permission(self, repo, group_name):
699 700 """
700 701 Revoke permission for user group on given repository
701 702
702 703 :param repo: Instance of Repository, repository_id, or repository name
703 704 :param group_name: Instance of UserGroup, users_group_id,
704 705 or user group name
705 706 """
706 707 repo = self._get_repo(repo)
707 708 group_name = self._get_user_group(group_name)
708 709
709 710 obj = self.sa.query(UserGroupRepoToPerm) \
710 711 .filter(UserGroupRepoToPerm.repository == repo) \
711 712 .filter(UserGroupRepoToPerm.users_group == group_name) \
712 713 .scalar()
713 714 if obj:
714 715 self.sa.delete(obj)
715 716 log.debug('Revoked perm to %s on %s', repo, group_name)
716 717 action_logger_generic(
717 718 'revoked permission from usergroup: {} on repo: {}'.format(
718 719 group_name, repo), namespace='security.repo')
719 720
720 721 def delete_stats(self, repo_name):
721 722 """
722 723 removes stats for given repo
723 724
724 725 :param repo_name:
725 726 """
726 727 repo = self._get_repo(repo_name)
727 728 try:
728 729 obj = self.sa.query(Statistics) \
729 730 .filter(Statistics.repository == repo).scalar()
730 731 if obj:
731 732 self.sa.delete(obj)
732 733 except Exception:
733 734 log.error(traceback.format_exc())
734 735 raise
735 736
736 737 def add_repo_field(self, repo_name, field_key, field_label, field_value='',
737 738 field_type='str', field_desc=''):
738 739
739 740 repo = self._get_repo(repo_name)
740 741
741 742 new_field = RepositoryField()
742 743 new_field.repository = repo
743 744 new_field.field_key = field_key
744 745 new_field.field_type = field_type # python type
745 746 new_field.field_value = field_value
746 747 new_field.field_desc = field_desc
747 748 new_field.field_label = field_label
748 749 self.sa.add(new_field)
749 750 return new_field
750 751
751 752 def delete_repo_field(self, repo_name, field_key):
752 753 repo = self._get_repo(repo_name)
753 754 field = RepositoryField.get_by_key_name(field_key, repo)
754 755 if field:
755 756 self.sa.delete(field)
756 757
757 758 def _create_filesystem_repo(self, repo_name, repo_type, repo_group,
758 759 clone_uri=None, repo_store_location=None,
759 760 use_global_config=False):
760 761 """
761 762 makes repository on filesystem. It's group aware means it'll create
762 763 a repository within a group, and alter the paths accordingly of
763 764 group location
764 765
765 766 :param repo_name:
766 767 :param alias:
767 768 :param parent:
768 769 :param clone_uri:
769 770 :param repo_store_location:
770 771 """
771 772 from rhodecode.lib.utils import is_valid_repo, is_valid_repo_group
772 773 from rhodecode.model.scm import ScmModel
773 774
774 775 if Repository.NAME_SEP in repo_name:
775 776 raise ValueError(
776 777 'repo_name must not contain groups got `%s`' % repo_name)
777 778
778 779 if isinstance(repo_group, RepoGroup):
779 780 new_parent_path = os.sep.join(repo_group.full_path_splitted)
780 781 else:
781 782 new_parent_path = repo_group or ''
782 783
783 784 if repo_store_location:
784 785 _paths = [repo_store_location]
785 786 else:
786 787 _paths = [self.repos_path, new_parent_path, repo_name]
787 788 # we need to make it str for mercurial
788 789 repo_path = os.path.join(*map(lambda x: safe_str(x), _paths))
789 790
790 791 # check if this path is not a repository
791 792 if is_valid_repo(repo_path, self.repos_path):
792 793 raise Exception('This path %s is a valid repository' % repo_path)
793 794
794 795 # check if this path is a group
795 796 if is_valid_repo_group(repo_path, self.repos_path):
796 797 raise Exception('This path %s is a valid group' % repo_path)
797 798
798 799 log.info('creating repo %s in %s from url: `%s`',
799 800 repo_name, safe_unicode(repo_path),
800 801 obfuscate_url_pw(clone_uri))
801 802
802 803 backend = get_backend(repo_type)
803 804
804 805 config_repo = None if use_global_config else repo_name
805 806 if config_repo and new_parent_path:
806 807 config_repo = Repository.NAME_SEP.join(
807 808 (new_parent_path, config_repo))
808 809 config = make_db_config(clear_session=False, repo=config_repo)
809 810 config.set('extensions', 'largefiles', '')
810 811
811 812 # patch and reset hooks section of UI config to not run any
812 813 # hooks on creating remote repo
813 814 config.clear_section('hooks')
814 815
815 816 # TODO: johbo: Unify this, hardcoded "bare=True" does not look nice
816 817 if repo_type == 'git':
817 818 repo = backend(
818 819 repo_path, config=config, create=True, src_url=clone_uri,
819 820 bare=True)
820 821 else:
821 822 repo = backend(
822 823 repo_path, config=config, create=True, src_url=clone_uri)
823 824
824 825 ScmModel().install_hooks(repo, repo_type=repo_type)
825 826
826 827 log.debug('Created repo %s with %s backend',
827 828 safe_unicode(repo_name), safe_unicode(repo_type))
828 829 return repo
829 830
830 831 def _rename_filesystem_repo(self, old, new):
831 832 """
832 833 renames repository on filesystem
833 834
834 835 :param old: old name
835 836 :param new: new name
836 837 """
837 838 log.info('renaming repo from %s to %s', old, new)
838 839
839 840 old_path = os.path.join(self.repos_path, old)
840 841 new_path = os.path.join(self.repos_path, new)
841 842 if os.path.isdir(new_path):
842 843 raise Exception(
843 844 'Was trying to rename to already existing dir %s' % new_path
844 845 )
845 846 shutil.move(old_path, new_path)
846 847
847 848 def _delete_filesystem_repo(self, repo):
848 849 """
849 850 removes repo from filesystem, the removal is acctually made by
850 851 added rm__ prefix into dir, and rename internat .hg/.git dirs so this
851 852 repository is no longer valid for rhodecode, can be undeleted later on
852 853 by reverting the renames on this repository
853 854
854 855 :param repo: repo object
855 856 """
856 857 rm_path = os.path.join(self.repos_path, repo.repo_name)
857 858 repo_group = repo.group
858 859 log.info("Removing repository %s", rm_path)
859 860 # disable hg/git internal that it doesn't get detected as repo
860 861 alias = repo.repo_type
861 862
862 863 config = make_db_config(clear_session=False)
863 864 config.set('extensions', 'largefiles', '')
864 865 bare = getattr(repo.scm_instance(config=config), 'bare', False)
865 866
866 867 # skip this for bare git repos
867 868 if not bare:
868 869 # disable VCS repo
869 870 vcs_path = os.path.join(rm_path, '.%s' % alias)
870 871 if os.path.exists(vcs_path):
871 872 shutil.move(vcs_path, os.path.join(rm_path, 'rm__.%s' % alias))
872 873
873 874 _now = datetime.now()
874 875 _ms = str(_now.microsecond).rjust(6, '0')
875 876 _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms),
876 877 repo.just_name)
877 878 if repo_group:
878 879 # if repository is in group, prefix the removal path with the group
879 880 args = repo_group.full_path_splitted + [_d]
880 881 _d = os.path.join(*args)
881 882
882 883 if os.path.isdir(rm_path):
883 884 shutil.move(rm_path, os.path.join(self.repos_path, _d))
884 885
885 886
886 887 class ReadmeFinder:
887 888 """
888 889 Utility which knows how to find a readme for a specific commit.
889 890
890 891 The main idea is that this is a configurable algorithm. When creating an
891 892 instance you can define parameters, currently only the `default_renderer`.
892 893 Based on this configuration the method :meth:`search` behaves slightly
893 894 different.
894 895 """
895 896
896 897 readme_re = re.compile(r'^readme(\.[^\.]+)?$', re.IGNORECASE)
897 898 path_re = re.compile(r'^docs?', re.IGNORECASE)
898 899
899 900 default_priorities = {
900 901 None: 0,
901 902 '.text': 2,
902 903 '.txt': 3,
903 904 '.rst': 1,
904 905 '.rest': 2,
905 906 '.md': 1,
906 907 '.mkdn': 2,
907 908 '.mdown': 3,
908 909 '.markdown': 4,
909 910 }
910 911
911 912 path_priority = {
912 913 'doc': 0,
913 914 'docs': 1,
914 915 }
915 916
916 917 FALLBACK_PRIORITY = 99
917 918
918 919 RENDERER_TO_EXTENSION = {
919 920 'rst': ['.rst', '.rest'],
920 921 'markdown': ['.md', 'mkdn', '.mdown', '.markdown'],
921 922 }
922 923
923 924 def __init__(self, default_renderer=None):
924 925 self._default_renderer = default_renderer
925 926 self._renderer_extensions = self.RENDERER_TO_EXTENSION.get(
926 927 default_renderer, [])
927 928
928 929 def search(self, commit, path='/'):
929 930 """
930 931 Find a readme in the given `commit`.
931 932 """
932 933 nodes = commit.get_nodes(path)
933 934 matches = self._match_readmes(nodes)
934 935 matches = self._sort_according_to_priority(matches)
935 936 if matches:
936 937 return matches[0].node
937 938
938 939 paths = self._match_paths(nodes)
939 940 paths = self._sort_paths_according_to_priority(paths)
940 941 for path in paths:
941 942 match = self.search(commit, path=path)
942 943 if match:
943 944 return match
944 945
945 946 return None
946 947
947 948 def _match_readmes(self, nodes):
948 949 for node in nodes:
949 950 if not node.is_file():
950 951 continue
951 952 path = node.path.rsplit('/', 1)[-1]
952 953 match = self.readme_re.match(path)
953 954 if match:
954 955 extension = match.group(1)
955 956 yield ReadmeMatch(node, match, self._priority(extension))
956 957
957 958 def _match_paths(self, nodes):
958 959 for node in nodes:
959 960 if not node.is_dir():
960 961 continue
961 962 match = self.path_re.match(node.path)
962 963 if match:
963 964 yield node.path
964 965
965 966 def _priority(self, extension):
966 967 renderer_priority = (
967 968 0 if extension in self._renderer_extensions else 1)
968 969 extension_priority = self.default_priorities.get(
969 970 extension, self.FALLBACK_PRIORITY)
970 971 return (renderer_priority, extension_priority)
971 972
972 973 def _sort_according_to_priority(self, matches):
973 974
974 975 def priority_and_path(match):
975 976 return (match.priority, match.path)
976 977
977 978 return sorted(matches, key=priority_and_path)
978 979
979 980 def _sort_paths_according_to_priority(self, paths):
980 981
981 982 def priority_and_path(path):
982 983 return (self.path_priority.get(path, self.FALLBACK_PRIORITY), path)
983 984
984 985 return sorted(paths, key=priority_and_path)
985 986
986 987
987 988 class ReadmeMatch:
988 989
989 990 def __init__(self, node, match, priority):
990 991 self.node = node
991 992 self._match = match
992 993 self.priority = priority
993 994
994 995 @property
995 996 def path(self):
996 997 return self.node.path
997 998
998 999 def __repr__(self):
999 1000 return '<ReadmeMatch {} priority={}'.format(self.path, self.priority)
@@ -1,711 +1,712 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 """
23 23 repo group model for RhodeCode
24 24 """
25 25
26 26 import os
27 27 import datetime
28 28 import itertools
29 29 import logging
30 30 import shutil
31 31 import traceback
32 32 import string
33 33
34 34 from zope.cachedescriptors.property import Lazy as LazyProperty
35 35
36 36 from rhodecode import events
37 37 from rhodecode.model import BaseModel
38 from rhodecode.model.db import (
38 from rhodecode.model.db import (_hash_key,
39 39 RepoGroup, UserRepoGroupToPerm, User, Permission, UserGroupRepoGroupToPerm,
40 40 UserGroup, Repository)
41 41 from rhodecode.model.settings import VcsSettingsModel, SettingsModel
42 42 from rhodecode.lib.caching_query import FromCache
43 43 from rhodecode.lib.utils2 import action_logger_generic
44 44
45 45 log = logging.getLogger(__name__)
46 46
47 47
48 48 class RepoGroupModel(BaseModel):
49 49
50 50 cls = RepoGroup
51 51 PERSONAL_GROUP_DESC = 'personal repo group of user `%(username)s`'
52 52 PERSONAL_GROUP_PATTERN = '${username}' # default
53 53
54 54 def _get_user_group(self, users_group):
55 55 return self._get_instance(UserGroup, users_group,
56 56 callback=UserGroup.get_by_group_name)
57 57
58 58 def _get_repo_group(self, repo_group):
59 59 return self._get_instance(RepoGroup, repo_group,
60 60 callback=RepoGroup.get_by_group_name)
61 61
62 62 @LazyProperty
63 63 def repos_path(self):
64 64 """
65 65 Gets the repositories root path from database
66 66 """
67 67
68 68 settings_model = VcsSettingsModel(sa=self.sa)
69 69 return settings_model.get_repos_location()
70 70
71 71 def get_by_group_name(self, repo_group_name, cache=None):
72 72 repo = self.sa.query(RepoGroup) \
73 73 .filter(RepoGroup.group_name == repo_group_name)
74 74
75 75 if cache:
76 repo = repo.options(FromCache(
77 "sql_cache_short", "get_repo_group_%s" % repo_group_name))
76 name_key = _hash_key(repo_group_name)
77 repo = repo.options(
78 FromCache("sql_cache_short", "get_repo_group_%s" % name_key))
78 79 return repo.scalar()
79 80
80 81 def get_default_create_personal_repo_group(self):
81 82 value = SettingsModel().get_setting_by_name(
82 83 'create_personal_repo_group')
83 84 return value.app_settings_value if value else None or False
84 85
85 86 def get_personal_group_name_pattern(self):
86 87 value = SettingsModel().get_setting_by_name(
87 88 'personal_repo_group_pattern')
88 89 val = value.app_settings_value if value else None
89 90 group_template = val or self.PERSONAL_GROUP_PATTERN
90 91
91 92 group_template = group_template.lstrip('/')
92 93 return group_template
93 94
94 95 def get_personal_group_name(self, user):
95 96 template = self.get_personal_group_name_pattern()
96 97 return string.Template(template).safe_substitute(
97 98 username=user.username,
98 99 user_id=user.user_id,
99 100 )
100 101
101 102 def create_personal_repo_group(self, user, commit_early=True):
102 103 desc = self.PERSONAL_GROUP_DESC % {'username': user.username}
103 104 personal_repo_group_name = self.get_personal_group_name(user)
104 105
105 106 # create a new one
106 107 RepoGroupModel().create(
107 108 group_name=personal_repo_group_name,
108 109 group_description=desc,
109 110 owner=user.username,
110 111 personal=True,
111 112 commit_early=commit_early)
112 113
113 114 def _create_default_perms(self, new_group):
114 115 # create default permission
115 116 default_perm = 'group.read'
116 117 def_user = User.get_default_user()
117 118 for p in def_user.user_perms:
118 119 if p.permission.permission_name.startswith('group.'):
119 120 default_perm = p.permission.permission_name
120 121 break
121 122
122 123 repo_group_to_perm = UserRepoGroupToPerm()
123 124 repo_group_to_perm.permission = Permission.get_by_key(default_perm)
124 125
125 126 repo_group_to_perm.group = new_group
126 127 repo_group_to_perm.user_id = def_user.user_id
127 128 return repo_group_to_perm
128 129
129 130 def _get_group_name_and_parent(self, group_name_full, repo_in_path=False,
130 131 get_object=False):
131 132 """
132 133 Get's the group name and a parent group name from given group name.
133 134 If repo_in_path is set to truth, we asume the full path also includes
134 135 repo name, in such case we clean the last element.
135 136
136 137 :param group_name_full:
137 138 """
138 139 split_paths = 1
139 140 if repo_in_path:
140 141 split_paths = 2
141 142 _parts = group_name_full.rsplit(RepoGroup.url_sep(), split_paths)
142 143
143 144 if repo_in_path and len(_parts) > 1:
144 145 # such case last element is the repo_name
145 146 _parts.pop(-1)
146 147 group_name_cleaned = _parts[-1] # just the group name
147 148 parent_repo_group_name = None
148 149
149 150 if len(_parts) > 1:
150 151 parent_repo_group_name = _parts[0]
151 152
152 153 parent_group = None
153 154 if parent_repo_group_name:
154 155 parent_group = RepoGroup.get_by_group_name(parent_repo_group_name)
155 156
156 157 if get_object:
157 158 return group_name_cleaned, parent_repo_group_name, parent_group
158 159
159 160 return group_name_cleaned, parent_repo_group_name
160 161
161 162 def check_exist_filesystem(self, group_name, exc_on_failure=True):
162 163 create_path = os.path.join(self.repos_path, group_name)
163 164 log.debug('creating new group in %s', create_path)
164 165
165 166 if os.path.isdir(create_path):
166 167 if exc_on_failure:
167 168 abs_create_path = os.path.abspath(create_path)
168 169 raise Exception('Directory `{}` already exists !'.format(abs_create_path))
169 170 return False
170 171 return True
171 172
172 173 def _create_group(self, group_name):
173 174 """
174 175 makes repository group on filesystem
175 176
176 177 :param repo_name:
177 178 :param parent_id:
178 179 """
179 180
180 181 self.check_exist_filesystem(group_name)
181 182 create_path = os.path.join(self.repos_path, group_name)
182 183 log.debug('creating new group in %s', create_path)
183 184 os.makedirs(create_path, mode=0755)
184 185 log.debug('created group in %s', create_path)
185 186
186 187 def _rename_group(self, old, new):
187 188 """
188 189 Renames a group on filesystem
189 190
190 191 :param group_name:
191 192 """
192 193
193 194 if old == new:
194 195 log.debug('skipping group rename')
195 196 return
196 197
197 198 log.debug('renaming repository group from %s to %s', old, new)
198 199
199 200 old_path = os.path.join(self.repos_path, old)
200 201 new_path = os.path.join(self.repos_path, new)
201 202
202 203 log.debug('renaming repos paths from %s to %s', old_path, new_path)
203 204
204 205 if os.path.isdir(new_path):
205 206 raise Exception('Was trying to rename to already '
206 207 'existing dir %s' % new_path)
207 208 shutil.move(old_path, new_path)
208 209
209 210 def _delete_filesystem_group(self, group, force_delete=False):
210 211 """
211 212 Deletes a group from a filesystem
212 213
213 214 :param group: instance of group from database
214 215 :param force_delete: use shutil rmtree to remove all objects
215 216 """
216 217 paths = group.full_path.split(RepoGroup.url_sep())
217 218 paths = os.sep.join(paths)
218 219
219 220 rm_path = os.path.join(self.repos_path, paths)
220 221 log.info("Removing group %s", rm_path)
221 222 # delete only if that path really exists
222 223 if os.path.isdir(rm_path):
223 224 if force_delete:
224 225 shutil.rmtree(rm_path)
225 226 else:
226 227 # archive that group`
227 228 _now = datetime.datetime.now()
228 229 _ms = str(_now.microsecond).rjust(6, '0')
229 230 _d = 'rm__%s_GROUP_%s' % (
230 231 _now.strftime('%Y%m%d_%H%M%S_' + _ms), group.name)
231 232 shutil.move(rm_path, os.path.join(self.repos_path, _d))
232 233
233 234 def create(self, group_name, group_description, owner, just_db=False,
234 235 copy_permissions=False, personal=None, commit_early=True):
235 236
236 237 (group_name_cleaned,
237 238 parent_group_name) = RepoGroupModel()._get_group_name_and_parent(group_name)
238 239
239 240 parent_group = None
240 241 if parent_group_name:
241 242 parent_group = self._get_repo_group(parent_group_name)
242 243 if not parent_group:
243 244 # we tried to create a nested group, but the parent is not
244 245 # existing
245 246 raise ValueError(
246 247 'Parent group `%s` given in `%s` group name '
247 248 'is not yet existing.' % (parent_group_name, group_name))
248 249
249 250 # because we are doing a cleanup, we need to check if such directory
250 251 # already exists. If we don't do that we can accidentally delete
251 252 # existing directory via cleanup that can cause data issues, since
252 253 # delete does a folder rename to special syntax later cleanup
253 254 # functions can delete this
254 255 cleanup_group = self.check_exist_filesystem(group_name,
255 256 exc_on_failure=False)
256 257 try:
257 258 user = self._get_user(owner)
258 259 new_repo_group = RepoGroup()
259 260 new_repo_group.user = user
260 261 new_repo_group.group_description = group_description or group_name
261 262 new_repo_group.parent_group = parent_group
262 263 new_repo_group.group_name = group_name
263 264 new_repo_group.personal = personal
264 265
265 266 self.sa.add(new_repo_group)
266 267
267 268 # create an ADMIN permission for owner except if we're super admin,
268 269 # later owner should go into the owner field of groups
269 270 if not user.is_admin:
270 271 self.grant_user_permission(repo_group=new_repo_group,
271 272 user=owner, perm='group.admin')
272 273
273 274 if parent_group and copy_permissions:
274 275 # copy permissions from parent
275 276 user_perms = UserRepoGroupToPerm.query() \
276 277 .filter(UserRepoGroupToPerm.group == parent_group).all()
277 278
278 279 group_perms = UserGroupRepoGroupToPerm.query() \
279 280 .filter(UserGroupRepoGroupToPerm.group == parent_group).all()
280 281
281 282 for perm in user_perms:
282 283 # don't copy over the permission for user who is creating
283 284 # this group, if he is not super admin he get's admin
284 285 # permission set above
285 286 if perm.user != user or user.is_admin:
286 287 UserRepoGroupToPerm.create(
287 288 perm.user, new_repo_group, perm.permission)
288 289
289 290 for perm in group_perms:
290 291 UserGroupRepoGroupToPerm.create(
291 292 perm.users_group, new_repo_group, perm.permission)
292 293 else:
293 294 perm_obj = self._create_default_perms(new_repo_group)
294 295 self.sa.add(perm_obj)
295 296
296 297 # now commit the changes, earlier so we are sure everything is in
297 298 # the database.
298 299 if commit_early:
299 300 self.sa.commit()
300 301 if not just_db:
301 302 self._create_group(new_repo_group.group_name)
302 303
303 304 # trigger the post hook
304 305 from rhodecode.lib.hooks_base import log_create_repository_group
305 306 repo_group = RepoGroup.get_by_group_name(group_name)
306 307 log_create_repository_group(
307 308 created_by=user.username, **repo_group.get_dict())
308 309
309 310 # Trigger create event.
310 311 events.trigger(events.RepoGroupCreateEvent(repo_group))
311 312
312 313 return new_repo_group
313 314 except Exception:
314 315 self.sa.rollback()
315 316 log.exception('Exception occurred when creating repository group, '
316 317 'doing cleanup...')
317 318 # rollback things manually !
318 319 repo_group = RepoGroup.get_by_group_name(group_name)
319 320 if repo_group:
320 321 RepoGroup.delete(repo_group.group_id)
321 322 self.sa.commit()
322 323 if cleanup_group:
323 324 RepoGroupModel()._delete_filesystem_group(repo_group)
324 325 raise
325 326
326 327 def update_permissions(
327 328 self, repo_group, perm_additions=None, perm_updates=None,
328 329 perm_deletions=None, recursive=None, check_perms=True,
329 330 cur_user=None):
330 331 from rhodecode.model.repo import RepoModel
331 332 from rhodecode.lib.auth import HasUserGroupPermissionAny
332 333
333 334 if not perm_additions:
334 335 perm_additions = []
335 336 if not perm_updates:
336 337 perm_updates = []
337 338 if not perm_deletions:
338 339 perm_deletions = []
339 340
340 341 req_perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin')
341 342
342 343 def _set_perm_user(obj, user, perm):
343 344 if isinstance(obj, RepoGroup):
344 345 self.grant_user_permission(
345 346 repo_group=obj, user=user, perm=perm)
346 347 elif isinstance(obj, Repository):
347 348 # private repos will not allow to change the default
348 349 # permissions using recursive mode
349 350 if obj.private and user == User.DEFAULT_USER:
350 351 return
351 352
352 353 # we set group permission but we have to switch to repo
353 354 # permission
354 355 perm = perm.replace('group.', 'repository.')
355 356 RepoModel().grant_user_permission(
356 357 repo=obj, user=user, perm=perm)
357 358
358 359 def _set_perm_group(obj, users_group, perm):
359 360 if isinstance(obj, RepoGroup):
360 361 self.grant_user_group_permission(
361 362 repo_group=obj, group_name=users_group, perm=perm)
362 363 elif isinstance(obj, Repository):
363 364 # we set group permission but we have to switch to repo
364 365 # permission
365 366 perm = perm.replace('group.', 'repository.')
366 367 RepoModel().grant_user_group_permission(
367 368 repo=obj, group_name=users_group, perm=perm)
368 369
369 370 def _revoke_perm_user(obj, user):
370 371 if isinstance(obj, RepoGroup):
371 372 self.revoke_user_permission(repo_group=obj, user=user)
372 373 elif isinstance(obj, Repository):
373 374 RepoModel().revoke_user_permission(repo=obj, user=user)
374 375
375 376 def _revoke_perm_group(obj, user_group):
376 377 if isinstance(obj, RepoGroup):
377 378 self.revoke_user_group_permission(
378 379 repo_group=obj, group_name=user_group)
379 380 elif isinstance(obj, Repository):
380 381 RepoModel().revoke_user_group_permission(
381 382 repo=obj, group_name=user_group)
382 383
383 384 # start updates
384 385 updates = []
385 386 log.debug('Now updating permissions for %s in recursive mode:%s',
386 387 repo_group, recursive)
387 388
388 389 # initialize check function, we'll call that multiple times
389 390 has_group_perm = HasUserGroupPermissionAny(*req_perms)
390 391
391 392 for obj in repo_group.recursive_groups_and_repos():
392 393 # iterated obj is an instance of a repos group or repository in
393 394 # that group, recursive option can be: none, repos, groups, all
394 395 if recursive == 'all':
395 396 obj = obj
396 397 elif recursive == 'repos':
397 398 # skip groups, other than this one
398 399 if isinstance(obj, RepoGroup) and not obj == repo_group:
399 400 continue
400 401 elif recursive == 'groups':
401 402 # skip repos
402 403 if isinstance(obj, Repository):
403 404 continue
404 405 else: # recursive == 'none':
405 406 # DEFAULT option - don't apply to iterated objects
406 407 # also we do a break at the end of this loop. if we are not
407 408 # in recursive mode
408 409 obj = repo_group
409 410
410 411 # update permissions
411 412 for member_id, perm, member_type in perm_updates:
412 413 member_id = int(member_id)
413 414 if member_type == 'user':
414 415 # this updates also current one if found
415 416 _set_perm_user(obj, user=member_id, perm=perm)
416 417 else: # set for user group
417 418 member_name = UserGroup.get(member_id).users_group_name
418 419 if not check_perms or has_group_perm(member_name,
419 420 user=cur_user):
420 421 _set_perm_group(obj, users_group=member_id, perm=perm)
421 422
422 423 # set new permissions
423 424 for member_id, perm, member_type in perm_additions:
424 425 member_id = int(member_id)
425 426 if member_type == 'user':
426 427 _set_perm_user(obj, user=member_id, perm=perm)
427 428 else: # set for user group
428 429 # check if we have permissions to alter this usergroup
429 430 member_name = UserGroup.get(member_id).users_group_name
430 431 if not check_perms or has_group_perm(member_name,
431 432 user=cur_user):
432 433 _set_perm_group(obj, users_group=member_id, perm=perm)
433 434
434 435 # delete permissions
435 436 for member_id, perm, member_type in perm_deletions:
436 437 member_id = int(member_id)
437 438 if member_type == 'user':
438 439 _revoke_perm_user(obj, user=member_id)
439 440 else: # set for user group
440 441 # check if we have permissions to alter this usergroup
441 442 member_name = UserGroup.get(member_id).users_group_name
442 443 if not check_perms or has_group_perm(member_name,
443 444 user=cur_user):
444 445 _revoke_perm_group(obj, user_group=member_id)
445 446
446 447 updates.append(obj)
447 448 # if it's not recursive call for all,repos,groups
448 449 # break the loop and don't proceed with other changes
449 450 if recursive not in ['all', 'repos', 'groups']:
450 451 break
451 452
452 453 return updates
453 454
454 455 def update(self, repo_group, form_data):
455 456 try:
456 457 repo_group = self._get_repo_group(repo_group)
457 458 old_path = repo_group.full_path
458 459
459 460 # change properties
460 461 if 'group_description' in form_data:
461 462 repo_group.group_description = form_data['group_description']
462 463
463 464 if 'enable_locking' in form_data:
464 465 repo_group.enable_locking = form_data['enable_locking']
465 466
466 467 if 'group_parent_id' in form_data:
467 468 parent_group = (
468 469 self._get_repo_group(form_data['group_parent_id']))
469 470 repo_group.group_parent_id = (
470 471 parent_group.group_id if parent_group else None)
471 472 repo_group.parent_group = parent_group
472 473
473 474 # mikhail: to update the full_path, we have to explicitly
474 475 # update group_name
475 476 group_name = form_data.get('group_name', repo_group.name)
476 477 repo_group.group_name = repo_group.get_new_name(group_name)
477 478
478 479 new_path = repo_group.full_path
479 480
480 481 if 'user' in form_data:
481 482 repo_group.user = User.get_by_username(form_data['user'])
482 483
483 484 self.sa.add(repo_group)
484 485
485 486 # iterate over all members of this groups and do fixes
486 487 # set locking if given
487 488 # if obj is a repoGroup also fix the name of the group according
488 489 # to the parent
489 490 # if obj is a Repo fix it's name
490 491 # this can be potentially heavy operation
491 492 for obj in repo_group.recursive_groups_and_repos():
492 493 # set the value from it's parent
493 494 obj.enable_locking = repo_group.enable_locking
494 495 if isinstance(obj, RepoGroup):
495 496 new_name = obj.get_new_name(obj.name)
496 497 log.debug('Fixing group %s to new name %s',
497 498 obj.group_name, new_name)
498 499 obj.group_name = new_name
499 500 elif isinstance(obj, Repository):
500 501 # we need to get all repositories from this new group and
501 502 # rename them accordingly to new group path
502 503 new_name = obj.get_new_name(obj.just_name)
503 504 log.debug('Fixing repo %s to new name %s',
504 505 obj.repo_name, new_name)
505 506 obj.repo_name = new_name
506 507 self.sa.add(obj)
507 508
508 509 self._rename_group(old_path, new_path)
509 510
510 511 # Trigger update event.
511 512 events.trigger(events.RepoGroupUpdateEvent(repo_group))
512 513
513 514 return repo_group
514 515 except Exception:
515 516 log.error(traceback.format_exc())
516 517 raise
517 518
518 519 def delete(self, repo_group, force_delete=False, fs_remove=True):
519 520 repo_group = self._get_repo_group(repo_group)
520 521 if not repo_group:
521 522 return False
522 523 try:
523 524 self.sa.delete(repo_group)
524 525 if fs_remove:
525 526 self._delete_filesystem_group(repo_group, force_delete)
526 527 else:
527 528 log.debug('skipping removal from filesystem')
528 529
529 530 # Trigger delete event.
530 531 events.trigger(events.RepoGroupDeleteEvent(repo_group))
531 532 return True
532 533
533 534 except Exception:
534 535 log.error('Error removing repo_group %s', repo_group)
535 536 raise
536 537
537 538 def grant_user_permission(self, repo_group, user, perm):
538 539 """
539 540 Grant permission for user on given repository group, or update
540 541 existing one if found
541 542
542 543 :param repo_group: Instance of RepoGroup, repositories_group_id,
543 544 or repositories_group name
544 545 :param user: Instance of User, user_id or username
545 546 :param perm: Instance of Permission, or permission_name
546 547 """
547 548
548 549 repo_group = self._get_repo_group(repo_group)
549 550 user = self._get_user(user)
550 551 permission = self._get_perm(perm)
551 552
552 553 # check if we have that permission already
553 554 obj = self.sa.query(UserRepoGroupToPerm)\
554 555 .filter(UserRepoGroupToPerm.user == user)\
555 556 .filter(UserRepoGroupToPerm.group == repo_group)\
556 557 .scalar()
557 558 if obj is None:
558 559 # create new !
559 560 obj = UserRepoGroupToPerm()
560 561 obj.group = repo_group
561 562 obj.user = user
562 563 obj.permission = permission
563 564 self.sa.add(obj)
564 565 log.debug('Granted perm %s to %s on %s', perm, user, repo_group)
565 566 action_logger_generic(
566 567 'granted permission: {} to user: {} on repogroup: {}'.format(
567 568 perm, user, repo_group), namespace='security.repogroup')
568 569 return obj
569 570
570 571 def revoke_user_permission(self, repo_group, user):
571 572 """
572 573 Revoke permission for user on given repository group
573 574
574 575 :param repo_group: Instance of RepoGroup, repositories_group_id,
575 576 or repositories_group name
576 577 :param user: Instance of User, user_id or username
577 578 """
578 579
579 580 repo_group = self._get_repo_group(repo_group)
580 581 user = self._get_user(user)
581 582
582 583 obj = self.sa.query(UserRepoGroupToPerm)\
583 584 .filter(UserRepoGroupToPerm.user == user)\
584 585 .filter(UserRepoGroupToPerm.group == repo_group)\
585 586 .scalar()
586 587 if obj:
587 588 self.sa.delete(obj)
588 589 log.debug('Revoked perm on %s on %s', repo_group, user)
589 590 action_logger_generic(
590 591 'revoked permission from user: {} on repogroup: {}'.format(
591 592 user, repo_group), namespace='security.repogroup')
592 593
593 594 def grant_user_group_permission(self, repo_group, group_name, perm):
594 595 """
595 596 Grant permission for user group on given repository group, or update
596 597 existing one if found
597 598
598 599 :param repo_group: Instance of RepoGroup, repositories_group_id,
599 600 or repositories_group name
600 601 :param group_name: Instance of UserGroup, users_group_id,
601 602 or user group name
602 603 :param perm: Instance of Permission, or permission_name
603 604 """
604 605 repo_group = self._get_repo_group(repo_group)
605 606 group_name = self._get_user_group(group_name)
606 607 permission = self._get_perm(perm)
607 608
608 609 # check if we have that permission already
609 610 obj = self.sa.query(UserGroupRepoGroupToPerm)\
610 611 .filter(UserGroupRepoGroupToPerm.group == repo_group)\
611 612 .filter(UserGroupRepoGroupToPerm.users_group == group_name)\
612 613 .scalar()
613 614
614 615 if obj is None:
615 616 # create new
616 617 obj = UserGroupRepoGroupToPerm()
617 618
618 619 obj.group = repo_group
619 620 obj.users_group = group_name
620 621 obj.permission = permission
621 622 self.sa.add(obj)
622 623 log.debug('Granted perm %s to %s on %s', perm, group_name, repo_group)
623 624 action_logger_generic(
624 625 'granted permission: {} to usergroup: {} on repogroup: {}'.format(
625 626 perm, group_name, repo_group), namespace='security.repogroup')
626 627 return obj
627 628
628 629 def revoke_user_group_permission(self, repo_group, group_name):
629 630 """
630 631 Revoke permission for user group on given repository group
631 632
632 633 :param repo_group: Instance of RepoGroup, repositories_group_id,
633 634 or repositories_group name
634 635 :param group_name: Instance of UserGroup, users_group_id,
635 636 or user group name
636 637 """
637 638 repo_group = self._get_repo_group(repo_group)
638 639 group_name = self._get_user_group(group_name)
639 640
640 641 obj = self.sa.query(UserGroupRepoGroupToPerm)\
641 642 .filter(UserGroupRepoGroupToPerm.group == repo_group)\
642 643 .filter(UserGroupRepoGroupToPerm.users_group == group_name)\
643 644 .scalar()
644 645 if obj:
645 646 self.sa.delete(obj)
646 647 log.debug('Revoked perm to %s on %s', repo_group, group_name)
647 648 action_logger_generic(
648 649 'revoked permission from usergroup: {} on repogroup: {}'.format(
649 650 group_name, repo_group), namespace='security.repogroup')
650 651
651 652 def get_repo_groups_as_dict(self, repo_group_list=None, admin=False,
652 653 super_user_actions=False):
653 654
654 655 from rhodecode.lib.utils import PartialRenderer
655 656 _render = PartialRenderer('data_table/_dt_elements.mako')
656 657 c = _render.c
657 658 h = _render.h
658 659
659 660 def quick_menu(repo_group_name):
660 661 return _render('quick_repo_group_menu', repo_group_name)
661 662
662 663 def repo_group_lnk(repo_group_name):
663 664 return _render('repo_group_name', repo_group_name)
664 665
665 666 def desc(desc, personal):
666 667 prefix = h.escaped_stylize(u'[personal] ') if personal else ''
667 668
668 669 if c.visual.stylify_metatags:
669 670 desc = h.urlify_text(prefix + h.escaped_stylize(desc))
670 671 else:
671 672 desc = h.urlify_text(prefix + h.html_escape(desc))
672 673
673 674 return _render('repo_group_desc', desc)
674 675
675 676 def repo_group_actions(repo_group_id, repo_group_name, gr_count):
676 677 return _render(
677 678 'repo_group_actions', repo_group_id, repo_group_name, gr_count)
678 679
679 680 def repo_group_name(repo_group_name, children_groups):
680 681 return _render("repo_group_name", repo_group_name, children_groups)
681 682
682 683 def user_profile(username):
683 684 return _render('user_profile', username)
684 685
685 686 repo_group_data = []
686 687 for group in repo_group_list:
687 688
688 689 row = {
689 690 "menu": quick_menu(group.group_name),
690 691 "name": repo_group_lnk(group.group_name),
691 692 "name_raw": group.group_name,
692 693 "desc": desc(group.group_description, group.personal),
693 694 "top_level_repos": 0,
694 695 "owner": user_profile(group.user.username)
695 696 }
696 697 if admin:
697 698 repo_count = group.repositories.count()
698 699 children_groups = map(
699 700 h.safe_unicode,
700 701 itertools.chain((g.name for g in group.parents),
701 702 (x.name for x in [group])))
702 703 row.update({
703 704 "action": repo_group_actions(
704 705 group.group_id, group.group_name, repo_count),
705 706 "top_level_repos": repo_count,
706 707 "name": repo_group_name(group.group_name, children_groups),
707 708
708 709 })
709 710 repo_group_data.append(row)
710 711
711 712 return repo_group_data
@@ -1,901 +1,902 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 users model for RhodeCode
23 23 """
24 24
25 25 import logging
26 26 import traceback
27 27
28 28 import datetime
29 29 from pylons.i18n.translation import _
30 30
31 31 import ipaddress
32 32 from sqlalchemy.exc import DatabaseError
33 33 from sqlalchemy.sql.expression import true, false
34 34
35 35 from rhodecode import events
36 36 from rhodecode.lib.user_log_filter import user_log_filter
37 37 from rhodecode.lib.utils2 import (
38 38 safe_unicode, get_current_rhodecode_user, action_logger_generic,
39 39 AttributeDict, str2bool)
40 40 from rhodecode.lib.caching_query import FromCache
41 41 from rhodecode.model import BaseModel
42 42 from rhodecode.model.auth_token import AuthTokenModel
43 from rhodecode.model.db import (
43 from rhodecode.model.db import (_hash_key,
44 44 or_, joinedload, User, UserToPerm, UserEmailMap, UserIpMap, UserLog)
45 45 from rhodecode.lib.exceptions import (
46 46 DefaultUserException, UserOwnsReposException, UserOwnsRepoGroupsException,
47 47 UserOwnsUserGroupsException, NotAllowedToCreateUserError)
48 48 from rhodecode.model.meta import Session
49 49 from rhodecode.model.repo_group import RepoGroupModel
50 50
51 51
52 52 log = logging.getLogger(__name__)
53 53
54 54
55 55 class UserModel(BaseModel):
56 56 cls = User
57 57
58 58 def get(self, user_id, cache=False):
59 59 user = self.sa.query(User)
60 60 if cache:
61 user = user.options(FromCache("sql_cache_short",
62 "get_user_%s" % user_id))
61 user = user.options(
62 FromCache("sql_cache_short", "get_user_%s" % user_id))
63 63 return user.get(user_id)
64 64
65 65 def get_user(self, user):
66 66 return self._get_user(user)
67 67
68 68 def _serialize_user(self, user):
69 69 import rhodecode.lib.helpers as h
70 70
71 71 return {
72 72 'id': user.user_id,
73 73 'first_name': user.name,
74 74 'last_name': user.lastname,
75 75 'username': user.username,
76 76 'email': user.email,
77 77 'icon_link': h.gravatar_url(user.email, 30),
78 78 'value_display': h.person(user),
79 79 'value': user.username,
80 80 'value_type': 'user',
81 81 'active': user.active,
82 82 }
83 83
84 84 def get_users(self, name_contains=None, limit=20, only_active=True):
85 85
86 86 query = self.sa.query(User)
87 87 if only_active:
88 88 query = query.filter(User.active == true())
89 89
90 90 if name_contains:
91 91 ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
92 92 query = query.filter(
93 93 or_(
94 94 User.name.ilike(ilike_expression),
95 95 User.lastname.ilike(ilike_expression),
96 96 User.username.ilike(ilike_expression)
97 97 )
98 98 )
99 99 query = query.limit(limit)
100 100 users = query.all()
101 101
102 102 _users = [
103 103 self._serialize_user(user) for user in users
104 104 ]
105 105 return _users
106 106
107 107 def get_by_username(self, username, cache=False, case_insensitive=False):
108 108
109 109 if case_insensitive:
110 110 user = self.sa.query(User).filter(User.username.ilike(username))
111 111 else:
112 112 user = self.sa.query(User)\
113 113 .filter(User.username == username)
114 114 if cache:
115 user = user.options(FromCache("sql_cache_short",
116 "get_user_%s" % username))
115 name_key = _hash_key(username)
116 user = user.options(
117 FromCache("sql_cache_short", "get_user_%s" % name_key))
117 118 return user.scalar()
118 119
119 120 def get_by_email(self, email, cache=False, case_insensitive=False):
120 121 return User.get_by_email(email, case_insensitive, cache)
121 122
122 123 def get_by_auth_token(self, auth_token, cache=False):
123 124 return User.get_by_auth_token(auth_token, cache)
124 125
125 126 def get_active_user_count(self, cache=False):
126 127 return User.query().filter(
127 128 User.active == True).filter(
128 129 User.username != User.DEFAULT_USER).count()
129 130
130 131 def create(self, form_data, cur_user=None):
131 132 if not cur_user:
132 133 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
133 134
134 135 user_data = {
135 136 'username': form_data['username'],
136 137 'password': form_data['password'],
137 138 'email': form_data['email'],
138 139 'firstname': form_data['firstname'],
139 140 'lastname': form_data['lastname'],
140 141 'active': form_data['active'],
141 142 'extern_type': form_data['extern_type'],
142 143 'extern_name': form_data['extern_name'],
143 144 'admin': False,
144 145 'cur_user': cur_user
145 146 }
146 147
147 148 if 'create_repo_group' in form_data:
148 149 user_data['create_repo_group'] = str2bool(
149 150 form_data.get('create_repo_group'))
150 151
151 152 try:
152 153 if form_data.get('password_change'):
153 154 user_data['force_password_change'] = True
154 155 return UserModel().create_or_update(**user_data)
155 156 except Exception:
156 157 log.error(traceback.format_exc())
157 158 raise
158 159
159 160 def update_user(self, user, skip_attrs=None, **kwargs):
160 161 from rhodecode.lib.auth import get_crypt_password
161 162
162 163 user = self._get_user(user)
163 164 if user.username == User.DEFAULT_USER:
164 165 raise DefaultUserException(
165 166 _("You can't Edit this user since it's"
166 167 " crucial for entire application"))
167 168
168 169 # first store only defaults
169 170 user_attrs = {
170 171 'updating_user_id': user.user_id,
171 172 'username': user.username,
172 173 'password': user.password,
173 174 'email': user.email,
174 175 'firstname': user.name,
175 176 'lastname': user.lastname,
176 177 'active': user.active,
177 178 'admin': user.admin,
178 179 'extern_name': user.extern_name,
179 180 'extern_type': user.extern_type,
180 181 'language': user.user_data.get('language')
181 182 }
182 183
183 184 # in case there's new_password, that comes from form, use it to
184 185 # store password
185 186 if kwargs.get('new_password'):
186 187 kwargs['password'] = kwargs['new_password']
187 188
188 189 # cleanups, my_account password change form
189 190 kwargs.pop('current_password', None)
190 191 kwargs.pop('new_password', None)
191 192
192 193 # cleanups, user edit password change form
193 194 kwargs.pop('password_confirmation', None)
194 195 kwargs.pop('password_change', None)
195 196
196 197 # create repo group on user creation
197 198 kwargs.pop('create_repo_group', None)
198 199
199 200 # legacy forms send name, which is the firstname
200 201 firstname = kwargs.pop('name', None)
201 202 if firstname:
202 203 kwargs['firstname'] = firstname
203 204
204 205 for k, v in kwargs.items():
205 206 # skip if we don't want to update this
206 207 if skip_attrs and k in skip_attrs:
207 208 continue
208 209
209 210 user_attrs[k] = v
210 211
211 212 try:
212 213 return self.create_or_update(**user_attrs)
213 214 except Exception:
214 215 log.error(traceback.format_exc())
215 216 raise
216 217
217 218 def create_or_update(
218 219 self, username, password, email, firstname='', lastname='',
219 220 active=True, admin=False, extern_type=None, extern_name=None,
220 221 cur_user=None, plugin=None, force_password_change=False,
221 222 allow_to_create_user=True, create_repo_group=None,
222 223 updating_user_id=None, language=None, strict_creation_check=True):
223 224 """
224 225 Creates a new instance if not found, or updates current one
225 226
226 227 :param username:
227 228 :param password:
228 229 :param email:
229 230 :param firstname:
230 231 :param lastname:
231 232 :param active:
232 233 :param admin:
233 234 :param extern_type:
234 235 :param extern_name:
235 236 :param cur_user:
236 237 :param plugin: optional plugin this method was called from
237 238 :param force_password_change: toggles new or existing user flag
238 239 for password change
239 240 :param allow_to_create_user: Defines if the method can actually create
240 241 new users
241 242 :param create_repo_group: Defines if the method should also
242 243 create an repo group with user name, and owner
243 244 :param updating_user_id: if we set it up this is the user we want to
244 245 update this allows to editing username.
245 246 :param language: language of user from interface.
246 247
247 248 :returns: new User object with injected `is_new_user` attribute.
248 249 """
249 250 if not cur_user:
250 251 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
251 252
252 253 from rhodecode.lib.auth import (
253 254 get_crypt_password, check_password, generate_auth_token)
254 255 from rhodecode.lib.hooks_base import (
255 256 log_create_user, check_allowed_create_user)
256 257
257 258 def _password_change(new_user, password):
258 259 # empty password
259 260 if not new_user.password:
260 261 return False
261 262
262 263 # password check is only needed for RhodeCode internal auth calls
263 264 # in case it's a plugin we don't care
264 265 if not plugin:
265 266
266 267 # first check if we gave crypted password back, and if it
267 268 # matches it's not password change
268 269 if new_user.password == password:
269 270 return False
270 271
271 272 password_match = check_password(password, new_user.password)
272 273 if not password_match:
273 274 return True
274 275
275 276 return False
276 277
277 278 # read settings on default personal repo group creation
278 279 if create_repo_group is None:
279 280 default_create_repo_group = RepoGroupModel()\
280 281 .get_default_create_personal_repo_group()
281 282 create_repo_group = default_create_repo_group
282 283
283 284 user_data = {
284 285 'username': username,
285 286 'password': password,
286 287 'email': email,
287 288 'firstname': firstname,
288 289 'lastname': lastname,
289 290 'active': active,
290 291 'admin': admin
291 292 }
292 293
293 294 if updating_user_id:
294 295 log.debug('Checking for existing account in RhodeCode '
295 296 'database with user_id `%s` ' % (updating_user_id,))
296 297 user = User.get(updating_user_id)
297 298 else:
298 299 log.debug('Checking for existing account in RhodeCode '
299 300 'database with username `%s` ' % (username,))
300 301 user = User.get_by_username(username, case_insensitive=True)
301 302
302 303 if user is None:
303 304 # we check internal flag if this method is actually allowed to
304 305 # create new user
305 306 if not allow_to_create_user:
306 307 msg = ('Method wants to create new user, but it is not '
307 308 'allowed to do so')
308 309 log.warning(msg)
309 310 raise NotAllowedToCreateUserError(msg)
310 311
311 312 log.debug('Creating new user %s', username)
312 313
313 314 # only if we create user that is active
314 315 new_active_user = active
315 316 if new_active_user and strict_creation_check:
316 317 # raises UserCreationError if it's not allowed for any reason to
317 318 # create new active user, this also executes pre-create hooks
318 319 check_allowed_create_user(user_data, cur_user, strict_check=True)
319 320 events.trigger(events.UserPreCreate(user_data))
320 321 new_user = User()
321 322 edit = False
322 323 else:
323 324 log.debug('updating user %s', username)
324 325 events.trigger(events.UserPreUpdate(user, user_data))
325 326 new_user = user
326 327 edit = True
327 328
328 329 # we're not allowed to edit default user
329 330 if user.username == User.DEFAULT_USER:
330 331 raise DefaultUserException(
331 332 _("You can't edit this user (`%(username)s`) since it's "
332 333 "crucial for entire application") % {'username': user.username})
333 334
334 335 # inject special attribute that will tell us if User is new or old
335 336 new_user.is_new_user = not edit
336 337 # for users that didn's specify auth type, we use RhodeCode built in
337 338 from rhodecode.authentication.plugins import auth_rhodecode
338 339 extern_name = extern_name or auth_rhodecode.RhodeCodeAuthPlugin.name
339 340 extern_type = extern_type or auth_rhodecode.RhodeCodeAuthPlugin.name
340 341
341 342 try:
342 343 new_user.username = username
343 344 new_user.admin = admin
344 345 new_user.email = email
345 346 new_user.active = active
346 347 new_user.extern_name = safe_unicode(extern_name)
347 348 new_user.extern_type = safe_unicode(extern_type)
348 349 new_user.name = firstname
349 350 new_user.lastname = lastname
350 351
351 352 # set password only if creating an user or password is changed
352 353 if not edit or _password_change(new_user, password):
353 354 reason = 'new password' if edit else 'new user'
354 355 log.debug('Updating password reason=>%s', reason)
355 356 new_user.password = get_crypt_password(password) if password else None
356 357
357 358 if force_password_change:
358 359 new_user.update_userdata(force_password_change=True)
359 360 if language:
360 361 new_user.update_userdata(language=language)
361 362 new_user.update_userdata(notification_status=True)
362 363
363 364 self.sa.add(new_user)
364 365
365 366 if not edit and create_repo_group:
366 367 RepoGroupModel().create_personal_repo_group(
367 368 new_user, commit_early=False)
368 369
369 370 if not edit:
370 371 # add the RSS token
371 372 AuthTokenModel().create(username,
372 373 description='Generated feed token',
373 374 role=AuthTokenModel.cls.ROLE_FEED)
374 375 log_create_user(created_by=cur_user, **new_user.get_dict())
375 376 events.trigger(events.UserPostCreate(user_data))
376 377 return new_user
377 378 except (DatabaseError,):
378 379 log.error(traceback.format_exc())
379 380 raise
380 381
381 382 def create_registration(self, form_data):
382 383 from rhodecode.model.notification import NotificationModel
383 384 from rhodecode.model.notification import EmailNotificationModel
384 385
385 386 try:
386 387 form_data['admin'] = False
387 388 form_data['extern_name'] = 'rhodecode'
388 389 form_data['extern_type'] = 'rhodecode'
389 390 new_user = self.create(form_data)
390 391
391 392 self.sa.add(new_user)
392 393 self.sa.flush()
393 394
394 395 user_data = new_user.get_dict()
395 396 kwargs = {
396 397 # use SQLALCHEMY safe dump of user data
397 398 'user': AttributeDict(user_data),
398 399 'date': datetime.datetime.now()
399 400 }
400 401 notification_type = EmailNotificationModel.TYPE_REGISTRATION
401 402 # pre-generate the subject for notification itself
402 403 (subject,
403 404 _h, _e, # we don't care about those
404 405 body_plaintext) = EmailNotificationModel().render_email(
405 406 notification_type, **kwargs)
406 407
407 408 # create notification objects, and emails
408 409 NotificationModel().create(
409 410 created_by=new_user,
410 411 notification_subject=subject,
411 412 notification_body=body_plaintext,
412 413 notification_type=notification_type,
413 414 recipients=None, # all admins
414 415 email_kwargs=kwargs,
415 416 )
416 417
417 418 return new_user
418 419 except Exception:
419 420 log.error(traceback.format_exc())
420 421 raise
421 422
422 423 def _handle_user_repos(self, username, repositories, handle_mode=None):
423 424 _superadmin = self.cls.get_first_super_admin()
424 425 left_overs = True
425 426
426 427 from rhodecode.model.repo import RepoModel
427 428
428 429 if handle_mode == 'detach':
429 430 for obj in repositories:
430 431 obj.user = _superadmin
431 432 # set description we know why we super admin now owns
432 433 # additional repositories that were orphaned !
433 434 obj.description += ' \n::detached repository from deleted user: %s' % (username,)
434 435 self.sa.add(obj)
435 436 left_overs = False
436 437 elif handle_mode == 'delete':
437 438 for obj in repositories:
438 439 RepoModel().delete(obj, forks='detach')
439 440 left_overs = False
440 441
441 442 # if nothing is done we have left overs left
442 443 return left_overs
443 444
444 445 def _handle_user_repo_groups(self, username, repository_groups,
445 446 handle_mode=None):
446 447 _superadmin = self.cls.get_first_super_admin()
447 448 left_overs = True
448 449
449 450 from rhodecode.model.repo_group import RepoGroupModel
450 451
451 452 if handle_mode == 'detach':
452 453 for r in repository_groups:
453 454 r.user = _superadmin
454 455 # set description we know why we super admin now owns
455 456 # additional repositories that were orphaned !
456 457 r.group_description += ' \n::detached repository group from deleted user: %s' % (username,)
457 458 self.sa.add(r)
458 459 left_overs = False
459 460 elif handle_mode == 'delete':
460 461 for r in repository_groups:
461 462 RepoGroupModel().delete(r)
462 463 left_overs = False
463 464
464 465 # if nothing is done we have left overs left
465 466 return left_overs
466 467
467 468 def _handle_user_user_groups(self, username, user_groups, handle_mode=None):
468 469 _superadmin = self.cls.get_first_super_admin()
469 470 left_overs = True
470 471
471 472 from rhodecode.model.user_group import UserGroupModel
472 473
473 474 if handle_mode == 'detach':
474 475 for r in user_groups:
475 476 for user_user_group_to_perm in r.user_user_group_to_perm:
476 477 if user_user_group_to_perm.user.username == username:
477 478 user_user_group_to_perm.user = _superadmin
478 479 r.user = _superadmin
479 480 # set description we know why we super admin now owns
480 481 # additional repositories that were orphaned !
481 482 r.user_group_description += ' \n::detached user group from deleted user: %s' % (username,)
482 483 self.sa.add(r)
483 484 left_overs = False
484 485 elif handle_mode == 'delete':
485 486 for r in user_groups:
486 487 UserGroupModel().delete(r)
487 488 left_overs = False
488 489
489 490 # if nothing is done we have left overs left
490 491 return left_overs
491 492
492 493 def delete(self, user, cur_user=None, handle_repos=None,
493 494 handle_repo_groups=None, handle_user_groups=None):
494 495 if not cur_user:
495 496 cur_user = getattr(get_current_rhodecode_user(), 'username', None)
496 497 user = self._get_user(user)
497 498
498 499 try:
499 500 if user.username == User.DEFAULT_USER:
500 501 raise DefaultUserException(
501 502 _(u"You can't remove this user since it's"
502 503 u" crucial for entire application"))
503 504
504 505 left_overs = self._handle_user_repos(
505 506 user.username, user.repositories, handle_repos)
506 507 if left_overs and user.repositories:
507 508 repos = [x.repo_name for x in user.repositories]
508 509 raise UserOwnsReposException(
509 510 _(u'user "%s" still owns %s repositories and cannot be '
510 511 u'removed. Switch owners or remove those repositories:%s')
511 512 % (user.username, len(repos), ', '.join(repos)))
512 513
513 514 left_overs = self._handle_user_repo_groups(
514 515 user.username, user.repository_groups, handle_repo_groups)
515 516 if left_overs and user.repository_groups:
516 517 repo_groups = [x.group_name for x in user.repository_groups]
517 518 raise UserOwnsRepoGroupsException(
518 519 _(u'user "%s" still owns %s repository groups and cannot be '
519 520 u'removed. Switch owners or remove those repository groups:%s')
520 521 % (user.username, len(repo_groups), ', '.join(repo_groups)))
521 522
522 523 left_overs = self._handle_user_user_groups(
523 524 user.username, user.user_groups, handle_user_groups)
524 525 if left_overs and user.user_groups:
525 526 user_groups = [x.users_group_name for x in user.user_groups]
526 527 raise UserOwnsUserGroupsException(
527 528 _(u'user "%s" still owns %s user groups and cannot be '
528 529 u'removed. Switch owners or remove those user groups:%s')
529 530 % (user.username, len(user_groups), ', '.join(user_groups)))
530 531
531 532 # we might change the user data with detach/delete, make sure
532 533 # the object is marked as expired before actually deleting !
533 534 self.sa.expire(user)
534 535 self.sa.delete(user)
535 536 from rhodecode.lib.hooks_base import log_delete_user
536 537 log_delete_user(deleted_by=cur_user, **user.get_dict())
537 538 except Exception:
538 539 log.error(traceback.format_exc())
539 540 raise
540 541
541 542 def reset_password_link(self, data, pwd_reset_url):
542 543 from rhodecode.lib.celerylib import tasks, run_task
543 544 from rhodecode.model.notification import EmailNotificationModel
544 545 user_email = data['email']
545 546 try:
546 547 user = User.get_by_email(user_email)
547 548 if user:
548 549 log.debug('password reset user found %s', user)
549 550
550 551 email_kwargs = {
551 552 'password_reset_url': pwd_reset_url,
552 553 'user': user,
553 554 'email': user_email,
554 555 'date': datetime.datetime.now()
555 556 }
556 557
557 558 (subject, headers, email_body,
558 559 email_body_plaintext) = EmailNotificationModel().render_email(
559 560 EmailNotificationModel.TYPE_PASSWORD_RESET, **email_kwargs)
560 561
561 562 recipients = [user_email]
562 563
563 564 action_logger_generic(
564 565 'sending password reset email to user: {}'.format(
565 566 user), namespace='security.password_reset')
566 567
567 568 run_task(tasks.send_email, recipients, subject,
568 569 email_body_plaintext, email_body)
569 570
570 571 else:
571 572 log.debug("password reset email %s not found", user_email)
572 573 except Exception:
573 574 log.error(traceback.format_exc())
574 575 return False
575 576
576 577 return True
577 578
578 579 def reset_password(self, data):
579 580 from rhodecode.lib.celerylib import tasks, run_task
580 581 from rhodecode.model.notification import EmailNotificationModel
581 582 from rhodecode.lib import auth
582 583 user_email = data['email']
583 584 pre_db = True
584 585 try:
585 586 user = User.get_by_email(user_email)
586 587 new_passwd = auth.PasswordGenerator().gen_password(
587 588 12, auth.PasswordGenerator.ALPHABETS_BIG_SMALL)
588 589 if user:
589 590 user.password = auth.get_crypt_password(new_passwd)
590 591 # also force this user to reset his password !
591 592 user.update_userdata(force_password_change=True)
592 593
593 594 Session().add(user)
594 595
595 596 # now delete the token in question
596 597 UserApiKeys = AuthTokenModel.cls
597 598 UserApiKeys().query().filter(
598 599 UserApiKeys.api_key == data['token']).delete()
599 600
600 601 Session().commit()
601 602 log.info('successfully reset password for `%s`', user_email)
602 603
603 604 if new_passwd is None:
604 605 raise Exception('unable to generate new password')
605 606
606 607 pre_db = False
607 608
608 609 email_kwargs = {
609 610 'new_password': new_passwd,
610 611 'user': user,
611 612 'email': user_email,
612 613 'date': datetime.datetime.now()
613 614 }
614 615
615 616 (subject, headers, email_body,
616 617 email_body_plaintext) = EmailNotificationModel().render_email(
617 618 EmailNotificationModel.TYPE_PASSWORD_RESET_CONFIRMATION,
618 619 **email_kwargs)
619 620
620 621 recipients = [user_email]
621 622
622 623 action_logger_generic(
623 624 'sent new password to user: {} with email: {}'.format(
624 625 user, user_email), namespace='security.password_reset')
625 626
626 627 run_task(tasks.send_email, recipients, subject,
627 628 email_body_plaintext, email_body)
628 629
629 630 except Exception:
630 631 log.error('Failed to update user password')
631 632 log.error(traceback.format_exc())
632 633 if pre_db:
633 634 # we rollback only if local db stuff fails. If it goes into
634 635 # run_task, we're pass rollback state this wouldn't work then
635 636 Session().rollback()
636 637
637 638 return True
638 639
639 640 def fill_data(self, auth_user, user_id=None, api_key=None, username=None):
640 641 """
641 642 Fetches auth_user by user_id,or api_key if present.
642 643 Fills auth_user attributes with those taken from database.
643 644 Additionally set's is_authenitated if lookup fails
644 645 present in database
645 646
646 647 :param auth_user: instance of user to set attributes
647 648 :param user_id: user id to fetch by
648 649 :param api_key: api key to fetch by
649 650 :param username: username to fetch by
650 651 """
651 652 if user_id is None and api_key is None and username is None:
652 653 raise Exception('You need to pass user_id, api_key or username')
653 654
654 655 log.debug(
655 656 'doing fill data based on: user_id:%s api_key:%s username:%s',
656 657 user_id, api_key, username)
657 658 try:
658 659 dbuser = None
659 660 if user_id:
660 661 dbuser = self.get(user_id)
661 662 elif api_key:
662 663 dbuser = self.get_by_auth_token(api_key)
663 664 elif username:
664 665 dbuser = self.get_by_username(username)
665 666
666 667 if not dbuser:
667 668 log.warning(
668 669 'Unable to lookup user by id:%s api_key:%s username:%s',
669 670 user_id, api_key, username)
670 671 return False
671 672 if not dbuser.active:
672 673 log.debug('User `%s:%s` is inactive, skipping fill data',
673 674 username, user_id)
674 675 return False
675 676
676 677 log.debug('filling user:%s data', dbuser)
677 678
678 679 # TODO: johbo: Think about this and find a clean solution
679 680 user_data = dbuser.get_dict()
680 681 user_data.update(dbuser.get_api_data(include_secrets=True))
681 682
682 683 for k, v in user_data.iteritems():
683 684 # properties of auth user we dont update
684 685 if k not in ['auth_tokens', 'permissions']:
685 686 setattr(auth_user, k, v)
686 687
687 688 # few extras
688 689 setattr(auth_user, 'feed_token', dbuser.feed_token)
689 690 except Exception:
690 691 log.error(traceback.format_exc())
691 692 auth_user.is_authenticated = False
692 693 return False
693 694
694 695 return True
695 696
696 697 def has_perm(self, user, perm):
697 698 perm = self._get_perm(perm)
698 699 user = self._get_user(user)
699 700
700 701 return UserToPerm.query().filter(UserToPerm.user == user)\
701 702 .filter(UserToPerm.permission == perm).scalar() is not None
702 703
703 704 def grant_perm(self, user, perm):
704 705 """
705 706 Grant user global permissions
706 707
707 708 :param user:
708 709 :param perm:
709 710 """
710 711 user = self._get_user(user)
711 712 perm = self._get_perm(perm)
712 713 # if this permission is already granted skip it
713 714 _perm = UserToPerm.query()\
714 715 .filter(UserToPerm.user == user)\
715 716 .filter(UserToPerm.permission == perm)\
716 717 .scalar()
717 718 if _perm:
718 719 return
719 720 new = UserToPerm()
720 721 new.user = user
721 722 new.permission = perm
722 723 self.sa.add(new)
723 724 return new
724 725
725 726 def revoke_perm(self, user, perm):
726 727 """
727 728 Revoke users global permissions
728 729
729 730 :param user:
730 731 :param perm:
731 732 """
732 733 user = self._get_user(user)
733 734 perm = self._get_perm(perm)
734 735
735 736 obj = UserToPerm.query()\
736 737 .filter(UserToPerm.user == user)\
737 738 .filter(UserToPerm.permission == perm)\
738 739 .scalar()
739 740 if obj:
740 741 self.sa.delete(obj)
741 742
742 743 def add_extra_email(self, user, email):
743 744 """
744 745 Adds email address to UserEmailMap
745 746
746 747 :param user:
747 748 :param email:
748 749 """
749 750 from rhodecode.model import forms
750 751 form = forms.UserExtraEmailForm()()
751 752 data = form.to_python({'email': email})
752 753 user = self._get_user(user)
753 754
754 755 obj = UserEmailMap()
755 756 obj.user = user
756 757 obj.email = data['email']
757 758 self.sa.add(obj)
758 759 return obj
759 760
760 761 def delete_extra_email(self, user, email_id):
761 762 """
762 763 Removes email address from UserEmailMap
763 764
764 765 :param user:
765 766 :param email_id:
766 767 """
767 768 user = self._get_user(user)
768 769 obj = UserEmailMap.query().get(email_id)
769 770 if obj:
770 771 self.sa.delete(obj)
771 772
772 773 def parse_ip_range(self, ip_range):
773 774 ip_list = []
774 775 def make_unique(value):
775 776 seen = []
776 777 return [c for c in value if not (c in seen or seen.append(c))]
777 778
778 779 # firsts split by commas
779 780 for ip_range in ip_range.split(','):
780 781 if not ip_range:
781 782 continue
782 783 ip_range = ip_range.strip()
783 784 if '-' in ip_range:
784 785 start_ip, end_ip = ip_range.split('-', 1)
785 786 start_ip = ipaddress.ip_address(start_ip.strip())
786 787 end_ip = ipaddress.ip_address(end_ip.strip())
787 788 parsed_ip_range = []
788 789
789 790 for index in xrange(int(start_ip), int(end_ip) + 1):
790 791 new_ip = ipaddress.ip_address(index)
791 792 parsed_ip_range.append(str(new_ip))
792 793 ip_list.extend(parsed_ip_range)
793 794 else:
794 795 ip_list.append(ip_range)
795 796
796 797 return make_unique(ip_list)
797 798
798 799 def add_extra_ip(self, user, ip, description=None):
799 800 """
800 801 Adds ip address to UserIpMap
801 802
802 803 :param user:
803 804 :param ip:
804 805 """
805 806 from rhodecode.model import forms
806 807 form = forms.UserExtraIpForm()()
807 808 data = form.to_python({'ip': ip})
808 809 user = self._get_user(user)
809 810
810 811 obj = UserIpMap()
811 812 obj.user = user
812 813 obj.ip_addr = data['ip']
813 814 obj.description = description
814 815 self.sa.add(obj)
815 816 return obj
816 817
817 818 def delete_extra_ip(self, user, ip_id):
818 819 """
819 820 Removes ip address from UserIpMap
820 821
821 822 :param user:
822 823 :param ip_id:
823 824 """
824 825 user = self._get_user(user)
825 826 obj = UserIpMap.query().get(ip_id)
826 827 if obj:
827 828 self.sa.delete(obj)
828 829
829 830 def get_accounts_in_creation_order(self, current_user=None):
830 831 """
831 832 Get accounts in order of creation for deactivation for license limits
832 833
833 834 pick currently logged in user, and append to the list in position 0
834 835 pick all super-admins in order of creation date and add it to the list
835 836 pick all other accounts in order of creation and add it to the list.
836 837
837 838 Based on that list, the last accounts can be disabled as they are
838 839 created at the end and don't include any of the super admins as well
839 840 as the current user.
840 841
841 842 :param current_user: optionally current user running this operation
842 843 """
843 844
844 845 if not current_user:
845 846 current_user = get_current_rhodecode_user()
846 847 active_super_admins = [
847 848 x.user_id for x in User.query()
848 849 .filter(User.user_id != current_user.user_id)
849 850 .filter(User.active == true())
850 851 .filter(User.admin == true())
851 852 .order_by(User.created_on.asc())]
852 853
853 854 active_regular_users = [
854 855 x.user_id for x in User.query()
855 856 .filter(User.user_id != current_user.user_id)
856 857 .filter(User.active == true())
857 858 .filter(User.admin == false())
858 859 .order_by(User.created_on.asc())]
859 860
860 861 list_of_accounts = [current_user.user_id]
861 862 list_of_accounts += active_super_admins
862 863 list_of_accounts += active_regular_users
863 864
864 865 return list_of_accounts
865 866
866 867 def deactivate_last_users(self, expected_users):
867 868 """
868 869 Deactivate accounts that are over the license limits.
869 870 Algorithm of which accounts to disabled is based on the formula:
870 871
871 872 Get current user, then super admins in creation order, then regular
872 873 active users in creation order.
873 874
874 875 Using that list we mark all accounts from the end of it as inactive.
875 876 This way we block only latest created accounts.
876 877
877 878 :param expected_users: list of users in special order, we deactivate
878 879 the end N ammoun of users from that list
879 880 """
880 881
881 882 list_of_accounts = self.get_accounts_in_creation_order()
882 883
883 884 for acc_id in list_of_accounts[expected_users + 1:]:
884 885 user = User.get(acc_id)
885 886 log.info('Deactivating account %s for license unlock', user)
886 887 user.active = False
887 888 Session().add(user)
888 889 Session().commit()
889 890
890 891 return
891 892
892 893 def get_user_log(self, user, filter_term):
893 894 user_log = UserLog.query()\
894 895 .filter(or_(UserLog.user_id == user.user_id,
895 896 UserLog.username == user.username))\
896 897 .options(joinedload(UserLog.user))\
897 898 .options(joinedload(UserLog.repository))\
898 899 .order_by(UserLog.action_date.desc())
899 900
900 901 user_log = user_log_filter(user_log, filter_term)
901 902 return user_log
@@ -1,460 +1,460 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import base64
22 22
23 23 import mock
24 24 import pytest
25 25
26 26 from rhodecode.tests.utils import CustomTestApp
27 27
28 28 from rhodecode.lib.caching_query import FromCache
29 29 from rhodecode.lib.hooks_daemon import DummyHooksCallbackDaemon
30 30 from rhodecode.lib.middleware import simplevcs
31 31 from rhodecode.lib.middleware.https_fixup import HttpsFixup
32 32 from rhodecode.lib.middleware.utils import scm_app_http
33 33 from rhodecode.model.db import User, _hash_key
34 34 from rhodecode.model.meta import Session
35 35 from rhodecode.tests import (
36 36 HG_REPO, TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS)
37 37 from rhodecode.tests.lib.middleware import mock_scm_app
38 38 from rhodecode.tests.utils import set_anonymous_access
39 39
40 40
41 41 class StubVCSController(simplevcs.SimpleVCS):
42 42
43 43 SCM = 'hg'
44 44 stub_response_body = tuple()
45 45
46 46 def __init__(self, *args, **kwargs):
47 47 super(StubVCSController, self).__init__(*args, **kwargs)
48 48 self._action = 'pull'
49 49 self._name = HG_REPO
50 50 self.set_repo_names(None)
51 51
52 52 def _get_repository_name(self, environ):
53 53 return self._name
54 54
55 55 def _get_action(self, environ):
56 56 return self._action
57 57
58 58 def _create_wsgi_app(self, repo_path, repo_name, config):
59 59 def fake_app(environ, start_response):
60 60 start_response('200 OK', [])
61 61 return self.stub_response_body
62 62 return fake_app
63 63
64 64 def _create_config(self, extras, repo_name):
65 65 return None
66 66
67 67
68 68 @pytest.fixture
69 69 def vcscontroller(pylonsapp, config_stub):
70 70 config_stub.testing_securitypolicy()
71 71 config_stub.include('rhodecode.authentication')
72 72
73 73 set_anonymous_access(True)
74 74 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
75 75 app = HttpsFixup(controller, pylonsapp.config)
76 76 app = CustomTestApp(app)
77 77
78 78 _remove_default_user_from_query_cache()
79 79
80 80 # Sanity checks that things are set up correctly
81 81 app.get('/' + HG_REPO, status=200)
82 82
83 83 app.controller = controller
84 84 return app
85 85
86 86
87 87 def _remove_default_user_from_query_cache():
88 88 user = User.get_default_user(cache=True)
89 89 query = Session().query(User).filter(User.username == user.username)
90 query = query.options(FromCache(
91 "sql_cache_short", "get_user_%s" % _hash_key(user.username)))
90 query = query.options(
91 FromCache("sql_cache_short", "get_user_%s" % _hash_key(user.username)))
92 92 query.invalidate()
93 93 Session().expire(user)
94 94
95 95
96 96 @pytest.fixture
97 97 def disable_anonymous_user(request, pylonsapp):
98 98 set_anonymous_access(False)
99 99
100 100 @request.addfinalizer
101 101 def cleanup():
102 102 set_anonymous_access(True)
103 103
104 104
105 105 def test_handles_exceptions_during_permissions_checks(
106 106 vcscontroller, disable_anonymous_user):
107 107 user_and_pass = '%s:%s' % (TEST_USER_ADMIN_LOGIN, TEST_USER_ADMIN_PASS)
108 108 auth_password = base64.encodestring(user_and_pass).strip()
109 109 extra_environ = {
110 110 'AUTH_TYPE': 'Basic',
111 111 'HTTP_AUTHORIZATION': 'Basic %s' % auth_password,
112 112 'REMOTE_USER': TEST_USER_ADMIN_LOGIN,
113 113 }
114 114
115 115 # Verify that things are hooked up correctly
116 116 vcscontroller.get('/', status=200, extra_environ=extra_environ)
117 117
118 118 # Simulate trouble during permission checks
119 119 with mock.patch('rhodecode.model.db.User.get_by_username',
120 120 side_effect=Exception) as get_user:
121 121 # Verify that a correct 500 is returned and check that the expected
122 122 # code path was hit.
123 123 vcscontroller.get('/', status=500, extra_environ=extra_environ)
124 124 assert get_user.called
125 125
126 126
127 127 def test_returns_forbidden_if_no_anonymous_access(
128 128 vcscontroller, disable_anonymous_user):
129 129 vcscontroller.get('/', status=401)
130 130
131 131
132 132 class StubFailVCSController(simplevcs.SimpleVCS):
133 133 def _handle_request(self, environ, start_response):
134 134 raise Exception("BOOM")
135 135
136 136
137 137 @pytest.fixture(scope='module')
138 138 def fail_controller(pylonsapp):
139 139 controller = StubFailVCSController(pylonsapp, pylonsapp.config, None)
140 140 controller = HttpsFixup(controller, pylonsapp.config)
141 141 controller = CustomTestApp(controller)
142 142 return controller
143 143
144 144
145 145 def test_handles_exceptions_as_internal_server_error(fail_controller):
146 146 fail_controller.get('/', status=500)
147 147
148 148
149 149 def test_provides_traceback_for_appenlight(fail_controller):
150 150 response = fail_controller.get(
151 151 '/', status=500, extra_environ={'appenlight.client': 'fake'})
152 152 assert 'appenlight.__traceback' in response.request.environ
153 153
154 154
155 155 def test_provides_utils_scm_app_as_scm_app_by_default(pylonsapp):
156 156 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
157 157 assert controller.scm_app is scm_app_http
158 158
159 159
160 160 def test_allows_to_override_scm_app_via_config(pylonsapp):
161 161 config = pylonsapp.config.copy()
162 162 config['vcs.scm_app_implementation'] = (
163 163 'rhodecode.tests.lib.middleware.mock_scm_app')
164 164 controller = StubVCSController(pylonsapp, config, None)
165 165 assert controller.scm_app is mock_scm_app
166 166
167 167
168 168 @pytest.mark.parametrize('query_string, expected', [
169 169 ('cmd=stub_command', True),
170 170 ('cmd=listkeys', False),
171 171 ])
172 172 def test_should_check_locking(query_string, expected):
173 173 result = simplevcs._should_check_locking(query_string)
174 174 assert result == expected
175 175
176 176
177 177 class TestShadowRepoRegularExpression(object):
178 178 pr_segment = 'pull-request'
179 179 shadow_segment = 'repository'
180 180
181 181 @pytest.mark.parametrize('url, expected', [
182 182 # repo with/without groups
183 183 ('My-Repo/{pr_segment}/1/{shadow_segment}', True),
184 184 ('Group/My-Repo/{pr_segment}/2/{shadow_segment}', True),
185 185 ('Group/Sub-Group/My-Repo/{pr_segment}/3/{shadow_segment}', True),
186 186 ('Group/Sub-Group1/Sub-Group2/My-Repo/{pr_segment}/3/{shadow_segment}', True),
187 187
188 188 # pull request ID
189 189 ('MyRepo/{pr_segment}/1/{shadow_segment}', True),
190 190 ('MyRepo/{pr_segment}/1234567890/{shadow_segment}', True),
191 191 ('MyRepo/{pr_segment}/-1/{shadow_segment}', False),
192 192 ('MyRepo/{pr_segment}/invalid/{shadow_segment}', False),
193 193
194 194 # unicode
195 195 (u'Sp€çîál-Repö/{pr_segment}/1/{shadow_segment}', True),
196 196 (u'Sp€çîál-Gröüp/Sp€çîál-Repö/{pr_segment}/1/{shadow_segment}', True),
197 197
198 198 # trailing/leading slash
199 199 ('/My-Repo/{pr_segment}/1/{shadow_segment}', False),
200 200 ('My-Repo/{pr_segment}/1/{shadow_segment}/', False),
201 201 ('/My-Repo/{pr_segment}/1/{shadow_segment}/', False),
202 202
203 203 # misc
204 204 ('My-Repo/{pr_segment}/1/{shadow_segment}/extra', False),
205 205 ('My-Repo/{pr_segment}/1/{shadow_segment}extra', False),
206 206 ])
207 207 def test_shadow_repo_regular_expression(self, url, expected):
208 208 from rhodecode.lib.middleware.simplevcs import SimpleVCS
209 209 url = url.format(
210 210 pr_segment=self.pr_segment,
211 211 shadow_segment=self.shadow_segment)
212 212 match_obj = SimpleVCS.shadow_repo_re.match(url)
213 213 assert (match_obj is not None) == expected
214 214
215 215
216 216 @pytest.mark.backends('git', 'hg')
217 217 class TestShadowRepoExposure(object):
218 218
219 219 def test_pull_on_shadow_repo_propagates_to_wsgi_app(self, pylonsapp):
220 220 """
221 221 Check that a pull action to a shadow repo is propagated to the
222 222 underlying wsgi app.
223 223 """
224 224 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
225 225 controller._check_ssl = mock.Mock()
226 226 controller.is_shadow_repo = True
227 227 controller._action = 'pull'
228 228 controller.stub_response_body = 'dummy body value'
229 229 environ_stub = {
230 230 'HTTP_HOST': 'test.example.com',
231 231 'REQUEST_METHOD': 'GET',
232 232 'wsgi.url_scheme': 'http',
233 233 }
234 234
235 235 response = controller(environ_stub, mock.Mock())
236 236 response_body = ''.join(response)
237 237
238 238 # Assert that we got the response from the wsgi app.
239 239 assert response_body == controller.stub_response_body
240 240
241 241 def test_push_on_shadow_repo_raises(self, pylonsapp):
242 242 """
243 243 Check that a push action to a shadow repo is aborted.
244 244 """
245 245 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
246 246 controller._check_ssl = mock.Mock()
247 247 controller.is_shadow_repo = True
248 248 controller._action = 'push'
249 249 controller.stub_response_body = 'dummy body value'
250 250 environ_stub = {
251 251 'HTTP_HOST': 'test.example.com',
252 252 'REQUEST_METHOD': 'GET',
253 253 'wsgi.url_scheme': 'http',
254 254 }
255 255
256 256 response = controller(environ_stub, mock.Mock())
257 257 response_body = ''.join(response)
258 258
259 259 assert response_body != controller.stub_response_body
260 260 # Assert that a 406 error is returned.
261 261 assert '406 Not Acceptable' in response_body
262 262
263 263 def test_set_repo_names_no_shadow(self, pylonsapp):
264 264 """
265 265 Check that the set_repo_names method sets all names to the one returned
266 266 by the _get_repository_name method on a request to a non shadow repo.
267 267 """
268 268 environ_stub = {}
269 269 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
270 270 controller._name = 'RepoGroup/MyRepo'
271 271 controller.set_repo_names(environ_stub)
272 272 assert not controller.is_shadow_repo
273 273 assert (controller.url_repo_name ==
274 274 controller.acl_repo_name ==
275 275 controller.vcs_repo_name ==
276 276 controller._get_repository_name(environ_stub))
277 277
278 278 def test_set_repo_names_with_shadow(self, pylonsapp, pr_util):
279 279 """
280 280 Check that the set_repo_names method sets correct names on a request
281 281 to a shadow repo.
282 282 """
283 283 from rhodecode.model.pull_request import PullRequestModel
284 284
285 285 pull_request = pr_util.create_pull_request()
286 286 shadow_url = '{target}/{pr_segment}/{pr_id}/{shadow_segment}'.format(
287 287 target=pull_request.target_repo.repo_name,
288 288 pr_id=pull_request.pull_request_id,
289 289 pr_segment=TestShadowRepoRegularExpression.pr_segment,
290 290 shadow_segment=TestShadowRepoRegularExpression.shadow_segment)
291 291 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
292 292 controller._name = shadow_url
293 293 controller.set_repo_names({})
294 294
295 295 # Get file system path to shadow repo for assertions.
296 296 workspace_id = PullRequestModel()._workspace_id(pull_request)
297 297 target_vcs = pull_request.target_repo.scm_instance()
298 298 vcs_repo_name = target_vcs._get_shadow_repository_path(
299 299 workspace_id)
300 300
301 301 assert controller.vcs_repo_name == vcs_repo_name
302 302 assert controller.url_repo_name == shadow_url
303 303 assert controller.acl_repo_name == pull_request.target_repo.repo_name
304 304 assert controller.is_shadow_repo
305 305
306 306 def test_set_repo_names_with_shadow_but_missing_pr(
307 307 self, pylonsapp, pr_util):
308 308 """
309 309 Checks that the set_repo_names method enforces matching target repos
310 310 and pull request IDs.
311 311 """
312 312 pull_request = pr_util.create_pull_request()
313 313 shadow_url = '{target}/{pr_segment}/{pr_id}/{shadow_segment}'.format(
314 314 target=pull_request.target_repo.repo_name,
315 315 pr_id=999999999,
316 316 pr_segment=TestShadowRepoRegularExpression.pr_segment,
317 317 shadow_segment=TestShadowRepoRegularExpression.shadow_segment)
318 318 controller = StubVCSController(pylonsapp, pylonsapp.config, None)
319 319 controller._name = shadow_url
320 320 controller.set_repo_names({})
321 321
322 322 assert not controller.is_shadow_repo
323 323 assert (controller.url_repo_name ==
324 324 controller.acl_repo_name ==
325 325 controller.vcs_repo_name)
326 326
327 327
328 328 @pytest.mark.usefixtures('db')
329 329 class TestGenerateVcsResponse:
330 330
331 331 def test_ensures_that_start_response_is_called_early_enough(self):
332 332 self.call_controller_with_response_body(iter(['a', 'b']))
333 333 assert self.start_response.called
334 334
335 335 def test_invalidates_cache_after_body_is_consumed(self):
336 336 result = self.call_controller_with_response_body(iter(['a', 'b']))
337 337 assert not self.was_cache_invalidated()
338 338 # Consume the result
339 339 list(result)
340 340 assert self.was_cache_invalidated()
341 341
342 342 @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC')
343 343 def test_handles_locking_exception(self, http_locked_rc):
344 344 result = self.call_controller_with_response_body(
345 345 self.raise_result_iter(vcs_kind='repo_locked'))
346 346 assert not http_locked_rc.called
347 347 # Consume the result
348 348 list(result)
349 349 assert http_locked_rc.called
350 350
351 351 @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPRequirementError')
352 352 def test_handles_requirement_exception(self, http_requirement):
353 353 result = self.call_controller_with_response_body(
354 354 self.raise_result_iter(vcs_kind='requirement'))
355 355 assert not http_requirement.called
356 356 # Consume the result
357 357 list(result)
358 358 assert http_requirement.called
359 359
360 360 @mock.patch('rhodecode.lib.middleware.simplevcs.HTTPLockedRC')
361 361 def test_handles_locking_exception_in_app_call(self, http_locked_rc):
362 362 app_factory_patcher = mock.patch.object(
363 363 StubVCSController, '_create_wsgi_app')
364 364 with app_factory_patcher as app_factory:
365 365 app_factory().side_effect = self.vcs_exception()
366 366 result = self.call_controller_with_response_body(['a'])
367 367 list(result)
368 368 assert http_locked_rc.called
369 369
370 370 def test_raises_unknown_exceptions(self):
371 371 result = self.call_controller_with_response_body(
372 372 self.raise_result_iter(vcs_kind='unknown'))
373 373 with pytest.raises(Exception):
374 374 list(result)
375 375
376 376 def test_prepare_callback_daemon_is_called(self):
377 377 def side_effect(extras):
378 378 return DummyHooksCallbackDaemon(), extras
379 379
380 380 prepare_patcher = mock.patch.object(
381 381 StubVCSController, '_prepare_callback_daemon')
382 382 with prepare_patcher as prepare_mock:
383 383 prepare_mock.side_effect = side_effect
384 384 self.call_controller_with_response_body(iter(['a', 'b']))
385 385 assert prepare_mock.called
386 386 assert prepare_mock.call_count == 1
387 387
388 388 def call_controller_with_response_body(self, response_body):
389 389 settings = {
390 390 'base_path': 'fake_base_path',
391 391 'vcs.hooks.protocol': 'http',
392 392 'vcs.hooks.direct_calls': False,
393 393 }
394 394 controller = StubVCSController(None, settings, None)
395 395 controller._invalidate_cache = mock.Mock()
396 396 controller.stub_response_body = response_body
397 397 self.start_response = mock.Mock()
398 398 result = controller._generate_vcs_response(
399 399 environ={}, start_response=self.start_response,
400 400 repo_path='fake_repo_path',
401 401 extras={}, action='push')
402 402 self.controller = controller
403 403 return result
404 404
405 405 def raise_result_iter(self, vcs_kind='repo_locked'):
406 406 """
407 407 Simulates an exception due to a vcs raised exception if kind vcs_kind
408 408 """
409 409 raise self.vcs_exception(vcs_kind=vcs_kind)
410 410 yield "never_reached"
411 411
412 412 def vcs_exception(self, vcs_kind='repo_locked'):
413 413 locked_exception = Exception('TEST_MESSAGE')
414 414 locked_exception._vcs_kind = vcs_kind
415 415 return locked_exception
416 416
417 417 def was_cache_invalidated(self):
418 418 return self.controller._invalidate_cache.called
419 419
420 420
421 421 class TestInitializeGenerator:
422 422
423 423 def test_drains_first_element(self):
424 424 gen = self.factory(['__init__', 1, 2])
425 425 result = list(gen)
426 426 assert result == [1, 2]
427 427
428 428 @pytest.mark.parametrize('values', [
429 429 [],
430 430 [1, 2],
431 431 ])
432 432 def test_raises_value_error(self, values):
433 433 with pytest.raises(ValueError):
434 434 self.factory(values)
435 435
436 436 @simplevcs.initialize_generator
437 437 def factory(self, iterable):
438 438 for elem in iterable:
439 439 yield elem
440 440
441 441
442 442 class TestPrepareHooksDaemon(object):
443 443 def test_calls_imported_prepare_callback_daemon(self, app_settings):
444 444 expected_extras = {'extra1': 'value1'}
445 445 daemon = DummyHooksCallbackDaemon()
446 446
447 447 controller = StubVCSController(None, app_settings, None)
448 448 prepare_patcher = mock.patch.object(
449 449 simplevcs, 'prepare_callback_daemon',
450 450 return_value=(daemon, expected_extras))
451 451 with prepare_patcher as prepare_mock:
452 452 callback_daemon, extras = controller._prepare_callback_daemon(
453 453 expected_extras.copy())
454 454 prepare_mock.assert_called_once_with(
455 455 expected_extras,
456 456 protocol=app_settings['vcs.hooks.protocol'],
457 457 use_direct_calls=app_settings['vcs.hooks.direct_calls'])
458 458
459 459 assert callback_daemon == daemon
460 460 assert extras == extras
General Comments 0
You need to be logged in to leave comments. Login now