/** * Get user permissions and restrictions * * @param User $user * * @return array */ public function getPermissionsAndRestrictions(User $user) { $permissions = array(); $restrictions = array(); $username = $user->getUsername(); try { $roles = Config::app('roles'); } catch (NotReadableError $e) { Logger::error('Can\'t get permissions and restrictions for user \'%s\'. An exception was thrown:', $username, $e); return array($permissions, $restrictions); } $userGroups = $user->getGroups(); foreach ($roles as $role) { if ($this->match($username, $userGroups, $role)) { $permissions = array_merge($permissions, array_diff(String::trimSplit($role->permissions), $permissions)); $restrictionsFromRole = $role->toArray(); unset($restrictionsFromRole['users']); unset($restrictionsFromRole['groups']); unset($restrictionsFromRole['permissions']); foreach ($restrictionsFromRole as $name => $restriction) { if (!isset($restrictions[$name])) { $restrictions[$name] = array(); } $restrictions[$name][] = $restriction; } } } return array($permissions, $restrictions); }
/** * Return a list of groups for an username * * @param string $username * * @return array */ public function getGroupsByUsername($username) { $groups = array(); try { $config = Config::app('memberships'); } catch (NotReadableError $e) { return $groups; } foreach ($config as $section) { $users = String::trimSplit($section->users); if (in_array($username, $users)) { $groups = array_merge($groups, String::trimSplit($section->groups)); } } return $groups; }
/** * Retrieve permissions * * @param string $username * @param array $groups * * @return array */ public function getPermissions($username, array $groups) { $permissions = array(); try { $config = Config::app('permissions'); } catch (NotReadableError $e) { return $permissions; } foreach ($config as $section) { if ($this->match($section, $username, $groups)) { foreach ($section as $key => $value) { if (strpos($key, 'permission') === 0) { $permissions = array_merge($permissions, String::trimSplit($value)); } } } } return $permissions; }
/** * Load a role * * @param string $name The name of the role * * @return $this * * @throws LogicException If the config is not set * @see ConfigForm::setConfig() For setting the config. */ public function load($name) { if (!isset($this->config)) { throw new LogicException(sprintf('Can\'t load role \'%s\'. Config is not set', $name)); } if (!$this->config->hasSection($name)) { throw new InvalidArgumentException(sprintf($this->translate('Can\'t load role \'%s\'. Role does not exist'), $name)); } $role = $this->config->getSection($name)->toArray(); $role['permissions'] = !empty($role['permissions']) ? String::trimSplit($role['permissions']) : null; $role['name'] = $name; $restrictions = array(); foreach ($this->providedRestrictions as $name => $spec) { if (isset($role[$spec['name']])) { // Translate restriction names to filtered element names $restrictions[$name] = $role[$spec['name']]; unset($role[$spec['name']]); } } $role = array_merge($role, $restrictions); $this->populate($role); return $this; }
/** * Return the groups the given user is a member of * * @param User $user * * @return array */ public function getMemberships(User $user) { $result = $this->select()->fetchAll(); $groups = array(); foreach ($result as $group) { $groups[$group->group_name] = $group->parent; } $username = strtolower($user->getUsername()); $memberships = array(); foreach ($result as $group) { if ($group->users && !in_array($group->group_name, $memberships)) { $users = array_map('strtolower', String::trimSplit($group->users)); if (in_array($username, $users)) { $memberships[] = $group->group_name; $parent = $groups[$group->group_name]; while ($parent !== null) { $memberships[] = $parent; $parent = isset($groups[$parent]) ? $groups[$parent] : null; } } } } return $memberships; }
/** * Convert the given comma separated string to an array * * @param string $value * * @return array */ protected function retrieveCommaSeparatedString($value) { if ($value && is_string($value)) { $value = String::trimSplit($value); } elseif ($value !== null) { throw new ProgrammingError('Cannot retrieve value "%s" as array. It\'s not a string', $value); } return $value; }
public function testWhetherTrimSplitSplitsByTheGivenDelimiter() { $this->assertEquals(array('one', 'two', 'three'), String::trimSplit('one.two.three', '.'), 'String::trimSplit does not split a string by the given delimiter'); }