File: class.roles.php

package info (click to toggle)
collabtive 2.0%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,708 kB
  • ctags: 27,425
  • sloc: php: 100,872; sh: 72; makefile: 50
file content (323 lines) | stat: -rw-r--r-- 9,980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
 * This class provides user roles
 *
 * @author Philipp Kiszka <info@o-dyn.de>
 * @name roles
 * @package Collabtive
 * @version 0.5
 * @link http://www.o-dyn.de
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
 */
class roles {
    function __construct()
    {
    }

    /**
     * Add a role
     * This method takes an array with permissions, serializes it to string, and saves it to the Database
     *
     * @param string $name Name of the role (for display)
     * @param array $projects Role permissions for projects
     * @param array $tasks Role permissions for tasks
     * @param array $milestones Role permissions for milestones
     * @param array $customers Role permissions for customers
     * @param array $messages Role permissions for messages
     * @param array $files Role permissions for files
     * @param array $timetracker Role permissions for timetracker
     * @param array $admin
     * @param array $chat
     * @return bool
     */
    function add($name, array $projects, array $tasks, array $milestones, array $messages, array $files, array $timetracker, array $chat, array $admin)
    {
        global $conn;
        $projects = serialize($projects);
        $tasks = serialize($tasks);
        $milestones = serialize($milestones);
        $messages = serialize($messages);
        $files = serialize($files);
        $timetracker = serialize($timetracker);
        $chat = serialize($chat);
        $admin = serialize($admin);

        $insStmt = $conn->prepare("INSERT INTO roles (name,projects,tasks,milestones,messages,files,timetracker,chat,admin) VALUES (?,?,?,?,?,?,?,?,?)");
        $ins = $insStmt->execute(array($name, $projects, $tasks, $milestones, $messages, $files, $timetracker, $chat, $admin));

        if ($ins) {
            $insid = $conn->lastInsertId();
            return $insid;
        } else {
            return false;
        }
    }

