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\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\User; | use App\User; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
Show All 19 Lines | class SignupController extends Controller | ||||
* @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 plans(Request $request) | public function plans(Request $request) | ||||
{ | { | ||||
$plans = []; | $plans = []; | ||||
Plan::all()->map(function ($plan) use (&$plans) { | // Use reverse order just to have individual on left, group on right ;) | ||||
Plan::select()->orderByDesc('title')->get()->map(function ($plan) use (&$plans) { | |||||
$plans[] = [ | $plans[] = [ | ||||
'title' => $plan->title, | 'title' => $plan->title, | ||||
'name' => $plan->name, | 'name' => $plan->name, | ||||
'button' => __('app.planbutton', ['plan' => $plan->name]), | 'button' => __('app.planbutton', ['plan' => $plan->name]), | ||||
'description' => $plan->description, | 'description' => $plan->description, | ||||
]; | ]; | ||||
}); | }); | ||||
Show All 14 Lines | class SignupController extends Controller | ||||
{ | { | ||||
// Check required fields | // Check required fields | ||||
$v = Validator::make( | $v = Validator::make( | ||||
$request->all(), | $request->all(), | ||||
[ | [ | ||||
'email' => 'required', | 'email' => 'required', | ||||
'name' => 'required|max:512', | 'name' => 'required|max:512', | ||||
'plan' => 'nullable|alpha_num|max:128', | 'plan' => 'nullable|alpha_num|max:128', | ||||
'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); | ||||
} | } | ||||
// Validate user email (or phone) | // Validate user email (or phone) | ||||
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); | return response()->json(['status' => 'error', 'errors' => ['email' => $error]], 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, | 'name' => $request->name, | ||||
'plan' => $request->plan, | 'plan' => $request->plan, | ||||
'voucher' => $request->voucher, | |||||
] | ] | ||||
]); | ]); | ||||
// Send email/sms message | // Send email/sms message | ||||
if ($is_phone) { | if ($is_phone) { | ||||
SignupVerificationSMS::dispatch($code); | SignupVerificationSMS::dispatch($code); | ||||
} else { | } else { | ||||
SignupVerificationEmail::dispatch($code); | SignupVerificationEmail::dispatch($code); | ||||
Show All 37 Lines | public function verify(Request $request) | ||||
} | } | ||||
// For signup last-step mode remember the code object, so we can delete it | // For signup last-step mode remember the code object, so we can delete it | ||||
// with single SQL query (->delete()) instead of two (::destroy()) | // with single SQL query (->delete()) instead of two (::destroy()) | ||||
$this->code = $code; | $this->code = $code; | ||||
$has_domain = $this->getPlan()->hasDomain(); | $has_domain = $this->getPlan()->hasDomain(); | ||||
// Return user name and email/phone 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'], | 'name' => $code->data['name'], | ||||
'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. | ||||
* | * | ||||
* @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 signup(Request $request) | public function signup(Request $request) | ||||
{ | { | ||||
// Validate input | // Validate input | ||||
$v = Validator::make( | $v = Validator::make( | ||||
$request->all(), | $request->all(), | ||||
[ | [ | ||||
'login' => 'required|min:2', | 'login' => 'required|min:2', | ||||
'password' => 'required|min:4|confirmed', | 'password' => 'required|min:4|confirmed', | ||||
'domain' => 'required', | 'domain' => 'required', | ||||
'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); | ||||
} | } | ||||
// 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; | ||||
} | } | ||||
// Find the voucher discount | |||||
if ($request->voucher) { | |||||
$discount = Discount::where('code', \strtoupper($request->voucher)) | |||||
->where('active', true)->first(); | |||||
if (!$discount) { | |||||
$errors = ['voucher' => \trans('validation.voucherinvalid')]; | |||||
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 = $request->domain; | $domain = $request->domain; | ||||
// Validate login | // Validate login | ||||
Show All 24 Lines | public function signup(Request $request) | ||||
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)) { | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->discount()->associate($discount); | |||||
$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->setSetting('external_email', $user_email); | ||||
// Remove the verification code | // Remove the verification code | ||||
$this->code->delete(); | $this->code->delete(); | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |