vpn.keks.cloud/app/Models/User.php
2020-11-08 03:07:10 +01:00

79 lines
2 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;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function hasPrivilege($name) {
$p = Privilege::query()->where("user_id", "=", $this->id)->first();
if($p->$name) {
return true;
}
return false;
}
public function setVPNRole(int $vpnID, string $accessName) {
$access = VPNAccess::query()->where("user_id", "=", $this->id)->where("vpn_id", "=", $vpnID)->first();
if($access === null) {
$access = new VPNAccess();
$access->user_id = $this->id;
$access->vpn_id = $vpnID;
}
$access->status = $accessName;
$access->saveOrFail();
}
public function getVPNRole(int $vpnId) {
$access = VPNAccess::query()->where("user_id", "=", $this->id)->where("vpn_id", "=", $vpnId)->first();
if($access === null) {
return "None";
}
return $access->status;
}
static public function findAdmins() {
$admins = Privilege::query()->where("admin", "=", true)->get();
$users = [];
foreach($admins as $admin) {
$users[] = User::query()->where("id", "=", $admin->user_id)->firstOrFail();
}
return $users;
}
}