Typos and small changes.

This commit is contained in:
Norman Jäckel 2017-01-26 21:15:35 +01:00
parent 2daafa8db9
commit ad937aecb3
3 changed files with 27 additions and 17 deletions

View File

@ -102,7 +102,7 @@ Other:
- Accelerated startup process (send all data to the client after login). - Accelerated startup process (send all data to the client after login).
- Added function utils.auth.anonymous_is_enabled which returns true, if it is. - Added function utils.auth.anonymous_is_enabled which returns true, if it is.
- Changed has_perm to support an user id or None (for anyonmous) as first argument. - Changed has_perm to support an user id or None (for anyonmous) as first argument.
- Removed our AnonymousUser. Please make sure not to use user.has_perm() anymore. - Removed our AnonymousUser. Make sure not to use user.has_perm() anymore.
Version 2.0 (2016-04-18) Version 2.0 (2016-04-18)

View File

@ -216,6 +216,12 @@ class User(RESTModelMixin, PermissionsMixin, AbstractBaseUser):
self.structure_level, self.structure_level,
self.about_me)) self.about_me))
def has_perm(self, perm):
"""
This method is closed. Do not use it but use openslides.utils.auth.has_perm.
"""
raise RuntimeError('Do not use user.has_perm() but use openslides.utils.auth.has_perm')
class GroupManager(GroupManager): class GroupManager(GroupManager):
""" """

View File

@ -8,22 +8,24 @@ def has_perm(user, perm):
""" """
Checks that user has a specific permission. Checks that user has a specific permission.
User can be an a CollectionElement for a user or None. User can be a CollectionElement of a user or None.
""" """
group_collection_string = 'users/group' # This is the hard coded collection string for openslides.users.models.Group
# Convert user to right type # Convert user to right type
user = user_to_collection_user(user) user = user_to_collection_user(user)
if user is None and not anonymous_is_enabled(): if user is None and not anonymous_is_enabled():
has_perm = False has_perm = False
elif user is None: elif user is None:
# Use the permissions from the default group. # Use the permissions from the default group with id 1.
default_group = CollectionElement.from_values('users/group', 1) default_group = CollectionElement.from_values(group_collection_string, 1)
has_perm = perm in default_group.get_full_data()['permissions'] has_perm = perm in default_group.get_full_data()['permissions']
else: else:
# Get all groups of the user and then see, if one group has the required # Get all groups of the user and then see, if one group has the required
# permission. If the user has no groups, then use group 1. # permission. If the user has no groups, then use group 1.
group_ids = user.get_full_data()['groups_id'] or [1] group_ids = user.get_full_data()['groups_id'] or [1]
for group_id in group_ids: for group_id in group_ids:
group = CollectionElement.from_values('users/group', group_id) group = CollectionElement.from_values(group_collection_string, group_id)
if perm in group.get_full_data()['permissions']: if perm in group.get_full_data()['permissions']:
has_perm = True has_perm = True
break break
@ -34,7 +36,7 @@ def has_perm(user, perm):
def anonymous_is_enabled(): def anonymous_is_enabled():
""" """
Returns true, when the anonymous user is enabled in the settings. Returns True if the anonymous user is enabled in the settings.
""" """
return (CollectionElement.from_values('core/config', 'general_system_enable_anonymous') return (CollectionElement.from_values('core/config', 'general_system_enable_anonymous')
.get_full_data()['value']) .get_full_data()['value'])
@ -42,21 +44,23 @@ def anonymous_is_enabled():
def user_to_collection_user(user): def user_to_collection_user(user):
""" """
Taks an object, that represents a user an converts it to a collection_element Takes an object, that represents a user and converts it to a CollectionElement
or None, if it is an anonymous user. or to None, if it is an anonymous user.
User can be User can be
* a user object, * an user object,
* a collection_element for an user * a CollectionElement of an user,
* an user id * an user id or
* an anonymous user. * an anonymous user.
Raises an TypeError, if the given user object can not be converted Raises an TypeError, if the given user object can not be converted.
""" """
User = get_user_model()
if user is None: if user is None:
# Nothing to do # Nothing to do
pass pass
elif isinstance(user, CollectionElement) and user.collection_string == 'users/user': elif isinstance(user, CollectionElement) and user.collection_string == User.get_collection_string():
# Nothing to do # Nothing to do
pass pass
elif isinstance(user, CollectionElement): elif isinstance(user, CollectionElement):
@ -64,15 +68,15 @@ def user_to_collection_user(user):
"Unsupported type for user. Only CollectionElements for users can be" "Unsupported type for user. Only CollectionElements for users can be"
"used. Not {}".format(user.collection_string)) "used. Not {}".format(user.collection_string))
elif isinstance(user, int): elif isinstance(user, int):
user = CollectionElement.from_values('users/user', user) user = CollectionElement.from_values(User.get_collection_string(), user)
elif isinstance(user, AnonymousUser): elif isinstance(user, AnonymousUser):
user = None user = None
elif isinstance(user, get_user_model()): elif isinstance(user, User):
# Converts a user object to a collection element. # Converts a user object to a collection element.
# from_instance can not be used because the user serializer loads # from_instance can not be used because the user serializer loads
# the group from the db. So each call to from_instance(user) consts # the group from the db. So each call to from_instance(user) costs
# one db query. # one db query.
user = CollectionElement.from_values('users/user', user.id) user = CollectionElement.from_values(User.get_collection_string(), user.id)
else: else:
raise TypeError( raise TypeError(
"Unsupported type for user. User {} has type {}.".format(user, type(user))) "Unsupported type for user. User {} has type {}.".format(user, type(user)))