1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Repositories; |
17
|
|
|
|
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
use Rinvex\Fort\Traits\HasRoles; |
20
|
|
|
use Illuminate\Database\Eloquent\Model; |
21
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
22
|
|
|
use Illuminate\Contracts\Foundation\Application; |
23
|
|
|
use Rinvex\Fort\Contracts\UserRepositoryContract; |
24
|
|
|
use Rinvex\Fort\Contracts\AuthenticatableContract; |
25
|
|
|
use Rinvex\Repository\Repositories\EloquentRepository; |
26
|
|
|
|
27
|
|
|
class UserRepository extends EloquentRepository implements UserRepositoryContract |
28
|
|
|
{ |
29
|
|
|
use HasRoles; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The hasher implementation. |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher |
35
|
|
|
*/ |
36
|
|
|
protected $hasher; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* create a new user repository instance. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
42
|
|
|
* |
43
|
|
|
* @return void |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function __construct(Application $app, Hasher $hasher) |
46
|
|
|
{ |
47
|
|
|
$this->setContainer($app) |
48
|
|
|
->setHasher($hasher) |
49
|
|
|
->setRepositoryId('rinvex.fort.user') |
50
|
|
|
->setModel($app['config']['rinvex.fort.models.user']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function findByToken($identifier, $token) |
57
|
|
|
{ |
58
|
|
|
return $this->where($this->getAuthIdentifierName(), $identifier) |
|
|
|
|
59
|
|
|
->where($this->getRememberTokenName(), $token) |
|
|
|
|
60
|
|
|
->findFirst(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function findByCredentials(array $credentials) |
67
|
|
|
{ |
68
|
|
|
if (empty($credentials)) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// First we will add each credential element to the query as a where clause. |
73
|
|
|
// Then we can execute the query and, if we found a user, return it in a |
74
|
|
|
// Eloquent User "model" that will be utilized by the Guard instances. |
75
|
|
|
$model = $this; |
76
|
|
|
|
77
|
|
|
foreach ($credentials as $key => $value) { |
78
|
|
|
if (! Str::contains($key, 'password')) { |
79
|
|
|
$model = $model->where($key, $value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $model->findFirst(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function updateRememberToken(AuthenticatableContract $user, $token) |
90
|
|
|
{ |
91
|
|
|
$this->update($user, [$this->getRememberTokenName() => $token]); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function validateCredentials(AuthenticatableContract $user, array $credentials) |
98
|
|
|
{ |
99
|
|
|
$plain = $credentials['password']; |
100
|
|
|
|
101
|
|
|
return $this->getHasher()->check($plain, $user->getAuthPassword()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Gets the hasher implementation. |
106
|
|
|
* |
107
|
|
|
* @return \Illuminate\Contracts\Hashing\Hasher |
108
|
|
|
*/ |
109
|
|
|
public function getHasher() |
110
|
|
|
{ |
111
|
|
|
return $this->hasher; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets the hasher implementation. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setHasher(Hasher $hasher) |
122
|
|
|
{ |
123
|
|
|
$this->hasher = $hasher; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the menus for the given user. |
130
|
|
|
* |
131
|
|
|
* @param int $modelId |
132
|
|
|
* @param bool $root |
133
|
|
|
* |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public function menus($modelId, $root = true) |
137
|
|
|
{ |
138
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($modelId, $root) { |
|
|
|
|
139
|
|
|
if (! is_int($modelId) || ! $model = $this->with(['abilities'])->find($modelId)) { |
140
|
|
|
return []; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$roleAbilities = null; |
144
|
|
|
$callback = function ($item) { |
145
|
|
|
if (strpos($item, '.index') === false) { |
146
|
|
|
return str_replace(strrchr($item, '.'), '.index', $item); |
147
|
|
|
} else { |
148
|
|
|
return 'root'; |
149
|
|
|
} |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
if (! $model instanceof Role) { |
|
|
|
|
153
|
|
|
$roleAbilities = $model->roles()->with(['abilities'])->get()->pluck('abilities')->flatten()->pluck('slug'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$userAbilities = $model->abilities->pluck('slug')->merge($roleAbilities)->unique()->groupBy($callback)->toArray(); |
|
|
|
|
157
|
|
|
|
158
|
|
|
return $root ? $userAbilities['root'] : $userAbilities; |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.