-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthController.php
More file actions
186 lines (163 loc) · 4.78 KB
/
AuthController.php
File metadata and controls
186 lines (163 loc) · 4.78 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* 認証Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AuthAppController', 'Auth.Controller');
App::uses('UserAttributeChoice', 'UserAttributes.Model');
App::uses('User', 'Users.Model');
/**
* 認証Controller
*
* @property AuthComponent $Auth
* @property SessionComponent $Session
* @property ForgotPass $ForgotPass
* @property NetCommonsComponent $NetCommons
* @property User $User
* @property DeprecatedBrowserComponent $DeprecatedBrowser
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Auth\Controller
*/
class AuthController extends AuthAppController {
/**
* 使用するComponents
*
* @var array
*/
public $components = array(
'Security',
'NetCommons.DeprecatedBrowser',
);
/**
* 使用するModels
*
* - [SiteManager.SiteSetting](../../SiteManager/classes/SiteSetting.html)
* - [Users.User](../../Users/classes/User.html)
*
* @var array
*/
public $uses = array(
'Auth.ForgotPass',
'SiteManager.SiteSetting',
'Users.User',
);
/**
* beforeFilter
*
* @return void
**/
public function beforeFilter() {
// Load available authenticators
$authenticators = $this->_getAuthenticators();
$this->set('authenticators', $authenticators);
$this->__setDefaultAuthenticator();
// @see https://book.cakephp.org/2.0/ja/core-libraries/components/authentication.html#id5
// 認証ハンドラは $this->Auth->authenticate を使って設定します。
// コントローラの beforeFilter の中、もしくは $components 配列の中に、 認証ハンドラ()を設定することができます。
$this->_setNc2Authenticate();
parent::beforeFilter();
$this->Auth->allow('login', 'logout');
$this->Session->delete('AutoUserRegist');
$this->Session->delete('ForgotPass');
$this->Auth->authenticate['all']['userModel'] = 'Users.User';
$this->Auth->authenticate['all']['scope'] = array(
'User.status' => UserAttributeChoice::STATUS_CODE_ACTIVE
);
$this->Auth->authenticate['all']['passwordHasher'] = [
'className' => 'Simple',
'hashType' => User::PASSWORD_HASH_TYPE,
];
}
/**
* index
*
* @return void
**/
public function index() {
$this->redirect($this->Auth->loginAction);
}
/**
* ログイン処理
*
* @return void
* @throws InternalErrorException
**/
public function login() {
if ((bool)$this->Auth->user()) {
// 既にログイン済 => トップページへ
return $this->redirect('/');
}
//ページタイトル
$this->set('pageTitle', __d('auth', 'Login'));
//メールを送れるかどうか
$this->set('isMailSend', $this->ForgotPass->isMailSendCommon('auth', 'auth'));
if ($this->request->is('post')) {
//$this->_setNc2Authenticate();
if ($this->Auth->login()) {
//非推奨ブラウザチェック
if ($this->DeprecatedBrowser->isDeprecatedBrowser()) {
$this->DeprecatedBrowser->setFlashNotification();
}
ClassRegistry::removeObject('User');
$this->User = ClassRegistry::init('Users.User');
$this->User->updateLoginTime($this->Auth->user('id'));
Current::write('User', $this->Auth->user());
if ($this->Auth->user('language') !== UserAttributeChoice::LANGUAGE_KEY_AUTO) {
$this->Session->write('Config.language', $this->Auth->user('language'));
}
$this->Auth->loginRedirect = $this->_getDefaultStartPage();
return $this->redirect($this->Auth->redirect());
}
$this->NetCommons->setFlashNotification(
__d('auth', 'Invalid username or password, try again'),
array(
'class' => 'danger',
'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL,
),
400
);
//$this->redirect($this->Auth->loginAction);
}
}
/**
* logout
*
* @return void
**/
public function logout() {
$this->Session->delete('Config.language');
$this->redirect($this->Auth->logout());
}
/**
* Set authenticator
*
* @return void
**/
private function __setDefaultAuthenticator() {
$scheme = strtr(Inflector::camelize($this->request->offsetGet('plugin')), array('Auth' => ''));
$callee = array(sprintf('Auth%sAppController', $scheme), '_getAuthenticator');
if (is_callable($callee)) {
$authenticator = call_user_func($callee);
$this->Auth->authenticate = array($authenticator => array());
//CakeLog::info(sprintf('Will load %s authenticator', $authenticator), true);
} else {
//CakeLog::info(sprintf('Unknown authenticator %s.%s', $plugin, $scheme), true);
}
}
/**
* Set nc2 authenticator
*
* @return void
**/
protected function _setNc2Authenticate() {
if (CakePlugin::loaded('Nc2ToNc3')) {
$this->Auth->authenticate['Nc2ToNc3.Nc2'] = [];
}
}
}