diff --git a/app/Http/Controllers/Auth/ContactRegisterController.php b/app/Http/Controllers/Auth/ContactRegisterController.php index 71af89c4a5fa..c74ea40c7c99 100644 --- a/app/Http/Controllers/Auth/ContactRegisterController.php +++ b/app/Http/Controllers/Auth/ContactRegisterController.php @@ -18,11 +18,13 @@ class ContactRegisterController extends Controller $this->middleware(['guest', 'contact.register']); } - public function showRegisterForm(string $company_key) + public function showRegisterForm(string $company_key = '') { - $company = Company::where('company_key', $company_key)->firstOrFail(); + $key = request()->has('key') ? request('key') : $company_key; - return render('auth.register', compact(['company'])); + $company = Company::where('company_key', $key)->firstOrFail(); + + return render('auth.register', ['company' => $company]); } public function register(RegisterRequest $request) diff --git a/app/Http/Middleware/ContactRegister.php b/app/Http/Middleware/ContactRegister.php index b22ab6ecd75c..33c9549a781a 100644 --- a/app/Http/Middleware/ContactRegister.php +++ b/app/Http/Middleware/ContactRegister.php @@ -2,7 +2,9 @@ namespace App\Http\Middleware; +use App\Models\Account; use App\Models\Company; +use App\Utils\Ninja; use Closure; use Illuminate\Http\Request; @@ -11,35 +13,45 @@ class ContactRegister /** * Handle an incoming request. * - * @param Request $request + * @param Request $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next) { - /* - * Notes: - * - * 1. If request supports subdomain (for hosted) check domain and continue request. - * 2. If request doesn't support subdomain and doesn' have company_key, abort - * 3. firstOrFail() will abort with 404 if company with company_key wasn't found. - * 4. Abort if setting isn't enabled. - */ - + // Resolving based on subdomain. Used in version 5 hosted platform. if ($request->subdomain) { $company = Company::where('subdomain', $request->subdomain)->firstOrFail(); abort_unless($company->getSetting('enable_client_registration'), 404); + $request->merge(['key' => $company->company_key]); + return $next($request); } - abort_unless($request->company_key, 404); + // For self-hosted platforms with multiple companies, resolving is done using company key + // if it doesn't resolve using a domain. + if ($request->route()->parameter('company_key') && Ninja::isSelfHost()) { + $company = Company::where('company_key', $request->company_key)->firstOrFail(); - $company = Company::where('company_key', $request->company_key)->firstOrFail(); + abort_unless($company->client_can_register, 404); - abort_unless($company->client_can_register, 404); + return $next($request); + } - return $next($request); + // As a fallback for self-hosted, it will use default company in the system + // if key isn't provided in the url. + if (!$request->route()->parameter('company_key') && Ninja::isSelfHost()) { + $company = Account::first()->default_company; + + abort_unless($company->client_can_register, 404); + + $request->merge(['key' => $company->company_key]); + + return $next($request); + } + + return abort(404); } }