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

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

116 lines
3.2 KiB
PHP
Raw Normal View History

2018-05-09 08:05:17 -05:00
<?php
/**
* BaseModel.php
*
* -Description-
*
* 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
2021-09-10 20:09:53 +02:00
*
2018-05-09 08:05:17 -05:00
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Models;
2019-03-19 08:14:01 -05:00
use Illuminate\Database\Eloquent\Builder;
2018-05-09 08:05:17 -05:00
use Illuminate\Database\Eloquent\Model;
abstract class BaseModel extends Model
{
/**
* Check if query is already joined with a table
*
2021-09-08 23:35:56 +02:00
* @param Builder $query
* @param string $table
2018-05-09 08:05:17 -05:00
* @return bool
*/
public static function isJoined($query, $table)
{
$joins = $query->getQuery()->joins;
if ($joins == null) {
return false;
}
foreach ($joins as $join) {
if ($join->table == $table) {
return true;
}
}
2020-09-21 14:54:51 +02:00
2018-05-09 08:05:17 -05:00
return false;
}
/**
* Helper function to determine if user has access based on device permissions
*
2021-09-08 23:35:56 +02:00
* @param Builder $query
* @param User $user
* @param string $table
2018-05-09 08:05:17 -05:00
* @return Builder
*/
protected function hasDeviceAccess($query, User $user, $table = null)
{
if ($user->hasGlobalRead()) {
return $query;
}
if (is_null($table)) {
$table = $this->getTable();
}
return $query->whereIntegerInRaw("$table.device_id", \Permissions::devicesForUser($user));
2018-05-09 08:05:17 -05:00
}
/**
* Helper function to determine if user has access based on port permissions
*
2021-09-08 23:35:56 +02:00
* @param Builder $query
* @param User $user
* @param string $table
2018-05-09 08:05:17 -05:00
* @return Builder
*/
protected function hasPortAccess($query, User $user, $table = null)
{
if ($user->hasGlobalRead()) {
return $query;
}
if (is_null($table)) {
$table = $this->getTable();
}
2019-03-19 08:14:01 -05:00
return $query->where(function ($query) use ($table, $user) {
return $query->whereIntegerInRaw("$table.port_id", \Permissions::portsForUser($user))
->orWhereIntegerInRaw("$table.device_id", \Permissions::devicesForUser($user));
2019-03-19 08:14:01 -05:00
});
2018-05-09 08:05:17 -05:00
}
2023-11-01 13:52:21 -05:00
public static function definedRelations(): array
{
$reflector = new \ReflectionClass(get_called_class());
return collect($reflector->getMethods())
->filter(
fn ($method) => ! empty($method->getReturnType()) &&
str_contains(
$method->getReturnType(),
'Illuminate\Database\Eloquent\Relations'
)
)
->pluck('name')
->all();
}
2018-05-09 08:05:17 -05:00
}