* @package tao */ class tao_models_classes_RoleService extends tao_models_classes_GenerisService implements core_kernel_users_RolesManagement { use EventManagerAwareTrait; /** * the core user service * * @access public * @var core_kernel_users_Service */ protected $generisUserService = null; /** * the class of the target role * * @access public * @var core_kernel_classes_Class */ private $roleClass = null; /** * Constructor, calls the initRole method. * * @access protected * @author Joel Bout, */ protected function __construct() { parent::__construct(); $this->generisUserService = core_kernel_users_Service::singleton(); $this->initRole(); } /** * Initialize the allowed role. * To be overriden. * * @access protected * @author Joel Bout, * @return mixed */ protected function initRole() { $this->roleClass = new core_kernel_classes_Class(GenerisRdf::CLASS_ROLE); } /** * Get the Role matching the uri * * @access public * @author Joel Bout, * @param string uri * @return core_kernel_classes_Resource */ public function getRole($uri) { $returnValue = null; if (!empty($uri)) { $returnValue = new core_kernel_classes_Resource($uri); } return $returnValue; } /** * get the target role class * * @access public * @author Joel Bout, * @return core_kernel_classes_Class */ public function getRoleClass() { $returnValue = null; $returnValue = $this->roleClass; return $returnValue; } /** * assign a role to a set of users * * @access public * @author Joel Bout, * @param Resource role * @param array users * @return boolean */ public function setRoleToUsers(core_kernel_classes_Resource $role, $users = []) { $returnValue = (bool) false; $userService = tao_models_classes_UserService::singleton(); $rolesProperty = new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_ROLES); foreach ($users as $u) { $u = ($u instanceof core_kernel_classes_Resource) ? $u : new core_kernel_classes_Resource($u); // just in case of ... $userService->unnatachRole($u, $role); // assign the new role. $u->setPropertyValue($rolesProperty, $role); if (common_session_SessionManager::getSession()->getUserUri() == $u->getUri()) { common_session_SessionManager::getSession()->refresh(); } } $returnValue = true; return (bool) $returnValue; } /** * get the users who have the role in parameter * * @access public * @author Joel Bout, * @param core_kernel_classes_Resource role * @return array */ public function getUsers(core_kernel_classes_Resource $role) { $returnValue = []; $filters = [GenerisRdf::PROPERTY_USER_ROLES => $role->getUri()]; $options = ['like' => false, 'recursive' => true]; $userClass = new core_kernel_classes_Class(GenerisRdf::CLASS_GENERIS_USER); $results = $userClass->searchInstances($filters, $options); $returnValue = array_keys($results); return (array) $returnValue; } /** * Creates a new Role in persistent memory. * * @param string label The label of the new role. * @param mixed includedRoles The roles to include to the new role. Can be either a core_kernel_classes_Resource or an array of core_kernel_classes_Resource. * @param core_kernel_classes_Class (optional) A specific class for the new role. * @return core_kernel_classes_Resource The newly created role. */ public function addRole($label, $includedRoles = null, core_kernel_classes_Class $class = null) { return $this->generisUserService->addRole($label, $includedRoles, $class); } /** * Remove a given Role from persistent memory. References to this role * will also be removed from the persistent memory. * * @param core_kernel_classes_Resource $role The Role to remove. * @return boolean True if the Role was removed, false otherwise. */ public function removeRole(core_kernel_classes_Resource $role) { $this->getEventManager()->trigger(new RoleRemovedEvent($role->getUri())); return $this->generisUserService->removeRole($role); } /** * Returns the Roles included by a given Role. * * @param core_kernel_classes_Resource $role The Role you want to know what are its included Roles. * @return array An array of core_kernel_classes_Resource corresponding to the included Roles. */ public function getIncludedRoles(core_kernel_classes_Resource $role) { return $this->generisUserService->getIncludedRoles($role); } /** * Includes the $roleToInclude Role to the $role Role. * * @param core_kernel_classes_Resource $role A Role. * @param core_kernel_classes_Resource $roleToInclude A Role to include. */ public function includeRole(core_kernel_classes_Resource $role, core_kernel_classes_Resource $roleToInclude) { $this->generisUserService->includeRole($role, $roleToInclude); $this->getEventManager()->trigger(new RoleChangedEvent($role->getUri(), 'included role', $roleToInclude->getUri())); } /** * Uninclude a Role from another Role. * * @param core_kernel_classes_Resource $role The Role from which you want to uninclude another Role. * @param core_kernel_classes_Resource $roleToUninclude The Role to uninclude. */ public function unincludeRole(core_kernel_classes_Resource $role, core_kernel_classes_Resource $roleToUninclude) { $this->generisUserService->unincludeRole($role, $roleToUninclude); $this->getEventManager()->trigger(new RoleChangedEvent($role->getUri(), 'excluded role', $roleToUninclude->getUri())); } /** * Returns the whole collection of Roles stored into TAO. * * @return array An associative array where keys are Role URIs and values are core_kernel_classes_Resource instances. */ public function getAllRoles() { return $this->generisUserService->getAllRoles(); } /** * @inheritdoc */ public function createInstance(core_kernel_classes_Class $clazz, $label = '') { $instance = parent::createInstance($clazz, $label); if ($instance) { $this->getEventManager()->trigger(new RoleCreatedEvent($instance->getUri())); } return $instance; } }