source: app/Http/Controllers/Dashboard/UsersController.php@ deff0bc

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

bug fixes

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[194a359]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewUserRequest;
[24a616f]7use App\Http\Requests\Dashboard\UpdateUserData;
[194a359]8use App\Http\Requests\Dashboard\UpdateUserRequest;
[24a616f]9use App\Models\Avatar;
[0df7a93]10use App\Models\Department;
11use App\Models\Folder;
[d795fa6]12use Carbon\Carbon;
[24a616f]13use Illuminate\Support\Facades\File;
[194a359]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;
[24a616f]20use Illuminate\Support\Facades\Storage;
[194a359]21
22class UsersController extends Controller
23{
[24a616f]24
[194a359]25 public function index()
26 {
27 return view("dashboard.users.index")->with([
[c6b84df]28 "users" => User::all(),
[0a1fb54]29 "roles" => Role::all()
[194a359]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;
[d795fa6]40 $user->phone_number = $request->phone_number;
[194a359]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
[24a616f]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 }
[c6b84df]60
61 $user->created_by = auth()->user()->id;
[194a359]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;
[d795fa6]78 $user->phone_number = $request->phone_number;
[194a359]79 $user->role_id = $request->userRole;
[c6b84df]80 $user->updated_at = Carbon::now();
[24a616f]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
[194a359]95 if($user->isDirty('email')) {
[0a1fb54]96 $user->is_active = false;
97 $user->security_code = $user->generateSecurityCode();
98 $user->verify_token = $user->generateVerifyToken();
[194a359]99 $user->notify(new VerifyNewEmail($user));
100 }
[0a1fb54]101
[194a359]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
[0df7a93]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 }
[194a359]135
[0df7a93]136 $user->delete();
[194a359]137
[0df7a93]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 }
[194a359]147 }
148
149 public function getUserRoles()
150 {
151 $roles = Role::get();
152 return $roles;
153 }
[24a616f]154
155 public function editUserData(UpdateUserData $request, $id)
156 {
157 $user = User::findOrFail($id);
158 $user->name = $request->name;
159 $user->surname = $request->surname;
[d795fa6]160 $user->phone_number = $request->phone_number;
[24a616f]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 }
[194a359]181}
Note: See TracBrowser for help on using the repository browser.