source: app/Http/Controllers/Dashboard/UsersController.php@ 62d01a6

Last change on this file since 62d01a6 was 0a1fb54, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

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