Issues (2963)

app/Models/BaseModel.php (3 issues)

1
<?php
2
/**
3
 * BaseModel.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Models;
27
28
use Illuminate\Database\Eloquent\Builder;
29
use Illuminate\Database\Eloquent\Model;
30
31
abstract class BaseModel extends Model
32
{
33
    /**
34
     * Check if query is already joined with a table
35
     *
36
     * @param  Builder  $query
37
     * @param  string  $table
38
     * @return bool
39
     */
40
    public static function isJoined($query, $table)
41
    {
42
        $joins = $query->getQuery()->joins;
43
        if ($joins == null) {
44
            return false;
45
        }
46
        foreach ($joins as $join) {
47
            if ($join->table == $table) {
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * Helper function to determine if user has access based on device permissions
57
     *
58
     * @param  Builder  $query
59
     * @param  User  $user
60
     * @param  string  $table
61
     * @return Builder
62
     */
63
    protected function hasDeviceAccess($query, User $user, $table = null)
64
    {
65
        if ($user->hasGlobalRead()) {
66
            return $query;
67
        }
68
69
        if (is_null($table)) {
70
            $table = $this->getTable();
71
        }
72
73
        return $query->whereIn("$table.device_id", \Permissions::devicesForUser($user));
0 ignored issues
show
The method devicesForUser() does not exist on App\Facades\Permissions. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return $query->whereIn("$table.device_id", \Permissions::/** @scrutinizer ignore-call */ devicesForUser($user));
Loading history...
Bug Best Practice introduced by
The expression return $query->whereIn($...:devicesForUser($user)) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
74
    }
75
76
    /**
77
     * Helper function to determine if user has access based on port permissions
78
     *
79
     * @param  Builder  $query
80
     * @param  User  $user
81
     * @param  string  $table
82
     * @return Builder
83
     */
84
    protected function hasPortAccess($query, User $user, $table = null)
85
    {
86
        if ($user->hasGlobalRead()) {
87
            return $query;
88
        }
89
90
        if (is_null($table)) {
91
            $table = $this->getTable();
92
        }
93
94
        return $query->where(function ($query) use ($table, $user) {
95
            return $query->whereIn("$table.port_id", \Permissions::portsForUser($user))
0 ignored issues
show
The method portsForUser() does not exist on App\Facades\Permissions. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            return $query->whereIn("$table.port_id", \Permissions::/** @scrutinizer ignore-call */ portsForUser($user))
Loading history...
96
                ->orWhereIn("$table.device_id", \Permissions::devicesForUser($user));
97
        });
98
    }
99
}
100