Code Duplication    Length = 36-36 lines in 2 locations

eZ/Publish/Core/Repository/RoleService.php 2 locations

@@ 933-968 (lines=36) @@
930
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
931
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
932
     */
933
    public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null)
934
    {
935
        if ($this->repository->canUser('role', 'assign', $userGroup, $role) !== true) {
936
            throw new UnauthorizedException('role', 'assign');
937
        }
938
939
        if ($roleLimitation === null) {
940
            $limitation = null;
941
        } else {
942
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
943
            if (!empty($limitationValidationErrors)) {
944
                throw new LimitationValidationException($limitationValidationErrors);
945
            }
946
947
            $limitation = array($roleLimitation->getIdentifier() => $roleLimitation->limitationValues);
948
        }
949
950
        // Check if objects exists
951
        $spiRole = $this->userHandler->loadRole($role->id);
952
        $loadedUserGroup = $this->repository->getUserService()->loadUserGroup($userGroup->id);
953
954
        $limitation = $this->checkAssignmentAndFilterLimitationValues($loadedUserGroup->id, $spiRole, $limitation);
955
956
        $this->repository->beginTransaction();
957
        try {
958
            $this->userHandler->assignRole(
959
                $loadedUserGroup->id,
960
                $spiRole->id,
961
                $limitation
962
            );
963
            $this->repository->commit();
964
        } catch (Exception $e) {
965
            $this->repository->rollback();
966
            throw $e;
967
        }
968
    }
969
970
    /**
971
     * removes a role from the given user group.
@@ 1022-1057 (lines=36) @@
1019
     * @param \eZ\Publish\API\Repository\Values\User\User $user
1020
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
1021
     */
1022
    public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null)
1023
    {
1024
        if ($this->repository->canUser('role', 'assign', $user, $role) !== true) {
1025
            throw new UnauthorizedException('role', 'assign');
1026
        }
1027
1028
        if ($roleLimitation === null) {
1029
            $limitation = null;
1030
        } else {
1031
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
1032
            if (!empty($limitationValidationErrors)) {
1033
                throw new LimitationValidationException($limitationValidationErrors);
1034
            }
1035
1036
            $limitation = array($roleLimitation->getIdentifier() => $roleLimitation->limitationValues);
1037
        }
1038
1039
        // Check if objects exists
1040
        $spiRole = $this->userHandler->loadRole($role->id);
1041
        $spiUser = $this->userHandler->load($user->id);
1042
1043
        $limitation = $this->checkAssignmentAndFilterLimitationValues($spiUser->id, $spiRole, $limitation);
1044
1045
        $this->repository->beginTransaction();
1046
        try {
1047
            $this->userHandler->assignRole(
1048
                $spiUser->id,
1049
                $spiRole->id,
1050
                $limitation
1051
            );
1052
            $this->repository->commit();
1053
        } catch (Exception $e) {
1054
            $this->repository->rollback();
1055
            throw $e;
1056
        }
1057
    }
1058
1059
    /**
1060
     * removes a role from the given user.