예제 #1
0
    def post(self, request, group_name, user_name):
        """
        Adds a user to a group
        Args:
            request: Django rest framework request
            group_name: Group name from the request
            user_name: User name from the request

        Returns:
            Http status of the request

        """
        try:
            if group_name == PUBLIC_GRP or group_name == ADMIN_GRP:
                return BossHTTPError(
                    'Cannot add a member to the group {}. This is an admin managed group'
                    .format(group_name), ErrorCodes.BAD_REQUEST)

            group = Group.objects.get(name=group_name)
            bgroup = BossGroup.objects.get(group=group)

            # Check the users permissions.
            if request.user.has_perm("maintain_group", bgroup):
                usr = User.objects.get(username=user_name)
                bgroup.group.user_set.add(usr)
                return HttpResponse(status=204)
            else:
                return BossHTTPError(
                    'The user {} does not have the {} permission on the group {}'
                    .format(request.user.username, 'maintain_group',
                            group_name), ErrorCodes.MISSING_PERMISSION)

        except Group.DoesNotExist:
            return BossGroupNotFoundError(group_name)
        except User.DoesNotExist:
            return BossUserNotFoundError(user_name)