<?php// +---------------------------------------------------------------------------+// | This file is part of the Agavi package. |// | Copyright (c) 2003-2006 the Agavi Project. |// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. |// | |// | For the full copyright and license information, please view the LICENSE |// | file that was distributed with this source code. You can also view the |// | LICENSE file online at http://www.agavi.org/LICENSE.txt |// | vi: set noexpandtab: |// | Local Variables: |// | indent-tabs-mode: t |// | End: |// +---------------------------------------------------------------------------+/** * AgaviRbacUser will handle roles and permissions for users * * @package agavi * @subpackage user * * @copyright (c) Authors * @since 0.11.0 * * @version $Id: AgaviSecurityUser.class.php 903 2006-08-05 00:44:02Z david $ */class AgaviRbacUser extends AgaviSecurityUser implements AgaviISecurityUser{ /** * The namespace under which roles will be stored. */ const ROLES_NAMESPACE = 'org.agavi.user.BasicSecurityUser.roles'; /** * @var array An array of roles and permissions. */ protected $hierarchy = null; /** * @var array An array of assigned user roles. */ protected $roles = null; /** * Add a role to this user. * * @param mixed Role data. * * @since 0.11.0 */ public function addHierarchy($role, $permissions, $opts, $parent) { if(!isset($this->hierarchy[$role]) ) { $this->hierarchy[$role] = array( 'permissions' => is_null($parent) ? $permissions : array_merge($permissions, $this->hierarchy[$parent]), 'parent' => $parent ); if (is_array($opts) && count($opts) ) { $this->hierarchy[$role] = array_merge($this->hierarchy[$role], $opts); } } return $role; } /** * Clear all roles. * * @since 0.11.0 */ public function clearHierarchy() { $this->hierarchy = null; $this->hierarchy = array(); } /** * Indicates whether or not this role is in the hierarchy. * * @param mixed Role data. * * @return bool true, if this role exists. * * @since 0.11.0 */ public function hasHierarchy($role) { return isset($this->hierarchy[$role]); } /** * Remove a role from hierarchy. * * @param mixed Role data. * * @since 0.11.0 */ public function removeHierarchy($role) { if($this->hasHierarchy($role) ) { unset($this->hierarchy[$role]); } } /** * Retrieve the internal representation of the role info. * * @return array The info about all roles. * * @since 0.11.0 */ public function exportHierarchy() { return $this->hierarchy; } /** * Sets the internal representation of the role info. * * @param array The info about all roles. * * @since 0.11.0 */ public function importHierarchy($roles) { $this->hierarchy = $roles; } /** * Add a role to this user. * * @param string Role name. * * @since 0.11.0 */ public function addRole($role) { if(!in_array($role, $this->roles, true) ) { $this->roles[] = $role; } } /** * Clear all assigned roles. * * @since 0.11.0 */ public function clearRoles() { $this->roles = null; $this->roles = array(); } /** * Indicates whether or not this role is assigned to user. * * @param string Role name. * * @return bool true, if this role exists. * * @since 0.11.0 */ public function hasRole($role) { return in_array($role, $this->roles); } /** * Remove a role from hierarchy. * * @param mixed Role data. * * @since 0.11.0 */ public function removeRole($role) { if($this->hasRole($role) ) { unset($this->roles[array_search($role, $this->roles)]); } } /** * Indicates whether or not a user has the permission. * * @param string Role name. * * @return bool true, if this permission exists. * * @since 0.11.0 */ public function hasPermissions($permissions) { foreach ($this->roles as $role) { if (isset($this->hierarchy[$role]) ) { if (isset($this->hierarchy[$role]['global_access']) && $this->hierarchy[$role]['global_access'] == true) { return true; } if (!is_array($permissions) ) { $permissions = array($permissions); } foreach ($permissions as $permission) { if (isset($this->hierarchy[$role]['permissions'][$permission]) ) { return true; } } } } return false; } /** * Initialize this User. * * @param AgaviContext An AgaviContext instance. * @param array An associative array of initialization parameters. * * @return bool true, if initialization completes successfully, * otherwise false. * * @throws <b>AgaviInitializationException</b> If an error occurs while * initializing this User. * * @since 0.9.0 */ public function initialize(AgaviContext $context, $parameters = array()) { parent::initialize($context, $parameters); $storage = $this->getContext()->getStorage(); $this->roles = $storage->read(self::ROLES_NAMESPACE); if($this->authenticated == null) { $this->roles = array(); } $cfg = AgaviConfig::get("core.config_dir") . '/rbac.xml'; include(AgaviConfigCache::checkConfig($cfg, $context->getName())); } /** * Execute the shutdown procedure. * * @since 0.9.0 */ public function shutdown() { $storage = $this->getContext()->getStorage(); $storage->write(self::ROLES_NAMESPACE, $this->roles); // call the parent shutdown method parent::shutdown(); }}?>