Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/SignupController.php
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | class SignupController extends Controller | ||||
*/ | */ | ||||
public function init(Request $request) | public function init(Request $request) | ||||
{ | { | ||||
// Check required fields | // Check required fields | ||||
$v = Validator::make( | $v = Validator::make( | ||||
$request->all(), | $request->all(), | ||||
[ | [ | ||||
'email' => 'required', | 'email' => 'required', | ||||
'name' => 'required|max:512', | 'first_name' => 'max:128', | ||||
'last_name' => 'max:128', | |||||
'plan' => 'nullable|alpha_num|max:128', | 'plan' => 'nullable|alpha_num|max:128', | ||||
'voucher' => 'max:32', | 'voucher' => 'max:32', | ||||
] | ] | ||||
); | ); | ||||
if ($v->fails()) { | $is_phone = false; | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | $errors = $v->fails() ? $v->errors()->toArray() : []; | ||||
} | |||||
// Validate user email (or phone) | // Validate user email (or phone) | ||||
if (empty($errors['email'])) { | |||||
if ($error = $this->validatePhoneOrEmail($request->email, $is_phone)) { | if ($error = $this->validatePhoneOrEmail($request->email, $is_phone)) { | ||||
return response()->json(['status' => 'error', 'errors' => ['email' => $error]], 422); | $errors['email'] = $error; | ||||
} | |||||
} | |||||
if (!empty($errors)) { | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | } | ||||
// Generate the verification code | // Generate the verification code | ||||
$code = SignupCode::create([ | $code = SignupCode::create([ | ||||
'data' => [ | 'data' => [ | ||||
'email' => $request->email, | 'email' => $request->email, | ||||
'name' => $request->name, | 'first_name' => $request->first_name, | ||||
'last_name' => $request->last_name, | |||||
'plan' => $request->plan, | 'plan' => $request->plan, | ||||
'voucher' => $request->voucher, | 'voucher' => $request->voucher, | ||||
] | ] | ||||
]); | ]); | ||||
// Send email/sms message | // Send email/sms message | ||||
if ($is_phone) { | if ($is_phone) { | ||||
SignupVerificationSMS::dispatch($code); | SignupVerificationSMS::dispatch($code); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | public function verify(Request $request) | ||||
$has_domain = $this->getPlan()->hasDomain(); | $has_domain = $this->getPlan()->hasDomain(); | ||||
// Return user name and email/phone/voucher from the codes database, | // Return user name and email/phone/voucher from the codes database, | ||||
// domains list for selection and "plan type" flag | // domains list for selection and "plan type" flag | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'email' => $code->data['email'], | 'email' => $code->data['email'], | ||||
'name' => $code->data['name'], | 'first_name' => $code->data['first_name'], | ||||
'last_name' => $code->data['last_name'], | |||||
'voucher' => $code->data['voucher'], | 'voucher' => $code->data['voucher'], | ||||
'is_domain' => $has_domain, | 'is_domain' => $has_domain, | ||||
'domains' => $has_domain ? [] : Domain::getPublicDomains(), | 'domains' => $has_domain ? [] : Domain::getPublicDomains(), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Finishes the signup process by creating the user account. | * Finishes the signup process by creating the user account. | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | public function signup(Request $request) | ||||
// Validate login | // Validate login | ||||
if ($errors = self::validateLogin($login, $domain, $is_domain)) { | if ($errors = self::validateLogin($login, $domain, $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 | // Get user name/email from the verification code database | ||||
$code_data = $v->getData(); | $code_data = $v->getData(); | ||||
$user_name = $code_data->name; | |||||
$user_email = $code_data->email; | $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 = Str::lower($domain); | $domain = Str::lower($domain); | ||||
DB::beginTransaction(); | DB::beginTransaction(); | ||||
// Create user record | // Create user record | ||||
$user = User::create([ | $user = User::create([ | ||||
'name' => $user_name, | |||||
'email' => $login . '@' . $domain, | 'email' => $login . '@' . $domain, | ||||
'password' => $request->password, | 'password' => $request->password, | ||||
]); | ]); | ||||
// Create domain record | // Create domain record | ||||
// FIXME: Should we do this in UserObserver::created()? | // FIXME: Should we do this in UserObserver::created()? | ||||
if ($is_domain) { | if ($is_domain) { | ||||
$domain = Domain::create([ | $domain = Domain::create([ | ||||
'namespace' => $domain, | 'namespace' => $domain, | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_EXTERNAL, | 'type' => Domain::TYPE_EXTERNAL, | ||||
]); | ]); | ||||
} | } | ||||
if (!empty($discount)) { | if (!empty($discount)) { | ||||
$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->setSetting('external_email', $user_email); | $user->setSettings([ | ||||
'external_email' => $user_email, | |||||
'first_name' => $code_data->first_name, | |||||
'last_name' => $code_data->last_name, | |||||
]); | |||||
// Remove the verification code | // Remove the verification code | ||||
$this->code->delete(); | $this->code->delete(); | ||||
DB::commit(); | DB::commit(); | ||||
return AuthController::logonResponse($user); | return AuthController::logonResponse($user); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 111 Lines • Show Last 20 Lines |