cineflex/app/Models/User.php

108 lines
2.8 KiB
PHP
Raw Normal View History

2022-11-23 09:32:34 +01:00
<?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'
2022-11-23 09:32:34 +01:00
];
/**
* 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) {
2023-02-02 08:17:38 +01:00
// $query->where('user_permission_start', '<=', now())->where(function ($query) {
// $query->where('user_permission_end', '>=', now())->orWhereNull('user_permission_end');
// });
});
}
2023-02-02 08:17:38 +01:00
public function hasPermission($permission): bool
{
2023-02-02 08:17:38 +01:00
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,
2023-02-02 08:17:38 +01:00
'manage' => 2,
'admin' => 3
];
return $hierarchy[$this->role] >= $hierarchy[$role];
}
public static function find($id)
{
return User::all()->where('user_id', $id)->first();
}
2022-11-23 09:32:34 +01:00
2023-02-02 08:17:38 +01:00
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');
}
2022-11-23 09:32:34 +01:00
}