2017-04-30 19:08:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Eloquent;
|
2017-04-30 22:07:58 +02:00
|
|
|
use App\Models\User;
|
2017-04-30 19:08:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExpenseCategory.
|
|
|
|
*/
|
2017-04-30 21:18:17 +02:00
|
|
|
class LookupUser extends LookupModel
|
2017-04-30 19:08:49 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'lookup_account_id',
|
|
|
|
'email',
|
2017-04-30 21:29:15 +02:00
|
|
|
'user_id',
|
2017-05-10 12:02:20 +02:00
|
|
|
'confirmation_code',
|
2017-05-10 17:18:48 +02:00
|
|
|
'oauth_user_key',
|
2017-05-14 11:11:38 +02:00
|
|
|
'referral_code',
|
2017-04-30 19:08:49 +02:00
|
|
|
];
|
|
|
|
|
2017-05-10 17:18:48 +02:00
|
|
|
public static function updateUser($accountKey, $user)
|
2017-05-01 09:19:27 +02:00
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
$lookupAccount = LookupAccount::whereAccountKey($accountKey)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
$lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id)
|
2017-05-10 17:18:48 +02:00
|
|
|
->whereUserId($user->id)
|
2017-05-01 09:19:27 +02:00
|
|
|
->firstOrFail();
|
|
|
|
|
2017-05-10 17:18:48 +02:00
|
|
|
$lookupUser->email = $user->email;
|
2017-06-20 20:35:01 +02:00
|
|
|
$lookupUser->confirmation_code = $user->confirmation_code ?: null;
|
2017-05-10 17:18:48 +02:00
|
|
|
$lookupUser->oauth_user_key = ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null;
|
2017-05-14 11:11:38 +02:00
|
|
|
$lookupUser->referral_code = $user->referral_code;
|
2017-05-01 09:19:27 +02:00
|
|
|
$lookupUser->save();
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:49:58 +02:00
|
|
|
public static function validateField($field, $value, $user = false)
|
2017-05-01 20:46:31 +02:00
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
2017-05-11 14:41:19 +02:00
|
|
|
$accountKey = $user ? $user->account->account_key : false;
|
2017-05-11 11:27:15 +02:00
|
|
|
|
2017-05-01 20:46:31 +02:00
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
2017-05-15 15:49:58 +02:00
|
|
|
$lookupUser = LookupUser::where($field, '=', $value)->first();
|
2017-05-01 20:46:31 +02:00
|
|
|
|
|
|
|
if ($user) {
|
2017-05-11 11:27:15 +02:00
|
|
|
$lookupAccount = LookupAccount::whereAccountKey($accountKey)->firstOrFail();
|
2017-05-01 20:46:31 +02:00
|
|
|
$isValid = ! $lookupUser || ($lookupUser->lookup_account_id == $lookupAccount->id && $lookupUser->user_id == $user->id);
|
|
|
|
} else {
|
|
|
|
$isValid = ! $lookupUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
|
|
|
|
return $isValid;
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:08:49 +02:00
|
|
|
}
|