| Conditions | 25 |
| Paths | 120 |
| Total Lines | 163 |
| Code Lines | 78 |
| 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 |
||
| 105 | protected function voteOnAttribute($attribute, $resourceNode, TokenInterface $token): bool |
||
| 106 | { |
||
| 107 | $user = $token->getUser(); |
||
| 108 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 109 | if (!$user instanceof UserInterface) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | |||
| 113 | // Checking admin roles |
||
| 114 | $authChecker = $this->container->get('security.authorization_checker'); |
||
| 115 | |||
| 116 | // Admins have access to everything |
||
| 117 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Check if I'm the owner |
||
| 122 | $creator = $resourceNode->getCreator(); |
||
| 123 | |||
| 124 | if ($creator instanceof UserInterface && |
||
| 125 | $user->getUsername() === $creator->getUsername()) { |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Checking possible links connected to this resource |
||
| 131 | $request = $this->container->get('request_stack')->getCurrentRequest(); |
||
| 132 | |||
| 133 | $courseCode = $request->get('course'); |
||
| 134 | $sessionId = $request->get('session'); |
||
| 135 | |||
| 136 | $links = $resourceNode->getLinks(); |
||
| 137 | $linkFound = false; |
||
| 138 | |||
| 139 | /** @var ResourceLink $link */ |
||
| 140 | foreach ($links as $link) { |
||
| 141 | $linkUser = $link->getUser(); |
||
| 142 | $linkCourse = $link->getCourse(); |
||
| 143 | $linkSession = $link->getSession(); |
||
| 144 | $linkUserGroup = $link->getUserGroup(); |
||
| 145 | |||
| 146 | // Check if resource was sent to the current user |
||
| 147 | if ($linkUser instanceof UserInterface && |
||
| 148 | $linkUser->getUsername() === $creator->getUsername() |
||
| 149 | ) { |
||
| 150 | $linkFound = true; |
||
| 151 | break; |
||
| 152 | } |
||
| 153 | |||
| 154 | // @todo Check if resource was sent to a usergroup |
||
| 155 | // @todo Check if resource was sent to a group inside a course |
||
| 156 | |||
| 157 | // Check if resource was sent to a course inside a session |
||
| 158 | if ($linkSession instanceof Session && !empty($sessionId) && |
||
| 159 | $linkCourse instanceof Course && !empty($courseCode) |
||
| 160 | ) { |
||
| 161 | $session = $this->container->get('chamilo_core.entity.manager.session_manager')->find($sessionId); |
||
| 162 | $course = $this->container->get('chamilo_core.entity.manager.course_manager')->findOneByCode($courseCode); |
||
| 163 | if ($session instanceof Session && |
||
| 164 | $course instanceof Course && |
||
| 165 | $linkCourse->getCode() === $course->getCode() && |
||
| 166 | $linkSession->getId() === $session->getId() |
||
| 167 | ) { |
||
| 168 | $linkFound = true; |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | // Check if resource was sent to a course |
||
| 174 | if ($linkCourse instanceof Course && !empty($courseCode)) { |
||
| 175 | $course = $this->container->get('chamilo_core.manager.course')->findOneByCode($courseCode); |
||
| 176 | if ($course instanceof Course && |
||
| 177 | $linkCourse->getCode() === $course->getCode() |
||
| 178 | ) { |
||
| 179 | $linkFound = true; |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | // No link was found! |
||
| 186 | if ($linkFound === false) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | // Getting rights from the link |
||
| 191 | $rightFromResourceLink = $link->getRights(); |
||
|
|
|||
| 192 | |||
| 193 | if ($rightFromResourceLink->count()) { |
||
| 194 | // Taken rights from the link |
||
| 195 | $rights = $rightFromResourceLink; |
||
| 196 | } else { |
||
| 197 | // Taken the rights from the default tool |
||
| 198 | $rights = $link->getResourceNode()->getTool()->getToolResourceRights(); |
||
| 199 | } |
||
| 200 | |||
| 201 | // Asked mask |
||
| 202 | $mask = new MaskBuilder(); |
||
| 203 | $mask->add($attribute); |
||
| 204 | $askedMask = $mask->get(); |
||
| 205 | |||
| 206 | // Check all the right this link has. |
||
| 207 | $roles = []; |
||
| 208 | foreach ($rights as $right) { |
||
| 209 | $roles[$right->getMask()] = $right->getRole(); |
||
| 210 | } |
||
| 211 | |||
| 212 | // Setting zend simple ACL |
||
| 213 | $acl = new Acl(); |
||
| 214 | |||
| 215 | // Creating roles |
||
| 216 | // @todo move this in a service |
||
| 217 | $userRole = new Role('ROLE_USER'); |
||
| 218 | $teacher = new Role(self::ROLE_CURRENT_COURSE_TEACHER); |
||
| 219 | $student = new Role(self::ROLE_CURRENT_COURSE_STUDENT); |
||
| 220 | $superAdmin = new Role('ROLE_SUPER_ADMIN'); |
||
| 221 | $admin = new Role('ROLE_ADMIN'); |
||
| 222 | |||
| 223 | // Adding roles to the ACL |
||
| 224 | // User role |
||
| 225 | $acl->addRole($userRole); |
||
| 226 | // Adds role student |
||
| 227 | $acl->addRole($student); |
||
| 228 | // Adds teacher role, inherit student role |
||
| 229 | $acl->addRole($teacher, $student); |
||
| 230 | $acl->addRole($superAdmin); |
||
| 231 | $acl->addRole($admin); |
||
| 232 | |||
| 233 | // Adds a resource |
||
| 234 | $resource = new Resource($link); |
||
| 235 | $acl->addResource($resource); |
||
| 236 | |||
| 237 | // Role and permissions settings |
||
| 238 | // Students can view |
||
| 239 | |||
| 240 | // Student can just view (read) |
||
| 241 | $acl->allow($student, null, self::getReaderMask()); |
||
| 242 | |||
| 243 | // Teacher can view/edit |
||
| 244 | $acl->allow( |
||
| 245 | $teacher, |
||
| 246 | null, |
||
| 247 | [ |
||
| 248 | self::getReaderMask(), |
||
| 249 | self::getEditorMask(), |
||
| 250 | ] |
||
| 251 | ); |
||
| 252 | |||
| 253 | // Admin can do everything |
||
| 254 | $acl->allow($admin); |
||
| 255 | $acl->allow($superAdmin); |
||
| 256 | |||
| 257 | foreach ($user->getRoles() as $role) { |
||
| 258 | if ($acl->isAllowed($role, $resource, $askedMask)) { |
||
| 259 | |||
| 260 | //dump('passed'); |
||
| 261 | return true; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | //dump('not allowed to '.$attribute); |
||
| 266 | |||
| 267 | return false; |
||
| 268 | } |
||
| 270 |