Ejemplo n.º 1
0
 public function testRolesCache()
 {
     $includedRole = $this->service->addRole('INCLUDED ROLE');
     $role = $this->service->addRole('CACHE ROLE', $includedRole);
     // Nothing is in the cache, we should get an exception.
     try {
         core_kernel_users_Cache::retrieveIncludedRoles($role);
         $this->assertTrue(false, 'An exception should be raised when trying to retrieve included roles that are not yet in the cache memory.');
     } catch (core_kernel_users_CacheException $e) {
         $this->assertTrue(true);
     }
     $includedRoles = $this->service->getIncludedRoles($role);
     $this->assertEquals(count($includedRoles), 1);
     $this->assertTrue(array_key_exists($includedRole->getUri(), $includedRoles));
     // try to put included roles in the cache.
     try {
         core_kernel_users_Cache::cacheIncludedRoles($role, $includedRoles);
         // now try to retrieve it.
         $includedRolesFromCache = core_kernel_users_Cache::retrieveIncludedRoles($role);
         $this->assertTrue(is_array($includedRolesFromCache));
         $this->assertEquals(count($includedRolesFromCache), 1);
         $this->assertTrue(array_key_exists($includedRole->getUri(), $includedRolesFromCache));
         // and remove it !
         $this->assertTrue(core_kernel_users_Cache::removeIncludedRoles($role));
         $this->assertFalse(core_kernel_users_Cache::areIncludedRolesInCache($role));
     } catch (core_kernel_users_CacheException $e) {
         $this->assertTrue(false, 'An exception occured while writing included roles in the cache memory.');
     }
     // try to flush users cache.
     try {
         core_kernel_users_Cache::flush();
     } catch (core_kernel_users_CacheException $e) {
         $this->assertTrue(false, 'An error occured while flushing the users cache.');
     }
     $includedRole->delete();
     $role->delete();
 }
Ejemplo n.º 2
0
 /**
  * Get an array of the Roles included by a Generis Role.
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  *
  * @param  core_kernel_classes_Resource $role A Generis Role.
  *
  * @return array An associative array where keys are Role URIs and values are instances of core_kernel_classes_Resource.
  * @throws core_kernel_users_CacheException
  * @throws core_kernel_users_Exception
  */
 public function getIncludedRoles(core_kernel_classes_Resource $role)
 {
     $returnValue = array();
     if (GENERIS_CACHE_USERS_ROLES === true && core_kernel_users_Cache::areIncludedRolesInCache($role) === true) {
         $returnValue = core_kernel_users_Cache::retrieveIncludedRoles($role);
     } else {
         // We use a Depth First Search approach to flatten the Roles Graph.
         $includesRoleProperty = new core_kernel_classes_Property(PROPERTY_ROLE_INCLUDESROLE);
         $visitedRoles = array();
         $s = array();
         // vertex stack.
         array_push($s, $role);
         // begin with $role as the first vertex.
         while (!empty($s)) {
             $u = array_pop($s);
             if (false === in_array($u->getUri(), $visitedRoles, true)) {
                 $visitedRoles[] = $u->getUri();
                 $returnValue[$u->getUri()] = $u;
                 $ar = $u->getPropertyValuesCollection($includesRoleProperty);
                 foreach ($ar->getIterator() as $w) {
                     if (false === in_array($w->getUri(), $visitedRoles, true)) {
                         // not visited
                         array_push($s, $w);
                     }
                 }
             }
         }
         // remove the root vertex which is actually the role we are testing.
         unset($returnValue[$role->getUri()]);
         if (GENERIS_CACHE_USERS_ROLES === true) {
             try {
                 core_kernel_users_Cache::cacheIncludedRoles($role, $returnValue);
             } catch (core_kernel_users_CacheException $e) {
                 $roleUri = $role->getUri();
                 $msg = "Unable to retrieve included roles from cache memory for role '{$roleUri}': ";
                 $msg .= $e->getMessage();
                 throw new core_kernel_users_Exception($msg);
             }
         }
     }
     return (array) $returnValue;
 }