1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\ManagerAuth;
|
---|
4 |
|
---|
5 | use App\Http\Controllers\Controller;
|
---|
6 | use App\Http\Controllers\Utils\UtilController;
|
---|
7 | use App\Models\Artist;
|
---|
8 | use App\Models\Manager;
|
---|
9 | use App\Models\ManagerInvite;
|
---|
10 | use App\Providers\RouteServiceProvider;
|
---|
11 | use App\Models\User;
|
---|
12 | use Illuminate\Contracts\Foundation\Application;
|
---|
13 | use Illuminate\Contracts\View\Factory;
|
---|
14 | use Illuminate\Contracts\View\View;
|
---|
15 | use Illuminate\Http\Request;
|
---|
16 | use Illuminate\Foundation\Auth\RegistersUsers;
|
---|
17 | use Illuminate\Support\Facades\Hash;
|
---|
18 | use Illuminate\Support\Facades\Validator;
|
---|
19 | use Laravolt\Avatar\Avatar;
|
---|
20 | use Exception;
|
---|
21 |
|
---|
22 | class RegisterController extends Controller
|
---|
23 | {
|
---|
24 | /*
|
---|
25 | |--------------------------------------------------------------------------
|
---|
26 | | Register Controller
|
---|
27 | |--------------------------------------------------------------------------
|
---|
28 | |
|
---|
29 | | This controller handles the registration of new users as well as their
|
---|
30 | | validation and creation. By default this controller uses a trait to
|
---|
31 | | provide this functionality without requiring any additional code.
|
---|
32 | |
|
---|
33 | */
|
---|
34 |
|
---|
35 | use RegistersUsers;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Where to redirect users after registration.
|
---|
39 | *
|
---|
40 | * @var string
|
---|
41 | */
|
---|
42 | protected $redirectTo = RouteServiceProvider::HOME;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Create a new controller instance.
|
---|
46 | *
|
---|
47 | * @return void
|
---|
48 | */
|
---|
49 | public function __construct()
|
---|
50 | {
|
---|
51 | $this->middleware('guest');
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Get a validator for an incoming registration request.
|
---|
56 | *
|
---|
57 | * @param array $data
|
---|
58 | * @return \Illuminate\Contracts\Validation\Validator
|
---|
59 | */
|
---|
60 | protected function validator(array $data): \Illuminate\Contracts\Validation\Validator
|
---|
61 | {
|
---|
62 | return Validator::make($data, [
|
---|
63 | 'name' => ['required', 'string', 'max:80'],
|
---|
64 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
---|
65 | 'password' => ['required', 'string', 'min:8', 'confirmed'],
|
---|
66 | ]);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Create a new user instance after a valid registration.
|
---|
71 | *
|
---|
72 | * @param array $data
|
---|
73 | * @return User|\Illuminate\Http\RedirectResponse
|
---|
74 | */
|
---|
75 | protected function create(array $data)
|
---|
76 | {
|
---|
77 | $invitation_token = $data['token'];
|
---|
78 | $new_invitation = null;
|
---|
79 |
|
---|
80 | $invitations = ManagerInvite::where('email', $data['email'])->get();
|
---|
81 | foreach($invitations as $invitation) {
|
---|
82 | if (Hash::check($invitation_token, $invitation->invitation_token)) {
|
---|
83 | $new_invitation = ManagerInvite::where('invitation_token', $invitation->invitation_token)->firstOrFail();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | $username = strtolower(preg_replace('/\s+/', '', $data['name'])) . uniqid();
|
---|
88 | $image_name = ($username . '-' . uniqid() . '.jpg');
|
---|
89 |
|
---|
90 | $user = User::create([
|
---|
91 | 'name' => $data['name'],
|
---|
92 | 'email' => $data['email'],
|
---|
93 | 'password' => Hash::make($data['password']),
|
---|
94 | 'username' => $username,
|
---|
95 | 'profile_picture' => $image_name,
|
---|
96 | 'type' => 'MANAGER',
|
---|
97 | ]);
|
---|
98 | $user->markEmailAsVerified();
|
---|
99 |
|
---|
100 | $manager = Manager::create([
|
---|
101 | 'user_id' => $user->id
|
---|
102 | ]);
|
---|
103 |
|
---|
104 | $manager->user()->associate($user);
|
---|
105 |
|
---|
106 | try {
|
---|
107 | $manager->save();
|
---|
108 | } catch (Exception $e) {
|
---|
109 | return back()
|
---|
110 | ->with('error', $e->getMessage());
|
---|
111 | }
|
---|
112 |
|
---|
113 | $new_invitation->registered_at = $user->created_at;
|
---|
114 |
|
---|
115 | $artist = Artist::where('user_id', $new_invitation->artist_id)->firstOrFail();
|
---|
116 | $artist->manager()->associate($manager);
|
---|
117 |
|
---|
118 | try {
|
---|
119 | $artist->save();
|
---|
120 | } catch (Exception $e) {
|
---|
121 | return back()
|
---|
122 | ->with('error', $e->getMessage());
|
---|
123 | }
|
---|
124 |
|
---|
125 | try {
|
---|
126 | $new_invitation->save();
|
---|
127 | } catch(Exception $e) {
|
---|
128 | return back()
|
---|
129 | ->with('error', $e->getMessage());
|
---|
130 | }
|
---|
131 |
|
---|
132 | UtilController::saveAvatarImage($image_name, $data['name']);
|
---|
133 | return $user;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * @param Request $request
|
---|
138 | * @param $token
|
---|
139 | * @return Application|Factory|View
|
---|
140 | */
|
---|
141 | public function showRegistrationForm(Request $request, $token): View|Factory|Application
|
---|
142 | {
|
---|
143 | return view('auth.manager.register')
|
---|
144 | ->with('token', $token)
|
---|
145 | ->with('email', $request->get('email'));
|
---|
146 | }
|
---|
147 | }
|
---|