eZ\Publish\Core\Repository\UserService::unAssignUserFromUserGroup PHP Method

unAssignUserFromUserGroup() public method

Removes a user group from the user.
public unAssignUserFromUserGroup ( eZ\Publish\API\Repository\Values\User\User $user, eZ\Publish\API\Repository\Values\User\UserGroup $userGroup )
$user eZ\Publish\API\Repository\Values\User\User
$userGroup eZ\Publish\API\Repository\Values\User\UserGroup
    public function unAssignUserFromUserGroup(APIUser $user, APIUserGroup $userGroup)
    {
        $loadedUser = $this->loadUser($user->id);
        $loadedGroup = $this->loadUserGroup($userGroup->id);
        $locationService = $this->repository->getLocationService();
        $userLocations = $locationService->loadLocations($loadedUser->getVersionInfo()->getContentInfo());
        if (empty($userLocations)) {
            throw new BadStateException('user', 'user has no locations, cannot unassign from group');
        }
        if ($loadedGroup->getVersionInfo()->getContentInfo()->mainLocationId === null) {
            throw new BadStateException('userGroup', 'user group has no main location or no locations, cannot unassign');
        }
        foreach ($userLocations as $userLocation) {
            if ($userLocation->parentLocationId == $loadedGroup->getVersionInfo()->getContentInfo()->mainLocationId) {
                // Throw this specific BadState when we know argument is valid
                if (count($userLocations) === 1) {
                    throw new BadStateException('user', 'user only has one user group, cannot unassign from last group');
                }
                $this->repository->beginTransaction();
                try {
                    $locationService->deleteLocation($userLocation);
                    $this->repository->commit();
                    return;
                } catch (Exception $e) {
                    $this->repository->rollback();
                    throw $e;
                }
            }
        }
        throw new InvalidArgumentException('userGroup', 'user is not in the given user group');
    }