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

develop
Last change on this file since b9c4a92 was d795fa6, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added validation to blades

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