1 | <?php |
||||||
2 | |||||||
3 | namespace App\Services; |
||||||
4 | |||||||
5 | use App\Mail\UserInvitation; |
||||||
6 | use App\Models\User; |
||||||
7 | use App\TenantUser; |
||||||
8 | use Illuminate\Http\Request; |
||||||
9 | use Illuminate\Support\Facades\DB; |
||||||
10 | use Illuminate\Support\Facades\Mail; |
||||||
11 | |||||||
12 | class InvitationService |
||||||
13 | { |
||||||
14 | const KEY_MAXLENGTH = 8; |
||||||
15 | |||||||
16 | public static function invite(Request $request, $user, $tenant) |
||||||
17 | { |
||||||
18 | $invitation = new \App\Models\UserInvitation(); |
||||||
19 | |||||||
20 | $invitation->inviter_id = $user->id; |
||||||
0 ignored issues
–
show
|
|||||||
21 | $invitation->email = $request->get('email'); |
||||||
0 ignored issues
–
show
The method
get() does not exist on Illuminate\Http\Request .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
22 | if ($request->get('name')) { |
||||||
23 | $invitation->name = $request->get('name'); |
||||||
0 ignored issues
–
show
|
|||||||
24 | } |
||||||
25 | $invitation->token = self::generateKey(); |
||||||
26 | |||||||
27 | $user = new User(); |
||||||
28 | $user->email = $request->get('email'); |
||||||
29 | $user->pending_invitation = true; |
||||||
30 | |||||||
31 | $tenantUser = new TenantUser(); |
||||||
32 | $tenantUser->username = $user->email; |
||||||
33 | $tenantUser->tenant_id = $tenant->id; |
||||||
0 ignored issues
–
show
|
|||||||
34 | |||||||
35 | DB::transaction(function () use ($tenantUser, $user, $invitation) { |
||||||
36 | $tenantUser->save(); |
||||||
37 | $user->save(); |
||||||
38 | $invitation->save(); |
||||||
39 | }); |
||||||
40 | |||||||
41 | if ($request->get('role')) { |
||||||
42 | $user->assignRole($request->get('role')); |
||||||
43 | } |
||||||
44 | |||||||
45 | $invitation->subdomain = $tenant->subdomain; |
||||||
0 ignored issues
–
show
|
|||||||
46 | Mail::to($user->email)->queue(new UserInvitation($invitation)); |
||||||
47 | |||||||
48 | unset($invitation->token); |
||||||
49 | |||||||
50 | return $invitation; |
||||||
51 | } |
||||||
52 | |||||||
53 | public static function finishInvitation(Request $request, $token) |
||||||
54 | { |
||||||
55 | $invitation = \App\Models\UserInvitation::where('token', $token)->first(); |
||||||
56 | |||||||
57 | if (!$invitation) { |
||||||
58 | return false; |
||||||
59 | } |
||||||
60 | |||||||
61 | $user = User::where('email', $invitation->email)->first(); |
||||||
62 | if (!$user) { |
||||||
63 | throw new \Exception('Invited user not found'); |
||||||
64 | } |
||||||
65 | |||||||
66 | $user->name = $request->get('name'); |
||||||
67 | $user->password = bcrypt($request->get('password')); |
||||||
68 | $user->pending_invitation = false; |
||||||
69 | |||||||
70 | $invitation->token = null; |
||||||
71 | |||||||
72 | DB::transaction(function () use ($user, $invitation) { |
||||||
73 | $user->save(); |
||||||
74 | $invitation->save(); |
||||||
75 | }); |
||||||
76 | |||||||
77 | return $user; |
||||||
78 | } |
||||||
79 | |||||||
80 | private static function generateKey() |
||||||
81 | { |
||||||
82 | do { |
||||||
83 | $key = strtoupper(\Str::random(self::KEY_MAXLENGTH)); |
||||||
0 ignored issues
–
show
The method
random() does not exist on Str .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||
84 | $invitations = \App\Models\UserInvitation::where('token', $key)->count(); |
||||||
85 | } while ($invitations > 0); |
||||||
86 | |||||||
87 | return $key; |
||||||
88 | } |
||||||
89 | } |
||||||
90 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.