| Conditions | 7 |
| Paths | 28 |
| Total Lines | 75 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function isAllowed( |
||
| 26 | string $attribute, |
||
| 27 | ResourceLink $resourceLink, |
||
| 28 | iterable $rights, |
||
| 29 | bool $allowAnonsToView, |
||
| 30 | ): bool { |
||
| 31 | // Creating roles |
||
| 32 | $anon = new GenericRole('IS_AUTHENTICATED_ANONYMOUSLY'); |
||
| 33 | $userRole = new GenericRole('ROLE_USER'); |
||
| 34 | $student = new GenericRole('ROLE_STUDENT'); |
||
| 35 | $teacher = new GenericRole('ROLE_TEACHER'); |
||
| 36 | $studentBoss = new GenericRole('ROLE_STUDENT_BOSS'); |
||
| 37 | |||
| 38 | $currentStudent = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 39 | $currentTeacher = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 40 | |||
| 41 | $currentStudentGroup = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_STUDENT); |
||
| 42 | $currentTeacherGroup = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 43 | |||
| 44 | $currentStudentSession = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_STUDENT); |
||
| 45 | $currentTeacherSession = new GenericRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 46 | |||
| 47 | // Setting Simple ACL. |
||
| 48 | $acl = (new Acl()) |
||
| 49 | ->addRole($anon) |
||
| 50 | ->addRole($userRole) |
||
| 51 | ->addRole($student) |
||
| 52 | ->addRole($teacher) |
||
| 53 | ->addRole($studentBoss) |
||
| 54 | |||
| 55 | ->addRole($currentStudent) |
||
| 56 | ->addRole($currentTeacher, ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT) |
||
| 57 | |||
| 58 | ->addRole($currentStudentSession) |
||
| 59 | ->addRole($currentTeacherSession, ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_STUDENT) |
||
| 60 | |||
| 61 | ->addRole($currentStudentGroup) |
||
| 62 | ->addRole($currentTeacherGroup, ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_STUDENT) |
||
| 63 | ; |
||
| 64 | |||
| 65 | // Add a security resource. |
||
| 66 | $acl->addResource(new GenericResource((string) $resourceLink->getId())); |
||
| 67 | |||
| 68 | // Check all the right this link has. |
||
| 69 | // Set rights from the ResourceRight. |
||
| 70 | foreach ($rights as $right) { |
||
| 71 | $acl->allow($right->getRole(), null, (string) $right->getMask()); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Anons can see. |
||
| 75 | if ($allowAnonsToView) { |
||
| 76 | $acl->allow($anon, null, (string) ResourceNodeVoter::getReaderMask()); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Asked mask |
||
| 80 | $mask = new MaskBuilder(); |
||
| 81 | $mask->add($attribute); |
||
| 82 | |||
| 83 | $askedMask = (string) $mask->get(); |
||
| 84 | |||
| 85 | if ($this->security->getToken() instanceof NullToken) { |
||
| 86 | return (bool) $acl->isAllowed('IS_AUTHENTICATED_ANONYMOUSLY', $resourceLink->getId(), $askedMask); |
||
| 87 | } |
||
| 88 | |||
| 89 | $user = $this->security->getUser(); |
||
| 90 | |||
| 91 | $roles = $user instanceof UserInterface ? $user->getRoles() : []; |
||
| 92 | |||
| 93 | foreach ($roles as $role) { |
||
| 94 | if ($acl->isAllowed($role, $resourceLink->getId(), $askedMask)) { |
||
| 95 | return true; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return false; |
||
| 100 | } |
||
| 101 | } |