forked from CakeDC/cakephp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginAction.php
More file actions
143 lines (128 loc) · 3.86 KB
/
LoginAction.php
File metadata and controls
143 lines (128 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2016 - 2019, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Api\Service\Action\Auth;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use CakeDC\Api\Exception\ValidationException;
use CakeDC\Api\Service\Action\Action;
use CakeDC\Users\Controller\Traits\LoginTrait;
use CakeDC\Users\Exception\UserNotFoundException;
use Authentication\IdentityInterface;
use Cake\Datasource\EntityInterface;
/**
* Class LoginAction
*
* @package CakeDC\Api\Service\Action
* @property \CakeDC\Api\Service\Auth\Auth $Auth
*/
class LoginAction extends Action
{
use LoginTrait;
protected string $_identifiedField = 'username';
protected string $_passwordField = 'password';
/**
* Initialize an action instance
*
* @param array $config Configuration options passed to the constructor
* @return void
*/
public function initialize(array $config): void
{
if (isset($config['identifiedField'])) {
$this->_identifiedField = $config['identifiedField'];
}
if (isset($config['passwordField'])) {
$this->_passwordField = $config['passwordField'];
}
parent::initialize($config);
$this->Auth->allow($this->getName());
}
/**
* Apply validation process.
*
* @return bool
*/
public function validates(): bool
{
$validator = new Validator();
$validator
->requirePresence($this->_identifiedField, 'create')
->notBlank($this->_identifiedField);
$validator
->requirePresence($this->_passwordField, 'create')
->notBlank($this->_passwordField);
$errors = $validator->validate($this->getData());
if (!empty($errors)) {
throw new ValidationException(__('Validation failed'), 0, null, $errors);
}
return true;
}
/**
* Execute action.
*
* @return mixed
*/
public function execute()
{
$socialLogin = false;
$user = $this->Auth->getIdentity();
if ($user instanceof IdentityInterface) {
$user = $user->getOriginalData()->toArray();
}
if ($user instanceof EntityInterface) {
$user = $user->toArray();
}
$user = $this->_afterIdentifyUser($user, $socialLogin);
if (empty($user)) {
throw new UserNotFoundException(__d('CakeDC/Api', 'User not found'), 401);
} else {
return $user;
}
}
/**
* Update remember me and determine redirect url after user identified
*
* @param array $user user data after identified
* @param bool $socialLogin is social login
* @return array
*/
protected function _afterIdentifyUser($user, $socialLogin = false)
{
if (!empty($user)) {
//??? $this->Auth->setUser($user);
}
$event = $this->dispatchEvent('Action.Auth.onLoginFormat', ['user' => $user]);
if ($event->getResult()) {
$user = $event->getResult();
}
return $user;
}
/**
* Prepare Auth configuration.
*
* @return array
*/
protected function _authConfig(): array
{
return Hash::merge(parent::_authConfig(), [
'authenticate' => [
'CakeDC/Api.Form' => [
'fields' => [
'username' => $this->_identifiedField,
'password' => $this->_passwordField,
],
'finder' => 'active',
],
],
]);
}
}