def handle_delete(self, request, user, *args, **kwargs):
        """Treat DELETE requests to dissociate User and Group.

        URL: usergroup/user/<id_user>/ugroup/<id_group>/dissociate/
        """

        try:

            self.log.info('Dissociate User and Group.')

            # User permission
            if not has_perm(user, AdminPermission.USER_ADMINISTRATION,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_user = kwargs.get('id_user')
            id_group = kwargs.get('id_group')

            # Valid ID User
            if not is_valid_int_greater_zero_param(id_user):
                self.log.error(
                    u'The id_user parameter is not a valid value: %s.',
                    id_user)
                raise InvalidValueError(None, 'id_user', id_user)

            # Valid ID Group
            if not is_valid_int_greater_zero_param(id_group):
                self.log.error(
                    u'The id_group parameter is not a valid value: %s.',
                    id_group)
                raise InvalidValueError(None, 'id_group', id_group)

            # Find User by ID to check if it exist
            Usuario.get_by_pk(id_user)

            # Find Group by ID to check if it exist
            UGrupo.get_by_pk(id_group)

            # Find UserGroup by ID to check if it exist
            user_group = UsuarioGrupo.get_by_user_group(id_user, id_group)

            with distributedlock(LOCK_USER_GROUP % (id_user, id_group)):

                try:

                    # remove UserGroup
                    user_group.delete()

                except Exception, e:
                    self.log.error(u'Failed to remove the UserGroup.')
                    raise GrupoError(e, u'Failed to remove the UserGroup.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat DELETE requests to dissociate User and Group.

        URL: usergroup/user/<id_user>/ugroup/<id_group>/dissociate/
        """

        try:

            self.log.info('Dissociate User and Group.')

            # User permission
            if not has_perm(user, AdminPermission.USER_ADMINISTRATION, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_user = kwargs.get('id_user')
            id_group = kwargs.get('id_group')

            # Valid ID User
            if not is_valid_int_greater_zero_param(id_user):
                self.log.error(
                    u'The id_user parameter is not a valid value: %s.', id_user)
                raise InvalidValueError(None, 'id_user', id_user)

            # Valid ID Group
            if not is_valid_int_greater_zero_param(id_group):
                self.log.error(
                    u'The id_group parameter is not a valid value: %s.', id_group)
                raise InvalidValueError(None, 'id_group', id_group)

            # Find User by ID to check if it exist
            Usuario.get_by_pk(id_user)

            # Find Group by ID to check if it exist
            UGrupo.get_by_pk(id_group)

            # Find UserGroup by ID to check if it exist
            user_group = UsuarioGrupo.get_by_user_group(id_user, id_group)

            with distributedlock(LOCK_USER_GROUP % (id_user, id_group)):

                try:

                    # remove UserGroup
                    user_group.delete()

                except Exception, e:
                    self.log.error(u'Failed to remove the UserGroup.')
                    raise GrupoError(e, u'Failed to remove the UserGroup.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_put(self, request, user, *args, **kwargs):
        """Treat PUT requests to associate User and Group.

        URL: usergroup/user/<id_user>/ugroup/<id_group>/associate/
        """

        try:

            self.log.info("Associate User and Group.")

            # User permission
            if not has_perm(user, AdminPermission.USER_ADMINISTRATION, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_user = kwargs.get('id_user')
            id_group = kwargs.get('id_group')

            # Valid ID User
            if not is_valid_int_greater_zero_param(id_user):
                self.log.error(
                    u'The id_user parameter is not a valid value: %s.', id_user)
                raise InvalidValueError(None, 'id_user', id_user)

            # Valid ID Group
            if not is_valid_int_greater_zero_param(id_group):
                self.log.error(
                    u'The id_group parameter is not a valid value: %s.', id_group)
                raise InvalidValueError(None, 'id_group', id_group)

            # Find User by ID to check if it exist
            usr = Usuario.get_by_pk(id_user)

            # Find Group by ID to check if it exist
            group = UGrupo.get_by_pk(id_group)

            try:

                # Find UserGroup by ID to check if it exist
                user_group = UsuarioGrupo.get_by_user_group(id_user, id_group)
                raise UsuarioGrupoDuplicatedError(
                    None, u'Usuário já está associado ao Grupo.')
            except UserGroupNotFoundError:
                pass

            user_group = UsuarioGrupo()

            # set variables
            user_group.usuario = usr
            user_group.ugrupo = group

            try:
                # save UserGroup
                user_group.save(user)
            except Exception, e:
                self.log.error(u'Failed to save the UserGroup.')
                raise UsuarioError(e, u'Failed to save the UserGroup.')

            usr_grp_map = dict()
            usr_grp_map['user_group'] = model_to_dict(
                usr, exclude=["usuario", "ugrupo"])

            return self.response(dumps_networkapi(usr_grp_map))
Exemplo n.º 4
0
    def handle_put(self, request, user, *args, **kwargs):
        """Treat PUT requests to associate User and Group.

        URL: usergroup/user/<id_user>/ugroup/<id_group>/associate/
        """

        try:

            self.log.info("Associate User and Group.")

            # User permission
            if not has_perm(user, AdminPermission.USER_ADMINISTRATION,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_user = kwargs.get('id_user')
            id_group = kwargs.get('id_group')

            # Valid ID User
            if not is_valid_int_greater_zero_param(id_user):
                self.log.error(
                    u'The id_user parameter is not a valid value: %s.',
                    id_user)
                raise InvalidValueError(None, 'id_user', id_user)

            # Valid ID Group
            if not is_valid_int_greater_zero_param(id_group):
                self.log.error(
                    u'The id_group parameter is not a valid value: %s.',
                    id_group)
                raise InvalidValueError(None, 'id_group', id_group)

            # Find User by ID to check if it exist
            usr = Usuario.get_by_pk(id_user)

            # Find Group by ID to check if it exist
            group = UGrupo.get_by_pk(id_group)

            try:

                # Find UserGroup by ID to check if it exist
                user_group = UsuarioGrupo.get_by_user_group(id_user, id_group)
                raise UsuarioGrupoDuplicatedError(
                    None, u'Usuário já está associado ao Grupo.')
            except UserGroupNotFoundError:
                pass

            user_group = UsuarioGrupo()

            # set variables
            user_group.usuario = usr
            user_group.ugrupo = group

            try:
                # save UserGroup
                user_group.save()
            except Exception, e:
                self.log.error(u'Failed to save the UserGroup.')
                raise UsuarioError(e, u'Failed to save the UserGroup.')

            usr_grp_map = dict()
            usr_grp_map['user_group'] = model_to_dict(
                usr, exclude=["usuario", "ugrupo"])

            return self.response(dumps_networkapi(usr_grp_map))