-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUsersController.php
More file actions
407 lines (358 loc) · 10.9 KB
/
UsersController.php
File metadata and controls
407 lines (358 loc) · 10.9 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* Users Controller
*
* @property User $User
* @property PaginatorComponent $Paginator
*
* @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('UsersAppController', 'Users.Controller');
App::uses('UserSelectCount', 'Users.Model');
App::uses('UserAttribute', 'UserAttributes.Model');
/**
* Users Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\Controller
*/
class UsersController extends UsersAppController {
/**
* 会員一覧の表示する項目
*/
public static $displaField = array('handlename');
/**
* use model
*
* @var array
*/
public $uses = array(
'Rooms.Space',
'Rooms.RolesRoomsUser',
'Users.User',
'Users.UserSelectCount',
'Groups.Group',
'Groups.GroupsUser',
);
/**
* Components
*
* @var array
*/
public $components = array(
'Files.Download',
'M17n.SwitchLanguage',
'Rooms.Rooms',
'UserAttributes.UserAttributeLayout',
'Users.UserSearchComp',
'Groups.Groups',
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'NetCommons.Token',
'UserAttributes.UserAttributeLayout',
'Users.UserLayout',
'Groups.GroupUserList',
);
/**
* アクションの前処理
* Controller::beforeFilter()のあと、アクション前に実行する
*
* @return void
*/
private function __prepare() {
$this->Auth->deny('index', 'view');
//ユーザデータ取得
if ($this->request->is('put') || $this->request->is('delete')) {
$userId = $this->data['User']['id'];
} else {
$userId = $this->params['user_id'];
}
$user = $this->User->getUser($userId);
if (! $user || $user['User']['is_deleted']) {
return $this->setAction('throwBadRequest');
}
$this->set('user', $user);
$this->set('title', false);
//ルームデータチェック
if (Hash::get($this->data, 'Room.id')) {
$roomId = Hash::get($this->data, 'Room.id');
} elseif (Hash::get($this->request->query, 'room_id')) {
$roomId = Hash::get($this->request->query, 'room_id');
} else {
$roomId = null;
}
if ($roomId) {
//ルームデータ取得
$conditions = array('Room.id' => $roomId);
$count = $this->Room->find('count', $this->Room->getReadableRoomsConditions($conditions));
if (! $count) {
return $this->setAction('throwBadRequest');
}
$this->set('roomId', $roomId);
}
}
/**
* view method
*
* @return void
*/
public function view() {
$this->__prepare();
//レイアウトの設定
if ($this->request->is('ajax')) {
$this->viewClass = 'View';
$this->layout = 'NetCommons.modal';
} elseif (Current::isControlPanel()) {
$this->ControlPanelLayout = $this->Components->load('ControlPanel.ControlPanelLayout');
} else {
$this->PageLayout = $this->Components->load('Pages.PageLayout');
}
if (! Hash::get($this->request->query, 'tab')) {
$this->request->query = Hash::insert($this->request->query, 'tab', 'user-infomation');
}
//自分自身の場合、ルーム・グループデータ取得する
if (Hash::get($this->viewVars['user'], 'User.id') === Current::read('User.id')) {
//ルームデータ取得
$this->Rooms->setReadableRooms(Hash::get($this->viewVars['user'], 'User.id'));
// グループデータ取得・設定
$this->Groups->setGroupList($this);
} else {
if (Current::allowSystemPlugin('rooms')) {
//ルームデータ取得
$this->Rooms->setReadableRooms(Hash::get($this->viewVars['user'], 'User.id'));
}
}
}
/**
* edit method
*
* @return void
*/
public function edit() {
$this->helpers[] = 'Users.UserEditForm';
$this->__prepare();
if (Current::isControlPanel()) {
$this->ControlPanelLayout = $this->Components->load('ControlPanel.ControlPanelLayout');
} else {
$this->PageLayout = $this->Components->load('Pages.PageLayout');
}
if (Hash::get($this->viewVars['user'], 'User.id') !== Current::read('User.id')) {
return $this->throwBadRequest();
}
if ($this->request->is('put')) {
$redirectUrl = Hash::get($this->request->data, '_user.redirect');
if (array_key_exists('cancel', $this->request->data)) {
$this->NetCommons->setAppendHtml(
'<div class="hidden" ng-controller="Users.controller" ' .
'ng-init="showUser(null, ' . Current::read('User.id') . ')"></div>'
);
return $this->redirect($redirectUrl);
}
//登録処理
if ($this->User->saveUser($this->request->data, true)) {
//正常の場合
$this->NetCommons->setFlashNotification(
__d('net_commons', 'Successfully saved.'), array('class' => 'success')
);
$this->NetCommons->setAppendHtml(
'<div class="hidden" ng-controller="Users.controller" ' .
'ng-init="showUser(null, ' . Current::read('User.id') . ')"></div>'
);
return $this->redirect($redirectUrl);
}
$this->NetCommons->handleValidationError($this->User->validationErrors);
$this->request->data = Hash::merge($this->viewVars['user'], $this->request->data);
} else {
//表示処理
$this->User->languages = $this->viewVars['languages'];
$this->request->data = $this->viewVars['user'];
$redirectUrl = $this->request->referer(true);
}
$this->set('userCancel', SiteSettingUtil::read('UserCancel.use_cancel_feature', false));
$this->set('redirectUrl', $redirectUrl);
$this->set('activeUserId', Hash::get($this->viewVars['user'], 'User.id'));
}
/**
* delete method
*
* @return void
*/
public function delete() {
$this->__prepare();
if (! SiteSettingUtil::read('UserCancel.use_cancel_feature', false)) {
return $this->throwBadRequest();
}
if (Hash::get($this->viewVars['user'], 'User.id') !== Current::read('User.id') ||
$this->viewVars['user']['User']['role_key'] === UserRole::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR) {
return $this->throwBadRequest();
}
if (! $this->request->is('delete')) {
return $this->throwBadRequest();
}
$this->User->deleteUser($this->viewVars['user']);
$this->redirect('/auth/logout');
}
/**
* download method
*
* @return void
* @throws NotFoundException
*/
public function download() {
$this->__prepare();
$user = $this->viewVars['user'];
$fieldName = $this->params['field_name'];
$fieldSize = $this->params['size'];
$fileSetting = Hash::extract(
$this->viewVars['userAttributes'],
'{n}.{n}.{n}.UserAttributeSetting[user_attribute_key=' . $fieldName . ']'
);
if (! $fileSetting) {
$this->response->file(
$this->User->temporaryAvatar($user, $fieldName, $fieldSize),
array('name' => 'No Image')
);
return $this->response;
}
$userAttribute = Hash::get($this->viewVars['userAttributes'],
$fileSetting[0]['row'] . '.' . $fileSetting[0]['col'] . '.' . $fileSetting[0]['weight']
);
if (! Hash::get($user, 'UploadFile.' . $fieldName . '.field_name')) {
$this->response->file(
$this->User->temporaryAvatar($user, $fieldName, $fieldSize),
array('name' => 'No Image')
);
return $this->response;
}
//以下の場合、アバター表示
// * 自動生成画像
// * 自分自身
if (Hash::get($user, 'User.id') === Current::read('User.id')) {
return $this->Download->doDownload($user['User']['id'],
array('field' => $fieldName, 'size' => $fieldSize)
);
}
// 以下の条件の場合、ハンドル画像を表示する(他人)
// * 各自で公開・非公開が設定可 && 非公開
// * 権限設定の個人情報設定で閲覧不可、
// * 会員項目設定で非表示(display=OFF)項目、
if ($userAttribute['UserAttributeSetting']['self_public_setting'] &&
! Hash::get($user, 'User.' . sprintf(UserAttribute::PUBLIC_FIELD_FORMAT, $fieldName)) ||
! $userAttribute['UserAttributesRole']['other_readable'] ||
! $userAttribute['UserAttributeSetting']['display']) {
if (Hash::get($this->viewVars['user'], 'User.is_avatar_auto_created')) {
return $this->Download->doDownload($user['User']['id'],
array('field' => $fieldName, 'size' => $fieldSize)
);
} else {
$this->response->file(
$this->User->temporaryAvatar($user, $fieldName, $fieldSize),
array('name' => 'No Image')
);
return $this->response;
}
} else {
return $this->Download->doDownload($user['User']['id'],
array('field' => $fieldName, 'size' => $fieldSize)
);
}
}
/**
* search method
*
* @return void
*/
public function search() {
//$this->layout = 'NetCommons.default';
$this->viewClass = 'View';
$this->view = 'Users.Users/json/search';
$this->__prepare();
if (Hash::get($this->viewVars['user'], 'User.id') !== Current::read('User.id')) {
return;
}
$roomId = Hash::get($this->request->query, 'room_id');
$query = Hash::remove($this->request->query, 'room_id');
if ($roomId === Room::ROOM_PARENT_ID) {
$conditions = array(
'Room.id' => Room::ROOM_PARENT_ID
);
} else {
$conditions = array(
'Room.page_id_top NOT' => null
);
}
$this->UserSearchComp->search(array(
'fields' => self::$displaField,
'conditions' => Hash::merge(array(), $query),
'joins' => array('Room' => array(
'conditions' => $conditions
)),
'order' => array(),
'limit' => UserSelectCount::LIMIT
));
}
/**
* select method
*
* @return void
*/
public function select() {
$this->__prepare();
if (Hash::get($this->viewVars['user'], 'User.id') !== Current::read('User.id')) {
$this->throwBadRequest();
return;
}
$roomId = Hash::get($this->viewVars, 'roomId');
if (! $roomId) {
$this->throwBadRequest();
return;
}
if ($this->request->is('post')) {
//登録処理
//** ロールルームユーザデータ取得
$rolesRoomsUsers = $this->RolesRoomsUser->getRolesRoomsUsers(array(
'RolesRoomsUser.user_id' => $this->request->data['UserSelectCount']['user_id'],
'Room.id' => $roomId
));
$userIds = Hash::extract($rolesRoomsUsers, '{n}.RolesRoomsUser.user_id');
sort($userIds);
sort($this->request->data['UserSelectCount']['user_id']);
//** user_idのチェック
if (Hash::diff($userIds, $this->request->data['UserSelectCount']['user_id'])) {
//diffがあった場合は、不正ありと判断する
$this->throwBadRequest();
return;
}
$data = array_map(function ($userId) {
return array('UserSelectCount' => array(
'user_id' => $userId, 'created_user' => Current::read('User.id')
));
}, $this->request->data['UserSelectCount']['user_id']);
//** 登録処理
if (! $this->UserSelectCount->saveUserSelectCount($data)) {
$this->NetCommons->handleValidationError($this->UserSelectCount->validationErrors);
}
return;
} else {
//表示処理
//** レイアウトの設定
$this->viewClass = 'View';
$this->layout = 'NetCommons.modal';
//** 選択したユーザ取得
$users = $this->UserSelectCount->getUsers($roomId);
if (! $users) {
$users = array();
}
$this->set('searchResults', $users);
}
}
}