| 1: | <?php |
| 2: | /* |
| 3: | * SimpleID |
| 4: | * |
| 5: | * Copyright (C) Kelvin Mo 2021-2026 |
| 6: | * |
| 7: | * This program is free software; you can redistribute it and/or |
| 8: | * modify it under the terms of the GNU General Public |
| 9: | * License as published by the Free Software Foundation; either |
| 10: | * version 2 of the License, or (at your option) any later version. |
| 11: | * |
| 12: | * This program is distributed in the hope that it will be useful, |
| 13: | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14: | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15: | * General Public License for more details. |
| 16: | * |
| 17: | * You should have received a copy of the GNU General Public |
| 18: | * License along with this program; if not, write to the Free |
| 19: | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20: | * |
| 21: | */ |
| 22: | |
| 23: | namespace SimpleID\Auth; |
| 24: | |
| 25: | use SimpleID\Base\AuditEvent; |
| 26: | use SimpleID\Util\Forms\FormEventInterface; |
| 27: | |
| 28: | /** |
| 29: | * Event to log in a user. |
| 30: | * |
| 31: | * This event is dispatched when all the authentication components are |
| 32: | * completed. Listeners can use this hook to save any authentication |
| 33: | * information. |
| 34: | * |
| 35: | * The state of the login form (if any) used in the process can be |
| 36: | * obtained from the {@link getFormState()} method. It contains the same |
| 37: | * elements as per the `login_form_build` event. |
| 38: | * |
| 39: | * This event also provides further information on the authentication |
| 40: | * result via the {@link getAuthResult()} method. Convenience |
| 41: | * methods {@link getUser()} and {@link getAuthLevel()} are also |
| 42: | * included. |
| 43: | */ |
| 44: | class LoginEvent extends AuditEvent implements FormEventInterface { |
| 45: | /** @var AuthResultInterface */ |
| 46: | protected $result; |
| 47: | |
| 48: | /** @var \SimpleID\Util\Forms\FormState|null */ |
| 49: | protected $form_state = null; |
| 50: | |
| 51: | /** |
| 52: | * @param AuthResultInterface $result |
| 53: | * @param \SimpleID\Util\Forms\FormState|null $form_state |
| 54: | */ |
| 55: | public function __construct(AuthResultInterface $result, $form_state = null) { |
| 56: | parent::__construct($result->getUser()); |
| 57: | $this->result = $result; |
| 58: | $this->form_state = $form_state; |
| 59: | } |
| 60: | |
| 61: | /** |
| 62: | * Returns the underlying successful authentication result. |
| 63: | * |
| 64: | * @return AuthResultInterface the authentication result |
| 65: | */ |
| 66: | public function getAuthResult() { |
| 67: | return $this->result; |
| 68: | } |
| 69: | |
| 70: | /** |
| 71: | * {@inheritdoc} |
| 72: | */ |
| 73: | public function getFormState() { |
| 74: | return $this->form_state; |
| 75: | } |
| 76: | |
| 77: | /** |
| 78: | * Returns the user to be logged in. |
| 79: | * |
| 80: | * This is a proxy for `getAuthResult()->getUser()`. |
| 81: | * |
| 82: | * @return \SimpleID\Models\User the user |
| 83: | */ |
| 84: | public function getUser() { |
| 85: | return $this->result->getUser(); |
| 86: | } |
| 87: | |
| 88: | /** |
| 89: | * Returns the level of authentication achieved in this |
| 90: | * session. |
| 91: | * |
| 92: | * This is a proxy for `getAuthResult()->getAuthLevel()`. |
| 93: | * |
| 94: | * @return int the authentication level |
| 95: | */ |
| 96: | public function getAuthLevel() { |
| 97: | return $this->result->getAuthLevel(); |
| 98: | } |
| 99: | } |
| 100: | |
| 101: | ?> |