示例#1
0
    def test_user_has_no_role(self):
        user = self.user

        assign_role(user, 'sho_role1')
        remove_role(user)

        self.assertEquals(get_user_role(user), None)
    def test_user_has_no_role(self):
        user = self.user

        assign_role(user, 'sho_role1')
        remove_role(user)

        self.assertEquals(get_user_role(user), None)
示例#3
0
def delete_remove_role_from_user(sender, instance, **kwargs):  # pylint: disable=unused-argument
    """
    Signal handler that happens after a role removal is done.
    The role must be removed only if not correspondent to other programs.
    """
    # the reason why this check is "> 0" is because this happens AFTER the delete
    # there are no entries for the current value
    if Role.objects.filter(role=instance.role).count() > 0:
        return

    log.debug(
        'removing role % for user %s',
        instance.role,
        instance.user.username,
    )
    remove_role(instance.user)
    def assign_role_to_user(cls, user):
        """
        Deletes all of user's previous roles, and removes all permissions
        mentioned in their available_permissions property.

        :returns: :py:class:`django.contrib.auth.models.Group` The group for the
            new role.
        """
        from rolepermissions.shortcuts import remove_role

        remove_role(user)

        group, created = Group.objects.get_or_create(name=cls.get_name())
        user.groups.add(group)
        permissions_to_add = cls.get_default_true_permissions()
        user.user_permissions.add(*permissions_to_add)

        return group
    def assign_role_to_user(cls, user):
        """
        Deletes all of user's previous roles, and removes all permissions
        mentioned in their available_permissions property.

        :returns: :py:class:`django.contrib.auth.models.Group` The group for the
            new role.
        """
        from rolepermissions.shortcuts import remove_role

        remove_role(user)

        group, created = Group.objects.get_or_create(name=cls.get_name())
        user.groups.add(group)
        permissions_to_add = cls.get_default_true_permissions()
        user.user_permissions.add(*permissions_to_add)

        return group
示例#6
0
def save_remove_role_from_user(sender, instance, **kwargs):  # pylint: disable=unused-argument
    """
    Signal handler that happens before a role assignment is done.
    If the the save happens for a modification, the previous role must be removed
    if not correspondent to other programs.

    Theoretically this is not necessary with the current implementation of the
    django-role-permission library.
    """
    try:
        old_instance = Role.objects.get(pk=instance.pk)
    except Role.DoesNotExist:
        return
    # the reason why this check is "> 1" is because this happens BEFORE the save
    # so 1 entry is for the current value
    if Role.objects.filter(role=old_instance.role).count() > 1:
        return

    log.debug(
        'removing role % for user %s',
        instance.role,
        instance.user.username,
    )
    remove_role(instance.user)