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
|
<?php
class DbUpdate {
// insert new userXrole to database. "ignore" to ignore duplicate entry try
public static function addRoleToUser($users, $roles) {
foreach($users AS $user) {
foreach ($roles AS $role) {
$query = "INSERT IGNORE INTO userXrole (userid, roleid) VALUES ($user, $role)";
Database::exec($query);
}
}
}
// remove userXrole entry from database
public static function removeRoleFromUser($users, $roles) {
foreach($users AS $user) {
foreach ($roles AS $role) {
$query = "DELETE FROM userXrole WHERE userid = $user AND roleid = $role";
Database::exec($query);
}
}
}
// delete role, delete userXrole relationships, delete roleXlocation relationships, delete roleXpermission relationships
public static function deleteRole($id) {
$query = "DELETE FROM role WHERE id = $id";
Database::exec($query);
$query = "DELETE FROM userXrole WHERE roleid = $id";
Database::exec($query);
$query = "DELETE FROM roleXlocation WHERE roleid = $id";
Database::exec($query);
$query = "DELETE FROM roleXpermission WHERE roleid = $id";
Database::exec($query);
}
public static function saveRole($roleName, $locType, $locations, $permissions, $role = NULL) {
if ($role) {
Database::exec("UPDATE role SET name = '$roleName', locType = '$locType' WHERE id = $role");
Database::exec("DELETE FROM roleXlocation WHERE roleid = $role");
Database::exec("DELETE FROM roleXpermission WHERE roleid = $role");
} else {
Database::exec("INSERT INTO role (name, locType) VALUES ('$roleName', '$locType')");
$role = Database::lastInsertId();
}
foreach ($locations as $locID) {
Database::exec("INSERT INTO roleXlocation (roleid, locid) VALUES ($role, $locID)");
}
foreach ($permissions as $permission) {
Database::exec("INSERT INTO roleXpermission (roleid, permissionid) VALUES ($role, '$permission')");
}
}
}
|