1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\NewUserRequest;
|
---|
7 | use App\Http\Requests\Dashboard\UpdateUserRequest;
|
---|
8 | use App\Models\Role;
|
---|
9 | use App\Models\User;
|
---|
10 | use App\Notifications\VerifyNewEmail;
|
---|
11 | use Illuminate\Http\Request;
|
---|
12 | use App\Notifications\WelcomeUser;
|
---|
13 | use App\Http\Controllers\Controller;
|
---|
14 |
|
---|
15 | class UsersController extends Controller
|
---|
16 | {
|
---|
17 | public function index()
|
---|
18 | {
|
---|
19 | return view("dashboard.users.index")->with([
|
---|
20 | "users" => User::all()
|
---|
21 | ]);
|
---|
22 | }
|
---|
23 |
|
---|
24 | public function create()
|
---|
25 | {
|
---|
26 | return view("dashboard.users.create")->with([
|
---|
27 | "roles" => Role::all(),
|
---|
28 | ]);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public function store(NewUserRequest $request)
|
---|
32 | {
|
---|
33 | $user = new User();
|
---|
34 |
|
---|
35 | $user->name = $request->name;
|
---|
36 | $user->surname = $request->surname;
|
---|
37 | $user->email = $request->email;
|
---|
38 | $user->mobile_number = $request->mobile_number;
|
---|
39 | $user->username = $request->username;
|
---|
40 | $user->password = $user->generateTemporaryPassword();
|
---|
41 | $user->security_code = $user->generateSecurityCode();
|
---|
42 | $user->verify_token = $user->generateVerifyToken();
|
---|
43 |
|
---|
44 | $user->role_id = $request->userRole;
|
---|
45 |
|
---|
46 | $user->save();
|
---|
47 |
|
---|
48 | $user->notify(new WelcomeUser($user));
|
---|
49 |
|
---|
50 | Alert::flash("New user added successfully");
|
---|
51 |
|
---|
52 | return redirect()->route("dashboard.users.index");
|
---|
53 | }
|
---|
54 |
|
---|
55 | public function editShow($id)
|
---|
56 | {
|
---|
57 | return view("dashboard.users.edit")->with([
|
---|
58 | "user" => User::findOrFail($id),
|
---|
59 | "roles" => Role::all(),
|
---|
60 | ]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public function edit(UpdateUserRequest $request, $id)
|
---|
64 | {
|
---|
65 | $user = User::findOrFail($id);
|
---|
66 | $user->name = $request->name;
|
---|
67 | $user->surname = $request->surname;
|
---|
68 | $user->username = $request->username;
|
---|
69 | $user->email = $request->email;
|
---|
70 | $user->mobile_number = $request->mobile_number;
|
---|
71 | $user->role_id = $request->userRole;
|
---|
72 | if($user->isDirty('email')) {
|
---|
73 | $user->notify(new VerifyNewEmail($user));
|
---|
74 | }
|
---|
75 | $user->save();
|
---|
76 |
|
---|
77 | Alert::flash("User updated successfully");
|
---|
78 |
|
---|
79 | return redirect()->route("dashboard.users.index");
|
---|
80 | }
|
---|
81 |
|
---|
82 | public function editUserData(UpdateUserRequest $request, $id)
|
---|
83 | {
|
---|
84 | $user = User::findOrFail($id);
|
---|
85 | $user->name = $request->name;
|
---|
86 | $user->surname = $request->surname;
|
---|
87 | $user->mobile_number = $request->mobile_number;
|
---|
88 |
|
---|
89 | $user->save();
|
---|
90 |
|
---|
91 | Alert::flash("User data updated successfully");
|
---|
92 |
|
---|
93 | return redirect()->route("dashboard.settings.index");
|
---|
94 | }
|
---|
95 |
|
---|
96 | public function block(Request $request, $id)
|
---|
97 | {
|
---|
98 | $user = User::find($id);
|
---|
99 | $user->is_active = false;
|
---|
100 | $user->save();
|
---|
101 | Alert::flash($user->name . " User blocked successfully");
|
---|
102 | return redirect()->route("dashboard.users.index");
|
---|
103 | }
|
---|
104 |
|
---|
105 | public function unblock(Request $request, $id)
|
---|
106 | {
|
---|
107 | $user = User::find($id);
|
---|
108 | $user->is_active = true;
|
---|
109 | $user->save();
|
---|
110 | Alert::flash($user->name . " User unblocked successfully");
|
---|
111 | return redirect()->route("dashboard.users.index");
|
---|
112 | }
|
---|
113 |
|
---|
114 | public function destroy(Request $request, $id)
|
---|
115 | {
|
---|
116 | $user = User::find($id);
|
---|
117 |
|
---|
118 | $user->userProfile->delete();
|
---|
119 | $user->delete();
|
---|
120 |
|
---|
121 | Alert::flash($user->name . " deleted successfully");
|
---|
122 |
|
---|
123 | return redirect()->route("dashboard.users.index");
|
---|
124 | }
|
---|
125 |
|
---|
126 | public function getUserRoles()
|
---|
127 | {
|
---|
128 | $roles = Role::get();
|
---|
129 | return $roles;
|
---|
130 | }
|
---|
131 | }
|
---|