| Conditions | 25 |
| Paths | 253 |
| Total Lines | 127 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 56 | protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||
| 57 | { |
||
| 58 | /** @var User $user */ |
||
| 59 | $user = $token->getUser(); |
||
| 60 | |||
| 61 | // make sure there is a user object (i.e. that the user is logged in) |
||
| 62 | if (!$user instanceof UserInterface) { |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (false === $subject) { |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Admins have access to everything. |
||
| 71 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 72 | return true; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** @var CGroup $group */ |
||
| 76 | $group = $subject; |
||
| 77 | |||
| 78 | // The group's parent is the course. |
||
| 79 | /** @var Course $course */ |
||
| 80 | $course = $group->getParent(); |
||
| 81 | |||
| 82 | if ($course->isHidden()) { |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (Course::REGISTERED === $course->getVisibility()) { |
||
| 87 | if (!$course->hasUser($user)) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($course->hasTeacher($user)) { |
||
| 93 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 94 | |||
| 95 | return true; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Legacy |
||
| 99 | //\GroupManager::userHasAccessToBrowse($user->getId(), $group); |
||
| 100 | $isTutor = $group->hasTutor($user); |
||
| 101 | |||
| 102 | switch ($attribute) { |
||
| 103 | case self::VIEW: |
||
| 104 | if ($isTutor) { |
||
| 105 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!$group->getStatus()) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | $userIsInGroup = $group->hasMember($user); |
||
| 115 | |||
| 116 | if ($userIsInGroup) { |
||
| 117 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_STUDENT); |
||
| 118 | } |
||
| 119 | |||
| 120 | $requestUri = ''; |
||
| 121 | // Check if user has access in legacy tool. |
||
| 122 | $request = $this->requestStack->getCurrentRequest(); |
||
| 123 | if ($request) { |
||
| 124 | $requestUri = $request->getRequestUri(); |
||
| 125 | } |
||
| 126 | |||
| 127 | $tools = [ |
||
| 128 | '/main/forum/' => $group->getForumState(), |
||
| 129 | '/documents/' => $group->getDocState(), |
||
| 130 | '/main/calendar/' => $group->getCalendarState(), |
||
| 131 | '/main/announcements/' => $group->getAnnouncementsState(), |
||
| 132 | '/main/work/' => $group->getWorkState(), |
||
| 133 | '/main/wiki/' => $group->getWikiState(), |
||
| 134 | /*'/main/group/group_space' => GroupManager::TOOL_PUBLIC, |
||
| 135 | '/main/inc/ajax/model.ajax.php' => GroupManager::TOOL_PUBLIC, |
||
| 136 | '/main/inc/ajax/announcement.ajax.php' => GroupManager::TOOL_PUBLIC,*/ |
||
| 137 | //'/main/chat/' => $group->getAnnouncementsState(), ?? |
||
| 138 | ]; |
||
| 139 | |||
| 140 | $toolStatus = GroupManager::TOOL_PUBLIC; |
||
| 141 | foreach ($tools as $path => $status) { |
||
| 142 | if (str_contains($requestUri, $path)) { |
||
| 143 | $toolStatus = $status; |
||
| 144 | |||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | switch ($toolStatus) { |
||
| 150 | case GroupManager::TOOL_NOT_AVAILABLE: |
||
| 151 | return false; |
||
| 152 | case GroupManager::TOOL_PUBLIC: |
||
| 153 | return true; |
||
| 154 | case GroupManager::TOOL_PRIVATE: |
||
| 155 | if ($userIsInGroup) { |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 159 | break; |
||
| 160 | case GroupManager::TOOL_PRIVATE_BETWEEN_USERS: |
||
| 161 | // Only works for announcements for now |
||
| 162 | if ($userIsInGroup && '/main/announcements/' === $path) { |
||
|
|
|||
| 163 | return true; |
||
| 164 | } |
||
| 165 | |||
| 166 | break; |
||
| 167 | } |
||
| 168 | |||
| 169 | break; |
||
| 170 | case self::EDIT: |
||
| 171 | case self::DELETE: |
||
| 172 | if ($isTutor) { |
||
| 173 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 174 | |||
| 175 | return true; |
||
| 176 | } |
||
| 177 | |||
| 178 | break; |
||
| 179 | } |
||
| 180 | //dump("You don't have access to this group!!"); |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 185 |