Code Duplication    Length = 36-36 lines in 2 locations

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

@@ 959-994 (lines=36) @@
956
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
957
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
958
     */
959
    public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null)
960
    {
961
        if ($this->repository->canUser('role', 'assign', $userGroup, $role) !== true) {
962
            throw new UnauthorizedException('role', 'assign');
963
        }
964
965
        if ($roleLimitation === null) {
966
            $limitation = null;
967
        } else {
968
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
969
            if (!empty($limitationValidationErrors)) {
970
                throw new LimitationValidationException($limitationValidationErrors);
971
            }
972
973
            $limitation = array($roleLimitation->getIdentifier() => $roleLimitation->limitationValues);
974
        }
975
976
        // Check if objects exists
977
        $spiRole = $this->userHandler->loadRole($role->id);
978
        $loadedUserGroup = $this->repository->getUserService()->loadUserGroup($userGroup->id);
979
980
        $limitation = $this->checkAssignmentAndFilterLimitationValues($loadedUserGroup->id, $spiRole, $limitation);
981
982
        $this->repository->beginTransaction();
983
        try {
984
            $this->userHandler->assignRole(
985
                $loadedUserGroup->id,
986
                $spiRole->id,
987
                $limitation
988
            );
989
            $this->repository->commit();
990
        } catch (Exception $e) {
991
            $this->repository->rollback();
992
            throw $e;
993
        }
994
    }
995
996
    /**
997
     * removes a role from the given user group.
@@ 1048-1083 (lines=36) @@
1045
     * @param \eZ\Publish\API\Repository\Values\User\User $user
1046
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
1047
     */
1048
    public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null)
1049
    {
1050
        if ($this->repository->canUser('role', 'assign', $user, $role) !== true) {
1051
            throw new UnauthorizedException('role', 'assign');
1052
        }
1053
1054
        if ($roleLimitation === null) {
1055
            $limitation = null;
1056
        } else {
1057
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
1058
            if (!empty($limitationValidationErrors)) {
1059
                throw new LimitationValidationException($limitationValidationErrors);
1060
            }
1061
1062
            $limitation = array($roleLimitation->getIdentifier() => $roleLimitation->limitationValues);
1063
        }
1064
1065
        // Check if objects exists
1066
        $spiRole = $this->userHandler->loadRole($role->id);
1067
        $spiUser = $this->userHandler->load($user->id);
1068
1069
        $limitation = $this->checkAssignmentAndFilterLimitationValues($spiUser->id, $spiRole, $limitation);
1070
1071
        $this->repository->beginTransaction();
1072
        try {
1073
            $this->userHandler->assignRole(
1074
                $spiUser->id,
1075
                $spiRole->id,
1076
                $limitation
1077
            );
1078
            $this->repository->commit();
1079
        } catch (Exception $e) {
1080
            $this->repository->rollback();
1081
            throw $e;
1082
        }
1083
    }
1084
1085
    /**
1086
     * removes a role from the given user.