|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App; |
|
6
|
|
|
use A17\Twill\Models\User; |
|
7
|
|
|
use A17\Twill\Repositories\Behaviors\HandleMedias; |
|
8
|
|
|
use A17\Twill\Repositories\Behaviors\HandleOauth; |
|
9
|
|
|
use Illuminate\Auth\Passwords\PasswordBrokerManager; |
|
10
|
|
|
use Illuminate\Config\Repository as Config; |
|
11
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
|
12
|
|
|
use Illuminate\Database\DatabaseManager as DB; |
|
13
|
|
|
|
|
14
|
|
|
class UserRepository extends ModuleRepository |
|
15
|
|
|
{ |
|
16
|
|
|
use HandleMedias, HandleOauth; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Config |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $config; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var DB |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $db; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var PasswordBrokerManager |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $passwordBrokerManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var AuthFactory |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $authFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param DB $db |
|
40
|
|
|
* @param Config $config |
|
41
|
|
|
* @param PasswordBrokerManager $passwordBrokerManager |
|
42
|
|
|
* @param AuthFactory $authFactory |
|
43
|
|
|
* @param User $model |
|
44
|
|
|
*/ |
|
45
|
6 |
|
public function __construct( |
|
46
|
|
|
DB $db, |
|
47
|
|
|
Config $config, |
|
48
|
|
|
PasswordBrokerManager $passwordBrokerManager, |
|
49
|
|
|
AuthFactory $authFactory, |
|
50
|
|
|
User $model |
|
51
|
|
|
) { |
|
52
|
|
|
|
|
53
|
6 |
|
$this->model = $model; |
|
|
|
|
|
|
54
|
6 |
|
$this->passwordBrokerManager = $passwordBrokerManager; |
|
55
|
6 |
|
$this->authFactory = $authFactory; |
|
56
|
6 |
|
$this->config = $config; |
|
57
|
6 |
|
$this->db = $db; |
|
58
|
6 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
|
62
|
|
|
* @param array $scopes |
|
63
|
|
|
* @return \Illuminate\Database\Query\Builder |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function filter($query, array $scopes = []) |
|
66
|
|
|
{ |
|
67
|
|
|
$query->when(isset($scopes['role']), function ($query) use ($scopes) { |
|
68
|
|
|
$query->where('role', $scopes['role']); |
|
69
|
1 |
|
}); |
|
70
|
1 |
|
$query->where('role', '<>', 'SUPERADMIN'); |
|
71
|
1 |
|
$this->searchIn($query, $scopes, 'search', ['name', 'email', 'role']); |
|
72
|
1 |
|
return parent::filter($query, $scopes); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \A17\Twill\Models\Model $user |
|
77
|
|
|
* @param array $fields |
|
78
|
|
|
*/ |
|
79
|
|
|
public function afterUpdateBasic($user, $fields) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->sendWelcomeEmail($user); |
|
|
|
|
|
|
82
|
|
|
parent::afterUpdateBasic($user, $fields); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getCountForPublished() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->model->where('role', '<>', 'SUPERADMIN')->published()->count(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function getCountForDraft() |
|
97
|
|
|
{ |
|
98
|
1 |
|
return $this->model->where('role', '<>', 'SUPERADMIN')->draft()->count(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function getCountForTrash() |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->model->where('role', '<>', 'SUPERADMIN')->onlyTrashed()->count(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param \A17\Twill\Models\Model $user |
|
111
|
|
|
* @param array $fields |
|
112
|
|
|
* @return string[] |
|
113
|
|
|
*/ |
|
114
|
4 |
|
public function prepareFieldsBeforeSave($user, $fields) |
|
115
|
|
|
{ |
|
116
|
4 |
|
$editor = $this->authFactory->guard('twill_users')->user(); |
|
117
|
4 |
|
$with2faSettings = $this->config->get('twill.enabled.users-2fa', false) && $editor->id === $user->id; |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
4 |
|
if ($with2faSettings |
|
120
|
4 |
|
&& $user->google_2fa_enabled |
|
|
|
|
|
|
121
|
4 |
|
&& !($fields['google_2fa_enabled'] ?? false) |
|
122
|
|
|
) { |
|
123
|
|
|
$fields['google_2fa_secret'] = null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return parent::prepareFieldsBeforeSave($user, $fields); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param \A17\Twill\Models\Model|User $user |
|
131
|
|
|
* @param array $fields |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
4 |
|
public function afterSave($user, $fields) |
|
135
|
|
|
{ |
|
136
|
4 |
|
$this->sendWelcomeEmail($user); |
|
|
|
|
|
|
137
|
4 |
|
$language = $fields['language']; |
|
138
|
|
|
if ($language !== App::getLocale()) { |
|
139
|
|
|
$user->language = $language; |
|
|
|
|
|
|
140
|
|
|
$user->save(); |
|
141
|
|
|
} |
|
142
|
|
|
parent::afterSave($user, $fields); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param User $user |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
4 |
|
private function sendWelcomeEmail($user) |
|
150
|
|
|
{ |
|
151
|
4 |
|
if (empty($user->password) |
|
|
|
|
|
|
152
|
4 |
|
&& $user->published |
|
|
|
|
|
|
153
|
|
|
&& !$this->db |
|
154
|
|
|
->table($this->config->get('twill.password_resets_table', 'twill_password_resets')) |
|
155
|
|
|
->where('email', $user->email) |
|
|
|
|
|
|
156
|
4 |
|
->exists() |
|
157
|
|
|
) { |
|
158
|
|
|
$user->sendWelcomeNotification( |
|
159
|
|
|
$this->passwordBrokerManager->broker('twill_users')->getRepository()->create($user) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
4 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|