-
Notifications
You must be signed in to change notification settings - Fork 9
/
role.php
151 lines (127 loc) · 3.74 KB
/
role.php
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
<?php
/**
* A class designed to take care of roles
*
* @author Lauge Rud Knudsen <[email protected]>
* @version V2
*/
class Role {
/** @var string The unique id of this role */
protected $id;
/** @var string The text shown when hovering over the icon */
protected $title;
/** @var string The name of the image holding the icon */
protected $image;
/** @var int The display priority */
protected $priority;
/** @var string The 6 character hex color code for comments */
protected $color;
/** @var string The title displayed in comments */
protected $chatTitle;
/** @var string[] An array containing all the permission nodes of this role */
protected $permissions;
//getters
public function getId() {
return $this->id;
}
public function getTitle() {
return $this->title;
}
public function getImage() {
if (empty($this->image)) {
return false;
}
return WEBROOT . '/resources/roles/' . $this->image;
}
public function getPriority() {
return $this->priority;
}
public function getColor() {
return $this->color;
}
public function getChatTitle() {
return $this->chatTitle;
}
//setters
public function setId($id) {
$this->id = $id;
}
public function create() {
$output = false;
$statement = dbConnection::getInstance()->prepare('INSERT INTO roles (roleid) '
. 'VALUES (:id)');
$statement->bindParam(':id', $this->id);
$output = $statement->execute();
return $output;
}
static function readAll() {
$statement = dbConnection::getInstance()->prepare('SELECT roleid, title, image, priority, color, chattitle '
. 'FROM roles');
$statement->execute();
while ($row = $statement->fetch()) {
$role = new Role();
$role->setId($row['roleid']);
$role->read();
$output[] = $role;
}
return $output;
}
//public functions
public function read() {
$statement = dbConnection::getInstance()->prepare('SELECT title, image, priority, color, chattitle '
. 'FROM roles '
. 'WHERE roleid = :id');
$statement->bindParam(':id', $this->id);
$statement->execute();
while ($row = $statement->fetch()) {
$this->title = $row['title'];
$this->image = $row['image'];
$this->priority = $row['priority'];
$this->color = $row['color'];
$this->chatTitle = $row['chattitle'];
}
$this->readPermissions();
if (empty($this->priority)) {
$this->priority = 0;
}
}
protected function readPermissions() {
$statement = dbConnection::getInstance()->prepare('SELECT permissionid '
. 'FROM rolepermissions '
. 'WHERE roleid = :id');
$statement->bindParam(':id', $this->id);
$statement->execute();
while ($row = $statement->fetch()) {
$permissionIds[] = $row[0];
}
if (isset($permissionIds)) {
foreach ($permissionIds as $permissionid) {
$permission = new Permission();
$permission->read($this->id, $permissionid);
$output[$permission->getName()] = $permission;
}
$this->permissions = $output;
}
}
public function hasPermission_old($permission) {
if (empty($this->permissions)) {
$output = false;
} else {
$output = (array_search($permission, $this->permissions) !== FALSE);
}
return $output;
}
public function hasPermission($permission) {
if (empty($this->permissions)) {
$output = null;
} else {
if (array_key_exists($permission, $this->permissions)) {
$output = $this->permissions[$permission]->getAllowed();
} else {
$output = null;
}
}
return $output;
}
//private functions
}