[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(),
|
---|
[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 | }
|
---|