From 06d0b88a731273defd1889cce15e4171246f32f3 Mon Sep 17 00:00:00 2001 From: Thekindbull Date: Mon, 16 Dec 2024 00:51:01 +0800 Subject: [PATCH] add agents creation form --- .../Fortify/UpdateUserProfileInformation.php | 21 +++++---- .../Controllers/Auth/RegisterController.php | 17 +++---- .../Company/AgentsTableController.php | 2 +- .../Company/ConfirmCompanyController.php | 8 +--- .../Company/CreateAgentController.php | 45 +++++++++++++++++++ .../Company/CreateCompanyController.php | 13 +++--- .../Controllers/Company/DetailsController.php | 10 ++--- app/Models/ForcedPassword.php | 19 ++++++++ app/Models/User.php | 4 +- .../views/company/agents/table.blade.php | 45 ++++++++++++++++++- resources/views/left-panel.blade.php | 2 +- routes/web.php | 8 ++-- 12 files changed, 153 insertions(+), 41 deletions(-) create mode 100644 app/Http/Controllers/Company/CreateAgentController.php create mode 100644 app/Models/ForcedPassword.php diff --git a/app/Actions/Fortify/UpdateUserProfileInformation.php b/app/Actions/Fortify/UpdateUserProfileInformation.php index 0930ddf..bccacbe 100644 --- a/app/Actions/Fortify/UpdateUserProfileInformation.php +++ b/app/Actions/Fortify/UpdateUserProfileInformation.php @@ -9,14 +9,14 @@ use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; class UpdateUserProfileInformation implements UpdatesUserProfileInformation -{ + { /** * Validate and update the given user's profile information. * * @param array $input */ public function update(User $user, array $input): void - { + { Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], @@ -29,16 +29,21 @@ public function update(User $user, array $input): void ], ])->validateWithBag('updateProfileInformation'); - if ($input['email'] !== $user->email && - $user instanceof MustVerifyEmail) { + if ( + $input['email'] !== $user->email && + $user instanceof MustVerifyEmail + ) + { $this->updateVerifiedUser($user, $input); - } else { + } + else + { $user->forceFill([ 'name' => $input['name'], 'email' => $input['email'], ])->save(); + } } - } /** * Update the given verified user's profile information. @@ -46,7 +51,7 @@ public function update(User $user, array $input): void * @param array $input */ protected function updateVerifiedUser(User $user, array $input): void - { + { $user->forceFill([ 'name' => $input['name'], 'email' => $input['email'], @@ -54,5 +59,5 @@ protected function updateVerifiedUser(User $user, array $input): void ])->save(); $user->sendEmailVerificationNotification(); + } } -} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index e77297d..8c68ef2 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -4,15 +4,13 @@ use App\Http\Controllers\Controller; use App\Models\User; -use App\Models\Agent\Agent; use App\Models\Company\Company; +use App\Models\Company\CompanyAdmin; +use App\Models\Agent\Agent; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; -use Illuminate\Support\Str; - -use App\Notifications\UserRegistered; class RegisterController extends Controller { @@ -70,14 +68,12 @@ protected function validator(array $data) */ protected function create(array $data) { - $newUserPassword = Str::password(8); $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], - 'phone' => $data['phone'], - 'password' => Hash::make($newUserPassword), + 'phone' => $data['phone'] ]); - $user->notify(new UserRegistered(login: $user->email, password: $newUserPassword)); + $user->setForcedPassword(); $company = Company::create([ 'name' => $data['name'], @@ -88,6 +84,11 @@ protected function create(array $data) 'status' => 'new' ]); + CompanyAdmin::create([ + 'user_id' => $user->id, + 'company_id' => $company->id + ]); + Agent::create([ 'user_id' => $user->id, 'company_id' => $company->id diff --git a/app/Http/Controllers/Company/AgentsTableController.php b/app/Http/Controllers/Company/AgentsTableController.php index dedf3d5..5462332 100644 --- a/app/Http/Controllers/Company/AgentsTableController.php +++ b/app/Http/Controllers/Company/AgentsTableController.php @@ -36,7 +36,7 @@ public function index(Request $request) } else { - echo 'has no permissions'; + abort(code: 401); return; } } diff --git a/app/Http/Controllers/Company/ConfirmCompanyController.php b/app/Http/Controllers/Company/ConfirmCompanyController.php index 51a3b9b..d51e5cd 100644 --- a/app/Http/Controllers/Company/ConfirmCompanyController.php +++ b/app/Http/Controllers/Company/ConfirmCompanyController.php @@ -9,8 +9,6 @@ use App\Models\Company\Company; use App\Models\Company\CompanyAdmin; use App\Models\User; -use App\Notifications\UserRegistered; -use Illuminate\Support\Str; class ConfirmCompanyController extends Controller { @@ -29,14 +27,12 @@ public function __invoke(Request $request) } else { - $newUserPassword = Str::password(8); $user = User::create([ 'name' => $request->user_name, 'email' => $company->email, - 'phone' => $request->user_phone, - 'password' => Hash::make($newUserPassword), + 'phone' => $request->user_phone ]); - $user->notify(new UserRegistered($user->email, password: $newUserPassword)); + $user->setForcedPassword(); } CompanyAdmin::create([ 'user_id' => $user->id, diff --git a/app/Http/Controllers/Company/CreateAgentController.php b/app/Http/Controllers/Company/CreateAgentController.php new file mode 100644 index 0000000..c686d6c --- /dev/null +++ b/app/Http/Controllers/Company/CreateAgentController.php @@ -0,0 +1,45 @@ +only(['name', 'email', 'phone']); + $admin = CompanyAdmin::where('user_id', auth()->id()); + if (!$admin->count()) + { + abort(404); + return; + } + $admin = $admin->first(); + if (!$this->isUnique($request->email, $request->phone)) + { + return back()->with('error', __('Agent is not unique')); + } + if ($user = User::create($request->all())) + { + $user->setForcedPassword(); + $agent = Agent::create([ + 'user_id' => $user->id, + 'company_id' => $admin->company_id + ]); + } + return to_route('company.agents.table'); + } + + public function isUnique($email, $phone) + { + if (User::where('email', $email)->count() || User::where('phone', $phone)->count()) + { + return false; + } + return true; + } + } diff --git a/app/Http/Controllers/Company/CreateCompanyController.php b/app/Http/Controllers/Company/CreateCompanyController.php index 9cc15e9..0aff52b 100644 --- a/app/Http/Controllers/Company/CreateCompanyController.php +++ b/app/Http/Controllers/Company/CreateCompanyController.php @@ -9,24 +9,27 @@ use App\Models\Bitrix\SendCompany; class CreateCompanyController extends Controller -{ + { /** * Handle the incoming request. */ public function __invoke(Request $request) - { + { $request->request->add([ 'secret' => bin2hex(random_bytes(16)), 'status' => 'new', 'type' => 'AGENCY' ]); $company = Company::create($request->only('name', 'email', 'inn', 'legal_address', 'secret', 'status', 'type')); - if ($company) { + if ($company) + { $companyConfirmByBitrix = new SendCompany($company); $companyConfirmByBitrix->send(); return view('company.created'); - } else { + } + else + { return back()->withInputs(); + } } } -} diff --git a/app/Http/Controllers/Company/DetailsController.php b/app/Http/Controllers/Company/DetailsController.php index dfabfe9..7212ac9 100644 --- a/app/Http/Controllers/Company/DetailsController.php +++ b/app/Http/Controllers/Company/DetailsController.php @@ -7,7 +7,7 @@ use App\Models\Company\Company; use App\Models\Company\Details; -use App\Models\Agent\Agent; +use App\Models\Company\CompanyAdmin; class DetailsController extends Controller { @@ -15,15 +15,15 @@ public function index() { $company = false; $userId = auth()->user()->id; - $agent = Agent::where('user_id', $userId)->get(); - if ($agent->count() == 1) + $admin = CompanyAdmin::where('user_id', $userId)->get(); + if ($admin->count() == 1) { - $agent = $agent->first(); + $agent = $admin->first(); $company = Company::find($agent->company_id); } else { - return back(); + abort(code: 401); } ; diff --git a/app/Models/ForcedPassword.php b/app/Models/ForcedPassword.php new file mode 100644 index 0000000..1bd0975 --- /dev/null +++ b/app/Models/ForcedPassword.php @@ -0,0 +1,19 @@ +password = Hash::make($newPassword); + $this->save(); + $this->notify(instance: new UserRegistered($this->email, password: $newPassword)); + return true; + } + } \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index 38d4286..a82eebe 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,9 +7,11 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; + +use App\Models\ForcedPassword; class User extends Authenticatable { - use HasApiTokens, HasFactory, Notifiable; + use HasApiTokens, HasFactory, Notifiable, ForcedPassword; /** * The attributes that are mass assignable. diff --git a/resources/views/company/agents/table.blade.php b/resources/views/company/agents/table.blade.php index b6d7745..5fb6e11 100644 --- a/resources/views/company/agents/table.blade.php +++ b/resources/views/company/agents/table.blade.php @@ -2,8 +2,8 @@ @section('content')
-
-
+ +
@@ -16,6 +16,7 @@ autocomplete="off" onclick="this.form.submit()" {{ $status == $statuses::DISMISSED ? 'checked' : '' }}>
+
+
+ +
@if (!$status || $status == 'all' || $status == $statuses::ACTIVE)

Активные

@@ -39,4 +46,38 @@
@endif
+ + + + @endsection diff --git a/resources/views/left-panel.blade.php b/resources/views/left-panel.blade.php index 94e1508..19bb01d 100644 --- a/resources/views/left-panel.blade.php +++ b/resources/views/left-panel.blade.php @@ -12,7 +12,7 @@ class="nav-link d-flex align-items-center gap-2 fs-5 border rounded-4 active" ar