Skip to content

Commit

Permalink
Merge pull request #1755 from bakaphp/refactor-social-login-company
Browse files Browse the repository at this point in the history
refact: social login company
  • Loading branch information
kaioken authored Jul 31, 2024
2 parents c1ce35c + d37a570 commit 7bfbd78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kanvas\Auth\Actions\SocialLoginAction;
use Kanvas\Auth\DataTransferObject\LoginInput;
use Kanvas\Auth\DataTransferObject\RegisterInput;
use Kanvas\Auth\Services\AuthenticationService;
use Kanvas\Auth\Services\ForgotPassword as ForgotPasswordService;
use Kanvas\Auth\Socialite\SocialManager;
use Kanvas\Auth\Traits\AuthTrait;
Expand Down Expand Up @@ -122,12 +123,7 @@ public function register(
)->validate();
PasswordValidation::validateArray($request['data'], $app);

$userRegistrationAssignToAppDefaultCompanyBranch = $app->get(AppSettingsEnums::GLOBAL_USER_REGISTRATION_ASSIGN_GLOBAL_COMPANY->getValue());
$branch = null;
if ($userRegistrationAssignToAppDefaultCompanyBranch) {
$branch = CompaniesBranches::getById($userRegistrationAssignToAppDefaultCompanyBranch);
}

$branch = AuthenticationService::getAppDefaultAssignCompanyBranch($app);
$data = RegisterInput::fromArray($request['data'], $branch);
$user = new RegisterUsersAction($data);
$request = request();
Expand Down
7 changes: 6 additions & 1 deletion src/Kanvas/Auth/Actions/SocialLoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use Illuminate\Support\Str;
use Kanvas\Apps\Models\Apps;
use Kanvas\Auth\DataTransferObject\RegisterInput;
use Kanvas\Auth\Services\AuthenticationService;
use Kanvas\Auth\Socialite\DataTransferObject\User as SocialiteUser;
use Kanvas\Companies\Models\CompaniesBranches;
use Kanvas\Enums\AppSettingsEnums;
use Kanvas\Users\Models\Sources;
use Kanvas\Users\Models\UserLinkedSources;
use Kanvas\Users\Models\Users;
Expand Down Expand Up @@ -47,7 +50,9 @@ public function execute(): Users
'password' => Str::random(11),
'displayname' => $this->socialUser->nickname,
];
$userData = RegisterInput::fromArray($userData);

$branch = AuthenticationService::getAppDefaultAssignCompanyBranch($this->app);
$userData = RegisterInput::fromArray($userData, $branch);

$registeredUser = new RegisterUsersAction($userData, $this->app);
$existedUser = $registeredUser->execute();
Expand Down
18 changes: 18 additions & 0 deletions src/Kanvas/Auth/Services/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Baka\Users\Contracts\UserAppInterface;
use Baka\Users\Contracts\UserInterface;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException as EloquentModelNotFoundException;
use Illuminate\Support\Facades\Hash;
use Kanvas\Apps\Models\Apps;
use Kanvas\Auth\Actions\RegisterUsersAppAction;
use Kanvas\Auth\DataTransferObject\LoginInput;
use Kanvas\Auth\Exceptions\AuthenticationException;
use Kanvas\Companies\Models\CompaniesBranches;
use Kanvas\Enums\AppEnums;
use Kanvas\Enums\AppSettingsEnums;
use Kanvas\Exceptions\ModelNotFoundException;
use Kanvas\Sessions\Models\Sessions;
use Kanvas\Users\Enums\StatusEnums;
Expand Down Expand Up @@ -183,4 +186,19 @@ public static function getSocialite(Apps $app, string $provider)

return Socialite::driver($provider);
}

public static function getAppDefaultAssignCompanyBranch(Apps $app): ?CompaniesBranches
{
$userRegistrationAssignToAppDefaultCompanyBranch = $app->get(AppSettingsEnums::GLOBAL_USER_REGISTRATION_ASSIGN_GLOBAL_COMPANY->getValue());
$branch = null;

try {
if ($userRegistrationAssignToAppDefaultCompanyBranch) {
$branch = CompaniesBranches::getById($userRegistrationAssignToAppDefaultCompanyBranch);
}
} catch (EloquentModelNotFoundException $e) {
}

return $branch;
}
}

0 comments on commit 7bfbd78

Please sign in to comment.