1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Auth; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Magister\Services\Contracts\Auth\UserProvider; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ElegantUserProvider. |
10
|
|
|
*/ |
11
|
|
|
class ElegantUserProvider implements UserProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The Elegant user model. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The active connection. |
22
|
|
|
* |
23
|
|
|
* @var \GuzzleHttp\Client |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new elegant user provider instance. |
29
|
|
|
* |
30
|
|
|
* @param \GuzzleHttp\Client $client |
31
|
|
|
* @param string $model |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Client $client, $model) |
34
|
|
|
{ |
35
|
|
|
$this->client = $client; |
36
|
|
|
$this->model = $model; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieve a user by their unique token. |
41
|
|
|
* |
42
|
|
|
* @return \Magister\Services\Database\Elegant\Model|null |
43
|
|
|
*/ |
44
|
|
|
public function retrieveByToken() |
45
|
|
|
{ |
46
|
|
|
return $this->createModel()->newQuery()->first(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Remove the token for the given user in storage. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function removeToken() |
55
|
|
|
{ |
56
|
|
|
$this->client->getDefaultOption('cookies')->clear(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Retrieve a user by the given credentials. |
61
|
|
|
* |
62
|
|
|
* @param array $credentials |
63
|
|
|
* |
64
|
|
|
* @return \Magister\Services\Database\Elegant\Model|null |
65
|
|
|
*/ |
66
|
|
|
public function retrieveByCredentials(array $credentials) |
67
|
|
|
{ |
68
|
|
|
$body = ['json' => $credentials]; |
69
|
|
|
|
70
|
|
|
$this->client->delete('sessies/huidige'); |
71
|
|
|
$this->client->post('sessies', $body); |
72
|
|
|
|
73
|
|
|
return $this->retrieveByToken(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create a new instance of the model. |
78
|
|
|
* |
79
|
|
|
* @return \Magister\Services\Database\Elegant\Model |
80
|
|
|
*/ |
81
|
|
|
public function createModel() |
82
|
|
|
{ |
83
|
|
|
$class = '\\'.ltrim($this->model, '\\'); |
84
|
|
|
|
85
|
|
|
return new $class(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|