-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserManagerController.php
More file actions
363 lines (312 loc) · 8.99 KB
/
UserManagerController.php
File metadata and controls
363 lines (312 loc) · 8.99 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* UserManager 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('UserManagerAppController', 'UserManager.Controller');
App::uses('Space', 'Rooms.Model');
App::uses('ImportExportBehavior', 'Users.Model/Behavior');
App::uses('User', 'Users.Model');
/**
* UserManager Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserManager\Controller
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UserManagerController extends UserManagerAppController {
/**
* 会員一覧の表示する項目
*
* @var const
*/
public static $displaFields = array(
'handlename',
'name',
'role_key',
'status',
'modified',
'last_login'
);
/**
* use model
*
* @var array
*/
public $uses = array(
'Auth.AutoUserRegist',
'Auth.AutoUserRegistMail',
'Users.User',
'Users.UserSearch'
);
/**
* use component
*
* @var array
*/
public $components = array(
'Files.Download',
'Files.FileUpload',
'M17n.SwitchLanguage',
'Rooms.Rooms',
'UserAttributes.UserAttributeLayout',
'UserManager.UserManager',
'Users.UserSearchComp',
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'UserAttributes.UserAttributeLayout',
'Users.UserLayout',
'NetCommons.TableList'
);
/**
* indexアクション
*
* @return void
*/
public function index() {
$this->helpers[] = 'Users.UserSearchForm';
//ユーザ一覧データ取得
$this->UserSearchComp->search([
'fields' => self::$displaFields,
'conditions' => array('space_id !=' => Space::PRIVATE_SPACE_ID),
'joins' => array('Room' => array(
'conditions' => array(
'Room.page_id_top NOT' => null,
)
))
]);
$queries = $this->request->query;
if (! empty($queries)) {
$this->Session->write(self::USER_MANAGER_SEARCH_CONDITIONS, $queries);
} else {
$this->Session->delete(self::USER_MANAGER_SEARCH_CONDITIONS);
}
$this->helpers[] = 'Users.UserSearch';
}
/**
* 検索フォーム表示アクション
*
* @return void
*/
public function search_conditions() {
//検索フォーム表示
$this->UserSearchComp->conditions();
}
/**
* view method
*
* @return void
*/
public function view() {
$userId = $this->params['user_id'];
$user = $this->User->getUser($userId);
if (! $user || $user['User']['is_deleted']) {
return $this->throwBadRequest();
}
$this->set('canUserEdit', $this->User->canUserEdit($user));
$this->set('user', $user);
$this->set('title', false);
$this->set('pluginName', Current::read('Plugin.name'));
//レイアウトの設定
$this->viewClass = 'View';
$this->layout = 'NetCommons.modal';
//ルームデータ取得
$this->Rooms->setReadableRooms($userId);
}
/**
* editアクション
*
* @return void
*/
public function edit() {
$this->helpers[] = 'Users.UserEditForm';
//システム管理者以外は、選択肢からシステム管理者を除外
$this->UserManager->setUserRoleAdminOnBasic();
if ($this->request->is('put')) {
$userId = $this->data['User']['id'];
} else {
$userId = $this->params['user_id'];
}
$user = $this->User->getUser($userId);
//編集できるかどうかチェック
if (! $this->User->canUserEdit($user)) {
return $this->throwBadRequest();
}
$this->set('canUserDelete', $this->User->canUserDelete($user));
//状態の選択肢から承認待ち、承認済みを除外
$this->UserManager->setStatusOnBasic($user);
if ($this->request->is('put')) {
//不要パラメータ除去
unset($this->request->data['save'], $this->request->data['active_lang_id']);
//登録処理
$this->UserManager->prepareBasicSave();
if ($this->User->saveUser($this->request->data)) {
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully saved.'), array('class' => 'success')
);
$user = $this->User->getUser($userId);
} else {
$this->NetCommons->handleValidationError($this->User->validationErrors);
$this->request->data = Hash::merge($user, $this->request->data);
}
} else {
//表示処理
$this->User->languages = $this->viewVars['languages'];
$this->request->data = $user;
}
// 絞り込み条件
$this->set('query', $this->Session->read(self::USER_MANAGER_SEARCH_CONDITIONS));
$this->set('user', $user['User']);
$this->set('userName', $this->request->data['User']['handlename']);
$this->set('activeUserId', $userId);
}
/**
* deleteアクション
*
* @return void
*/
public function delete() {
if (! $this->request->is('delete')) {
return $this->throwBadRequest();
}
//削除できるかチェック
$user = $this->User->getUser($this->data['User']['id']);
if (! $this->User->canUserDelete($user)) {
return $this->throwBadRequest();
}
$this->User->deleteUser($user);
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully deleted.'), array('class' => 'success')
);
$this->redirect(NetCommonsUrl::backToIndexUrl('default_setting_action'));
}
/**
* view method
*
* @return void
*/
public function status() {
if (! $this->request->is('put')) {
return $this->throwBadRequest();
}
$userId = $this->data['User']['id'];
$user = $this->User->getUser($userId);
//編集できるかどうかチェック
if (! $this->User->canUserEdit($user)) {
return $this->throwBadRequest();
}
$result = $this->AutoUserRegist->saveUserStatus(
['id' => $userId], AutoUserRegist::CONFIRMATION_ADMIN_APPROVAL, false
);
if ($result) {
$user = Hash::merge($user, $result);
$this->AutoUserRegistMail->sendMail(AutoUserRegist::CONFIRMATION_USER_OWN, $user);
$message = __d('auth', 'hank you for your registration.<br>' .
'We have sent you the registration key to your registered e-mail address.');
$this->NetCommons->setFlashNotification($message, array('class' => 'success'));
} else {
$message = __d('auth', 'Your registration was not approved.<br>' .
'Please consult with the system administrator.');
$this->NetCommons->setFlashNotification(__d('net_commons', 'Bad Request'), array(
'class' => 'danger',
'interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL,
'error' => $message
), 400);
}
$this->redirect($this->request->referer());
}
/**
* importアクション
*
* @return void
*/
public function import() {
//タイムアウトはっせいするなら適宜設定
set_time_limit(1800);
$this->set('importHelp', $this->User->getCsvHeader(true));
if ($this->request->is('post')) {
$file = $this->FileUpload->getTemporaryUploadFile('import_csv');
if (! $this->User->importUsers($file, $this->data['import_type'])) {
//バリデーションエラーの場合
$this->set('errorMessages', Hash::flatten($this->User->validationErrors));
$this->NetCommons->handleValidationError($this->User->validationErrors);
return;
}
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully saved.'), array('class' => 'success')
);
$this->redirect('/user_manager/user_manager/index/');
} else {
$this->set('errorMessages', null);
}
}
/**
* importファイルフォーマットのダウンロード
*
* @return void
*/
public function download_import_format() {
App::uses('CsvFileWriter', 'Files.Utility');
$header = $this->User->getCsvHeader();
$csvWriter = new CsvFileWriter(array('header' => $header));
$csvWriter->close();
return $csvWriter->download('import_file.csv');
}
/**
* exportアクション
*
* @return void
*/
public function export() {
//タイムアウトはっせいするなら適宜設定
set_time_limit(1800);
$this->helpers[] = 'Users.UserSearchForm';
$this->helpers[] = 'Users.UserSearch';
if (Hash::check($this->request->query, 'save')) {
App::uses('CsvFileWriter', 'Files.Utility');
$csvWriter = $this->User->exportUsers(
array(
'conditions' => array(
'space_id' => Space::PRIVATE_SPACE_ID,
'User.role_key NOT' => array(
UserRole::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR,
UserRole::USER_ROLE_KEY_ADMINISTRATOR
),
'User.is_deleted' => false,
),
'joins' => array('Room' => array(
'conditions' => array(
'Room.page_id_top NOT' => null,
)
))
),
$this->request->query
);
if (! $csvWriter) {
//バリデーションエラーの場合
$this->NetCommons->handleValidationError($this->User->validationErrors);
return;
}
return $csvWriter->zipDownload(
'export_user.zip', 'export_user.csv', $this->request->query['pass']
);
} else {
$defaultConditions = $this->UserSearch->cleanSearchFields($this->request->query);
$this->request->data['UserSearch'] = $defaultConditions;
$this->set('cancelQuery', $this->request->query);
$defaultConditions['search'] = '1';
$this->request->query = $defaultConditions;
}
}
}