-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUser.php
More file actions
735 lines (658 loc) · 19.2 KB
/
User.php
File metadata and controls
735 lines (658 loc) · 19.2 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
<?php
/**
* User Model
*
* @property Role $Role
* @property RolesRoom $RolesRoom
* @property Language $Language
*
* @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('UsersAppModel', 'Users.Model');
App::uses('NetCommonsTime', 'NetCommons.Utility');
App::uses('Current', 'NetCommons.Utility');
App::uses('UserAttribute', 'UserAttributes.Model');
/**
* User Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\Model
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class User extends UsersAppModel {
/**
* パスワードのHashType
*
* @var string
*/
const PASSWORD_HASH_TYPE = 'sha512';
/**
* 表示ページ数の定数
*
* @var const
*/
const DISPLAY_PAGE_NUMBER = 9;
/**
* 非公開の定数
*
* @var const
*/
const PUBLIC_TYPE_CONFIDENTIAL = '0';
/**
* 公開の定数
*
* @var const
*/
const PUBLIC_TYPE_DISCLOSE_TO_ALL = '1';
/**
* アバターNoimageサムネイル画像
*
* @var const
*/
const AVATAR_THUMB = 'noimage_thumbnail.gif';
/**
* アバターNoimage画像
*
* @var const
*/
const AVATAR_IMG = 'noimage.gif';
/**
* 公開・非公開のリスト
* __constructでセットする
*
* @var array
*/
public static $publicTypes = array();
/**
* language data.
*
* @var array
*/
public $languages = null;
/**
* user attribute data.
*
* @var array
*/
public $userAttributeData = array();
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
'Files.Attachment',
'Users.Avatar',
'Users.DeleteUser',
'Users.ImportExport',
'Users.UserPermission',
'Users.UsersValidationRule',
'Users.SaveUser',
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array();
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'UsersLanguage' => array(
'className' => 'Users.UsersLanguage',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array();
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @see Model::__construct()
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
self::$publicTypes = array(
self::PUBLIC_TYPE_CONFIDENTIAL => __d('users', 'Confidential'),
self::PUBLIC_TYPE_DISCLOSE_TO_ALL => __d('users', 'Disclose to all'),
);
//アバターの設定
if (! Configure::read('NetCommons.installed')) {
//インストール時は、アップロードビヘイビアを削除する
$this->Behaviors->unload('Files.Attachment');
$this->Behaviors->unload('Users.Avatar');
}
//RoleテーブルのbelongsToの定義は、こっちでやる
$this->bindModel(array(
'belongsTo' => array(
'Role' => array(
'className' => 'Roles.Role',
'foreignKey' => false,
'conditions' => array(
'User.role_key = Role.key',
'Role.type = 1',
'Role.language_id' => Current::read('Language.id', '2') //デフォルト日本語
),
'fields' => '',
'order' => ''
),
'UserRoleSetting' => array(
'className' => 'UserRoles.UserRoleSetting',
'foreignKey' => false,
'conditions' => array('User.role_key = UserRoleSetting.role_key'),
'fields' => '',
'order' => ''
),
)
), false);
}
/**
* UserModelの前準備
*
* 自動登録の場合、この処理を呼び出す前に$this->userAttributeDataをセットする。詳しくは、
* [Auth.AutoUserRegist::saveAutoUserRegist()](../../Auth/classes/AutoUserRegist.html#method_saveAutoUserRegist)
* を参照してください。
*
* @param bool $force 強制的に取得するフラグ
* @return void
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function prepare($force = false) {
$this->loadModels([
'UserAttribute' => 'UserAttributes.UserAttribute',
'UserAttributeSetting' => 'UserAttributes.UserAttributeSetting',
'DataType' => 'DataTypes.DataType',
]);
if ($force || ! $this->userAttributeData) {
$userAttributes = $this->UserAttribute->getUserAttributesForLayout($force);
$this->userAttributeData = [];
foreach ($userAttributes as $arr) {
foreach ($arr as $item) {
foreach ($item as $userAttribute) {
if (isset($userAttribute['UserAttribute'])) {
$this->userAttributeData[$userAttribute['UserAttribute']['id']] = $userAttribute;
}
}
}
}
}
if (Configure::read('NetCommons.installed')) {
$uploads = $this->UserAttributeSetting->find('list', array(
'recursive' => -1,
'fields' => array('id', 'user_attribute_key'),
'conditions' => array(
'data_type_key' => DataType::DATA_TYPE_IMG
),
));
foreach ($uploads as $upload) {
$this->uploadSettings($upload, array('contentKeyFieldName' => 'id'));
}
}
}
/**
* バリデーションのセット
*
* - ログインIDとパスワードのバリデーションルールをセットする。<br>
* その他のUserモデルのバリデーションルールのセットは、
* [Users.SaveUserBehavior::beforeValidate](../../Users/classes/SaveUserBehavior.html#method_beforeValidate)
* で行う。<br>
* また、UsersLanguageのバリデーションも実施する。
*
* - 自動登録のバリデーションの初期値のセットは、
* [Auth.AutoUserRegist::validateRequest](../../Users/classes/SaveUserBehavior.html#method_validateRequest)
* で行う。
*
* @param array $options Model::save()のオプション
* @return bool
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->loadModels([
'UsersLanguage' => 'Users.UsersLanguage',
]);
//ログインID
$this->_setUsernameValidate();
//パスワード
$this->_setPasswordValidate($options);
//ログイン、パスワード以外のUserモデルのバリデーションルールのセットは、ビヘイビアで行う
//(ログインとパスワードは、インストール時に使用するため)
//UsersLanguageのバリデーション実行
if (isset($this->data['UsersLanguage'])) {
// チェックボックスがある場合、arrayからstringへの変換をvalidateで行っている。
// そのため、値はvalidateに直でセットする
if (! $this->UsersLanguage->validateMany($this->data['UsersLanguage'])) {
$this->validationErrors = Hash::merge(
$this->validationErrors,
$this->UsersLanguage->validationErrors
);
return false;
}
}
return parent::beforeValidate($options);
}
/**
* バリデーションのセット(ログインID)
*
* @return void
*/
protected function _setUsernameValidate() {
//ログインID
if ($this->__isValidateUsernameRequired()) {
$this->validate = ValidateMerge::merge($this->validate, array(
'username' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(
__d('net_commons', 'Please input %s.'),
__d('users', 'username')
),
'required' => true
),
'alphaNumericSymbols' => array(
'rule' => array('alphaNumericSymbols', false),
'message' => sprintf(
__d('net_commons', 'Only alphabets, numbers and symbols are allowed to use for %s.'),
__d('users', 'username')
),
'allowEmpty' => false,
'required' => true,
),
'minLength' => array(
'rule' => array('minLength', 4),
'message' => __d('net_commons', 'Please choose at least %s characters string.', 4),
'required' => true
),
//notDuplicateテストは、SaveUserBehavior::__setValidates()でセットする
),
));
}
}
/**
* ログインID(username)のバリデーションを行うかどうか
*
* @return bool
*/
private function __isValidateUsernameRequired() {
if (!Hash::get($this->data, 'User.id')) {
// 新規登録ならば、必ずバリデーションを行う
return true;
}
// 更新時
if (!isset($this->data['User']['username'])) {
// ログインID(username)がPOSTされてなければバリデーション不要
return false;
}
$beforeUser = $this->find('first',
[
'conditions' => [
'id' => $this->data['User']['id']
],
'recursive' => -1,
'fields' => ['username']
]
);
// ログインID(username)が変更されてなければバリデーション不要。変更されてたらバリデーション必要
return $beforeUser['User']['username'] !== $this->data['User']['username'];
}
/**
* バリデーションのセット(パスワード)
*
* @param array $options Model::save()のオプション
* @return void
*/
protected function _setPasswordValidate($options = array()) {
//パスワード
if (Hash::get($this->data['User'], 'password') || ! isset($this->data['User']['id']) ||
Hash::get($options, 'validatePassword', false)) {
$this->validate = ValidateMerge::merge($this->validate, array(
'password' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(
__d('net_commons', 'Please input %s.'), __d('users', 'password')
),
'allowEmpty' => false,
'required' => true,
),
'alphaNumericSymbols' => array(
'rule' => array('alphaNumericSymbols', false),
'message' => sprintf(
__d('net_commons', 'Only alphabets, numbers and symbols are allowed to use for %s.'),
__d('users', 'password')
),
'allowEmpty' => false,
'required' => true,
),
'minLength' => array(
'rule' => array('minLength', 4),
'message' => __d('net_commons', 'Please choose at least %s characters string.', 4),
'required' => true
)
),
'password_again' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'allowEmpty' => false,
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('net_commons', 'Re-enter')),
'required' => true,
),
'equalToField' => array(
'rule' => array('equalToField', 'password'),
'message' => __d('net_commons', 'The input data does not match. Please try again.'),
'allowEmpty' => false,
'required' => true,
)
),
));
if (Hash::get($options, 'self', false)) {
$this->validate = ValidateMerge::merge($this->validate, array(
'password_current' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'allowEmpty' => false,
'message' => sprintf(
__d('net_commons', 'Please input %s.'), __d('net_commons', 'Current password')
),
'required' => true,
),
'currentPassword' => array(
'rule' => array('currentPassword'),
'message' => __d('net_commons', 'Current password is wrong.'),
'allowEmpty' => false,
'required' => true,
)
),
));
}
} elseif (isset($this->data['User']['password'])) {
//会員の編集時、パスワードを空にした場合、unsetする。
unset($this->data['User']['password']);
}
}
/**
* Called after data has been checked for errors
*
* @return void
*/
public function afterValidate() {
//パスワードのハッシュ化
if (Hash::get($this->data['User'], 'password')) {
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
$passwordHasher = new SimplePasswordHasher(['hashType' => self::PASSWORD_HASH_TYPE]);
$this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
$passwordAgain = $this->data['User']['password_again'];
$this->data['User']['password_again'] = $passwordHasher->hash($passwordAgain);
//パスワード変更日時セット
$this->data['User']['password_modified'] = NetCommonsTime::getNowDatetime();
}
}
/**
* Userの生成
*
* @return array
*/
public function createUser() {
$this->loadModels([
'UserRole' => 'UserRoles.UserRole',
'Language' => 'M17n.Language',
]);
if (! isset($this->languages)) {
$this->languages = $this->Language->getLanguage('list');
}
$results['UsersLanguage'] = array();
foreach (array_keys($this->languages) as $langId) {
$index = count($results['UsersLanguage']);
$usersLanguage = $this->UsersLanguage->create(array(
'id' => null,
'language_id' => $langId,
));
$results['UsersLanguage'][$index] = $usersLanguage['UsersLanguage'];
}
$results = Hash::merge($results,
$this->create(array(
'id' => null,
'role_key' => UserRole::USER_ROLE_KEY_COMMON_USER,
'timezone' => (new NetCommonsTime())->getSiteTimezone(),
'language' => 'auto',
))
);
return $results;
}
/**
* Userの存在チェック
*
* @param int|array $userId ユーザID
* @return bool True:正常、False:不正
*/
public function existsUser($userId) {
if (! $userId) {
return false;
}
if (! is_array($userId)) {
$userId = (array)$userId;
}
$count = $this->find('count', array(
'recursive' => -1,
'conditions' => array(
$this->alias . '.id' => $userId,
$this->alias . '.is_deleted' => false,
),
));
return count($userId) === $count;
}
/**
* Userの取得
*
* @param int $userId ユーザID
* @param int $languageId 言語ID
* @return array
*/
public function getUser($userId, $languageId = null) {
$this->prepare();
$user = $this->find('first', array(
'recursive' => 0,
'conditions' => array(
$this->alias . '.id' => $userId
),
));
unset($user['User']['password']);
$conditions = array(
$this->UsersLanguage->alias . '.user_id' => $userId
);
if (isset($languageId)) {
$conditions[$this->UsersLanguage->alias . '.language_id'] = $languageId;
}
if ($user) {
$usersLanguage = $this->UsersLanguage->find('all', array(
'recursive' => 0,
'fields' => array(
$this->UsersLanguage->alias . '.*'
),
'conditions' => $conditions,
));
$user[$this->UsersLanguage->alias] = [];
foreach ($usersLanguage as $item) {
$user[$this->UsersLanguage->alias][] = $item['UsersLanguage'];
}
}
return $user;
}
/**
* 管理者ユーザのメールアドレス取得
* ここでいう管理者権限とは、会員管理が使える権限のこと。
*
* @return array
*/
public function getMailAddressForAdmin() {
$this->loadModels([
'PluginsRole' => 'PluginManager.PluginsRole',
'User' => 'Users.User',
]);
$roleKeys = $this->PluginsRole->cacheFindQuery('list', array(
'recursive' => -1,
'fields' => array('id', 'role_key'),
'conditions' => array(
'plugin_key' => 'user_manager',
),
));
$conditions = array(
'role_key' => $roleKeys
);
$emailFields = $this->User->getEmailFields();
$fields = $emailFields;
$conditions['OR'] = array();
foreach ($emailFields as $field) {
$fields[] = sprintf(UserAttribute::MAIL_RECEPTION_FIELD_FORMAT, $field);
$conditions['OR'][] = array(
$field . ' !=' => '',
sprintf(UserAttribute::MAIL_RECEPTION_FIELD_FORMAT, $field) => true
);
}
$mails = $this->User->find('all', array(
'recursive' => -1,
'fields' => $fields,
'conditions' => $conditions,
));
$result = array();
foreach ($mails as $mail) {
foreach ($emailFields as $field) {
if ($mail['User'][sprintf(UserAttribute::MAIL_RECEPTION_FIELD_FORMAT, $field)] &&
$mail['User'][$field]) {
$result[] = $mail['User'][$field];
}
}
}
return $result;
}
/**
* ユーザの登録処理
*
* @param array $data data
* @param bool $self 自分自身かどうか(=UsersプラグインかUserManagerかどうか)
* @return mixed On success Model::$data, false on failure
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function saveUser($data, $self = false) {
//トランザクションBegin
$this->begin();
$this->prepare();
//プライベートルームの登録
$this->loadModels([
//'Space' => 'Rooms.Space',
'Room' => 'Rooms.Room',
]);
$currentRoom = Current::read('Room');
Current::$current['Room'] = null;
$beforeUser = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
$this->alias . '.id' => Hash::get($data, 'User.id')
),
));
if (Hash::get($data, 'User.' . UserAttribute::AVATAR_FIELD . '.remove')) {
$data['User']['is_avatar_auto_created'] = true;
} elseif (Hash::get($data, 'User.' . UserAttribute::AVATAR_FIELD . '.name')) {
$data['User']['is_avatar_auto_created'] = false;
} else {
$isAvatarAutoCreated = Hash::get($beforeUser, 'User.is_avatar_auto_created', true);
$data['User']['is_avatar_auto_created'] = (bool)$isAvatarAutoCreated;
}
//バリデーション
$this->set($data);
if (! $this->validates(array('self' => $self))) {
return false;
}
// もしも言語の値が未設定の場合はデフォルトの現在値を設定する
if (empty($this->data['User']['language'])) {
$this->data['User']['language'] = Current::read('Language.code');
}
try {
//Userデータの登録
if (! $user = $this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($this->Behaviors->hasMethod('createAvatarAutomatically')) {
//下記の条件の場合、自動的にアバターを生成する
// * 削除がチェックONになっている ||
// * アップロードファイルがない &&
// アバターを自動生成する場合 &&
// ハンドルを登録(POSTに含まれている)する場合 &&
// 登録前のハンドル名と登録後のハンドル名が異なる場合
if ($this->validAvatarAutomatically($data, $user, $beforeUser)) {
$this->temporaryAvatar($user, UserAttribute::AVATAR_FIELD);
}
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
Current::$current['Room'] = $currentRoom;
return $user;
}
/**
* ユーザのステータスを更新する
*
* @param string|int $userId ユーザID
* @param string|int $status 状態値
* @return bool
* @throws InternalErrorException
*/
public function updateStatus($userId, $status) {
try {
//トランザクションBegin
$this->begin();
$this->create(false);
$this->set(['id' => $userId, 'status' => $status]);
if (! $this->save(null, [
'validate' => false,
'callbacks' => false,
'fieldList' => ['status']
])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
};
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
}