source: app/Http/Controllers/Dashboard/UsersController.php@ 120759b

develop
Last change on this file since 120759b was 24a616f, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added documents crud, added last_seen_to_user, edited views

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