Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.39% |
4 / 23 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| common_user_auth_Service | |
17.39% |
4 / 23 |
|
33.33% |
2 / 6 |
108.27 | |
0.00% |
0 / 1 |
| singleton | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| login | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 | |||
| isASessionOpened | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startSession | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * The UserService aims at providing an API to manage Users and Roles within Generis. |
| 24 | * |
| 25 | * @access public |
| 26 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 27 | * @package generis |
| 28 | |
| 29 | */ |
| 30 | class common_user_auth_Service |
| 31 | { |
| 32 | /** |
| 33 | * |
| 34 | * @access private |
| 35 | * @var common_user_auth_Service |
| 36 | */ |
| 37 | private static $instance = null; |
| 38 | |
| 39 | /** |
| 40 | * Get a unique instance of the UserService. |
| 41 | * |
| 42 | * @access public |
| 43 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 44 | * @return common_user_auth_Service |
| 45 | */ |
| 46 | public static function singleton() |
| 47 | { |
| 48 | if (!isset(self::$instance)) { |
| 49 | self::$instance = new static(); |
| 50 | } |
| 51 | return self::$instance; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * The constructor is private to implement the Singleton Design Pattern. |
| 56 | * |
| 57 | * @access private |
| 58 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 59 | */ |
| 60 | private function __construct() |
| 61 | { |
| 62 | // Only to restrict instances of this class to a single instance. |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Log in a user into Generis that has one of the provided $allowedRoles. |
| 67 | * |
| 68 | * @access public |
| 69 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 70 | * @param string $login The login of the user. |
| 71 | * @param string $password the md5 hash of the password. |
| 72 | * @param $allowedRoles - A Role or an array of Roles that are allowed to be logged in. If the user has a Role that |
| 73 | * matches one or more Roles in this array, the login request will be accepted. |
| 74 | * @return boolean |
| 75 | */ |
| 76 | public function login(common_user_auth_Adapter $adapter, $allowedRoles = []) |
| 77 | { |
| 78 | $returnValue = (bool) false; |
| 79 | |
| 80 | try { |
| 81 | $user = $adapter->authenticate(); |
| 82 | if (!empty($allowedRoles)) { |
| 83 | // Role can be either a scalar value or a collection. |
| 84 | $allowedRoles = is_array($allowedRoles) ? $allowedRoles : [$allowedRoles]; |
| 85 | $roles = []; |
| 86 | foreach ($allowedRoles as $r) { |
| 87 | $roles[] = (($r instanceof core_kernel_classes_Resource) ? $r->getUri() : $r); |
| 88 | } |
| 89 | unset($allowedRoles); |
| 90 | $intersect = array_intersect($roles, $user->getRoles()); |
| 91 | if (empty($intersect)) { |
| 92 | common_Logger::w('User ' . $user->getIdentifier() . ' does not have the nescessary role'); |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | $returnValue = $this->startSession($user); |
| 97 | } catch (common_user_auth_AuthFailedException $exception) { |
| 98 | // failed return false; |
| 99 | } |
| 100 | |
| 101 | return (bool) $returnValue; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Indicates if an Authenticated Session is open. |
| 106 | * |
| 107 | * @access public |
| 108 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 109 | * @return boolean |
| 110 | */ |
| 111 | public function isASessionOpened() |
| 112 | { |
| 113 | return !common_session_SessionManager::isAnonymous(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Logout the current user. The session will be entirely reset. |
| 118 | * |
| 119 | * @access public |
| 120 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 121 | * @return boolean |
| 122 | */ |
| 123 | public function logout() |
| 124 | { |
| 125 | return \common_session_SessionManager::endSession(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Short description of method startSession |
| 130 | * |
| 131 | * @access public |
| 132 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 133 | * @param Resource user |
| 134 | * @return boolean |
| 135 | */ |
| 136 | public function startSession(common_user_User $user) |
| 137 | { |
| 138 | $session = new common_session_DefaultSession($user); |
| 139 | return \common_session_SessionManager::startSession($session); |
| 140 | } |
| 141 | } |