1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Sync; |
4
|
|
|
|
5
|
|
|
use Slides\Connector\Auth\Sync\Syncable as LocalUser; |
6
|
|
|
use Slides\Connector\Auth\Sync\User as RemoteUser; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Slides\Connector\Auth\AuthService; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait HandlesActions |
12
|
|
|
* |
13
|
|
|
* @property AuthService $authService |
14
|
|
|
* |
15
|
|
|
* @package Slides\Connector\Auth\Sync |
16
|
|
|
*/ |
17
|
|
|
trait HandlesActions |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Handle a user action. |
21
|
|
|
* |
22
|
|
|
* @param RemoteUser $remote |
23
|
|
|
* @param string $action |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
protected function handleAction(RemoteUser $remote, string $action) |
28
|
|
|
{ |
29
|
|
|
$handler = 'action' . studly_case($action); |
30
|
|
|
|
31
|
|
|
if(!method_exists($this, $handler)) { |
32
|
|
|
throw new \Slides\Connector\Auth\Exceptions\SyncException("User action handler {$handler} cannot be found."); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->{$handler}($remote); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handle the "create" action. |
40
|
|
|
* |
41
|
|
|
* @param RemoteUser $remote |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function actionCreate(RemoteUser $remote) |
46
|
|
|
{ |
47
|
|
|
// If a user with the same email was found, we need to skip the process |
48
|
|
|
if(Auth::getProvider()->retrieveByCredentials(['email' => strtolower($remote->getEmail())])) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->authService->handle(AuthService::HANDLER_USER_SYNC_CREATE, ['remote' => $remote]); |
53
|
|
|
|
54
|
|
|
$this->incrementStats('created'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handle the "update" action. |
59
|
|
|
* |
60
|
|
|
* @param RemoteUser $remote |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
protected function actionUpdate(RemoteUser $remote) |
65
|
|
|
{ |
66
|
|
|
// If a user with the same email cannot be found, we should skip the process |
67
|
|
|
if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// If a local user was updated later than remote one, we should skip the process |
72
|
|
|
// Since we have a latest one |
73
|
|
|
if($this->localNewerThanRemote($local, $remote)) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// If "password" mode is not enabled, we cannot update local passwords, so we simply resetting it |
78
|
|
|
if(!$this->hasMode(static::MODE_PASSWORDS)) { |
|
|
|
|
79
|
|
|
$remote->resetPassword(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->authService->handle(AuthService::HANDLER_USER_SYNC_UPDATE, [ |
83
|
|
|
'remote' => $remote, |
84
|
|
|
'local' => $local |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$this->incrementStats('updated'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Handle the "delete" action. |
92
|
|
|
* |
93
|
|
|
* @param RemoteUser $remote |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
protected function actionDelete(RemoteUser $remote) |
98
|
|
|
{ |
99
|
|
|
if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->authService->handle(AuthService::HANDLER_USER_SYNC_DELETE, ['remote' => $remote, 'local' => $local]); |
104
|
|
|
|
105
|
|
|
$this->incrementStats('deleted'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check whether remote user has updated earlier than local one. |
110
|
|
|
* |
111
|
|
|
* @param Syncable|\Illuminate\Contracts\Auth\Authenticatable $local |
112
|
|
|
* @param User $remote |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
private function localNewerThanRemote(LocalUser $local, RemoteUser $remote) |
117
|
|
|
{ |
118
|
|
|
if(!$local->retrieveRemoteId()) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if(!$remoteUpdated = $remote->getUpdated()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if(!$localUpdate = $local->retrieveUpdatedAt()) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $remoteUpdated->lessThan($localUpdate); |
131
|
|
|
} |
132
|
|
|
} |