users_group.py
192 lines
| 6.3 KiB
| text/x-python
|
PythonLexer
Nicolas VINOT
|
r1586 | # -*- coding: utf-8 -*- | ||
""" | ||||
rhodecode.model.users_group | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
Mads Kiilerich
|
r3410 | user group model for RhodeCode | ||
Nicolas VINOT
|
r1586 | |||
:created_on: Oct 1, 2011 | ||||
:author: nvinot | ||||
:copyright: (C) 2011-2011 Nicolas Vinot <aeris@imirhil.fr> | ||||
r1824 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |||
Nicolas VINOT
|
r1586 | :license: GPLv3, see COPYING for more details. | ||
""" | ||||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
import logging | ||||
import traceback | ||||
r1692 | from rhodecode.model import BaseModel | |||
Mads Kiilerich
|
r3417 | from rhodecode.model.db import UserGroupMember, UserGroup,\ | ||
UserGroupRepoToPerm, Permission, UserGroupToPerm, User | ||||
from rhodecode.lib.exceptions import UserGroupsAssignedException | ||||
Nicolas VINOT
|
r1586 | |||
Nicolas VINOT
|
r1593 | log = logging.getLogger(__name__) | ||
Nicolas VINOT
|
r1586 | |||
r1716 | ||||
Mads Kiilerich
|
r3417 | class UserGroupModel(BaseModel): | ||
Nicolas VINOT
|
r1586 | |||
Mads Kiilerich
|
r3417 | cls = UserGroup | ||
r2524 | ||||
r1713 | def __get_users_group(self, users_group): | |||
Mads Kiilerich
|
r3417 | return self._get_instance(UserGroup, users_group, | ||
callback=UserGroup.get_by_group_name) | ||||
r1982 | ||||
r1716 | def get(self, users_group_id, cache=False): | |||
Mads Kiilerich
|
r3417 | return UserGroup.get(users_group_id) | ||
Nicolas VINOT
|
r1586 | |||
r2522 | def get_group(self, users_group): | |||
return self.__get_users_group(users_group) | ||||
r1716 | def get_by_name(self, name, cache=False, case_insensitive=False): | |||
Mads Kiilerich
|
r3417 | return UserGroup.get_by_group_name(name, cache, case_insensitive) | ||
Nicolas VINOT
|
r1586 | |||
r1734 | def create(self, name, active=True): | |||
r1749 | try: | |||
Mads Kiilerich
|
r3417 | new = UserGroup() | ||
r1749 | new.users_group_name = name | |||
new.users_group_active = active | ||||
self.sa.add(new) | ||||
return new | ||||
except: | ||||
log.error(traceback.format_exc()) | ||||
raise | ||||
def update(self, users_group, form_data): | ||||
try: | ||||
users_group = self.__get_users_group(users_group) | ||||
for k, v in form_data.items(): | ||||
if k == 'users_group_members': | ||||
users_group.members = [] | ||||
self.sa.flush() | ||||
members_list = [] | ||||
if v: | ||||
v = [v] if isinstance(v, basestring) else v | ||||
for u_id in set(v): | ||||
Mads Kiilerich
|
r3417 | member = UserGroupMember(users_group.users_group_id, u_id) | ||
r1749 | members_list.append(member) | |||
setattr(users_group, 'members', members_list) | ||||
setattr(users_group, k, v) | ||||
self.sa.add(users_group) | ||||
except: | ||||
log.error(traceback.format_exc()) | ||||
raise | ||||
r1713 | ||||
r1982 | def delete(self, users_group, force=False): | |||
""" | ||||
Deletes repos group, unless force flag is used | ||||
raises exception if there are members in that group, else deletes | ||||
group and users | ||||
:param users_group: | ||||
:param force: | ||||
""" | ||||
r1749 | try: | |||
users_group = self.__get_users_group(users_group) | ||||
r1818 | ||||
r1749 | # check if this group is not assigned to repo | |||
Mads Kiilerich
|
r3417 | assigned_groups = UserGroupRepoToPerm.query()\ | ||
.filter(UserGroupRepoToPerm.users_group == users_group).all() | ||||
r1749 | ||||
Mads Kiilerich
|
r3625 | if assigned_groups and not force: | ||
Mads Kiilerich
|
r3417 | raise UserGroupsAssignedException('RepoGroup assigned to %s' % | ||
r1749 | assigned_groups) | |||
r1818 | ||||
r1749 | self.sa.delete(users_group) | |||
except: | ||||
log.error(traceback.format_exc()) | ||||
raise | ||||
r1713 | ||||
Nicolas VINOT
|
r1593 | def add_user_to_group(self, users_group, user): | ||
r1989 | users_group = self.__get_users_group(users_group) | |||
r2432 | user = self._get_user(user) | |||
r1989 | ||||
Nicolas VINOT
|
r1589 | for m in users_group.members: | ||
u = m.user | ||||
if u.user_id == user.user_id: | ||||
r1989 | return True | |||
Nicolas VINOT
|
r1589 | |||
Nicolas VINOT
|
r1586 | try: | ||
Mads Kiilerich
|
r3417 | users_group_member = UserGroupMember() | ||
Nicolas VINOT
|
r1586 | users_group_member.user = user | ||
users_group_member.users_group = users_group | ||||
Nicolas VINOT
|
r1593 | users_group.members.append(users_group_member) | ||
user.group_member.append(users_group_member) | ||||
Nicolas VINOT
|
r1587 | |||
Nicolas VINOT
|
r1593 | self.sa.add(users_group_member) | ||
Nicolas VINOT
|
r1586 | return users_group_member | ||
except: | ||||
Nicolas VINOT
|
r1593 | log.error(traceback.format_exc()) | ||
Nicolas VINOT
|
r1587 | raise | ||
r1749 | ||||
r1989 | def remove_user_from_group(self, users_group, user): | |||
users_group = self.__get_users_group(users_group) | ||||
r2432 | user = self._get_user(user) | |||
r1989 | ||||
users_group_member = None | ||||
for m in users_group.members: | ||||
if m.user.user_id == user.user_id: | ||||
# Found this user's membership row | ||||
users_group_member = m | ||||
break | ||||
if users_group_member: | ||||
try: | ||||
self.sa.delete(users_group_member) | ||||
return True | ||||
except: | ||||
log.error(traceback.format_exc()) | ||||
raise | ||||
else: | ||||
# User isn't in that group | ||||
return False | ||||
r1749 | def has_perm(self, users_group, perm): | |||
users_group = self.__get_users_group(users_group) | ||||
r2432 | perm = self._get_perm(perm) | |||
r1749 | ||||
Mads Kiilerich
|
r3417 | return UserGroupToPerm.query()\ | ||
.filter(UserGroupToPerm.users_group == users_group)\ | ||||
.filter(UserGroupToPerm.permission == perm).scalar() is not None | ||||
r1749 | ||||
def grant_perm(self, users_group, perm): | ||||
users_group = self.__get_users_group(users_group) | ||||
r2709 | perm = self._get_perm(perm) | |||
r1749 | ||||
r2078 | # if this permission is already granted skip it | |||
Mads Kiilerich
|
r3417 | _perm = UserGroupToPerm.query()\ | ||
.filter(UserGroupToPerm.users_group == users_group)\ | ||||
.filter(UserGroupToPerm.permission == perm)\ | ||||
r2078 | .scalar() | |||
if _perm: | ||||
return | ||||
Mads Kiilerich
|
r3417 | new = UserGroupToPerm() | ||
r1749 | new.users_group = users_group | |||
new.permission = perm | ||||
self.sa.add(new) | ||||
def revoke_perm(self, users_group, perm): | ||||
users_group = self.__get_users_group(users_group) | ||||
r2432 | perm = self._get_perm(perm) | |||
r1818 | ||||
Mads Kiilerich
|
r3417 | obj = UserGroupToPerm.query()\ | ||
.filter(UserGroupToPerm.users_group == users_group)\ | ||||
.filter(UserGroupToPerm.permission == perm).scalar() | ||||
r1932 | if obj: | |||
self.sa.delete(obj) | ||||