    /**
     * Edit a role
     * This method takes an array with permissions, serializes it to string, and saves it to the Database
     * Additionally it takes the ID of the role to edit
     *
     * @param int $id ID of the role to edit
     * @param string $name Name of the role (for display)
     * @param array $projects Role permissions for projects
     * @param array $tasks Role permissions for tasks
     * @param array $milestones Role permissions for milestones
     * @param array $customers Role permissions for customers
     * @param array $messages Role permissions for messages
     * @param array $files Role permissions for files
     * @param array $timetracker Role permissions for timetracker
     * @param array $chat
     * @param array $admin
     * @return bool
     */
    function edit($id, $name, array $projects, array $tasks, array $milestones, array $messages, array $files, array $timetracker, array $chat, array $admin)
    {
        global $conn;
        $id = (int) $id;
        $projects = serialize($projects);
        $tasks = serialize($tasks);
        $milestones = serialize($milestones);
        $messages = serialize($messages);
        $files = serialize($files);
        $timetracker = serialize($timetracker);
        $chat = serialize($chat);
        $admin = serialize($admin);

        $updStmt = $conn->prepare("UPDATE roles SET name=?,projects=?,tasks=?,milestones=?,messages=?,files=?,timetracker=?,chat=?,admin=? WHERE ID = ?");
        $upd = $updStmt->execute(array($name, $projects, $tasks, $milestones, $messages, $files, $timetracker, $chat, $admin, $id));

        if ($upd) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Delete a role
     * This method takes the ID of the role to be deleted.
     * It returns true if the deletion was sucessful, otherwise false
     *
     * @param int $id ID of the role to be deleted
     * @return bool
     */
    function del($id)
    {
        global $conn;
        $id = (int) $id;
        $del = $conn->query("DELETE FROM roles WHERE ID = $id");
        $del2 = $conn->query("DELETE FROM roles_assigned WHERE role = $id");

        if ($del) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Assign a role to a user
     * Assigns role $role to user $user
     *
     * @param int $role ID of the role
     * @param int $user ID of the user
     * @return bool
     */
    function assign($role, $user)
    {
        global $conn;
        $role = (int) $role;
        $user = (int) $user;
        // get the number of roles already assigned to $user
        $qry = $conn->query("SELECT COUNT(*) FROM roles_assigned WHERE user = $user");
	if ($qry) {
	    $chk = $qry->fetch();
	    $chk = $chk[0];
	}
        // If there already is a role assigned to the user, just update this entry
        // Otherwise create a new entry
        if ($chk > 0) {
            $ins = $conn->query("UPDATE roles_assigned SET role = $role WHERE user = $user");
        } else {
            $ins = $conn->query("INSERT INTO roles_assigned (user,role) VALUES ($user,$role)");
        }

        if ($ins) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Deassign a role from a user
     * Remove role $role from user $user
     *
     * @param int $role ID of the role
     * @param int $user ID of the user
     * @return bool
     */
    function deassign($role, $user)
    {
        global $conn;
        $role = (int) $role;
        $user = (int) $user;

        $del = $conn->query("DELETE FROM roles_assigned WHERE user = $user AND role = $role LIMIT 1");

        if ($del) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get all available roles
     *
     * @param bool $limit Limit the query or show all ?
     * @return array $roles Array with roles
     */
    function getAllRoles($limit = false)
    {
        global $conn;
        $roles = array();

        if (!$limit) {
            $sel = $conn->query("SELECT ID FROM roles ORDER BY ID DESC");
        } else {
            $sel = $conn->query("SELECT ID FROM roles ORDER BY ID DESC LIMIT $limit");
        } while ($sel and $role = $sel->fetch()) {
            /**
             * $role["projects"] = unserialize($role["projects"]);
             * $role["tasks"] = unserialize($role["tasks"]);
             * $role["milestones"] = unserialize($role["milestones"]);
             * $role["messages"] = unserialize($role["messages"]);
             * $role["files"] = unserialize($role["files"]);
             * $role["timetracker"] = unserialize($role["timetracker"]);
             * $role["admin"] = unserialize($role["admin"]);
             */
            // array_push($roles, $role);
            $therole = $this->getRole($role["ID"]);
            array_push($roles, $therole);
        }

        if (!empty($roles)) {
            return $roles;
        } else {
            return array();
        }
    }

    /**
     * Translate name of default roles
     *
     * Intended for viewing translated list of AllRoles.
     * Be sure that rolenames in output
     * are not used for other things than viewing.
     *
     * Default Roles are Admin, User, Client
     *
     * @param array $roles Array with names to translate
     * @return array $roles Array with translated role names
     */

    /**
     * Get the role of a user
     * This is mainly called by class user
     *
     * @param int $user ID of the user
     * @return bool
     */
    function getUserRole($user)
    {
        global $conn;
        $user = (int) $user;

        $qry = $conn->query("SELECT role FROM roles_assigned WHERE user = $user");
	if ($qry) {
	    $usr = $qry->fetch();
	    $usr = $usr[0];
	}
        if ($usr) {
            $role = $this->getRole($usr);
        } else {
            return false;
        }

        if (!empty($role)) {
            return $role;
        } else {
            return array();
        }
    }

    /**
     * make sure all the fields are either 1 or 0 , fill empty ones with 0
     * This is mainly called when adding a role
     *
     * @param array $inarr Array to sanitize
     * @return array $inarr Sanitized array
     */
    function sanitizeArray($inarr)
    {
        if (!is_array($inarr)) {
            $inarr = array();
        }
        if (empty($inarr["add"])) {
            $inarr["add"] = 0;
        }
        if (empty($inarr["edit"])) {
            $inarr["edit"] = 0;
        }
        if (empty($inarr["del"])) {
            $inarr["del"] = 0;
        }
        if (empty($inarr["close"])) {
            $inarr["close"] = 0;
        }
        if (empty($inarr["read"])) {
            $inarr["read"] = 0;
        }
        if (empty($inarr["view"])) {
            $inarr["view"] = 0;
        }

        return (array) $inarr;
    }

    /**
     * Get an array for a role
     *
     * @param int $role Role ID
     * @return array $therole Role as array
     */
    private function getRole($role)
    {
        global $conn;
        $role = (int) $role;
        // Get the serialized strings from the db
        $sel2 = $conn->query("SELECT * FROM roles WHERE ID = $role");
	if ($sel2) {
	    $therole = $sel2->fetch();
	}
        // Unserialize to an array
        $therole["projects"] = unserialize($therole["projects"]);
        $therole["tasks"] = unserialize($therole["tasks"]);
        $therole["milestones"] = unserialize($therole["milestones"]);
        $therole["messages"] = unserialize($therole["messages"]);
        $therole["files"] = unserialize($therole["files"]);
        $therole["timetracker"] = unserialize($therole["timetracker"]);
        $therole["chat"] = unserialize($therole["chat"]);
        $therole["admin"] = unserialize($therole["admin"]);

        if (!empty($therole)) {
            return $therole;
        } else {
            return array();
        }
    }
}

?>