permissiondescriptor.inc.php
5.16 KB
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
<?php
require_once(KT_LIB_DIR . "/ktentity.inc");
class KTPermissionDescriptor extends KTEntity {
/** primary key */
var $iId = -1;
var $_aFieldToSelect = array(
"iId" => "id",
"sDescriptor" => "descriptor",
"sDescriptorText" => "descriptor_text",
);
var $_bUsePearError = true;
function getID() { return $this->iId; }
function setID($iId) { $this->iId = $iId; }
function getDescriptor() { return $this->sDescriptor; }
function setDescriptor($sDescriptor) { $this->sDescriptor = $sDescriptor; }
function getDescriptorText() { return $this->sDescriptorText; }
function setDescriptorText($sDescriptorText) { $this->sDescriptorText = $sDescriptorText; }
function _table () {
global $default;
return $default->permission_descriptors_table;
}
function create() {
if (empty($this->sDescriptor)) {
$this->sDescriptor = md5($this->sDescriptorText);
}
return parent::create();
}
function update() {
if (empty($this->sDescriptor)) {
$this->sDescriptor = md5($this->sDescriptorText);
}
return parent::update();
}
// STATIC
function &get($iId) {
return KTEntityUtil::get('KTPermissionDescriptor', $iId);
}
// STATIC
function &createFromArray($aOptions) {
return KTEntityUtil::createFromArray('KTPermissionDescriptor', $aOptions);
}
// STATIC
function &getList($sWhereClause = null) {
global $default;
return KTEntityUtil::getList($default->permission_objects_table, 'KTPermissionDescriptor', $sWhereClause);
}
// STATIC
function &getByDescriptor($sDescriptor) {
return KTEntityUtil::getBy('KTPermissionDescriptor', 'descriptor', $sDescriptor);
}
function saveAllowed($aAllowed) {
foreach ($aAllowed as $k => $aIDs) {
if ($k === "group") {
$this->_clearGroups();
foreach ($aIDs as $iID) {
$this->_addGroup($iID);
}
}
}
}
function _clearGroups() {
global $default;
$sQuery = "DELETE FROM $default->permission_descriptor_groups_table WHERE descriptor_id = ?";
$aParams = array($this->getID());
$res = DBUtil::runQuery(array($sQuery, $aParams));
return $res;
}
function _addGroup($iID) {
global $default;
$sQuery = "INSERT INTO $default->permission_descriptor_groups_table (descriptor_id, group_id) VALUES (?, ?)";
$aParams = array($this->getID(), $iID);
$res = DBUtil::runQuery(array($sQuery, $aParams));
return $res;
}
function hasGroups($aGroups) {
global $default;
if (count($aGroups) === 0) {
return false;
}
$aGroupIDs = array();
foreach ($aGroups as $oGroup) {
$aGroupIDs[] = $oGroup->getID();
}
$sGroupIDs = DBUtil::paramArray($aGroupIDs);
$sQuery = "SELECT COUNT(group_id) AS num FROM $default->permission_descriptor_groups_table
WHERE descriptor_id = ? AND group_id IN ($sGroupIDs)";
$aParams = array($this->getID());
$aParams = array_merge($aParams, $aGroupIDs);
$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'num');
if (PEAR::isError($res)) {
return $res;
}
if ((int)$res === 0) {
return false;
}
return true;
}
function getGroups() {
global $default;
$sQuery = "SELECT group_id FROM $default->permission_descriptor_groups_table WHERE descriptor_id = ?";
$aParams = array($this->getID());
return DBUtil::getResultArrayKey(array($sQuery, $aParams), 'group_id');
}
function &getByGroup($oGroup) {
global $default;
$sQuery = "SELECT descriptor_id FROM $default->permission_descriptor_groups_table WHERE group_id = ?";
$aParams = array($oGroup->getID());
$aIDs = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'descriptor_id');
$aRet = array();
foreach ($aIDs as $iID) {
$aRet[] =& KTPermissionDescriptor::get($iID);
}
return $aRet;
}
function &getByGroups($aGroups, $aOptions = null) {
global $default;
if (is_null($aOptions)) {
$aOptions = array();
}
$ids = KTUtil::arrayGet($aOptions, 'ids');
$aGroupIDs = array();
foreach ($aGroups as $oGroup) {
if (is_numeric($oGroup)) {
$aGroupIDs[] = $oGroup;
} else {
$aGroupIDs[] = $oGroup->getID();
}
}
$sGroupIDs = DBUtil::paramArray($aGroupIDs);
$sQuery = "SELECT DISTINCT descriptor_id FROM $default->permission_descriptor_groups_table WHERE group_id IN ( $sGroupIDs )";
$aParams = $aGroupIDs;
$aIDs = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'descriptor_id');
$aRet = array();
foreach ($aIDs as $iID) {
if ($ids === true) {
$aRet[] = $iID;
} else {
$aRet[] =& KTPermissionDescriptor::get($iID);
}
}
return $aRet;
}
}
?>