/** * Unit test for eZRole::fetchByUser **/ public function testFetchByUser() { // test with an empty array $roles = eZRole::fetchByUser(array()); $this->assertType('array', $roles, "eZRole::fetchByUser with an empty array should have returned an array"); $this->assertEquals(0, count($roles), "eZRole::fetchByUser with an empty array should have returned an empty array"); // test with users with no direct role assignment (group assignment) // should return an empty array since anonymous/admin role is assigned // by group $parameter = array(self::$anonymousUserID, self::$adminUserID); $roles = eZRole::fetchByUser($parameter); $this->assertType('array', $roles, "eZRole::fetchByUser with admin & anonymous should have returned an array"); $this->assertEquals(0, count($roles), "eZRole::fetchByUser with admin & anonymous should have returned an empty array"); foreach ($roles as $role) { $this->assertType('eZRole', $role, "Returned items should be roles"); } // test with the same users, but recursively: should return anonymous // and administrator roles $parameter = array(self::$anonymousUserID, self::$adminUserID); $roles = eZRole::fetchByUser($parameter, true); $this->assertType('array', $roles, "recursive eZRole::fetchByUser with admin & anonymous should have returned an array"); $this->assertEquals(2, count($roles), "recursive eZRole::fetchByUser with admin & anonymous should have returned an empty array"); foreach ($roles as $role) { $this->assertType('eZRole', $role, "Returned items should be roles"); } }
/** * Test for eZRole::policyList() * Checks that policyList doesn't return temporary policies */ public function testPolicyList() { $role = array_shift(eZRole::fetchByUser(array(self::$anonymousUserID), true)); $policy = array_shift($role->policyList()); // create a temporary copy of one of the role's policies $policy->createTemporaryCopy(); // check that the role's policies all are final (not temporary) foreach ($role->policyList() as $policy) { $this->assertType('eZPolicy', $policy); $this->assertEquals(0, $policy->attribute('original_id')); } }
function roles() { $groups = $this->attribute('groups'); $groups[] = $this->attribute('contentobject_id'); return eZRole::fetchByUser($groups); }
/** * Fetch role list * Used by fetch( 'user', 'member_of', hash( 'id', $id ) ) template function. * * @param int $id User id or normal content object id in case of none user object (user group) * @return array(string=>array) */ function fetchMemberOf($id) { $user = eZUser::fetch($id); if ($user instanceof eZUser) { $roleList = $user->roles(); } else { // user group or other non user classes: $roleList = eZRole::fetchByUser(array($id), true); } return array('result' => $roleList); }
/** * Return access array by passing in list of groups user belongs to and his user id * * @param array $idArray Array of eZContentObject IDs, either groups + user id or user id's only * If only user id's, then remember to set $recursive to true * @param bool $recursive See {@link eZRole::fetchByUser()} * @return array Hash with complete access limitation description */ static function accessArrayByUserID($idArray, $recursive = false) { $roles = eZRole::fetchByUser($idArray, $recursive); $userLimitation = false; $accessArray = array(); foreach (array_keys($roles) as $roleKey) { $accessArray = array_merge_recursive($accessArray, $roles[$roleKey]->accessArray()); if ($roles[$roleKey]->attribute('limit_identifier')) { $userLimitation = true; } } if ($userLimitation) { foreach ($accessArray as $moduleKey => $functionList) { foreach ($functionList as $functionKey => $policyList) { foreach ($policyList as $policyKey => $limitationList) { if (is_array($limitationList)) { foreach ($limitationList as $limitationKey => $limitKeyArray) { if (is_array($limitKeyArray)) { $accessArray[$moduleKey][$functionKey][$policyKey][$limitationKey] = array_unique($limitKeyArray); } } } } } } } return $accessArray; }