forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocialAuthenticate.php
More file actions
executable file
·75 lines (68 loc) · 2.17 KB
/
Copy pathSocialAuthenticate.php
File metadata and controls
executable file
·75 lines (68 loc) · 2.17 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
<?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 CakeDC\Users\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
/**
* Class SocialAuthenticate
*/
class SocialAuthenticate extends BaseAuthenticate
{
/**
* Authenticate callback
*
* @param Request $request Cake request object.
* @param Response $response Cake response object.
* @return bool|mixed
*/
public function authenticate(Request $request, Response $response)
{
$data = $request->session()->read(Configure::read('Users.Key.Session.social'));
if (empty($data)) {
return false;
}
$socialMail = Hash::get((array)$data->info, Configure::read('Users.Key.Data.email'));
if (!empty($socialMail)) {
$data->email = $socialMail;
$data->validated = true;
} else {
$data->email = $request->data(Configure::read('Users.Key.Data.email'));
$data->validated = false;
}
$user = $this->_findOrCreateUser($data);
return $user;
}
/**
* Checks the social user against the database
*
* @param array $data User data array.
* @return mixed
*/
protected function _findOrCreateUser($data)
{
$userModel = Configure::read('Users.table');
$User = TableRegistry::get($userModel);
$options = [
'use_email' => Configure::read('Users.Email.required'),
'validate_email' => Configure::read('Users.Email.validate'),
'token_expiration' => Configure::read('Users.Token.expiration')
];
$user = $User->socialLogin($data, $options);
if (!empty($user->username)) {
$user = $this->_findUser($user->username);
}
return $user;
}
}