1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/ValidationRules/UniqueUserRule.php
David Bomba 62e2444a2c
Sign Up Scaffolding (#2453)
* Fix js dependencies

* Breadcrumb implementation

* Test for UniqueEmailRule Validation

* reduce length of account_key to prevent key too long error

* Fixes for travis - reduce user email length

* Reduce all unique field lengths to 100 to prevent key overflow

* Fix for Bank Model

* Prevent a user from registering multiple account with one email address when using multiple databases
2018-10-17 23:26:27 +11:00

43 lines
983 B
PHP

<?php
namespace App\Http\ValidationRules;
use App\Models\User;
use Illuminate\Contracts\Validation\Rule;
class UniqueUserRule implements Rule
{
public function passes($attribute, $value)
{
return $this->checkIfEmailExists($value);
}
public function message()
{
return trans('texts.email_already_register');
}
private function checkIfEmailExists($email) : bool
{
if (config('auth.providers.users.driver') == 'eloquent') //default eloquent = single DB
{
return User::where(['email' => $email])->get()->count() == 0 ?? false; // true -> 0 emails found / false -> >=1 emails found
}
//multi-db active
foreach (unserialize(MULTI_DBS) as $db)
{
if(User::on($db)->where(['email' => $email])->get()->count() >=1) // if user already exists, validation will fail
return false;
}
return true;
}
}