perms_model::file_get_user_role()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
class perms_model extends Model {
4
5
    private $all_permissions = [
6
        'read_file',
7
        'write_file',
8
        'manage_user',
9
        'see_global_trash',
10
        'see_history_detail'
11
    ];
12
13
    private $implied_permissions = [
14
        "write_file" => ["read_file"],
15
    ];
16
17
    private $default_rp = [
18
        "none" => [],
19
        "reader" => ['read_file'],
20
        "author" => ['write_file', 'see_history_detail'],
21
        "admin" => ['write_file', 'manage_user', 'see_global_trash', 'see_history_detail']
22
    ];
23
24
    private $role_permissions = [];
25
26
    function __construct() {
27
        $this->load_model("jugaad_model");
28
        $this->load_model("auth_model");
29
30
        $this->load_library("db_lib");
31
32
        $this->default_rp["superadmin"] = $this->all_permissions;
33
34
        $this->role_permissions = $this->get_role_info();
35
    }
36
37
    private function extend_permissions($permissions) {
38
        $e_perms = [];
39
40
        foreach ($permissions as $perm) {
41
            array_push($e_perms, $perm);
42
            if (isset($this->implied_permissions[$perm])) {
43
                $e_perms = array_merge($e_perms, $this->implied_permissions[$perm]);
44
            }
45
        }
46
47
        return array_unique($e_perms);
48
    }
49
50
    function get_role_info() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
        $perms = [];
52
53
        foreach ($this->default_rp as $role => $value) {
54
            $perms[$role] = $this->extend_permissions($value);
55
        }
56
57
        return $perms;
58
    }
59
60
    function add_user_role($file_id, $user, $role) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
        // Add admin user for a file
62
        if ($file_id === false || !$user) {
63
            return false;
64
        }
65
        return $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
67
            "INSERT INTO `file_permissions` (`file_id`, `user`, `role`) VALUES (?, ?, ?)",
68
            "iss",
69
            [$file_id, $user, $role],
70
            false
71
        );
72
    }
73
74
    function remove_user_role($file_id, $user) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
        // Remove admin user for a file
76
        if ($file_id === false || !$user) {
77
            return false;
78
        }
79
        return $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
81
            "DELETE FROM `file_permissions` WHERE `file_id`=? AND `user`=?",
82
            "is",
83
            [$file_id, $user],
84
            false
85
        );
86
    }
87
88
    function get_default_role($file_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
        // Get default role for a file
90
        if ($file_id === false) {
91
            return false;
92
        }
93
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
95
            "SELECT `default_role` FROM `files` WHERE `id`=?",
96
            "i",
97
            [$file_id]
98
        );
99
        if (!$stmt) {
100
            return false;
101
        }
102
        if ($row = $stmt->get_result()->fetch_row()) {
103
            return $row[0];
104
        }
105
        return false;
106
    }
107
108
    function set_default_role($file_id, $role) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
        // Set default role for a file
110
        if ($file_id === false) {
111
            return false;
112
        }
113
        return $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
115
            "UPDATE `files` SET `default_role`=? WHERE `id`=?",
116
            "si",
117
            [$role, $file_id],
118
            false
119
        );
120
    }
121
122
    private function file_get_user_role($file_id, $user) {
123
        // Check permissions for a single file
124
        if ($file_id === false) {
125
            return false;
126
        }
127
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
128
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
129
            "SELECT `role` FROM `file_permissions` WHERE `file_id`=? AND `user`=?",
130
            "is",
131
            [$file_id, $user]
132
        );
133
        if (!$stmt) {
134
            return false;
135
        }
136
        if ($row = $stmt->get_result()->fetch_row()) {
137
            return $row[0];
138
        }
139
        return false;
140
    }
141
142
    function get_user_role($file_id, $user) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
143
        // Check permissions for file with inherited permissions
144
        if ($this->auth_model->is_admin($user)) {
0 ignored issues
show
Bug Best Practice introduced by
The property auth_model does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
145
            return 'superadmin';
146
        }
147
148
        if ($file_id === false) {
149
            return false;
150
        }
151
        $orig_file_id = $file_id;
152
        do {
153
            $file = $this->jugaad_model->get_file($file_id);
0 ignored issues
show
Bug Best Practice introduced by
The property jugaad_model does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
154
            if ($file === false) {
155
                return false;
156
            }
157
            $role = $this->file_get_user_role($file_id, $user);
158
            if ($role !== false) {
159
                return $role;
160
            }
161
            $file_id = $file['parent'];
162
        } while ($file_id >= 0);
163
164
        return $this->get_default_role($orig_file_id);
165
    }
166
167
    private function file_get_users($file_id) {
168
        // Get list of privileged users for a single file
169
        if ($file_id === false) {
170
            return false;
171
        }
172
        $stmt = $this->db_lib->prepared_execute(
0 ignored issues
show
Bug Best Practice introduced by
The property db_lib does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
173
            $this->DB->jugaad,
0 ignored issues
show
Bug Best Practice introduced by
The property $DB is declared private in Model. Since you implement __get, consider adding a @property or @property-read.
Loading history...
174
            "SELECT `file_id`, `user`, `role` FROM `file_permissions` WHERE `file_id`=?",
175
            "i",
176
            [$file_id]
177
        );
178
        if (!$stmt) {
179
            return false;
180
        }
181
        $user_list = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
182
        return $user_list;
183
    }
184
185
    function get_user_list($file_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
186
        // Get list of privileged users for file with inherited permissions
187
        if ($file_id === false) {
188
            return false;
189
        }
190
        $user_list = [];
191
        do {
192
            $file = $this->jugaad_model->get_file($file_id);
0 ignored issues
show
Bug Best Practice introduced by
The property jugaad_model does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
193
            if ($file === false) {
194
                return false;
195
            }
196
            $file_users = $this->file_get_users($file_id);
197
            foreach ($file_users as $user) {
198
                if (!isset($user_list[$user['user']])) {
199
                    $user_list[$user['user']] = $user;
200
                }
201
            }
202
            $file_id = $file['parent'];
203
        } while ($file_id >= 0);
204
205
        return $user_list;
206
    }
207
208
    function get_role_permissions($role) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
209
        if ($role === false) {
210
            return false;
211
        }
212
213
        $perm_list = $this->role_permissions[$role];
214
215
        $user_can = [];
216
217
        foreach ($this->all_permissions as $perm) {
218
            $user_can[$perm] = false;
219
            if (in_array($perm, $perm_list)) {
220
                $user_can[$perm] = true;
221
            }
222
        }
223
224
        return $user_can;
225
    }
226
227
    function get_permissions($file_id, $user) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
228
        if ($file_id === false) {
229
            return false;
230
        }
231
232
        // Return false if file does not exists
233
        if (false === $this->jugaad_model->get_file_type($file_id)) {
0 ignored issues
show
Bug Best Practice introduced by
The property jugaad_model does not exist on perms_model. Since you implemented __get, consider adding a @property annotation.
Loading history...
234
            return false;
235
        }
236
237
        $role = $this->get_user_role($file_id, $user);
238
        $user_can = $this->get_role_permissions($role);
239
240
        return $user_can;
241
    }
242
243
}
244