Package coprs :: Package logic :: Module users_logic
[hide private]
[frames] | no frames]

Source Code for Module coprs.logic.users_logic

  1  from coprs import exceptions 
  2   
  3  from coprs import app, db 
  4  from coprs.models import User, Group 
5 6 7 -class UsersLogic(object):
8 9 @classmethod
10 - def get(cls, username):
11 return User.query.filter(User.username == username)
12 13 @classmethod
14 - def get_by_api_login(cls, login):
15 return User.query.filter(User.api_login == login)
16 17 @classmethod
18 - def raise_if_cant_update_copr(cls, user, copr, message):
19 """ 20 Raise InsufficientRightsException if given user cant update 21 given copr. Return None otherwise. 22 """ 23 24 # TODO: this is a bit inconsistent - shouldn't the user method be 25 # called can_update? 26 if not user.can_edit(copr): 27 raise exceptions.InsufficientRightsException(message)
28 29 @classmethod
30 - def raise_if_cant_build_in_copr(cls, user, copr, message):
31 """ 32 Raises InsufficientRightsException if given user cant build in 33 given copr. Return None otherwise. 34 """ 35 36 if not user.can_build_in(copr): 37 raise exceptions.InsufficientRightsException(message)
38 39 @classmethod
40 - def raise_if_not_in_group(cls, user, group):
41 if group.fas_name not in user.user_teams: 42 raise exceptions.InsufficientRightsException( 43 "User '{}' doesn't have access to group {}({})" 44 .format(user.username, group.name, group.fas_name))
45 46 @classmethod
47 - def get_group_by_alias(cls, name):
48 return Group.query.filter(Group.name == name)
49 50 @classmethod
51 - def group_alias_exists(cls, name):
52 query = cls.get_group_by_alias(name) 53 return query.count() != 0
54 55 @classmethod
56 - def get_group_by_fas_name(cls, fas_name):
57 return Group.query.filter(Group.fas_name == fas_name)
58 59 @classmethod
60 - def get_groups_by_fas_names_list(cls, fas_name_list):
61 return Group.query.filter(Group.fas_name.in_(fas_name_list))
62 63 @classmethod
64 - def get_groups_by_names_list(cls, name_list):
65 return Group.query.filter(Group.name.in_(name_list))
66 67 @classmethod
68 - def create_group_by_fas_name(cls, fas_name, alias=None):
69 if alias is None: 70 alias = fas_name 71 72 group = Group( 73 fas_name=fas_name, 74 name=alias, 75 ) 76 db.session.add(group) 77 return group
78 79 @classmethod
80 - def get_group_by_fas_name_or_create(cls, fas_name, alias=None):
81 mb_group = cls.get_group_by_fas_name(fas_name).first() 82 if mb_group is not None: 83 return mb_group 84 85 group = cls.create_group_by_fas_name(fas_name, alias) 86 db.session.flush() 87 return group
88 89 @classmethod
90 - def filter_blacklisted_teams(cls, teams):
91 """ removes blacklisted groups from teams list 92 :type teams: list of str 93 :return: filtered teams 94 :rtype: list of str 95 """ 96 blacklist = set(app.config.get("BLACKLISTED_GROUPS", [])) 97 return filter(lambda t: t not in blacklist, teams)
98 99 @classmethod
100 - def is_blacklisted_group(cls, fas_group):
101 if "BLACKLISTED_GROUPS" in app.config: 102 return fas_group in app.config["BLACKLISTED_GROUPS"] 103 else: 104 return False
105