-
Notifications
You must be signed in to change notification settings - Fork 554
/
UserRepository.php
170 lines (142 loc) · 5.32 KB
/
UserRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace A17\Twill\Repositories;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\User;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\Behaviors\HandleOauth;
use A17\Twill\Repositories\Behaviors\HandleUserPermissions;
use Carbon\Carbon;
use Illuminate\Auth\Passwords\PasswordBrokerManager;
use Illuminate\Config\Repository as Config;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Database\DatabaseManager as DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Hash;
class UserRepository extends ModuleRepository
{
use HandleMedias;
use HandleOauth;
use HandleUserPermissions;
protected Config $config;
protected DB $db;
protected PasswordBrokerManager $passwordBrokerManager;
protected AuthFactory $authFactory;
public function __construct(
DB $db,
Config $config,
PasswordBrokerManager $passwordBrokerManager,
AuthFactory $authFactory
) {
$userModel = twillModel('user');
$this->model = new $userModel();
$this->passwordBrokerManager = $passwordBrokerManager;
$this->authFactory = $authFactory;
$this->config = $config;
$this->db = $db;
}
public function getFormFields(TwillModelContract|User $user): array
{
$fields = parent::getFormFields($user);
if ($user->isSuperAdmin()) {
return $fields;
}
if (config('twill.enabled.permissions-management')) {
$fields['browsers']['groups'] = $this->getFormFieldsForBrowser($user, 'groups');
}
return $fields;
}
public function filter(Builder $query, array $scopes = []): Builder
{
if (TwillPermissions::enabled()) {
$query->where('is_superadmin', '<>', true);
} else {
$query->where('role', '<>', 'SUPERADMIN');
}
return parent::filter($query, $scopes);
}
public function getFormFieldsForBrowser(
$object,
$relation,
$routePrefix = null,
$titleKey = 'title',
$moduleName = null
) {
$browserFields = parent::getFormFieldsForBrowser($object, $relation, $routePrefix, $titleKey, $moduleName);
if (TwillPermissions::enabled()) {
$everyoneGroup = twillModel('group')::getEveryoneGroup();
foreach ($browserFields as $index => $browserField) {
if (
$browserField['id'] === $everyoneGroup->id &&
$browserField['name'] === $everyoneGroup->name
) {
$browserFields[$index]['edit'] = false;
$browserFields[$index]['deletable'] = false;
}
}
}
return $browserFields;
}
public function afterUpdateBasic(TwillModelContract|User $user, array $fields): void
{
$this->sendWelcomeEmail($user);
parent::afterUpdateBasic($user, $fields);
}
public function prepareFieldsBeforeSave(TwillModelContract|User $user, array $fields): array
{
/** @var \A17\Twill\Models\User $editor */
$editor = $this->authFactory->guard('twill_users')->user();
$with2faSettings = $this->config->get('twill.enabled.users-2fa', false) && $editor?->id === $user->id;
if (
$with2faSettings
&& $user->google_2fa_enabled
&& !($fields['google_2fa_enabled'] ?? false)
) {
$fields['google_2fa_secret'] = null;
}
if (
$this->config->get('twill.enabled.users-2fa', false)
&& ($fields['force-2fa-disable-challenge'] ?? false)
) {
$user->google_2fa_enabled = false;
$user->google_2fa_secret = null;
}
return parent::prepareFieldsBeforeSave($user, $fields);
}
public function afterSave(TwillModelContract|user $user, array $fields): void
{
$this->sendWelcomeEmail($user);
if (!empty($fields['reset_password']) && !empty($fields['new_password'])) {
$user->password = Hash::make($fields['new_password']);
if (!$user->isActivated()) {
$user->registered_at = Carbon::now();
}
if (!empty($fields['require_password_change'])) {
$user->require_new_password = true;
$user->sendTemporaryPasswordNotification($fields['new_password']);
} else {
$user->sendPasswordResetByAdminNotification($fields['new_password']);
}
$user->save();
}
if (TwillPermissions::enabled() && auth('twill_users')->user()->can('edit-user-groups')) {
$this->updateBrowser($user, $fields, 'groups');
}
parent::afterSave($user, $fields);
}
private function sendWelcomeEmail(User $user): void
{
if (
empty($user->password)
&& $user->published
&& !$this->db
->table($this->config->get('twill.password_resets_table', 'twill_password_resets'))
->where('email', $user->email)
->exists()
) {
$user->sendWelcomeNotification(
$this->passwordBrokerManager->broker('twill_users')->getRepository()->create($user)
);
}
}
}