improved tenant identification
This commit is contained in:
@ -5,7 +5,9 @@ namespace App\Models\Auth;
|
||||
use App\Traits\Tenants;
|
||||
use App\Notifications\Auth\Reset;
|
||||
use App\Traits\Media;
|
||||
use App\Traits\Users;
|
||||
use Date;
|
||||
use Illuminate\Contracts\Translation\HasLocalePreference;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@ -14,9 +16,9 @@ use Kyslik\ColumnSortable\Sortable;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
|
||||
|
||||
class User extends Authenticatable
|
||||
class User extends Authenticatable implements HasLocalePreference
|
||||
{
|
||||
use HasFactory, LaratrustUserTrait, Notifiable, SearchString, SoftDeletes, Sortable, Media, Tenants;
|
||||
use HasFactory, LaratrustUserTrait, Notifiable, SearchString, SoftDeletes, Sortable, Media, Tenants, Users;
|
||||
|
||||
protected $table = 'users';
|
||||
|
||||
@ -193,20 +195,39 @@ class User extends Authenticatable
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert tax to Array.
|
||||
* Attach company_ids attribute to model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setCompanyIds()
|
||||
{
|
||||
$this->setAttribute('company_ids', $this->companies->pluck('id')->toArray());
|
||||
$company_ids = $this->withoutEvents(function () {
|
||||
return $this->companies->pluck('id')->toArray();
|
||||
});
|
||||
|
||||
$this->setAttribute('company_ids', $company_ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach company_ids attribute from model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unsetCompanyIds()
|
||||
{
|
||||
$this->offsetUnset('company_ids');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's preferred locale.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function preferredLocale()
|
||||
{
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new factory instance for the model.
|
||||
*
|
||||
|
@ -2,11 +2,16 @@
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Events\Common\CompanyForgettingCurrent;
|
||||
use App\Events\Common\CompanyForgotCurrent;
|
||||
use App\Events\Common\CompanyMadeCurrent;
|
||||
use App\Events\Common\CompanyMakingCurrent;
|
||||
use App\Models\Document\Document;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Media;
|
||||
use App\Traits\Tenants;
|
||||
use App\Traits\Transactions;
|
||||
use App\Utilities\Overrider;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Kyslik\ColumnSortable\Sortable;
|
||||
@ -435,4 +440,82 @@ class Company extends Eloquent
|
||||
{
|
||||
return $this->getMedia('company_logo')->last();
|
||||
}
|
||||
|
||||
public function makeCurrent()
|
||||
{
|
||||
if ($this->isCurrent()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
static::forgetCurrent();
|
||||
|
||||
event(new CompanyMakingCurrent($this));
|
||||
|
||||
// Bind to container
|
||||
app()->instance(static::class, $this);
|
||||
|
||||
// Set session for backward compatibility @deprecated
|
||||
//session(['company_id' => $this->id]);
|
||||
|
||||
// Load settings
|
||||
setting()->setExtraColumns(['company_id' => $this->id]);
|
||||
setting()->forgetAll();
|
||||
setting()->load(true);
|
||||
|
||||
// Override settings and currencies
|
||||
Overrider::load('settings');
|
||||
Overrider::load('currencies');
|
||||
|
||||
event(new CompanyMadeCurrent($this));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isCurrent()
|
||||
{
|
||||
return optional(static::getCurrent())->id === $this->id;
|
||||
}
|
||||
|
||||
public function isNotCurrent()
|
||||
{
|
||||
return !$this->isCurrent();
|
||||
}
|
||||
|
||||
public static function getCurrent()
|
||||
{
|
||||
if (!app()->has(static::class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return app(static::class);
|
||||
}
|
||||
|
||||
public static function forgetCurrent()
|
||||
{
|
||||
$current = static::getCurrent();
|
||||
|
||||
if (is_null($current)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
event(new CompanyForgettingCurrent($current));
|
||||
|
||||
// Remove from container
|
||||
app()->forgetInstance(static::class);
|
||||
|
||||
// Unset session for backward compatibility @deprecated
|
||||
//session()->forget('company_id');
|
||||
|
||||
// Remove settings
|
||||
setting()->forgetAll();
|
||||
|
||||
event(new CompanyForgotCurrent($current));
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
public static function hasCurrent()
|
||||
{
|
||||
return static::getCurrent() !== null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user