forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialAccountsTable.php
More file actions
240 lines (211 loc) · 6.92 KB
/
Copy pathSocialAccountsTable.php
File metadata and controls
240 lines (211 loc) · 6.92 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
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Users\Model\Table;
use App\Model\Entity\Account;
use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\Event;
use Cake\Network\Email\Email;
use Cake\ORM\Entity;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Users\Exception\AccountAlreadyActiveException;
use Users\Model\Entity\User;
/**
* SocialAccounts Model
*/
class SocialAccountsTable extends Table
{
/**
* Constants
*/
const PROVIDER_TWITTER = 'Twitter';
const PROVIDER_FACEBOOK = 'Facebook';
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('social_accounts');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
'className' => Configure::read('Users.table')
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'uuid'])
->allowEmpty('id', 'create');
$validator
->requirePresence('provider', 'create')
->notEmpty('provider');
$validator
->allowEmpty('username');
$validator
->requirePresence('reference', 'create')
->notEmpty('reference');
$validator
->requirePresence('link', 'create')
->notEmpty('reference');
$validator
->allowEmpty('avatar');
$validator
->allowEmpty('description');
$validator
->requirePresence('token', 'create')
->notEmpty('token');
$validator
->allowEmpty('token_secret');
$validator
->add('token_expires', 'valid', ['rule' => 'datetime'])
->allowEmpty('token_expires');
$validator
->add('active', 'valid', ['rule' => 'boolean'])
->requirePresence('active', 'create')
->notEmpty('active');
$validator
->requirePresence('data', 'create')
->notEmpty('data');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['username']));
$rules->add($rules->existsIn(['user_id'], 'Users'));
return $rules;
}
/**
* After save callback
*
* @param Event $event event
* @param Entity $entity entity
* @param ArrayObject $options options
* @return mixed
*/
public function afterSave(Event $event, Entity $entity, $options)
{
if ($entity->active) {
return true;
}
$user = $this->Users->find()->where(['Users.id' => $entity->user_id, 'Users.active' => true])->first();
if (empty($user)) {
return true;
}
return $this->sendSocialValidationEmail($entity, $user);
}
/**
* Send social validation email to the user
*
* @param EntityInterface $socialAccount social account
* @param EntityInterface $user user
* @param Email $email Email instance or null to use 'default' configuration
* @return mixed
*/
public function sendSocialValidationEmail(EntityInterface $socialAccount, EntityInterface $user, Email $email = null)
{
$email = $this->Users->getEmailInstance($email);
$email->template('Users.social_account_validation');
$firstName = isset($user['first_name'])? $user['first_name'] . ', ' : '';
//note: we control the space after the username in the previous line
$subject = __d('Users', '{0}Your social account validation link', $firstName);
return $email
->to($user['email'])
->subject($subject)
->viewVars(compact('user', 'socialAccount'))
->send();
}
/**
* Validates the social account
*
* @param string $provider provider
* @param string $reference reference
* @param string $token token
* @throws RecordNotFoundException
* @throws AccountAlreadyActiveException
* @return User
*/
public function validateAccount($provider, $reference, $token)
{
$socialAccount = $this->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('Users', "Account already validated"));
}
} else {
throw new RecordNotFoundException(__d('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 RecordNotFoundException
* @throws AccountAlreadyActiveException
* @return User
*/
public function resendValidation($provider, $reference)
{
$socialAccount = $this->find()
->where(['provider' => $provider, 'reference' => $reference])
->contain('Users')
->first();
if (!empty($socialAccount)) {
if ($socialAccount->active) {
throw new AccountAlreadyActiveException(__d('Users', "Account already validated"));
}
} else {
throw new RecordNotFoundException(__d('Users', "Account not found for the given token and email."));
}
return $this->sendSocialValidationEmail($socialAccount, $socialAccount->user);
}
/**
* Activates an account
*
* @param Account $socialAccount social account
* @return Account
*/
protected function _activateAccount($socialAccount)
{
$socialAccount->active = true;
$result = $this->save($socialAccount);
return $result;
}
}