Files
librenms-librenms/app/Models/AlertRule.php
T

93 lines
2.6 KiB
PHP
Raw Normal View History

2018-05-09 08:05:17 -05:00
<?php
/**
* app/Models/AlertRule.php
*
* Model for access to alert_rules table data
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-09 08:05:17 -05:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2018-05-09 08:05:17 -05:00
* @copyright 2016 Neil Lathwood
* @author Neil Lathwood <neil@lathwood.co.uk>
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
2021-03-30 11:14:34 +02:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
2020-05-24 04:14:36 +02:00
use LibreNMS\Enum\AlertState;
2018-05-09 08:05:17 -05:00
class AlertRule extends BaseModel
{
public $timestamps = false;
// ---- Query scopes ----
/**
2021-03-24 15:13:43 +01:00
* @param Builder<AlertRule> $query
2018-05-09 08:05:17 -05:00
* @return Builder
*/
public function scopeEnabled($query)
{
return $query->where('alert_rules.disabled', 0);
2018-05-09 08:05:17 -05:00
}
/**
* Scope for only alert rules that are currently in alarm
*
2021-03-24 15:13:43 +01:00
* @param Builder<AlertRule> $query
2018-05-09 08:05:17 -05:00
* @return Builder
*/
public function scopeIsActive($query)
{
return $query->enabled()
->join('alerts', 'alerts.rule_id', 'alert_rules.id')
2020-05-24 04:14:36 +02:00
->whereNotIn('alerts.state', [AlertState::CLEAR, AlertState::ACKNOWLEDGED, AlertState::RECOVERED]);
2018-05-09 08:05:17 -05:00
}
/**
* Scope to filter rules for devices permitted to user
* (do not use for admin and global read-only users)
*
2021-03-24 15:13:43 +01:00
* @param Builder<AlertRule> $query
2018-05-09 08:05:17 -05:00
* @param User $user
* @return mixed
*/
public function scopeHasAccess($query, User $user)
{
if ($user->hasGlobalRead()) {
return $query;
}
2020-09-21 14:54:51 +02:00
if (! $this->isJoined($query, 'alerts')) {
2018-05-09 08:05:17 -05:00
$query->join('alerts', 'alerts.rule_id', 'alert_rules.id');
}
return $this->hasDeviceAccess($query, $user, 'alerts');
}
// ---- Define Relationships ----
2021-03-30 11:14:34 +02:00
public function alerts(): HasMany
2018-05-09 08:05:17 -05:00
{
return $this->hasMany(\App\Models\Alert::class, 'rule_id');
2018-05-09 08:05:17 -05:00
}
2021-03-30 11:14:34 +02:00
public function devices(): BelongsToMany
2018-05-09 08:05:17 -05:00
{
return $this->belongsToMany(\App\Models\Device::class, 'alert_device_map', 'device_id', 'device_id');
2018-05-09 08:05:17 -05:00
}
}