| Conditions | 25 |
| Paths | 469 |
| Total Lines | 162 |
| Code Lines | 77 |
| 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 |
||
| 79 | /** |
||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | protected function voteOnAttribute($attribute, $resourceNode, TokenInterface $token) |
||
| 83 | { |
||
| 84 | $user = $token->getUser(); |
||
| 85 | |||
| 86 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 87 | if (!$user instanceof UserInterface) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Checking admin roles |
||
| 92 | $authChecker = $this->container->get('security.authorization_checker'); |
||
| 93 | |||
| 94 | // Admins have access to everything |
||
| 95 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
|
|
|||
| 96 | // return true; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Check if I'm the owner |
||
| 100 | /*$creator = $resourceNode->getCreator(); |
||
| 101 | if ($creator instanceof UserInterface && |
||
| 102 | $user->getUsername() == $creator->getUsername()) { |
||
| 103 | |||
| 104 | //return true; |
||
| 105 | }*/ |
||
| 106 | |||
| 107 | // Checking possible links connected to this resource |
||
| 108 | $request = $this->container->get('request_stack')->getCurrentRequest(); |
||
| 109 | |||
| 110 | $courseCode = $request->get('course'); |
||
| 111 | $sessionId = $request->get('session'); |
||
| 112 | |||
| 113 | $links = $resourceNode->getLinks(); |
||
| 114 | $linkFound = false; |
||
| 115 | |||
| 116 | /** @var ResourceLink $link */ |
||
| 117 | foreach ($links as $link) { |
||
| 118 | $linkUser = $link->getUser(); |
||
| 119 | $linkCourse = $link->getCourse(); |
||
| 120 | $linkSession = $link->getSession(); |
||
| 121 | $linkUserGroup = $link->getUserGroup(); |
||
| 122 | |||
| 123 | // Check if resource was sent to the current user |
||
| 124 | if ($linkUser instanceof UserInterface && |
||
| 125 | $linkUser->getUsername() == $creator->getUsername() |
||
| 126 | ) { |
||
| 127 | $linkFound = true; |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | |||
| 131 | // @todo Check if resource was sent to a usergroup |
||
| 132 | // @todo Check if resource was sent to a group inside a course |
||
| 133 | |||
| 134 | // Check if resource was sent to a course inside a session |
||
| 135 | if ($linkSession instanceof Session && !empty($sessionId) && |
||
| 136 | $linkCourse instanceof Course && !empty($courseCode) |
||
| 137 | ) { |
||
| 138 | $session = $this->container->get('chamilo_core.entity.manager.session_manager')->find($sessionId); |
||
| 139 | $course = $this->container->get('chamilo_core.entity.manager.course_manager')->findOneByCode($courseCode); |
||
| 140 | if ($session instanceof Session && |
||
| 141 | $course instanceof Course && |
||
| 142 | $linkCourse->getCode() == $course->getCode() && |
||
| 143 | $linkSession->getId() == $session->getId() |
||
| 144 | ) { |
||
| 145 | $linkFound = true; |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Check if resource was sent to a course |
||
| 151 | if ($linkCourse instanceof Course && !empty($courseCode)) { |
||
| 152 | $course = $this->container->get('chamilo_core.manager.course')->findOneByCode($courseCode); |
||
| 153 | if ($course instanceof Course && |
||
| 154 | $linkCourse->getCode() == $course->getCode() |
||
| 155 | ) { |
||
| 156 | $linkFound = true; |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // No link was found! |
||
| 163 | if ($linkFound === false) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | // Getting rights from the link |
||
| 168 | $rightFromResourceLink = $link->getRights(); |
||
| 169 | |||
| 170 | if ($rightFromResourceLink->count()) { |
||
| 171 | // Taken rights from the link |
||
| 172 | $rights = $rightFromResourceLink; |
||
| 173 | } else { |
||
| 174 | // Taken the rights from the default tool |
||
| 175 | $rights = $link->getResourceNode()->getTool()->getToolResourceRights(); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Asked mask |
||
| 179 | $mask = new MaskBuilder(); |
||
| 180 | $mask->add($attribute); |
||
| 181 | $askedMask = $mask->get(); |
||
| 182 | |||
| 183 | // Check all the right this link has. |
||
| 184 | $roles = array(); |
||
| 185 | foreach ($rights as $right) { |
||
| 186 | $roles[$right->getMask()] = $right->getRole(); |
||
| 187 | } |
||
| 188 | |||
| 189 | // Setting zend simple ACL |
||
| 190 | $acl = new Acl(); |
||
| 191 | |||
| 192 | // Creating roles |
||
| 193 | // @todo move this in a service |
||
| 194 | $userRole = new Role('ROLE_USER'); |
||
| 195 | $teacher = new Role(self::ROLE_CURRENT_COURSE_TEACHER); |
||
| 196 | $student = new Role(self::ROLE_CURRENT_COURSE_STUDENT); |
||
| 197 | $superAdmin = new Role('ROLE_SUPER_ADMIN'); |
||
| 198 | $admin = new Role('ROLE_ADMIN'); |
||
| 199 | |||
| 200 | // Adding roles to the ACL |
||
| 201 | // User role |
||
| 202 | $acl->addRole($userRole); |
||
| 203 | // Adds role student |
||
| 204 | $acl->addRole($student); |
||
| 205 | // Adds teacher role, inherit student role |
||
| 206 | $acl->addRole($teacher, $student); |
||
| 207 | $acl->addRole($superAdmin); |
||
| 208 | $acl->addRole($admin); |
||
| 209 | |||
| 210 | // Adds a resource |
||
| 211 | $resource = new Resource($link); |
||
| 212 | $acl->addResource($resource); |
||
| 213 | |||
| 214 | // Role and permissions settings |
||
| 215 | // Students can view |
||
| 216 | |||
| 217 | // Student can just view (read) |
||
| 218 | $acl->allow($student, null, self::getReaderMask()); |
||
| 219 | |||
| 220 | // Teacher can view/edit |
||
| 221 | $acl->allow( |
||
| 222 | $teacher, |
||
| 223 | null, |
||
| 224 | array( |
||
| 225 | self::getReaderMask(), |
||
| 226 | self::getEditorMask() |
||
| 227 | ) |
||
| 228 | ); |
||
| 229 | |||
| 230 | // Admin can do everything |
||
| 231 | $acl->allow($admin); |
||
| 232 | $acl->allow($superAdmin); |
||
| 233 | |||
| 234 | foreach ($user->getRoles() as $role) { |
||
| 235 | if ($acl->isAllowed($role, $resource, $askedMask)) { |
||
| 236 | //dump('passed'); |
||
| 237 | return true; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | //dump('not allowed to '.$attribute); |
||
| 273 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.