This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
UserBehavior.php
789 lines (719 loc) · 20.7 KB
/
UserBehavior.php
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<?php
/**
* UserBehavior
*
* @author Florian Krämer
* @copyright 2013 - 2015 Florian Krämer
* @copyright 2012 Cake Development Corporation
* @license MIT
*/
namespace Burzum\UserTools\Model\Behavior;
use Cake\Auth\PasswordHasherFactory;
use Cake\Core\Configure;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\Event;
use Cake\Event\EventManagerTrait;
use Cake\I18n\Time;
use Cake\Network\Email\Email;
use Cake\ORM\Behavior;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use Cake\Validation\Validator;
class UserBehavior extends Behavior {
use EventManagerTrait;
/**
* Default config
*
* @var array
*/
protected $_defaultConfig = [
'emailConfig' => 'default',
'defaultValidation' => true,
'validatorClass' => '\Burzum\UserTools\Validation\UsersValidator',
'useUuid' => true,
'passwordHasher' => 'Default',
'register' => [
'defaultRole' => null,
'hashPassword' => true,
'userActive' => true,
'generatePassword' => false,
'emailVerification' => true,
'verificationExpirationTime' => '+1 day',
'beforeRegister' => true,
'afterRegister' => true,
'tokenLength' => 32,
],
'loginFields' => [
'username' => 'email',
'password' => 'password'
],
'fieldMap' => [
'username' => 'username',
'password' => 'password',
'email' => 'email',
'passwordCheck' => 'confirm_password',
'lastAction' => 'last_action',
'lastLogin' => 'last_login',
'role' => 'role',
'emailToken' => 'email_token',
'emailVerified' => 'email_verified',
'emailTokenExpires' => 'email_token_expires',
'passwordToken' => 'password_token',
'passwordTokenExpires' => 'password_token_expires',
'active' => 'active',
'slug' => 'slug',
],
'updateLastActivity' => [
'dateFormat' => 'Y-m-d H:i:s',
],
'initPasswordReset' => [
'tokenLength' => 32,
'expires' => '+1 day'
],
'sendVerificationEmail' => [
'template' => 'Burzum/UserTools.Users/verification_email',
],
'sendNewPasswordEmail' => [
'template' => 'Burzum/UserTools.Users/new_password',
],
'sendPasswordResetToken' => [
'template' => 'Burzum/UserTools.Users/password_reset',
],
'implementedFinders' => [
'active' => 'findActive',
'emailVerified' => 'findEmailVerified'
]
];
/**
* Keeping a reference to the table in order to be able to retrieve associations
* and fetch records for counting.
*
* @var array
*/
protected $_table;
/**
* AbstractPasswordHasher instance.
*
* @var AbstractPasswordHasher
*/
protected $_passwordHasher;
/**
* Constructor
*
* @param \Cake\ORM\Table $table The table this behavior is attached to.
* @param array $config The settings for this behavior.
*/
public function __construct(Table $table, array $config = []) {
$this->_defaultConfig = Hash::merge($this->_defaultConfig, (array) Configure::read('UserTools.Behavior'));
parent::__construct($table, $config);
$this->_table = $table;
if ($this->_config['defaultValidation'] === true) {
$this->setupDefaultValidation($this->_table);
}
$this->eventManager()->attach($this->_table);
}
/**
* Gets the mapped field name of the table
*
* @param string $field
* @throws \RuntimeException
* @return string field name of the table
*/
protected function _field($field) {
if (!isset($this->_config['fieldMap'][$field])) {
throw new \RuntimeException(__d('user_tools', 'Invalid field "%s"!', $field));
}
return $this->_config['fieldMap'][$field];
}
/**
* Sets validation rules for users up.
*
* @return void
*/
public function setupDefaultValidation() {
$class = $this->_config['validatorClass'];
$Validator = new $class($this->_table);
$this->_table->validator('default', $Validator);
}
/**
* Returns a datetime in the format Y-m-d H:i:s
*
* @param string strtotime compatible string, default is "+1 day"
* @param string date() compatible date format string
* @return string
*/
public function expirationTime($time = '+1 day', $dateFormat = 'Y-m-d H:i:s') {
return date($dateFormat, strtotime($time));
}
/**
* Updates a given field with the current date time in the format Y-m-d H:i:s
*
* @param string $userId User id
* @param string $field Default is "last_action", changing it allows you to use this method also for "last_login" for example
* @param array $options
* @return boolean True on success
*/
public function updateLastActivity($userId = null, $field = 'last_action', $options = []) {
$options = Hash::merge($this->_config['updateLastActivity'], $options);
if ($this->_table->exists([
$this->_table->alias() . '.' . $this->_table->primaryKey() => $userId
])) {
return $this->_table->updateAll(
[$field => date($options['dateFormat'])],
[$this->_table->primaryKey() => $userId]
);
}
return false;
}
/**
* Hashes a password
*
* @param $password
* @return string Hash
*/
public function hashPassword($password) {
return $this->passwordHasher()->hash($password);
}
/**
* Behavior internal before registration callback
*
* This method deals with most of the settings for the registration that can be
* applied before the actual user record is saved.
*
* @param \Cake\ORM\Entity $entity
* @param array $options
* @return Entity
*/
protected function _beforeRegister(Entity $entity, $options = []) {
extract(Hash::merge($this->_config['register'], $options));
if ($this->_config['useUuid'] === true) {
$primaryKey = $this->_table->primaryKey();
$entity->{$primaryKey} = Text::uuid();
}
if ($userActive === true) {
$entity->{$this->_field('active')} = true;
}
if ($emailVerification === true) {
$entity->{$this->_field('emailToken')} = $this->generateToken($tokenLength);
if ($verificationExpirationTime !== false) {
$entity->{$this->_field('emailTokenExpires')} = $this->expirationTime($verificationExpirationTime);
}
$entity->{$this->_field('emailVerified')} = false;
} else {
$entity->{$this->_field('emailVerified')} = true;
}
if (!isset($entity->{$this->_field('role')})) {
$entity->{$this->_field('role')} = $defaultRole;
}
if ($generatePassword === true) {
$password = $this->generatePassword((int) $generatePassword);
$entity->{$this->_field('password')} = $password;
$entity->clear_password = $password;
}
if ($hashPassword === true) {
$entity->{$this->_field('password')} = $this->hashPassword($entity->{$this->_field('password')});
}
return $entity;
}
/**
* Find users with verified emails.
*
* @param Query $query
* @param array $options
* @return Query
*/
public function findEmailVerified(Query $query, array $options) {
$query->where([
$this->alias() . '.email_verified' => true,
]);
return $query;
}
/**
* Find Active Users.
*
* @param Query $query
* @param array $options
* @return Query
*/
public function findActive(Query $query, array $options) {
$query->where([
$this->alias() . '.active' => true,
]);
return $query;
}
/**
* Registers a new user
*
* Flow:
* - validates the passed $postData
* - calls the behaviors _beforeRegister if not disabled
* - calls Model::beforeRegister if implemented
* - saves the user data
* - calls Model::afterRegister if implemented
*
* @param \Cake\ORM\Entity $entity
* @param array $options
* @throws \InvalidArgumentException
* @return boolean
*/
public function register(Entity $entity, $options = []) {
$options = array_merge($this->_config['register'], $options);
if ($entity->errors()) {
$this->_table->entity = $entity;
return false;
}
if ($options['beforeRegister'] === true) {
$entity = $this->_beforeRegister($entity, $options);
}
$event = new Event('User.beforeRegister', $this, [
'data' => $entity,
'table' => $this->_table
]);
$this->eventManager()->dispatch($event);
if ($event->isStopped()) {
return (bool) $event->result;
}
$result = $this->_table->save($entity, array('validate' => false));
if ($options['afterRegister'] === true) {
$entity = $this->_afterRegister($result, $options);
}
$event = new Event('User.afterRegister', $this, [
'data' => $result,
'table' => $this->_table
]);
$this->eventManager()->dispatch($event);
if ($event->isStopped()) {
return $event->result;
}
return $result;
}
/**
* _afterRegister
*
* @param \Cake\ORM\Entity $entity
* @param array $options
* @return \Cake\ORM\Entity
*/
protected function _afterRegister($entity, $options) {
if ($entity) {
if ($options['emailVerification'] === true) {
$this->sendVerificationEmail($entity, array(
'to' => $entity->{$this->_field('email')}
));
}
}
return $entity;
}
/**
* Verify the email token
*
* @param string $token The token to check.
* @param array $options Options array.
* @throws \Cake\Datasource\Exception\RecordNotFoundException if the token was not found at all
* @return boolean|\Cake\ORM\Entity Returns false if the token has expired
*/
public function verifyToken($token, $options = []) {
$defaults = [
'tokenField' => $this->_field('emailToken'),
'expirationField' => $this->_field('emailTokenExpires'),
'returnData' => false,
];
$options = Hash::merge($defaults, $options);
$result = $this->_getUser($token, [
'field' => $options['tokenField'],
'notFoundErrorMessage' => __d('user_tools', 'Invalid token.')
]);
$time = new Time();
$result->token_is_expired = $result->{$options['expirationField']} <= $time;
$this->afterTokenVerification($result, $options);
$event = new Event('User.afterTokenVerification', $this, [
'data' => $result,
'options' => $options
]);
$this->eventManager()->dispatch($event);
if ($event->isStopped()) {
return (bool) $event->result;
}
if ($options['returnData'] === true) {
return $result;
}
return $result->token_is_expired;
}
/**
* afterTokenVerification
*
* @param \Cake\ORM\Entity $user Entity object.
* @param array $options Options array.
* @return mixed
*/
public function afterTokenVerification(Entity $user, $options = []) {
if ($user->token_is_expired === true) {
return false;
}
if ($options['tokenField'] === $this->_field('emailToken')) {
$user->{$this->_field('emailVerified')} = 1;
$user->{$this->_field('emailToken')} = null;
$user->{$this->_field('emailTokenExpires')} = null;
}
return $this->_table->save($user, ['validate' => false]);
}
/**
* Verify the email token
*
* @param string $token Token string to check.
* @param array $options Options array.
* @throws \Cake\Datasource\Exception\RecordNotFoundException if the token was not found at all
* @return boolean Returns false if the token has expired
*/
public function verifyEmailToken($token, $options = []) {
$defaults = [
'tokenField' => $this->_field('emailToken'),
'expirationField' => $this->_field('emailTokenExpires'),
];
return $this->verifyToken($token, Hash::merge($defaults, $options));
}
/**
* Verify the password reset token
*
* @param string $token
* @param array $options
* @throws \Cake\Datasource\Exception\RecordNotFoundException if the token was not found at all
* @return boolean Returns false if the token has expired
*/
public function verifyPasswordResetToken($token, $options = []) {
$defaults = array(
'tokenField' => $this->_field('passwordToken'),
'expirationField' => $this->_field('passwordTokenExpires'),
'returnData' => true,
);
return $this->verifyToken($token, Hash::merge($defaults, $options));
}
/**
* Password reset, compares the two passwords and saves the new password
*
* @param \Cake\ORM\Entity $user Entity object.
* @return void
*/
public function resetPassword(Entity $user) {
if (!$user->errors()) {
$user->{$this->_field('password')} = $this->hashPassword($user->{$this->_field('password')});
$user->{$this->_field('passwordToken')} = null;
$user->{$this->_field('passwordTokenExpires')} = null;
return $this->_table->save($user, ['checkRules' => false]);
}
return false;
}
/**
* Generates a random password that is more or less user friendly
*
* @param int $length Password length, default is 8
* @param array $options Options array.
* @return string
*/
public function generatePassword($length = 8, $options = []) {
srand((double) microtime() * 1000000);
$defaults = [
'vowels' => [
'a', 'e', 'i', 'o', 'u'
],
'cons' => [
'b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n',
'p', 'r', 's', 't', 'u', 'v', 'w', 'tr', 'cr', 'br', 'fr', 'th',
'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl'
]
];
if (isset($options['cons'])) {
unset($defaults['cons']);
}
if (isset($options['vowels'])) {
unset($defaults['vowels']);
}
$options = Hash::merge($defaults, $options);
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .=
$options['cons'][mt_rand(0, count($options['cons']) - 1)] .
$options['vowels'][mt_rand(0, count($options['vowels']) - 1)];
}
return substr($password, 0, $length);
}
/**
* Generate token used by the user registration system
*
* @param integer $length Token Length
* @param string $chars
* @return string
*/
public function generateToken($length = 10, $chars = '0123456789abcdefghijklmnopqrstuvwxyz') {
$token = '';
$i = 0;
while ($i < $length) {
$char = substr($chars, mt_rand(0, strlen($chars) - 1), 1);
if (!stristr($token, $char)) {
$token .= $char;
$i++;
}
}
return $token;
}
/**
* Removes all users from the user table that did not complete the registration
*
* @param array $conditions
* @return integer Number of removed records
*/
public function removeExpiredRegistrations($conditions = []) {
$defaults = [
$this->_table->alias() . '.' . $this->_field('emailVerified') => 0,
$this->_table->alias() . '.' . $this->_field('emailTokenExpires') . ' <' => date('Y-m-d H:i:s')
];
$conditions = Hash::merge($defaults, $conditions);
$count = $this->_table
->find()
->where($conditions)
->count();
if ($count > 0) {
$this->_table->deleteAll($conditions);
}
return $count;
}
/**
* Changes the password for an user.
*
* @param \Cake\ORM\Entity $entity User entity
* @return boolean
*/
public function changePassword(Entity $entity) {
$validator = $this->_table->validator('default');
$validator->provider('userTable', $this->_table);
$validator->add('old_password', 'notBlank', [
'rule' => 'notBlank',
'message' => __d('userTools', 'Enter your old password.')
]);
$validator->add('old_password', 'oldPassword', [
'rule' => ['validateOldPassword', 'password'],
'provider' => 'userTable',
'message' => __d('user_tools', 'Wrong password, please try again.')
]);
$this->_table->validator('default', $validator);
if ($entity->errors()) {
return false;
}
$entity->password = $this->hashPassword($entity->password);
if ($this->_table->save($entity, ['validate' => false])) {
return true;
}
return false;
}
/**
*
*/
public function validateOldPassword($value, $field, $context) {
$result = $this->_table->find('all', [
'fields' => [
'password'
],
'conditions' => [
$this->_table->primaryKey() => $context['data'][$this->_table->primaryKey()],
]
])->first();
if (!$result) {
return false;
}
return $this->passwordHasher()->check($value, $result->password);
}
/**
* Validation rules for the password reset request.
*
* @param \Cake\Validation\Validator $validator
* @return \Cake\Validation\Validator
* @see Burzum\UserTools\Controller\Component\UserToolComponent::requestPassword()
*/
public function validationRequestPassword(Validator $validator) {
$validator = $this->_table->validationDefault($validator);
$validator->remove($this->_field('email'), 'unique');
return $validator;
}
/**
* Initializes a password reset process.
*
* @param mixed $value
* @param array $options
* @return array
*/
public function initPasswordReset($value, $options = []) {
$defaults = [
'field' => [
$this->_table->alias() . '.' . $this->_field('email'),
$this->_table->alias() . '.' . $this->_field('username')
]
];
$options = Hash::merge($defaults, $this->_config['initPasswordReset'], $options);
$result = $this->_getUser($value, $options);
if (empty($result)) {
throw new RecordNotFoundException(__d('user_tools', 'User not found.'));
}
$result->{$this->_field('passwordToken')} = $this->generateToken($options['tokenLength']);
$result->{$this->_field('passwordTokenExpires')} = $this->expirationTime($options['expires']);
$this->_table->save($result, ['checkRules' => false]);
return $this->sendPasswordResetToken($result, [
'to' => $result->{$this->_field('email')}
]);
}
/**
* Finds a single user, convenience method
*
* @param mixed $value
* @param array $options
* @throws \Cake\Datasource\Exception\RecordNotFoundException;
* @return \Cake\ORM\Entity
*/
public function getUser($value, $options = []) {
return $this->_getUser($value, $options);
}
/**
* Finds the user for the password reset
*
* Extend the behavior and override this method if the configuration options
* are not sufficient.
*
* @param mixed $value
* @param array $options
* @throws \Cake\Datasource\Exception\RecordNotFoundException
* @return \Cake\ORM\Entity
*/
protected function _getUser($value, $options = []) {
$defaults = [
'notFoundErrorMessage' => __d('user_tools', 'User not found'),
'field' => $this->_table->alias() . '.' . $this->_table->primaryKey()
];
$options = Hash::merge($defaults, $options);
$query = $this->_table->find();
if (is_array($options['field'])) {
foreach ($options['field'] as $field) {
$query->orWhere([$field => $value]);
}
} else {
$query->where([$options['field'] => $value]);
}
$result = $query->first();
if (empty($result)) {
throw new RecordNotFoundException($options['notFoundErrorMessage']);
}
return $result;
}
/**
* Sends a new password to an user by email
*
* Note that this is *not* a recommended way to reset an user password. A much
* more secure approach is to have the user manually enter a new password and
* only send him an URL with a token.
*
* @param string $email
* @param array $options
* @throws \Cake\Datasource\Exception\RecordNotFoundException
* @return boolean
*/
public function sendNewPassword($email, $options = []) {
$result = $this->_table->find()
->where([
$this->_table->alias() . '.' . $this->_field('email') => $email
])
->first();
if (empty($result)) {
throw new RecordNotFoundException(__d('user_tools', 'Invalid user'));
}
$result->password = $result->clear_password = $this->generatePassword();
$result->password = $this->hashPassword($result->password);
$this->_table->save($result, ['validate' => false]);
return $this->sendNewPasswordEmail($result, ['to' => $result->{$this->_field('email')}]);
}
/**
* Return password hasher object
*
* @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
* @throws \RuntimeException If password hasher class not found or
* it does not extend AbstractPasswordHasher
*/
public function passwordHasher() {
if ($this->_passwordHasher) {
return $this->_passwordHasher;
}
return $this->_passwordHasher = PasswordHasherFactory::build($this->_config['passwordHasher']);
}
/**
* Returns an email instance
*
* @param array $config
* @return \Cake\Network\Email\Email Email instance
*/
public function getMailInstance($config = null) {
if (empty($config)) {
$config = $this->_config['emailConfig'];
}
return new Email($config);
}
/**
* sendEmail
*
* @param array $options
* @return array
*/
public function sendEmail($options = []) {
$Email = $this->getMailInstance();
foreach ($options as $option => $value) {
$Email->{$option}($value);
}
return $Email->send();
}
/**
* sendNewPasswordEmail
*
* @param \Cake\ORM\Entity $user
* @param array $options
* @return array
*/
public function sendPasswordResetToken(Entity $user, $options = []) {
$defaults = [
'subject' => __d('user_tools', 'Your password reset'),
'viewVars' => [
'user' => $user
]
];
return $this->sendEmail(Hash::merge($defaults, $this->_config['sendPasswordResetToken'], $options));
}
/**
* sendNewPasswordEmail
*
* @param \Cake\ORM\Entity $user
* @param array $options
* @return void
*/
public function sendNewPasswordEmail(Entity $user, $options = []) {
$defaults = [
'subject' => __d('user_tools', 'Your new password'),
'viewVars' => [
'user' => $user
]
];
return $this->sendEmail(Hash::merge($defaults, $this->_config['sendNewPasswordEmail'], $options));
}
/**
* sendVerificationEmail
*
* @param \Cake\ORM\Entity $data
* @param array $options
* @return boolean
*/
public function sendVerificationEmail(Entity $data, $options = []) {
$defaults = [
'subject' => __d('user_tools', 'Please verify your Email'),
'viewVars' => [
'user' => $data
]
];
return $this->sendEmail(Hash::merge($defaults, $this->_config['sendVerificationEmail'], $options));
}
}