* @package generis */ class common_user_auth_Service { /** * * @access private * @var common_user_auth_Service */ private static $instance = null; /** * Get a unique instance of the UserService. * * @access public * @author Jerome Bogaerts, * @return common_user_auth_Service */ public static function singleton() { if (!isset(self::$instance)) { self::$instance = new static(); } return self::$instance; } /** * The constructor is private to implement the Singleton Design Pattern. * * @access private * @author Jerome Bogaerts, */ private function __construct() { // Only to restrict instances of this class to a single instance. } /** * Log in a user into Generis that has one of the provided $allowedRoles. * * @access public * @author Jerome Bogaerts, * @param string login The login of the user. * @param string password the md5 hash of the password. * @param allowedRoles A Role or an array of Roles that are allowed to be logged in. If the user has a Role that matches one or more Roles in this array, the login request will be accepted. * @return boolean */ public function login(common_user_auth_Adapter $adapter, $allowedRoles = []) { $returnValue = (bool) false; try { $user = $adapter->authenticate(); if (!empty($allowedRoles)) { // Role can be either a scalar value or a collection. $allowedRoles = is_array($allowedRoles) ? $allowedRoles : [$allowedRoles]; $roles = []; foreach ($allowedRoles as $r) { $roles[] = (($r instanceof core_kernel_classes_Resource) ? $r->getUri() : $r); } unset($allowedRoles); $intersect = array_intersect($roles, $user->getRoles()); if (empty($intersect)) { common_Logger::w('User ' . $user->getIdentifier() . ' does not have the nescessary role'); return false; } } $returnValue = $this->startSession($user); } catch (common_user_auth_AuthFailedException $exception) { // failed return false; } return (bool) $returnValue; } /** * Indicates if an Authenticated Session is open. * * @access public * @author Jerome Bogaerts, * @return boolean */ public function isASessionOpened() { return !common_session_SessionManager::isAnonymous(); } /** * Logout the current user. The session will be entirely reset. * * @access public * @author Jerome Bogaerts, * @return boolean */ public function logout() { return \common_session_SessionManager::endSession(); } /** * Short description of method startSession * * @access public * @author Jerome Bogaerts, * @param Resource user * @return boolean */ public function startSession(common_user_User $user) { $session = new common_session_DefaultSession($user); return \common_session_SessionManager::startSession($session); } }