1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Forms; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Validation\Rule; |
9
|
|
|
use Livewire\Form; |
10
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
11
|
|
|
use Xetaravel\Models\Account; |
12
|
|
|
use Xetaravel\Models\User; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
class UserForm extends Form |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The category to update. |
19
|
|
|
* |
20
|
|
|
* @var User|null |
21
|
|
|
*/ |
22
|
|
|
public ?User $user = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The username of the user. |
26
|
|
|
* |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
public ?string $username = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The email of the user. |
33
|
|
|
* |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
public ?string $email = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The first_name of the user. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
public ?string $first_name = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The last_name of the user. |
47
|
|
|
* |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
public ?string $last_name = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The facebook of the user. |
54
|
|
|
* |
55
|
|
|
* @var string|null |
56
|
|
|
*/ |
57
|
|
|
public ?string $facebook = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The twitter of the user. |
61
|
|
|
* |
62
|
|
|
* @var string|null |
63
|
|
|
*/ |
64
|
|
|
public ?string $twitter = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The biography of the user. |
68
|
|
|
* |
69
|
|
|
* @var string|null |
70
|
|
|
*/ |
71
|
|
|
public ?string $biography = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The signature of the user. |
75
|
|
|
* |
76
|
|
|
* @var string|null |
77
|
|
|
*/ |
78
|
|
|
public ?string $signature = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The roles of the user. |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
public array $roles = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The permissions of the user. |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
public array $permissions = []; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Whatever the user have the by_pass permission. |
96
|
|
|
* |
97
|
|
|
* @var bool|null |
98
|
|
|
*/ |
99
|
|
|
public ?bool $can_bypass = null; |
100
|
|
|
|
101
|
|
|
protected function rules(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'username' => [ |
105
|
|
|
'required', |
106
|
|
|
'alpha_num', |
107
|
|
|
'min:4', |
108
|
|
|
'max:20', |
109
|
|
|
Rule::unique('users')->ignore($this->user) |
110
|
|
|
], |
111
|
|
|
'email' => [ |
112
|
|
|
'required', |
113
|
|
|
'email', |
114
|
|
|
Rule::unique('users')->ignore($this->user) |
115
|
|
|
], |
116
|
|
|
'first_name' => 'max:20', |
117
|
|
|
'last_name' => 'max:20', |
118
|
|
|
'facebook' => 'max:50', |
119
|
|
|
'twitter' => 'max:50' |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Function to update the model. |
125
|
|
|
* |
126
|
|
|
* @return User |
127
|
|
|
* |
128
|
|
|
* @throws Throwable |
129
|
|
|
*/ |
130
|
|
|
public function update(): User |
131
|
|
|
{ |
132
|
|
|
return DB::transaction(function () { |
133
|
|
|
// Update the user |
134
|
|
|
$this->user->username = $this->username; |
135
|
|
|
$this->user->email = $this->email; |
136
|
|
|
$this->user->save(); |
|
|
|
|
137
|
|
|
|
138
|
|
|
// Create or Update the account |
139
|
|
|
$account = Account::updateOrCreate( |
140
|
|
|
[ |
141
|
|
|
'user_id' => $this->user->getKey(), |
142
|
|
|
], |
143
|
|
|
[ |
144
|
|
|
'first_name' => $this->first_name, |
145
|
|
|
'last_name' => $this->last_name, |
146
|
|
|
'facebook' => $this->facebook, |
147
|
|
|
'twitter' => $this->twitter, |
148
|
|
|
'biography' => $this->biography, |
149
|
|
|
'signature' => $this->signature, |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$parser = new MentionParser($account, [ |
|
|
|
|
154
|
|
|
'regex' => config('mentions.regex'), |
155
|
|
|
'mention' => false |
156
|
|
|
]); |
157
|
|
|
$signature = $parser->parse($account->signature); |
|
|
|
|
158
|
|
|
$biography = $parser->parse($account->biography); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$account->signature = $signature; |
161
|
|
|
$account->biography = $biography; |
162
|
|
|
$account->save(); |
163
|
|
|
|
164
|
|
|
$this->user->syncRoles($this->roles); |
165
|
|
|
|
166
|
|
|
// Link the selected permissions to the user. |
167
|
|
|
if (auth()->user()->can('assignDirectPermission', $this->user)) { |
168
|
|
|
$this->user->syncPermissions($this->permissions); |
169
|
|
|
|
170
|
|
|
if ($this->can_bypass) { |
171
|
|
|
$this->user->givePermissionTo('bypass login'); |
172
|
|
|
} else { |
173
|
|
|
$this->user->revokePermissionTo('bypass login'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $this->user; |
178
|
|
|
}); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.