Code Duplication    Length = 36-36 lines in 2 locations

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

@@ 643-678 (lines=36) @@
640
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
641
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
642
     */
643
    public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null)
644
    {
645
        if ($this->permissionResolver->canUser('role', 'assign', $userGroup, [$role]) !== true) {
646
            throw new UnauthorizedException('role', 'assign');
647
        }
648
649
        if ($roleLimitation === null) {
650
            $limitation = null;
651
        } else {
652
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
653
            if (!empty($limitationValidationErrors)) {
654
                throw new LimitationValidationException($limitationValidationErrors);
655
            }
656
657
            $limitation = [$roleLimitation->getIdentifier() => $roleLimitation->limitationValues];
658
        }
659
660
        // Check if objects exists
661
        $spiRole = $this->userHandler->loadRole($role->id);
662
        $loadedUserGroup = $this->repository->getUserService()->loadUserGroup($userGroup->id);
663
664
        $limitation = $this->checkAssignmentAndFilterLimitationValues($loadedUserGroup->id, $spiRole, $limitation);
665
666
        $this->repository->beginTransaction();
667
        try {
668
            $this->userHandler->assignRole(
669
                $loadedUserGroup->id,
670
                $spiRole->id,
671
                $limitation
672
            );
673
            $this->repository->commit();
674
        } catch (Exception $e) {
675
            $this->repository->rollback();
676
            throw $e;
677
        }
678
    }
679
680
    /**
681
     * Assigns a role to the given user.
@@ 691-726 (lines=36) @@
688
     * @param \eZ\Publish\API\Repository\Values\User\User $user
689
     * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
690
     */
691
    public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null)
692
    {
693
        if ($this->permissionResolver->canUser('role', 'assign', $user, [$role]) !== true) {
694
            throw new UnauthorizedException('role', 'assign');
695
        }
696
697
        if ($roleLimitation === null) {
698
            $limitation = null;
699
        } else {
700
            $limitationValidationErrors = $this->limitationService->validateLimitation($roleLimitation);
701
            if (!empty($limitationValidationErrors)) {
702
                throw new LimitationValidationException($limitationValidationErrors);
703
            }
704
705
            $limitation = [$roleLimitation->getIdentifier() => $roleLimitation->limitationValues];
706
        }
707
708
        // Check if objects exists
709
        $spiRole = $this->userHandler->loadRole($role->id);
710
        $spiUser = $this->userHandler->load($user->id);
711
712
        $limitation = $this->checkAssignmentAndFilterLimitationValues($spiUser->id, $spiRole, $limitation);
713
714
        $this->repository->beginTransaction();
715
        try {
716
            $this->userHandler->assignRole(
717
                $spiUser->id,
718
                $spiRole->id,
719
                $limitation
720
            );
721
            $this->repository->commit();
722
        } catch (Exception $e) {
723
            $this->repository->rollback();
724
            throw $e;
725
        }
726
    }
727
728
    /**
729
     * Removes the given role assignment.