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