mega-commit: migrations, controllers, models, etc.

This commit is contained in:
Didier Slof 2022-12-08 09:30:07 +01:00
parent 9732135e90
commit 2c6745e812
Signed by: didier
GPG key ID: 01E71F18AA4398E5
70 changed files with 2124 additions and 400 deletions

View file

@ -14,10 +14,6 @@ class User extends Authenticatable
protected $primaryKey = 'user_id';
// User can be a customer or an employee
// If customer, then there'll be a customer_id in the users table
// If employee, then there'll be an employee_id in the users table
/**
* The attributes that are mass assignable.
*
@ -26,10 +22,8 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'password',
'customer_id', // if this is filled, then this user is a customer
'employee_id', // if this is filled, then this user is an employee
// if both are filled, then this user is both a customer and an employee
'role', // default, employee, manage
'password'
];
/**
@ -53,5 +47,43 @@ class User extends Authenticatable
'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 allowedTo($permission): bool
{
if($this->role === 'manage') {
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
];
return $hierarchy[$this->role] >= $hierarchy[$role];
}
public static function find($id)
{
return User::all()->where('user_id', $id)->first();
}
}