-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.php
294 lines (256 loc) · 7.25 KB
/
backend.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
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
<?php
/*
* to use any function in this module first call connect()
* at the end of a page call disconnect()
*/
$LINK = NULL;
// get db connection
function connect() {
global $LINK;
// Try localhost connection first
// $conn = mysql_connect("localhost","root","") or die ("dbconnect failed");
// if ($conn) {
// mysql_select_db("db_final") or die ("dbconnect failed");
// $LINK = $conn;
// return $conn;
// }
// Try server connections
$url=parse_url(getenv("CLEARDB_DATABASE_URL"));
$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"],1);
// echo "Server: " . $server;
// echo "Username: " . $username;
// echo "Password: " . $password;
$conn = mysql_connect($server, $username, $password);
if ($conn) {
mysql_select_db($db);
$LINK = $conn;
return $conn;
}
}
// unget db connection
function disconnect() {
mysql_close($LINK);
}
/*
* Creating
*/
// String [[Arrayof Int] = array()] ->
function create_user($name, $groups = array()){
global $LINK;
$result = mysql_query("INSERT INTO users (name) VALUES (\"$name\");",$LINK);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$id = get_user_id($name);
add_user_to_groups($id,$groups);
}
// String [[Arrayof int] = array()] [[Arrayof int] = array()] ->
function create_group($name,$users = array(), $groups = array()) {
global $LINK;
$result = mysql_query("INSERT INTO groups (name) VALUES (\"$name\");",$LINK);
$id = get_group_id($name);
foreach($users as $user) {
add_user_to_groups($user,array($id));
}
foreach($groups as $group) {
add_group_to_groups($group,array($id));
}
}
// String ->
function create_resource($uri) {
global $LINK;
mysql_query("INSERT INTO resources VALUES (\"$uri\")",$LINK);
}
// Int String String [Boolean = true] ->
function create_permission_set($group,$uri,$action,$access = True) {
global $LINK;
$query = "INSERT INTO permission_sets (is_allowed, group_id,resource_uri,action_name) VALUES ($access,$group,\"$uri\",\"$action\")";
$result = mysql_query($query,$LINK);
if (!$result) {
echo "ERROR: " . mysql_error();
}
}
/*
* Modifying
*/
// Int [Arrayof Int] ->
function add_user_to_groups($id,$groups) {
global $LINK;
foreach($groups as $gid) {
mysql_query("INSERT INTO user_group_mapping (user_id,group_id) VALUES (\"$id\",\"$gid\")",$LINK);
}
}
// Int [Arrayof Int] ->
function add_group_to_groups($id,$groups) {
global $LINK;
foreach($groups as $group) {
mysql_query("INSERT INTO group_group_mapping (contained_id,container_id) VALUES (\"$id\",\"$group\")",$LINK);
}
}
// Int ->
function delete_user($id) {
global $LINK;
$result = mysql_query("DELETE FROM users WHERE id = \"$id\"",$LINK);
if (!$result) {
echo mysql_error();
}
}
function delete_group($id) {
global $LINK;
$result = mysql_query("DELETE FROM groups WHERE id = \"$id\"",$LINK);
if (!$result) {
echo mysql_error();
}
}
function delete_resource($id) {
global $LINK;
$result = mysql_query("DELETE FROM resources WHERE uri = \"$id\"",$LINK);
if (!$result) {
echo mysql_error();
}
}
// Int Int ->
function delete_group_from_group($contained,$container) {
global $LINK;
$query = "DELETE FROM group_group_mapping WHERE container_id = $container AND contained_id = $contained";
$result = mysql_query($query, $LINK);
if (!$result) {
echo mysql_error();
}
}
function delete_user_from_group($user_id,$group_id) {
global $LINK;
$query = "DELETE FROM user_group_mapping WHERE user_id = $user_id AND group_id = $group_id";
$result = mysql_query($query, $LINK);
if (!$result) {
echo mysql_error();
}
}
/*
* querying
*/
// Int -> [Arrayof Int]
function users_in_group($id) {
global $LINK;
$query = "SELECT user_id FROM user_group_mapping WHERE group_id = \"$id\"";
$result = mysql_query($query, $LINK);
if (!$result) {
echo "ERROR: " . mysql_error();
}
return single_results_to_array($result);
}
function user_group_direct($id) {
global $LINK;
$query = "SELECT group_id FROM user_group_mapping WHERE user_id = $id";
$groups_id = mysql_query($query,$LINK);
return single_results_to_array($groups_id);
}
// Int String String -> Boolean
function does_user_has_access($uid,$uri,$action){
$user_groups = get_user_groups($uid);
$permission_sets = get_permission_sets($uri, $action);
$permission_set_groups = get_permission_set_groups($permission_sets);
foreach($user_groups as $id) {
if(in_array($id,$permission_set_groups)) {
return true;
}
}
return false;
}
// [Array Ints] -> [Arrayof Ints]
function get_permission_set_groups($ids){
global $LINK;
$query = "SELECT group_id FROM permission_sets WHERE group_id in (" . implode("\", \"",$ids) . ")";
$results = single_results_to_array(mysql_query($query,$LINK));
return $results;
}
// String String -> [Arrayof Ints]
function get_permission_sets($uri,$action){
global $LINK;
return single_results_to_array(mysql_query("SELECT group_id FROM permission_sets WHERE resource_uri = \"$uri\" AND action_name = \"$action\"",$LINK));
}
// Int -> Arrayof Int
function get_group_groups($id) {
return get_groups_groups(array($id));
}
// [Arrayof Int] -> [Arrayof Int]
function get_groups_groups($ids) {
global $LINK;
$todos = $ids;
$acc = array();
$results = $todos;
while(!empty($todos)) {
foreach($todos as $id) {
$query = "SELECT contained_id FROM group_group_mapping WHERE container_id = $id";
$news = single_results_to_array(mysql_query($query, $LINK));
foreach($news as $new) {
if(!in_array($new,$results)){
array_push($results,$new);
array_push($acc,$new);
}
}
$todos = $acc;
$acc = array();
}
}
return $results;
}
// Int -> String
function get_group_name($id) {
global $LINK;
$query = "SELECT name FROM groups WHERE id = $id";
$result = mysql_query($query);
$result = mysql_result($result, 0);
return $result;
}
function get_user_name($id) {
global $LINK;
$query = "SELECT name FROM users WHERE id = $id";
$result = mysql_query($query);
$result = mysql_result($result, 0);
return $result;
}
// Int -> [Arrayof Int]
function get_user_groups($id) {
$groups = user_group_direct($id);
return get_groups_groups($groups);
}
// String -> Int
function get_user_id($user) {
global $LINK;
$res = mysql_fetch_array(mysql_query("SELECT id FROM users WHERE name = \"$user\"",$LINK));
return $res[0];
}
// String -> Int
function get_group_id($group){
global $LINK;
$res = single_results_to_array(mysql_query("SELECT id FROM groups WHERE name = \"$group\"",$LINK));
return $res[0];
}
// -> Arrayof Strings
function get_all_users() {
global $LINK;
return single_results_to_array(mysql_query("SELECT name FROM users",$LINK));
}
function get_all_groups(){
global $LINK;
return single_results_to_array(mysql_query("SELECT name FROM groups",$LINK));
}
function get_all_resources(){
global $LINK;
return single_results_to_array(mysql_query("SELECT uri FROM resources",$LINK));
}
function get_all_actions(){
global $LINK;
return single_results_to_array(mysql_query("SELECT name FROM actions",$LINK));
}
function single_results_to_array($result) {
$res = array();
while ($next = mysql_fetch_array($result)) {
array_push($res,$next[0]);
}
return $res;
}