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

Last change on this file since d25ba66 was d25ba66, checked in by Özkan İliyaz <iliyaz_96@…>, 4 years ago

ADD post confirmation with multiple phases, notification after succesfully password creation

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[0924b6c]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewUserRequest;
7use App\Models\Role;
8use App\Models\User;
9use Illuminate\Http\Request;
10use App\Notifications\WelcomeUser;
11use App\Http\Controllers\Controller;
[d25ba66]12use Illuminate\Support\Facades\DB;
[0924b6c]13use Illuminate\Support\Facades\Storage;
14use Propaganistas\LaravelPhone\PhoneNumber;
15
16class UsersController extends Controller
17{
18 public function index()
19 {
20 return view("dashboard.users.index")->with([
21 "users" => User::all()
22 ]);
23 }
24
25 public function create()
26 {
27 return view("dashboard.users.create")->with([
28 "roles" => Role::all(),
29 "countries" => country()->all()
30 ]);
31 }
32
33 public function store(NewUserRequest $request)
34 {
[d25ba66]35 DB::transaction(function () use ($request) {
[0924b6c]36
[d25ba66]37 $user = new User();
[0924b6c]38
[d25ba66]39 $user->name = $request->name;
40 $user->surname = $request->surname;
41 $user->email = $request->email;
42 $user->country_code = $request->mobile_number_country;
43 $user->mobile_number = PhoneNumber::make($request->mobile_number, $request->mobile_number_country)->formatInternational();
44 $user->username = $request->username;
45 $user->password = $user->generateTemporaryPassword();
46 $user->security_code = $user->generateSecurityCode();
47 $user->verify_token = $user->generateVerifyToken();
[0924b6c]48
[d25ba66]49 $user->role_id = $request->userRole;
[0924b6c]50
[d25ba66]51 $user->save();
52
53 $user->userProfile()->create([
54 "profile_link" => $user->generateProfileLink($request->name, $request->surname),
55 "technoblog_email" => $user->generateTechnoblogEmail($request->name, $request->surname)
56 ]);
57
58 $user->notify(new WelcomeUser($user));
[0924b6c]59
[d25ba66]60 Alert::flash("New user added successfully");
[0924b6c]61
[d25ba66]62 });
[0924b6c]63
64 return redirect()->route("dashboard.users.create");
65 }
66
67 public function block(Request $request, $id)
68 {
69 $user = User::find($id);
70 $user->is_active = false;
71 $user->save();
72 Alert::flash($user->name . " blocked successfully");
73 return redirect()->route("dashboard.users.index");
74 }
75
76 public function unblock(Request $request, $id)
77 {
78 $user = User::find($id);
79 $user->is_active = true;
80 $user->save();
81 Alert::flash($user->name . " unblocked successfully");
82 return redirect()->route("dashboard.users.index");
83 }
84
85 public function destroy(Request $request, $id)
86 {
87 $user = User::find($id);
88
89 if (Storage::disk("uploads")->exists("/users/" . $user->userProfile->profile_photo_link)) {
90 Storage::disk("uploads")->delete("/users/" . $user->userProfile->profile_photo_link);
91 }
92
93 if (Storage::disk("uploads")->exists("/users/" . $user->userProfile->cover_photo_link)) {
94 Storage::disk("uploads")->delete("/users/" . $user->userProfile->cover_photo_link);
95 }
96
97 $user->userProfile->delete();
98 $user->delete();
99
100 Alert::flash($user->name . " deleted successfully");
101
102 return redirect()->route("dashboard.users.index");
103 }
104}
Note: See TracBrowser for help on using the repository browser.