1
|
|
|
<?php namespace AdamWathan\EloquentOAuth; |
2
|
|
|
|
3
|
|
|
class Authenticator |
4
|
|
|
{ |
5
|
|
|
protected $auth; |
6
|
|
|
protected $users; |
7
|
|
|
protected $identities; |
8
|
|
|
|
9
|
|
|
public function __construct($auth, $users, $identities) |
10
|
|
|
{ |
11
|
|
|
$this->auth = $auth; |
12
|
|
|
$this->users = $users; |
13
|
|
|
$this->identities = $identities; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function login($providerAlias, $userDetails, $callback = null, $remember = false) |
17
|
|
|
{ |
18
|
|
|
$user = $this->getUser($providerAlias, $userDetails); |
19
|
|
|
$user = $this->runCallback($callback, $user, $userDetails); |
20
|
|
|
$this->updateUser($user, $providerAlias, $userDetails); |
21
|
|
|
$this->auth->login($user, $remember); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function getUser($providerAlias, $details) |
25
|
|
|
{ |
26
|
|
|
if ($this->identities->userExists($providerAlias, $details)) { |
27
|
|
|
return $this->getExistingUser($providerAlias, $details); |
28
|
|
|
} |
29
|
|
|
return $this->users->create(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function runCallback($callback, $user, $userDetails) |
33
|
|
|
{ |
34
|
|
|
$callback = $callback ?: function () {}; |
35
|
|
|
$callbackUser = $callback($user, $userDetails); |
36
|
|
|
return $callbackUser ?: $user; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function updateUser($user, $providerAlias, $details) |
40
|
|
|
{ |
41
|
|
|
$this->users->store($user); |
42
|
|
|
$this->storeProviderIdentity($user, $providerAlias, $details); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getExistingUser($providerAlias, $details) |
46
|
|
|
{ |
47
|
|
|
$identity = $this->identities->getByProvider($providerAlias, $details); |
48
|
|
|
return $this->users->findByIdentity($identity); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function storeProviderIdentity($user, $providerAlias, $details) |
52
|
|
|
{ |
53
|
|
|
if ($this->identities->userExists($providerAlias, $details)) { |
54
|
|
|
$this->updateProviderIdentity($providerAlias, $details); |
55
|
|
|
} else { |
56
|
|
|
$this->addProviderIdentity($user, $providerAlias, $details); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function updateProviderIdentity($providerAlias, $details) |
61
|
|
|
{ |
62
|
|
|
$identity = $this->identities->getByProvider($providerAlias, $details); |
63
|
|
|
$identity->access_token = $details->access_token; |
64
|
|
|
$this->identities->store($identity); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function addProviderIdentity($user, $providerAlias, $details) |
68
|
|
|
{ |
69
|
|
|
$identity = new OAuthIdentity; |
70
|
|
|
$identity->user_id = $user->getKey(); |
|
|
|
|
71
|
|
|
$identity->provider = $providerAlias; |
|
|
|
|
72
|
|
|
$identity->provider_user_id = $details->id; |
|
|
|
|
73
|
|
|
$identity->access_token = $details->access_token; |
|
|
|
|
74
|
|
|
$this->identities->store($identity); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.