-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathSocialAccountBehavior.php
More file actions
159 lines (143 loc) · 4.97 KB
/
Copy pathSocialAccountBehavior.php
File metadata and controls
159 lines (143 loc) · 4.97 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
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Model\Behavior;
use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventInterface;
use Cake\Mailer\MailerAwareTrait;
use Cake\ORM\Behavior;
use CakeDC\Users\Exception\AccountAlreadyActiveException;
/**
* Covers social account features
*/
class SocialAccountBehavior extends Behavior
{
use MailerAwareTrait;
/**
* Initialize, attaching belongsTo Users association
*
* @param array $config config
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->_table->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
'className' => Configure::read('Users.table'),
]);
}
/**
* After save callback
*
* @param \Cake\Event\EventInterface $event event
* @param \Cake\Datasource\EntityInterface $entity entity
* @param \ArrayObject $options options
* @return mixed
*/
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
if ($entity->get('active')) {
return true;
}
$user = $this->_table->getAssociation('Users')->find()
->where(['Users.id' => $entity->get('user_id'), 'Users.active' => true])
->first();
if (empty($user)) {
return true;
}
return $this->sendSocialValidationEmail($entity, $user);
}
/**
* Send social validation email to the user
*
* @param \Cake\Datasource\EntityInterface $socialAccount social account
* @param \Cake\Datasource\EntityInterface $user user
* @return array
*/
protected function sendSocialValidationEmail(EntityInterface $socialAccount, EntityInterface $user)
{
return $this
->getMailer(Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users')
->send('socialAccountValidation', [$user, $socialAccount]);
}
/**
* Validates the social account
*
* @param string $provider provider
* @param string $reference reference
* @param string $token token
* @throws \Cake\Datasource\Exception\RecordNotFoundException
* @throws \CakeDC\Users\Exception\AccountAlreadyActiveException
* @return \CakeDC\Users\Model\Entity\User
*/
public function validateAccount($provider, $reference, $token)
{
$socialAccount = $this->_table->find()
->select(['id', 'provider', 'reference', 'active', 'token'])
->where(['provider' => $provider, 'reference' => $reference])
->first();
if (!empty($socialAccount) && $socialAccount->token === $token) {
if ($socialAccount->active) {
throw new AccountAlreadyActiveException(__d('cake_d_c/users', 'Account already validated'));
}
} else {
throw new RecordNotFoundException(
__d('cake_d_c/users', 'Account not found for the given token and email.')
);
}
return $this->_activateAccount($socialAccount);
}
/**
* Validates the social account
*
* @param string $provider provider
* @param string $reference reference
* @throws \Cake\Datasource\Exception\RecordNotFoundException
* @throws \CakeDC\Users\Exception\AccountAlreadyActiveException
* @return \CakeDC\Users\Model\Entity\User
*/
public function resendValidation($provider, $reference)
{
$socialAccount = $this->_table->find()
->where(['provider' => $provider, 'reference' => $reference])
->contain('Users')
->first();
if (!empty($socialAccount)) {
if ($socialAccount->active) {
throw new AccountAlreadyActiveException(
__d('cake_d_c/users', 'Account already validated')
);
}
} else {
throw new RecordNotFoundException(
__d('cake_d_c/users', 'Account not found for the given token and email.')
);
}
return $this->sendSocialValidationEmail($socialAccount, $socialAccount->user);
}
/**
* Activates an account
*
* @param \CakeDC\Users\Model\Entity\SocialAccount $socialAccount social account
* @return \Cake\Datasource\EntityInterface
*/
protected function _activateAccount($socialAccount)
{
$socialAccount->active = true;
$result = $this->_table->save($socialAccount);
return $result;
}
}