112 lines
3 KiB
PHP
112 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable;
|
|
|
|
protected $primaryKey = 'user_id';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'role', // default, employee, manage
|
|
'password'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
public function permissions()
|
|
{
|
|
// get permissions for this user
|
|
// note: permissions are linked to users by the user_permissions table
|
|
// the permissions are in the permissions table
|
|
// only return valid permissions
|
|
return $this->belongsToMany('App\Models\Permission', 'user_permissions', 'user_id', 'permission_id')->where(function ($query) {
|
|
// $query->where('user_permission_start', '<=', now())->where(function ($query) {
|
|
// $query->where('user_permission_end', '>=', now())->orWhereNull('user_permission_end');
|
|
// });
|
|
});
|
|
}
|
|
|
|
public function hasPermission($permission): bool
|
|
{
|
|
if ($this->role == 'admin') {
|
|
return true;
|
|
}
|
|
if ($this->permissions()->where('permission_name', $permission)->first()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function atleast($role): bool
|
|
{
|
|
$hierarchy = [
|
|
'default' => 0,
|
|
'employee' => 1,
|
|
'manage' => 2,
|
|
'admin' => 3
|
|
];
|
|
return $hierarchy[$this->role] >= $hierarchy[$role];
|
|
}
|
|
|
|
public static function find($id)
|
|
{
|
|
return User::all()->where('user_id', $id)->first();
|
|
}
|
|
|
|
public function cinemas()
|
|
{
|
|
// check user_assignments table for all assignments for this user
|
|
if ($this->atleast('admin')) {
|
|
// if user is a manager or admin, return all cinemas
|
|
return Cinema::all();
|
|
} else {
|
|
// if user is an employee, return only the cinemas they are assigned to
|
|
return $this->belongsToMany('App\Models\Cinema', 'user_assignments', 'user_id', 'cinema_id')->get();
|
|
}
|
|
}
|
|
|
|
public function orders()
|
|
{
|
|
return $this->hasMany('App\Models\Order', 'user_id', 'user_id');
|
|
}
|
|
|
|
public function address()
|
|
{
|
|
return $this->hasOne('App\Models\Address', 'address_id', 'address_id');
|
|
}
|
|
|
|
}
|