Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LoginService | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| login | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| authenticate | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| startSession | |
0.00% |
0 / 3 |
|
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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\oatbox\user; |
| 24 | |
| 25 | use oat\oatbox\user\auth\AuthFactory; |
| 26 | use common_user_auth_AuthFailedException; |
| 27 | use common_user_User; |
| 28 | use common_session_DefaultSession; |
| 29 | use common_session_SessionManager; |
| 30 | use common_Logger; |
| 31 | |
| 32 | /** |
| 33 | * Login service |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Joel Bout, <joel@taotesting.com> |
| 37 | * @package generis |
| 38 | */ |
| 39 | class LoginService |
| 40 | { |
| 41 | /** |
| 42 | * Login a user using login, password |
| 43 | * |
| 44 | * @param string $userLogin |
| 45 | * @param string $userPassword |
| 46 | * @return boolean |
| 47 | */ |
| 48 | public static function login($userLogin, $userPassword) |
| 49 | { |
| 50 | try { |
| 51 | $user = self::authenticate($userLogin, $userPassword); |
| 52 | $loggedIn = self::startSession($user); |
| 53 | } catch (common_user_auth_AuthFailedException $e) { |
| 54 | $loggedIn = false; |
| 55 | } |
| 56 | return $loggedIn; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * @param string $userLogin |
| 62 | * @param string $userPassword |
| 63 | * @throws LoginFailedException |
| 64 | * @return common_user_User |
| 65 | */ |
| 66 | public static function authenticate($userLogin, $userPassword) |
| 67 | { |
| 68 | $user = null; |
| 69 | |
| 70 | $adapters = AuthFactory::createAdapters(); |
| 71 | $exceptions = []; |
| 72 | while (!empty($adapters) && is_null($user)) { |
| 73 | $adapter = array_shift($adapters); |
| 74 | $adapter->setCredentials($userLogin, $userPassword); |
| 75 | try { |
| 76 | $user = $adapter->authenticate(); |
| 77 | } catch (common_user_auth_AuthFailedException $exception) { |
| 78 | // try next adapter or login failed |
| 79 | common_Logger::w($exception->getMessage()); |
| 80 | $exceptions[] = $exception; |
| 81 | } |
| 82 | } |
| 83 | if (!is_null($user)) { |
| 84 | return $user; |
| 85 | } else { |
| 86 | throw new LoginFailedException($exceptions); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Start a session for a provided user |
| 93 | * |
| 94 | * @param common_user_User $user |
| 95 | * @return boolean |
| 96 | */ |
| 97 | public static function startSession(common_user_User $user) |
| 98 | { |
| 99 | $session = new common_session_DefaultSession($user); |
| 100 | common_session_SessionManager::startSession($session); |
| 101 | return true; |
| 102 | } |
| 103 | } |