Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/SignupController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API; | namespace App\Http\Controllers\API; | ||||
use App\Http\Controllers\Controller; | use App\Http\Controllers\Controller; | ||||
use App\Jobs\SignupVerificationEmail; | use App\Jobs\SignupVerificationEmail; | ||||
use App\Jobs\SignupVerificationSMS; | use App\Jobs\SignupVerificationSMS; | ||||
use App\Discount; | use App\Discount; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Plan; | use App\Plan; | ||||
use App\Rules\ExternalEmail; | use App\Rules\ExternalEmail; | ||||
use App\Rules\UserEmailDomain; | use App\Rules\UserEmailDomain; | ||||
use App\Rules\UserEmailLocal; | use App\Rules\UserEmailLocal; | ||||
use App\SignupCode; | use App\SignupCode; | ||||
use App\SignupInvitation; | |||||
use App\User; | use App\User; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
/** | /** | ||||
* Signup process API | * Signup process API | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | public function init(Request $request) | ||||
} else { | } else { | ||||
SignupVerificationEmail::dispatch($code); | SignupVerificationEmail::dispatch($code); | ||||
} | } | ||||
return response()->json(['status' => 'success', 'code' => $code->code]); | return response()->json(['status' => 'success', 'code' => $code->code]); | ||||
} | } | ||||
/** | /** | ||||
* Returns signup invitation information. | |||||
* | |||||
* @param string $id Signup invitation identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse|void | |||||
*/ | |||||
public function invitation($id) | |||||
{ | |||||
$invitation = SignupInvitation::withEnvTenant()->find($id); | |||||
if (empty($invitation) || $invitation->isCompleted()) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
$has_domain = $this->getPlan()->hasDomain(); | |||||
$result = [ | |||||
'id' => $id, | |||||
'is_domain' => $has_domain, | |||||
'domains' => $has_domain ? [] : Domain::getPublicDomains(), | |||||
]; | |||||
return response()->json($result); | |||||
} | |||||
/** | |||||
* Validation of the verification code. | * Validation of the verification code. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request HTTP request | * @param \Illuminate\Http\Request $request HTTP request | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse JSON response | * @return \Illuminate\Http\JsonResponse JSON response | ||||
*/ | */ | ||||
public function verify(Request $request) | public function verify(Request $request) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | public function signup(Request $request) | ||||
'voucher' => 'max:32', | 'voucher' => 'max:32', | ||||
] | ] | ||||
); | ); | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
// Signup via invitation | |||||
if ($request->invitation) { | |||||
$invitation = SignupInvitation::withEnvTenant()->find($request->invitation); | |||||
if (empty($invitation) || $invitation->isCompleted()) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
// Check required fields | |||||
$v = Validator::make( | |||||
$request->all(), | |||||
[ | |||||
'first_name' => 'max:128', | |||||
'last_name' => 'max:128', | |||||
'voucher' => 'max:32', | |||||
] | |||||
); | |||||
$errors = $v->fails() ? $v->errors()->toArray() : []; | |||||
if (!empty($errors)) { | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
$settings = [ | |||||
'external_email' => $invitation->email, | |||||
'first_name' => $request->first_name, | |||||
'last_name' => $request->last_name, | |||||
]; | |||||
} else { | |||||
// Validate verification codes (again) | // Validate verification codes (again) | ||||
$v = $this->verify($request); | $v = $this->verify($request); | ||||
if ($v->status() !== 200) { | if ($v->status() !== 200) { | ||||
return $v; | return $v; | ||||
} | } | ||||
// Get user name/email from the verification code database | |||||
$code_data = $v->getData(); | |||||
$settings = [ | |||||
'external_email' => $code_data->email, | |||||
'first_name' => $code_data->first_name, | |||||
'last_name' => $code_data->last_name, | |||||
]; | |||||
} | |||||
// Find the voucher discount | // Find the voucher discount | ||||
if ($request->voucher) { | if ($request->voucher) { | ||||
$discount = Discount::where('code', \strtoupper($request->voucher)) | $discount = Discount::where('code', \strtoupper($request->voucher)) | ||||
->where('active', true)->first(); | ->where('active', true)->first(); | ||||
if (!$discount) { | if (!$discount) { | ||||
$errors = ['voucher' => \trans('validation.voucherinvalid')]; | $errors = ['voucher' => \trans('validation.voucherinvalid')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
} | } | ||||
// Get the plan | // Get the plan | ||||
$plan = $this->getPlan(); | $plan = $this->getPlan(); | ||||
$is_domain = $plan->hasDomain(); | $is_domain = $plan->hasDomain(); | ||||
$login = $request->login; | $login = $request->login; | ||||
$domain_name = $request->domain; | $domain_name = $request->domain; | ||||
// Validate login | // Validate login | ||||
if ($errors = self::validateLogin($login, $domain_name, $is_domain)) { | if ($errors = self::validateLogin($login, $domain_name, $is_domain)) { | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
// Get user name/email from the verification code database | |||||
$code_data = $v->getData(); | |||||
$user_email = $code_data->email; | |||||
// We allow only ASCII, so we can safely lower-case the email address | // We allow only ASCII, so we can safely lower-case the email address | ||||
$login = Str::lower($login); | $login = Str::lower($login); | ||||
$domain_name = Str::lower($domain_name); | $domain_name = Str::lower($domain_name); | ||||
$domain = null; | $domain = null; | ||||
DB::beginTransaction(); | DB::beginTransaction(); | ||||
// Create domain record | // Create domain record | ||||
Show All 15 Lines | public function signup(Request $request) | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->discount()->associate($discount); | $wallet->discount()->associate($discount); | ||||
$wallet->save(); | $wallet->save(); | ||||
} | } | ||||
$user->assignPlan($plan, $domain); | $user->assignPlan($plan, $domain); | ||||
// Save the external email and plan in user settings | // Save the external email and plan in user settings | ||||
$user->setSettings([ | $user->setSettings($settings); | ||||
'external_email' => $user_email, | |||||
'first_name' => $code_data->first_name, | // Update the invitation | ||||
'last_name' => $code_data->last_name, | if (!empty($invitation)) { | ||||
]); | $invitation->status = SignupInvitation::STATUS_COMPLETED; | ||||
$invitation->user_id = $user->id; | |||||
$invitation->save(); | |||||
} | |||||
// Remove the verification code | // Remove the verification code | ||||
if ($this->code) { | |||||
$this->code->delete(); | $this->code->delete(); | ||||
} | |||||
DB::commit(); | DB::commit(); | ||||
return AuthController::logonResponse($user); | return AuthController::logonResponse($user); | ||||
} | } | ||||
/** | /** | ||||
* Returns plan for the signup process | * Returns plan for the signup process | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |