Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

289 lines
7.5 KiB
PHP
Raw Permalink Normal View History

2018-05-09 08:05:17 -05:00
<?php
namespace App\Models;
2019-02-15 09:00:07 -06:00
use App\Events\UserCreated;
2018-09-11 07:51:35 -05:00
use Illuminate\Database\Eloquent\Builder;
2020-11-03 17:18:31 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
2018-05-09 08:05:17 -05:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
2018-09-11 07:51:35 -05:00
use LibreNMS\Authentication\LegacyAuth;
2021-10-06 07:29:47 -05:00
use NotificationChannels\WebPush\HasPushSubscriptions;
2019-12-30 12:11:26 +01:00
use Permissions;
use Silber\Bouncer\BouncerFacade as Bouncer;
use Silber\Bouncer\Database\HasRolesAndAbilities;
2018-05-09 08:05:17 -05:00
2021-04-01 00:35:19 +02:00
/**
* @method static \Database\Factories\UserFactory factory(...$parameters)
*/
2018-05-09 08:05:17 -05:00
class User extends Authenticatable
{
use HasRolesAndAbilities, Notifiable, HasFactory, HasPushSubscriptions;
2018-05-09 08:05:17 -05:00
protected $primaryKey = 'user_id';
protected $fillable = ['realname', 'username', 'email', 'descr', 'can_modify_passwd', 'auth_type', 'auth_id', 'enabled'];
2018-05-09 08:05:17 -05:00
protected $hidden = ['password', 'remember_token', 'pivot'];
2018-09-11 07:51:35 -05:00
protected $attributes = [ // default values
'descr' => '',
'realname' => '',
'email' => '',
];
2019-02-15 09:00:07 -06:00
protected $dispatchesEvents = [
'created' => UserCreated::class,
];
2018-05-09 08:05:17 -05:00
2019-04-22 19:01:39 -05:00
protected $casts = [
'realname' => 'string',
'descr' => 'string',
'email' => 'string',
'can_modify_passwd' => 'integer',
];
2022-08-24 00:33:28 +02:00
public function toFlare(): array
{
return $this->only(['auth_type', 'enabled']);
2022-08-24 00:33:28 +02:00
}
2018-05-09 08:05:17 -05:00
// ---- Helper Functions ----
/**
* Test if this user has global read access
*
* @return bool
*/
public function hasGlobalRead()
{
return $this->isA('admin', 'global-read');
2018-05-09 08:05:17 -05:00
}
/**
2018-09-11 07:51:35 -05:00
* Test if this user has global admin access
2018-05-09 08:05:17 -05:00
*
* @return bool
*/
2018-09-11 07:51:35 -05:00
public function hasGlobalAdmin()
2018-05-09 08:05:17 -05:00
{
return $this->isA('admin', 'demo');
2018-05-09 08:05:17 -05:00
}
2018-09-11 07:51:35 -05:00
/**
* Test if the User is an admin.
*
* @return bool
*/
public function isAdmin()
{
return $this->isA('admin');
2018-09-11 07:51:35 -05:00
}
/**
* Test if this user is the demo user
*
* @return bool
*/
public function isDemo()
{
return $this->isA('demo');
2018-09-11 07:51:35 -05:00
}
2018-05-09 08:05:17 -05:00
/**
* Check if this user has access to a device
*
2021-09-08 23:35:56 +02:00
* @param Device|int $device can be a device Model or device id
2018-05-09 08:05:17 -05:00
* @return bool
*/
public function canAccessDevice($device)
{
2019-12-30 12:11:26 +01:00
return $this->hasGlobalRead() || Permissions::canAccessDevice($device, $this->user_id);
2018-05-09 08:05:17 -05:00
}
2019-02-15 09:00:07 -06:00
/**
* Helper function to hash passwords before setting
*
2021-09-08 23:35:56 +02:00
* @param string $password
2019-02-15 09:00:07 -06:00
*/
public function setPassword($password)
{
$this->attributes['password'] = $password ? Hash::make($password) : null;
2019-02-15 09:00:07 -06:00
}
/**
* Set roles and remove extra roles, optionally creating non-existent roles, flush permissions cache for this user if roles changed
*/
public function setRoles(array $roles, bool $create = false): void
{
if ($roles != $this->getRoles()) {
if ($create) {
$this->assign($roles);
}
Bouncer::sync($this)->roles($roles);
Bouncer::refresh($this);
}
}
2019-04-22 19:01:39 -05:00
/**
* Check if the given user can set the password for this user
*
2021-09-08 23:35:56 +02:00
* @param User $user
2019-04-22 19:01:39 -05:00
* @return bool
*/
public function canSetPassword($user)
{
if ($user && LegacyAuth::get()->canUpdatePasswords()) {
if ($user->isAdmin()) {
return true;
}
return $user->is($this) && $this->can_modify_passwd;
}
return false;
}
2021-10-06 07:29:47 -05:00
/**
* Checks if this user has a browser push notification transport configured.
*
* @return bool
*/
public function hasBrowserPushTransport(): bool
{
$user_id = \Auth::id();
return AlertTransport::query()
->where('transport_type', 'browserpush')
->where('transport_config', 'regexp', "\"user\":\"(0|$user_id)\"")
->exists();
}
2018-09-11 07:51:35 -05:00
// ---- Query scopes ----
/**
* This restricts the query to only users that match the current auth method
* It is not needed when using user_id, but should be used for username and auth_id
*
2021-09-08 23:35:56 +02:00
* @param Builder $query
2018-09-11 07:51:35 -05:00
* @return Builder
*/
public function scopeThisAuth($query)
{
// find user including ones where we might not know the auth type
$type = LegacyAuth::getType();
return $query->where(function ($query) use ($type) {
$query->where('auth_type', $type)
->orWhereNull('auth_type')
->orWhere('auth_type', '');
});
}
2020-06-21 21:25:00 -05:00
public function scopeAdminOnly($query)
{
$query->whereIs('admin');
2020-06-19 00:17:18 -05:00
}
2019-04-22 19:01:39 -05:00
// ---- Accessors/Mutators ----
public function setRealnameAttribute($realname)
{
$this->attributes['realname'] = (string) $realname;
}
public function setDescrAttribute($descr)
{
$this->attributes['descr'] = (string) $descr;
}
public function setEmailAttribute($email)
{
$this->attributes['email'] = (string) $email;
}
public function setCanModifyPasswdAttribute($modify)
{
$this->attributes['can_modify_passwd'] = $modify ? 1 : 0;
}
public function setEnabledAttribute($enable)
{
$this->attributes['enabled'] = $enable ? 1 : 0;
}
2019-12-30 12:11:26 +01:00
public function getDevicesAttribute()
{
// pseudo relation
if (! array_key_exists('devices', $this->relations)) {
$this->setRelation('devices', $this->devices()->get());
}
2020-09-21 14:54:51 +02:00
2019-12-30 12:11:26 +01:00
return $this->getRelation('devices');
}
2018-05-09 08:05:17 -05:00
// ---- Define Relationships ----
2021-10-06 07:29:47 -05:00
public function apiTokens(): HasMany
2019-07-29 16:32:37 -05:00
{
2021-10-06 07:29:47 -05:00
return $this->hasMany(\App\Models\ApiToken::class, 'user_id', 'user_id');
2019-07-29 16:32:37 -05:00
}
public function bills(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Bill::class, 'bill_perms', 'user_id', 'bill_id');
}
2018-05-09 08:05:17 -05:00
public function devices()
{
2019-12-30 12:11:26 +01:00
// pseudo relation
return Device::query()->when(! $this->hasGlobalRead(), function ($query) {
return $query->whereIntegerInRaw('device_id', Permissions::devicesForUser($this));
2019-12-30 12:11:26 +01:00
});
}
public function devicesOwned(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Device::class, 'devices_perms', 'user_id', 'device_id');
}
public function deviceGroups(): BelongsToMany
2019-12-30 12:11:26 +01:00
{
return $this->belongsToMany(\App\Models\DeviceGroup::class, 'devices_group_perms', 'user_id', 'device_group_id');
2018-05-09 08:05:17 -05:00
}
public function ports()
{
if ($this->hasGlobalRead()) {
return Port::query();
} else {
//FIXME we should return all ports for a device if the user has been given access to the whole device.
return $this->portsOwned();
2018-05-09 08:05:17 -05:00
}
}
public function portsOwned(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Port::class, 'ports_perms', 'user_id', 'port_id');
}
public function dashboards(): HasMany
2018-05-09 08:05:17 -05:00
{
return $this->hasMany(\App\Models\Dashboard::class, 'user_id');
2018-05-09 08:05:17 -05:00
}
2021-10-06 07:29:47 -05:00
public function notificationAttribs(): HasMany
{
return $this->hasMany(NotificationAttrib::class, 'user_id');
}
public function preferences(): HasMany
2018-09-11 07:51:35 -05:00
{
return $this->hasMany(\App\Models\UserPref::class, 'user_id');
2018-09-11 07:51:35 -05:00
}
public function widgets(): HasMany
2018-05-09 08:05:17 -05:00
{
return $this->hasMany(\App\Models\UserWidget::class, 'user_id');
2018-05-09 08:05:17 -05:00
}
}