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