2022-09-20 21:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2023-11-01 16:58:10 +01:00
|
|
|
use App\Http\Resources\UserResource;
|
2022-09-20 21:59:52 +02:00
|
|
|
use App\Models\User;
|
2024-07-04 17:21:36 +02:00
|
|
|
use App\Models\UserInvite;
|
2024-02-23 11:54:12 +01:00
|
|
|
use App\Models\Workspace;
|
2022-09-20 21:59:52 +02:00
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
|
use Illuminate\Foundation\Auth\RegistersUsers;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2022-10-19 10:18:07 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
class RegisterController extends Controller
|
|
|
|
|
{
|
|
|
|
|
use RegistersUsers;
|
|
|
|
|
|
2023-11-01 16:58:10 +01:00
|
|
|
private ?bool $appsumoLicense = null;
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware('guest');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The user has been registered.
|
|
|
|
|
*
|
2024-02-23 11:54:12 +01:00
|
|
|
* @param \App\User $user
|
2022-09-20 21:59:52 +02:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
protected function registered(Request $request, User $user)
|
|
|
|
|
{
|
|
|
|
|
if ($user instanceof MustVerifyEmail) {
|
|
|
|
|
return response()->json(['status' => trans('verification.sent')]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 16:58:10 +01:00
|
|
|
return response()->json(array_merge(
|
|
|
|
|
(new UserResource($user))->toArray($request),
|
|
|
|
|
[
|
|
|
|
|
'appsumo_license' => $this->appsumoLicense,
|
2024-02-23 11:54:12 +01:00
|
|
|
]
|
|
|
|
|
));
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a validator for an incoming registration request.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\Validation\Validator
|
|
|
|
|
*/
|
|
|
|
|
protected function validator(array $data)
|
|
|
|
|
{
|
|
|
|
|
return Validator::make($data, [
|
|
|
|
|
'name' => 'required|max:255',
|
2023-04-28 11:37:39 +02:00
|
|
|
'email' => 'required|email:filter|max:255|unique:users|indisposable',
|
2022-09-20 21:59:52 +02:00
|
|
|
'password' => 'required|min:6|confirmed',
|
2022-10-19 10:18:07 +02:00
|
|
|
'hear_about_us' => 'required|string',
|
2023-11-01 16:58:10 +01:00
|
|
|
'agree_terms' => ['required', Rule::in([true])],
|
|
|
|
|
'appsumo_license' => ['nullable'],
|
2024-07-04 17:21:36 +02:00
|
|
|
'invite_token' => ['nullable', 'string'],
|
2023-11-01 16:58:10 +01:00
|
|
|
], [
|
2024-02-23 11:54:12 +01:00
|
|
|
'agree_terms' => 'Please agree with the terms and conditions.',
|
2022-09-20 21:59:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new user instance after a valid registration.
|
|
|
|
|
*/
|
|
|
|
|
protected function create(array $data)
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
$this->checkRegistrationAllowed($data);
|
|
|
|
|
[$workspace, $role] = $this->getWorkspaceAndRole($data);
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
$user = User::create([
|
|
|
|
|
'name' => $data['name'],
|
|
|
|
|
'email' => strtolower($data['email']),
|
|
|
|
|
'password' => bcrypt($data['password']),
|
2024-02-23 11:54:12 +01:00
|
|
|
'hear_about_us' => $data['hear_about_us'],
|
2022-09-20 21:59:52 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Add relation with user
|
|
|
|
|
$user->workspaces()->sync([
|
|
|
|
|
$workspace->id => [
|
2024-07-04 17:21:36 +02:00
|
|
|
'role' => $role,
|
2024-02-23 11:54:12 +01:00
|
|
|
],
|
2022-09-20 21:59:52 +02:00
|
|
|
], false);
|
|
|
|
|
|
2023-11-01 16:58:10 +01:00
|
|
|
$this->appsumoLicense = AppSumoAuthController::registerWithLicense($user, $data['appsumo_license'] ?? null);
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
return $user;
|
|
|
|
|
}
|
2024-07-04 17:21:36 +02:00
|
|
|
|
|
|
|
|
private function checkRegistrationAllowed(array $data)
|
|
|
|
|
{
|
|
|
|
|
if (config('app.self_hosted') && !array_key_exists('invite_token', $data)) {
|
|
|
|
|
response()->json(['message' => 'Registration is not allowed in self host mode'], 400)->throwResponse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getWorkspaceAndRole(array $data)
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists('invite_token', $data)) {
|
|
|
|
|
return [
|
|
|
|
|
Workspace::create([
|
|
|
|
|
'name' => 'My Workspace',
|
|
|
|
|
'icon' => '🧪',
|
|
|
|
|
]),
|
|
|
|
|
User::ROLE_ADMIN
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userInvite = UserInvite::where('email', $data['email'])
|
|
|
|
|
->where('token', $data['invite_token'])
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (!$userInvite) {
|
|
|
|
|
response()->json(['message' => 'Invite token is invalid.'], 400)->throwResponse();
|
|
|
|
|
}
|
|
|
|
|
if ($userInvite->hasExpired()) {
|
|
|
|
|
response()->json(['message' => 'Invite token has expired.'], 400)->throwResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($userInvite->status == UserInvite::ACCEPTED_STATUS) {
|
|
|
|
|
response()->json(['message' => 'Invite is already accepted.'], 400)->throwResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userInvite->markAsAccepted();
|
|
|
|
|
return [
|
|
|
|
|
$userInvite->workspace,
|
|
|
|
|
$userInvite->role,
|
|
|
|
|
];
|
|
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|