-
Notifications
You must be signed in to change notification settings - Fork 0
/
eduerp_global.inc
108 lines (101 loc) · 2.64 KB
/
eduerp_global.inc
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
<?php
/**
* Global eduerp utility functions
*/
/**
* Checks if a particular account has a certain role
* @param object $account - The user account to check
* @param mixed $role - role is either int (role id) or string (role name)
* @return bool
*/
function eduerp_has_role($account, $role_param) {
$roles_list = $account->roles;
if (is_int($role)) {
if (array_key_exists($role_param, $roles_list)) return true;
return false;
}
else {
if (array_search($role_param, $roles_list)) return true;
return false;
}
}
/**
* Fetches the role id for a particular role
* @param string $role - the role
* @return int - 0 for not found or the id for the role
*/
function _eduerp_get_role_id ($role) {
$query = "select * from {role} where name='%s'";
$rs = db_query($query, $role);
if ($rs) {
$result = db_fetch_object($rs);
return $result->rid;
} else {
return 0;
}
}
/**
* Fetches the role name for a particular role id
* @param string $rid - the role id
* @return mixed - false if not found or the role name if found
*/
function _eduerp_get_role ($rid) {
$query = "select * from {role} where rid=%d";
$rs = db_query($query, $role);
if ($rs) {
$result = db_fetch_object($rs);
return $result->name;
} else {
return false;
}
}
/**
* Deletes a particular role from a user's account - USE WITH CARE!
* @param object $account - the user object
* @param string $role - The role to delete
* @return bool - true on success, else false
*/
function eduerp_delete_role($account, $role) {
$roles = &$account->roles;
$rid = _eduerp_get_role_id($role);
if ($rid) {
unset($roles[$rid]);
user_save($account, array('roles' => $roles));
return true;
}
return false;
}
/**
* Adds a particular role to a user's account
* @param object $account - the user object
* @param string $role - the role to add
* @return bool - true on success, else false
*/
function eduerp_add_role($account, $role) {
$roles = &$account->roles;
$rid = _eduerp_get_role_id($role);
if ($rid) {
if (!array_key_exists($rid, $roles)) {
$roles = $roles + array($rid => _eduerp_get_role($rid));
user_save($account, array('roles' => $roles));
return true;
}
}
return false;
}
/**
* Sets a particular role to a user's account
* @param object $account - the user object
* @param string $role - the role to set
* @return bool - true on success, else false
*/
function eduerp_set_role($account, $role) {
$rid = _eduerp_get_role_id($role);
if ($rid) {
$set_role = array($rid => _eduerp_get_role($rid));
user_save($account, array('roles' => $set_role));
return true;
}
return false;
}
?>