1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\NewUserRequest;
|
---|
7 | use App\Http\Requests\Dashboard\UpdateUserData;
|
---|
8 | use App\Http\Requests\Dashboard\UpdateUserRequest;
|
---|
9 | use App\Models\Avatar;
|
---|
10 | use Carbon\Carbon;
|
---|
11 | use Illuminate\Support\Facades\File;
|
---|
12 | use App\Models\Role;
|
---|
13 | use App\Models\User;
|
---|
14 | use App\Notifications\VerifyNewEmail;
|
---|
15 | use Illuminate\Http\Request;
|
---|
16 | use App\Notifications\WelcomeUser;
|
---|
17 | use App\Http\Controllers\Controller;
|
---|
18 | use Illuminate\Support\Facades\Storage;
|
---|
19 |
|
---|
20 | class UsersController extends Controller
|
---|
21 | {
|
---|
22 |
|
---|
23 | public function index()
|
---|
24 | {
|
---|
25 | return view("dashboard.users.index")->with([
|
---|
26 | "users" => User::all(),
|
---|
27 | "roles" => Role::all(),
|
---|
28 | ]);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public function create()
|
---|
32 | {
|
---|
33 | return view("dashboard.users.create")->with([
|
---|
34 | "roles" => Role::all(),
|
---|
35 | ]);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public function store(NewUserRequest $request)
|
---|
39 | {
|
---|
40 | $user = new User();
|
---|
41 |
|
---|
42 | $user->name = $request->name;
|
---|
43 | $user->surname = $request->surname;
|
---|
44 | $user->email = $request->email;
|
---|
45 | $user->phone_number = $request->phone_number;
|
---|
46 | $user->username = $request->username;
|
---|
47 | $user->password = $user->generateTemporaryPassword();
|
---|
48 | $user->security_code = $user->generateSecurityCode();
|
---|
49 | $user->verify_token = $user->generateVerifyToken();
|
---|
50 |
|
---|
51 | $user->role_id = $request->userRole;
|
---|
52 |
|
---|
53 | if ($request->hasFile("avatar")) {
|
---|
54 | $image = $request->file("avatar");
|
---|
55 | $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
|
---|
56 |
|
---|
57 | if (!empty($user->avatar)) {
|
---|
58 | Storage::disk('uploads')->delete("/users/" . $user->avatar);
|
---|
59 | }
|
---|
60 |
|
---|
61 | Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
|
---|
62 |
|
---|
63 | $user->avatar = $avatarName;
|
---|
64 | }
|
---|
65 |
|
---|
66 | $user->created_by = auth()->user()->id;
|
---|
67 | $user->save();
|
---|
68 |
|
---|
69 | $user->notify(new WelcomeUser($user));
|
---|
70 |
|
---|
71 | Alert::flash("New user added successfully");
|
---|
72 |
|
---|
73 | return redirect()->route("dashboard.users.index");
|
---|
74 | }
|
---|
75 |
|
---|
76 | public function editShow($id)
|
---|
77 | {
|
---|
78 | return view("dashboard.users.edit")->with([
|
---|
79 | "user" => User::findOrFail($id),
|
---|
80 | "roles" => Role::all(),
|
---|
81 | ]);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public function edit(UpdateUserRequest $request, $id)
|
---|
85 | {
|
---|
86 | $user = User::findOrFail($id);
|
---|
87 | $user->name = $request->name;
|
---|
88 | $user->surname = $request->surname;
|
---|
89 | $user->username = $request->username;
|
---|
90 | $user->email = $request->email;
|
---|
91 | $user->phone_number = $request->phone_number;
|
---|
92 | $user->role_id = $request->userRole;
|
---|
93 | $user->updated_at = Carbon::now();
|
---|
94 |
|
---|
95 | if ($request->hasFile("avatar")) {
|
---|
96 | $image = $request->file("avatar");
|
---|
97 | $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
|
---|
98 |
|
---|
99 | if (!empty($user->avatar)) {
|
---|
100 | Storage::disk('uploads')->delete("/users/" . $user->avatar);
|
---|
101 | }
|
---|
102 |
|
---|
103 | Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
|
---|
104 |
|
---|
105 | $user->avatar = $avatarName;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if($user->isDirty('email')) {
|
---|
109 | $user->notify(new VerifyNewEmail($user));
|
---|
110 | }
|
---|
111 | $user->save();
|
---|
112 |
|
---|
113 | Alert::flash("User updated successfully");
|
---|
114 |
|
---|
115 | return redirect()->route("dashboard.users.index");
|
---|
116 | }
|
---|
117 |
|
---|
118 | public function block(Request $request, $id)
|
---|
119 | {
|
---|
120 | $user = User::find($id);
|
---|
121 | $user->is_active = false;
|
---|
122 | $user->save();
|
---|
123 | Alert::flash($user->name . " User blocked successfully");
|
---|
124 | return redirect()->route("dashboard.users.index");
|
---|
125 | }
|
---|
126 |
|
---|
127 | public function unblock(Request $request, $id)
|
---|
128 | {
|
---|
129 | $user = User::find($id);
|
---|
130 | $user->is_active = true;
|
---|
131 | $user->save();
|
---|
132 | Alert::flash($user->name . " User unblocked successfully");
|
---|
133 | return redirect()->route("dashboard.users.index");
|
---|
134 | }
|
---|
135 |
|
---|
136 | public function destroy(Request $request, $id)
|
---|
137 | {
|
---|
138 | $user = User::find($id);
|
---|
139 |
|
---|
140 | $user->userProfile->delete();
|
---|
141 | $user->delete();
|
---|
142 |
|
---|
143 | Alert::flash($user->name . " deleted successfully");
|
---|
144 |
|
---|
145 | return redirect()->route("dashboard.users.index");
|
---|
146 | }
|
---|
147 |
|
---|
148 | public function getUserRoles()
|
---|
149 | {
|
---|
150 | $roles = Role::get();
|
---|
151 | return $roles;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public function editUserData(UpdateUserData $request, $id)
|
---|
155 | {
|
---|
156 | $user = User::findOrFail($id);
|
---|
157 | $user->name = $request->name;
|
---|
158 | $user->surname = $request->surname;
|
---|
159 | $user->phone_number = $request->phone_number;
|
---|
160 |
|
---|
161 | if ($request->hasFile("avatar")) {
|
---|
162 | $image = $request->file("avatar");
|
---|
163 | $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
|
---|
164 |
|
---|
165 | if (!empty($user->avatar)) {
|
---|
166 | Storage::disk('uploads')->delete("/users/" . $user->avatar);
|
---|
167 | }
|
---|
168 |
|
---|
169 | Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
|
---|
170 |
|
---|
171 | $user->avatar = $avatarName;
|
---|
172 | }
|
---|
173 |
|
---|
174 | $user->save();
|
---|
175 |
|
---|
176 | Alert::flash("User data updated successfully");
|
---|
177 |
|
---|
178 | return redirect()->route("dashboard.settings.index");
|
---|
179 | }
|
---|
180 | }
|
---|