original_string: public function grantRoleAccess(RoleInterface $role, ResourceInterface $resource, string $action)
      {
          $resourcePermissions = $this->getGroupPermissions($resource);
          $matchedPermission = null;
  
          //
          // 1. Check to see if the role, or its parents already have access.  Don't pollute the database.
          //
          $examinedRole = $role;
          while ($examinedRole) {
              foreach ($resourcePermissions as $permission) {
                  if ($role === $permission->getRole()) {
                      $matchedPermission = $permission;
                  }
  
                  if ($permission->can($action)) {
                      throw new ExistingAccessException($role, $resource, $action, $permission->getRole()->getName());
                  }
              }
              $examinedRole = $examinedRole->getParent();
          }
  
          //
          // 2. Give access
          //
          if (!$matchedPermission) {
              $newPermission = $this->groupPermissions->create($role, $resource->getClass(), $resource->getId(), [$action]);
              $this->groupPermissions->save($newPermission);
          } else {
              $matchedPermission->addAction($action);
              $this->groupPermissions->update($matchedPermission);
  
          }
      }
code: public function grantRoleAccess(RoleInterface $role, ResourceInterface $resource, string $action)
      {
          $resourcePermissions = $this->getGroupPermissions($resource);
          $matchedPermission = null;
  
          //
          // 1. Check to see if the role, or its parents already have access.  Don't pollute the database.
          //
          $examinedRole = $role;
          while ($examinedRole) {
              foreach ($resourcePermissions as $permission) {
                  if ($role === $permission->getRole()) {
                      $matchedPermission = $permission;
                  }
  
                  if ($permission->can($action)) {
                      throw new ExistingAccessException($role, $resource, $action, $permission->getRole()->getName());
                  }
              }
              $examinedRole = $examinedRole->getParent();
          }
  
          //
          // 2. Give access
          //
          if (!$matchedPermission) {
              $newPermission = $this->groupPermissions->create($role, $resource->getClass(), $resource->getId(), [$action]);
              $this->groupPermissions->save($newPermission);
          } else {
              $matchedPermission->addAction($action);
              $this->groupPermissions->update($matchedPermission);
  
          }
      }
code_tokens: public function grantRoleAccess ( RoleInterface $ role , ResourceInterface $ resource , string $ action ) { $ resourcePermissions = $ this -> getGroupPermissions ( $ resource ) ; $ matchedPermission = null ; // // 1. Check to see if the role, or its parents already have access.  Don't pollute the database. // $ examinedRole = $ role ; while ( $ examinedRole ) { foreach ( $ resourcePermissions as $ permission ) { if ( $ role === $ permission -> getRole ( ) ) { $ matchedPermission = $ permission ; } if ( $ permission -> can ( $ action ) ) { throw new ExistingAccessException ( $ role , $ resource , $ action , $ permission -> getRole ( ) -> getName ( ) ) ; } } $ examinedRole = $ examinedRole -> getParent ( ) ; } // // 2. Give access // if ( ! $ matchedPermission ) { $ newPermission = $ this -> groupPermissions -> create ( $ role , $ resource -> getClass ( ) , $ resource -> getId ( ) , [ $ action ] ) ; $ this -> groupPermissions -> save ( $ newPermission ) ; } else { $ matchedPermission -> addAction ( $ action ) ; $ this -> groupPermissions -> update ( $ matchedPermission ) ; } }
docstring: Give a role, access to a specific resource
  
  @param RoleInterface     $role
  @param ResourceInterface $resource
  @param string            $action
  
  @throws ExistingAccessException
  @throws UnknownResourceTypeException
docstring_tokens: Give a role access to a specific resource