1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Client registration logic

This commit is contained in:
Benjamin Beganović 2020-06-22 10:26:48 +02:00
parent 6f34e083ae
commit 871d1057c7
2 changed files with 43 additions and 23 deletions

View File

@ -2,6 +2,8 @@
namespace App\Http\Controllers\Auth;
use App\Factory\ClientContactFactory;
use App\Factory\ClientFactory;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\RegisterRequest;
use App\Models\Client;
@ -24,33 +26,37 @@ class ContactRegisterController extends Controller
public function register(RegisterRequest $request)
{
if ($request->subdomain) {
$company = Company::where('subdomain', $request->subdomain)->firstOrFail();
}
$request->merge(['company' => $request->company()]);
if ($request->company_key) {
$company = Company::where('company_key', $request->company_key)->firstOrFail();
}
$client = factory(Client::class)->create([
'user_id' => $company->owner()->id,
'company_id' => $company->id
]);
$client_contact = ClientContact::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'email' => $request->email,
'company_id' => $company->id,
'password' => Hash::make($request->password),
'client_id' => $client->id,
'user_id' => $company->owner()->id,
'is_primary' => true,
'contact_key' => \Illuminate\Support\Str::random(40),
]);
$client = $this->getClient($request->all());
$client_contact = $this->getClientContact($request->all(), $client);
Auth::guard('contact')->login($client_contact, true);
return redirect()->route('client.dashboard');
}
private function getClient(array $data)
{
$client = ClientFactory::create($data['company']->id, $data['company']->owner()->id);
$client->fill($data);
$client->save();
return $client;
}
public function getClientContact(array $data, Client $client)
{
$client_contact = ClientContactFactory::create($data['company']->id, $data['company']->owner()->id);
$client_contact->fill($data);
$client_contact->client_id = $client->id;
$client_contact->is_primary = true;
$client_contact->password = Hash::make($data['password']);
$client_contact->save();
return $client_contact;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Requests\ClientPortal;
use App\Models\Company;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
@ -31,4 +32,17 @@ class RegisterRequest extends FormRequest
'password' => ['required', 'string', 'min:6', 'confirmed'],
];
}
public function company()
{
if ($this->subdomain) {
return Company::where('subdomain', $this->subdomain)->firstOrFail();
}
if ($this->company_key) {
return Company::where('company_key', $this->company_key)->firstOrFail();
}
abort(404);
}
}