mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 00:02:15 +00:00
- admin registration fixes
- alert popup refactoring
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Socialite\Controllers;
|
||||
|
||||
use App\Users\Models\User;
|
||||
@@ -7,15 +8,19 @@ use App\Http\Controllers\Controller;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
use VueFileManager\Subscription\Domain\Plans\Exceptions\MeteredBillingPlanDoesntExist;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
|
||||
class SocialiteCallbackController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected StatefulGuard $guard,
|
||||
protected StatefulGuard $guard,
|
||||
public CreateNewUserAction $createNewUser,
|
||||
) {
|
||||
}
|
||||
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws MeteredBillingPlanDoesntExist
|
||||
*/
|
||||
public function __invoke($provider)
|
||||
{
|
||||
$isAllowedRegistration = intval(get_settings('registration'));
|
||||
@@ -39,8 +44,23 @@ class SocialiteCallbackController extends Controller
|
||||
return redirect()->to('/platform/files');
|
||||
}
|
||||
|
||||
// Check for metered billing plan
|
||||
if (get_settings('subscription_type') === 'metered') {
|
||||
// Get metered plan
|
||||
$plan = Plan::where('status', 'active')
|
||||
->where('type', 'metered');
|
||||
|
||||
// TODO: redirect to the error page
|
||||
if ($plan->doesntExist()) {
|
||||
return response([
|
||||
'type' => 'error',
|
||||
'message' => 'User registrations are temporarily disabled',
|
||||
], 409);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if account registration is enabled
|
||||
if (! $isAllowedRegistration) {
|
||||
if (!$isAllowedRegistration) {
|
||||
return response([
|
||||
'type' => 'error',
|
||||
'message' => 'User registration is not allowed',
|
||||
@@ -49,6 +69,7 @@ class SocialiteCallbackController extends Controller
|
||||
|
||||
// Create data user data object
|
||||
$data = CreateUserData::fromArray([
|
||||
'role' => 'user',
|
||||
'name' => $socialite->getName(),
|
||||
'email' => $socialite->getEmail(),
|
||||
'avatar' => store_socialite_avatar($socialite->getAvatar()),
|
||||
|
||||
@@ -46,7 +46,7 @@ class AutoSubscribeForMeteredBillingAction
|
||||
// Create balance with 0 amount
|
||||
$user->balance()->create([
|
||||
'amount' => 0,
|
||||
'currency' => $plan->currency, // todo: issue where plan is not created yet
|
||||
'currency' => $plan->currency,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Domain\Teams\Models\TeamFolderMember;
|
||||
use Domain\Teams\Models\TeamFolderInvitation;
|
||||
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
|
||||
use VueFileManager\Subscription\Domain\Plans\Exceptions\MeteredBillingPlanDoesntExist;
|
||||
|
||||
class CreateNewUserAction extends Controller
|
||||
{
|
||||
@@ -17,14 +19,69 @@ class CreateNewUserAction extends Controller
|
||||
|
||||
/**
|
||||
* Validate and create a new user.
|
||||
*
|
||||
* @throws MeteredBillingPlanDoesntExist
|
||||
*/
|
||||
public function __invoke(CreateUserData $data): User
|
||||
{
|
||||
$settings = get_settings([
|
||||
'user_verification', 'subscription_type',
|
||||
]);
|
||||
// Check if subscription metered billing plan exist
|
||||
$this->checkMeteredBillingPlan($data);
|
||||
|
||||
// Create user
|
||||
$user = $this->createUser($data);
|
||||
|
||||
// Join to previously accepted team folder invitations
|
||||
$this->applyExistingTeamInvitations($user);
|
||||
|
||||
// Subscribe user for metered billing
|
||||
if (get_settings('subscription_type') === 'metered' && $data->role !== 'admin') {
|
||||
($this->autoSubscribeForMeteredBilling)($user);
|
||||
}
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if (! $data->password || ! intval(get_settings('user_verification'))) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws MeteredBillingPlanDoesntExist
|
||||
*/
|
||||
private function checkMeteredBillingPlan(CreateUserData $data): void
|
||||
{
|
||||
if (get_settings('subscription_type') === 'metered' && $data->role !== 'admin') {
|
||||
// Get metered plan
|
||||
$plan = Plan::where('status', 'active')
|
||||
->where('type', 'metered');
|
||||
|
||||
if ($plan->doesntExist()) {
|
||||
throw new MeteredBillingPlanDoesntExist();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function applyExistingTeamInvitations(User $user): void
|
||||
{
|
||||
TeamFolderInvitation::where('email', $user->email)
|
||||
->where('status', 'waiting-for-registration')
|
||||
->cursor()
|
||||
->each(function ($invitation) use ($user) {
|
||||
TeamFolderMember::create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $invitation->parent_id,
|
||||
'permission' => $invitation->permission,
|
||||
]);
|
||||
|
||||
$invitation->accept();
|
||||
});
|
||||
}
|
||||
|
||||
private function createUser(CreateUserData $data): User
|
||||
{
|
||||
$user = User::create([
|
||||
'password' => $data->password ? bcrypt($data->password) : null,
|
||||
'oauth_provider' => $data->oauth_provider,
|
||||
@@ -41,32 +98,6 @@ class CreateNewUserAction extends Controller
|
||||
'avatar' => $data->avatar,
|
||||
]);
|
||||
|
||||
// Join to previously accepted team folder invitations
|
||||
TeamFolderInvitation::where('email', $user->email)
|
||||
->where('status', 'waiting-for-registration')
|
||||
->cursor()
|
||||
->each(function ($invitation) use ($user) {
|
||||
TeamFolderMember::create([
|
||||
'user_id' => $user->id,
|
||||
'parent_id' => $invitation->parent_id,
|
||||
'permission' => $invitation->permission,
|
||||
]);
|
||||
|
||||
$invitation->accept();
|
||||
});
|
||||
|
||||
// Subscribe user for metered billing
|
||||
if ($settings['subscription_type'] === 'metered') {
|
||||
($this->autoSubscribeForMeteredBilling)($user);
|
||||
}
|
||||
|
||||
// Mark as verified if verification is disabled
|
||||
if (! $data->password || ! intval($settings['user_verification'])) {
|
||||
$user->markEmailAsVerified();
|
||||
}
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
|
||||
use App\Users\Actions\CreateNewUserAction;
|
||||
use App\Users\Requests\RegisterUserRequest;
|
||||
use Illuminate\Contracts\Auth\StatefulGuard;
|
||||
use VueFileManager\Subscription\Domain\Plans\Exceptions\MeteredBillingPlanDoesntExist;
|
||||
|
||||
class RegisterUserController extends Controller
|
||||
{
|
||||
@@ -26,10 +27,21 @@ class RegisterUserController extends Controller
|
||||
}
|
||||
|
||||
// Map registration data
|
||||
$data = CreateUserData::fromRequest($request);
|
||||
$data = CreateUserData::fromArray([
|
||||
'name' => $request->input('name'),
|
||||
'email' => $request->input('email'),
|
||||
'password' => $request->input('password'),
|
||||
]);
|
||||
|
||||
// Register user
|
||||
$user = ($this->createNewUser)($data);
|
||||
try {
|
||||
$user = ($this->createNewUser)($data);
|
||||
} catch (MeteredBillingPlanDoesntExist $e) {
|
||||
return response([
|
||||
'type' => 'error',
|
||||
'message' => 'User registrations are temporarily disabled',
|
||||
], 409);
|
||||
}
|
||||
|
||||
// Log in if verification is disabled
|
||||
if (! $user->password || ! intval(get_settings('user_verification'))) {
|
||||
|
||||
@@ -5,6 +5,7 @@ use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
class CreateUserData extends DataTransferObject
|
||||
{
|
||||
public string $role;
|
||||
public string $name;
|
||||
public string $email;
|
||||
public ?string $password;
|
||||
@@ -14,6 +15,7 @@ class CreateUserData extends DataTransferObject
|
||||
public static function fromRequest($request): self
|
||||
{
|
||||
return new self([
|
||||
'role' => $request->has('role') ? $request->input('role') : 'user',
|
||||
'name' => $request->input('name'),
|
||||
'email' => $request->input('email'),
|
||||
'avatar' => $request->input('avatar') ?? null,
|
||||
@@ -25,9 +27,10 @@ class CreateUserData extends DataTransferObject
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self([
|
||||
'role' => $array['role'] ?? 'user',
|
||||
'name' => $array['name'] ?? null,
|
||||
'email' => $array['email'],
|
||||
'avatar' => $array['avatar'],
|
||||
'avatar' => $array['avatar'] ?? null,
|
||||
'password' => $array['password'] ?? null,
|
||||
'oauth_provider' => $array['oauth_provider'] ?? null,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user