|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* For licensing terms, see /license.txt */ |
| 6 | + |
| 7 | +namespace Chamilo\Tests\CoreBundle\Security\Authorization; |
| 8 | + |
| 9 | +use Chamilo\CoreBundle\Entity\Session; |
| 10 | +use Chamilo\CoreBundle\Entity\User; |
| 11 | +use Chamilo\CoreBundle\Entity\UserRelUser; |
| 12 | +use Chamilo\CoreBundle\Framework\Container; |
| 13 | +use Chamilo\CoreBundle\Repository\SessionRepository; |
| 14 | +use Chamilo\CoreBundle\Security\Authorization\LoginAsAuthorizationChecker; |
| 15 | +use Chamilo\Tests\AbstractApiTest; |
| 16 | +use Chamilo\Tests\ChamiloTestTrait; |
| 17 | + |
| 18 | +/** |
| 19 | + * Authorization policy coverage for "login as" (switch_user). |
| 20 | + * |
| 21 | + * Every decision below is taken with the legacy Container forced to null, reproducing the |
| 22 | + * request ordering in production: the Symfony firewall fires switch_user on kernel.request |
| 23 | + * (priority 8) before LegacyListener (priority 7) wires the legacy Container. The checker |
| 24 | + * must therefore resolve every path natively (User entity + injected repositories), never |
| 25 | + * through legacy helpers that would fatal with "Call to a member function get() on null". |
| 26 | + */ |
| 27 | +class LoginAsAuthorizationCheckerTest extends AbstractApiTest |
| 28 | +{ |
| 29 | + use ChamiloTestTrait; |
| 30 | + |
| 31 | + // --- Platform admin paths --------------------------------------------------------- |
| 32 | + |
| 33 | + public function testGlobalAdminCanImpersonateAdmin(): void |
| 34 | + { |
| 35 | + $impersonator = $this->createUser('login_as_global_admin', '', '', 'ROLE_GLOBAL_ADMIN'); |
| 36 | + $target = $this->createUser('login_as_target_admin', '', '', 'ROLE_ADMIN'); |
| 37 | + |
| 38 | + $this->assertTrue($this->decide($impersonator, $target)); |
| 39 | + } |
| 40 | + |
| 41 | + public function testPlainAdminCanImpersonateAdmin(): void |
| 42 | + { |
| 43 | + $impersonator = $this->createUser('login_as_plain_admin', '', '', 'ROLE_ADMIN'); |
| 44 | + $target = $this->createUser('login_as_other_admin', '', '', 'ROLE_ADMIN'); |
| 45 | + |
| 46 | + $this->assertTrue($this->decide($impersonator, $target)); |
| 47 | + } |
| 48 | + |
| 49 | + public function testPlainAdminCannotImpersonateGlobalAdmin(): void |
| 50 | + { |
| 51 | + $impersonator = $this->createUser('login_as_plain_admin_2', '', '', 'ROLE_ADMIN'); |
| 52 | + $target = $this->createUser('login_as_global_admin_target', '', '', 'ROLE_GLOBAL_ADMIN'); |
| 53 | + |
| 54 | + $this->assertFalse($this->decide($impersonator, $target)); |
| 55 | + } |
| 56 | + |
| 57 | + // --- HR manager (DRH) paths ------------------------------------------------------- |
| 58 | + |
| 59 | + public function testHrManagerCanImpersonateRrhhFollowedStudent(): void |
| 60 | + { |
| 61 | + $drh = $this->createUser('login_as_drh', '', '', 'ROLE_HR'); |
| 62 | + $student = $this->createUser('login_as_drh_student'); |
| 63 | + $this->linkRrhh($student, $drh); |
| 64 | + |
| 65 | + $this->assertTrue($this->decide($drh, $student)); |
| 66 | + } |
| 67 | + |
| 68 | + public function testHrManagerCannotImpersonateUnfollowedStudent(): void |
| 69 | + { |
| 70 | + $drh = $this->createUser('login_as_drh_2', '', '', 'ROLE_HR'); |
| 71 | + $student = $this->createUser('login_as_unrelated_student'); |
| 72 | + |
| 73 | + $this->assertFalse($this->decide($drh, $student)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Legacy parity: belonging to a session the DRH coaches must NOT grant impersonation |
| 78 | + * rights — only the direct RRHH relation does. |
| 79 | + */ |
| 80 | + public function testHrManagerCannotImpersonateUserInCoachedSession(): void |
| 81 | + { |
| 82 | + $drh = $this->createUser('login_as_drh_3', '', '', 'ROLE_HR'); |
| 83 | + $student = $this->createUser('login_as_session_student'); |
| 84 | + // DRH coaches the session and the student is enrolled, but there is no RRHH relation. |
| 85 | + $this->enrolStudentInDrhSession($student, $drh); |
| 86 | + |
| 87 | + $this->assertFalse($this->decide($drh, $student)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Privilege-escalation guard: a followed user holding a role the DRH lacks |
| 92 | + * (here ROLE_SESSION_MANAGER) must never be impersonable. |
| 93 | + */ |
| 94 | + public function testHrManagerCannotImpersonateFollowedSessionAdmin(): void |
| 95 | + { |
| 96 | + $drh = $this->createUser('login_as_drh_4', '', '', 'ROLE_HR'); |
| 97 | + $sessionAdmin = $this->createUser('login_as_followed_session_admin', '', '', 'ROLE_SESSION_MANAGER'); |
| 98 | + $this->linkRrhh($sessionAdmin, $drh); |
| 99 | + |
| 100 | + $this->assertFalse($this->decide($drh, $sessionAdmin)); |
| 101 | + } |
| 102 | + |
| 103 | + public function testHrManagerCannotImpersonateFollowedAdmin(): void |
| 104 | + { |
| 105 | + $drh = $this->createUser('login_as_drh_5', '', '', 'ROLE_HR'); |
| 106 | + $admin = $this->createUser('login_as_followed_admin', '', '', 'ROLE_ADMIN'); |
| 107 | + $this->linkRrhh($admin, $drh); |
| 108 | + |
| 109 | + $this->assertFalse($this->decide($drh, $admin)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * A teacher is not "higher" than a DRH: ROLE_HR already reaches ROLE_TEACHER through the |
| 114 | + * role hierarchy, so impersonating a followed teacher is not an escalation. |
| 115 | + */ |
| 116 | + public function testHrManagerCanImpersonateFollowedTeacher(): void |
| 117 | + { |
| 118 | + $drh = $this->createUser('login_as_drh_6', '', '', 'ROLE_HR'); |
| 119 | + $teacher = $this->createUser('login_as_followed_teacher', '', '', 'ROLE_TEACHER'); |
| 120 | + $this->linkRrhh($teacher, $drh); |
| 121 | + |
| 122 | + $this->assertTrue($this->decide($drh, $teacher)); |
| 123 | + } |
| 124 | + |
| 125 | + // --- Helpers ---------------------------------------------------------------------- |
| 126 | + |
| 127 | + /** |
| 128 | + * Runs canLoginAs() with the legacy Container forced to null, proving the decision is |
| 129 | + * free of any legacy-Container dependency. |
| 130 | + */ |
| 131 | + private function decide(User $impersonator, User $target): bool |
| 132 | + { |
| 133 | + /** @var LoginAsAuthorizationChecker $checker */ |
| 134 | + $checker = self::getContainer()->get(LoginAsAuthorizationChecker::class); |
| 135 | + |
| 136 | + $previous = Container::$container; |
| 137 | + Container::$container = null; |
| 138 | + |
| 139 | + try { |
| 140 | + return $checker->canLoginAs($impersonator, $target); |
| 141 | + } finally { |
| 142 | + Container::$container = $previous; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private function linkRrhh(User $student, User $drh): void |
| 147 | + { |
| 148 | + $em = $this->getEntityManager(); |
| 149 | + $em->persist( |
| 150 | + (new UserRelUser()) |
| 151 | + ->setUser($student) |
| 152 | + ->setFriend($drh) |
| 153 | + ->setRelationType(UserRelUser::USER_RELATION_TYPE_RRHH) |
| 154 | + ); |
| 155 | + $em->flush(); |
| 156 | + } |
| 157 | + |
| 158 | + private function enrolStudentInDrhSession(User $student, User $drh): void |
| 159 | + { |
| 160 | + $sessionRepo = self::getContainer()->get(SessionRepository::class); |
| 161 | + |
| 162 | + $course = $this->createCourse('LoginAs DRH course'); |
| 163 | + $session = $this->createSession('LoginAs DRH session'); |
| 164 | + $session |
| 165 | + ->addCourse($course) |
| 166 | + ->addUserInSession(Session::DRH, $drh) |
| 167 | + ; |
| 168 | + $sessionRepo->update($session); |
| 169 | + |
| 170 | + $sessionRepo->addUserInCourse(Session::STUDENT, $student, $course, $session); |
| 171 | + $sessionRepo->update($session); |
| 172 | + } |
| 173 | +} |
0 commit comments