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: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400:
<?php
defined('ICMS_ROOT_PATH') or die("ImpressCMS root path not defined");
class icms_member_groupperm_Handler extends icms_core_ObjectHandler {
static public $_cachedRights;
public function &create($isNew = true) {
$perm = new icms_member_groupperm_Object();
if ($isNew) {
$perm->setNew();
}
return $perm;
}
public function &get($id) {
$id = (int) $id;
$perm = false;
if ($id > 0) {
$sql = sprintf("SELECT * FROM %s WHERE gperm_id = '%u'", icms::$xoopsDB->prefix('group_permission'), $id);
if (!$result = icms::$xoopsDB->query($sql)) {
return $perm;
}
$numrows = icms::$xoopsDB->getRowsNum($result);
if ($numrows == 1) {
$perm = new icms_member_groupperm_Object();
$perm->assignVars(icms::$xoopsDB->fetchArray($result));
}
}
return $perm;
}
public function insert(&$perm) {
if (!is_a($perm, 'icms_member_groupperm_Object')) {
return false;
}
if (!$perm->isDirty()) {
return true;
}
if (!$perm->cleanVars()) {
return false;
}
foreach ( $perm->cleanVars as $k => $v) {
${$k} = $v;
}
if ($perm->isNew()) {
$gperm_id = icms::$xoopsDB->genId('group_permission_gperm_id_seq');
$sql = sprintf(
"INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name)
VALUES ('%u', '%u', '%u', '%u', %s)",
icms::$xoopsDB->prefix('group_permission'),
(int) $gperm_id,
(int) $gperm_groupid,
(int) $gperm_itemid,
(int) $gperm_modid,
icms::$xoopsDB->quoteString($gperm_name)
);
} else {
$sql = sprintf(
"UPDATE %s SET gperm_groupid = '%u', gperm_itemid = '%u', gperm_modid = '%u' WHERE gperm_id = '%u'",
icms::$xoopsDB->prefix('group_permission'),
(int) $gperm_groupid,
(int) $gperm_itemid,
(int) $gperm_modid,
(int) $gperm_id
);
}
if (!$result = icms::$xoopsDB->query($sql)) {
return false;
}
if (empty($gperm_id)) {
$gperm_id = icms::$xoopsDB->getInsertId();
}
$perm->assignVar('gperm_id', $gperm_id);
return true;
}
public function delete(&$perm) {
if (!is_a($perm, 'icms_member_groupperm_Object')) {
return false;
}
$sql = sprintf(
"DELETE FROM %s WHERE gperm_id = '%u'",
icms::$xoopsDB->prefix('group_permission'),
(int) ($perm->getVar('gperm_id'))
);
if (!$result = icms::$xoopsDB->query($sql)) {
return false;
}
return true;
}
public function getObjects($criteria = null, $id_as_key = false) {
$ret = array();
$limit = $start = 0;
$sql = 'SELECT * FROM '.icms::$xoopsDB->prefix('group_permission');
if (isset($criteria) && is_subclass_of($criteria, 'icms_db_criteria_Element')) {
$sql .= ' ' . $criteria->renderWhere();
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
$result = icms::$xoopsDB->query($sql, $limit, $start);
if (!$result) {
return $ret;
}
while ($myrow = icms::$xoopsDB->fetchArray($result)) {
$perm = new icms_member_groupperm_Object();
$perm->assignVars($myrow);
if (!$id_as_key) {
$ret[] =& $perm;
} else {
$ret[$myrow['gperm_id']] =& $perm;
}
unset($perm);
}
return $ret;
}
public function getCount($criteria = null) {
$sql = 'SELECT COUNT(*) FROM '.icms::$xoopsDB->prefix('group_permission');
if (isset($criteria) && is_subclass_of($criteria, 'icms_db_criteria_Element')) {
$sql .= ' ' . $criteria->renderWhere();
}
$result = icms::$xoopsDB->query($sql);
if (!$result) {
return 0;
}
list($count) = icms::$xoopsDB->fetchRow($result);
return $count;
}
public function deleteAll($criteria = null) {
$sql = sprintf("DELETE FROM %s", icms::$xoopsDB->prefix('group_permission'));
if (isset($criteria) && is_subclass_of($criteria, 'icms_db_criteria_Element')) {
$sql .= ' ' . $criteria->renderWhere();
}
if (!$result = icms::$xoopsDB->query($sql)) {
return false;
}
return true;
}
public function deleteByGroup($gperm_groupid, $gperm_modid = null) {
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('gperm_groupid', (int) ($gperm_groupid)));
if (isset($gperm_modid)) {
$criteria->add(new icms_db_criteria_Item('gperm_modid', (int) $gperm_modid));
}
return $this->deleteAll($criteria);
}
public function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null) {
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('gperm_modid', (int) $gperm_modid));
if (isset($gperm_name)) {
$criteria->add(new icms_db_criteria_Item('gperm_name', $gperm_name));
if (isset($gperm_itemid)) {
$criteria->add(new icms_db_criteria_Item('gperm_itemid', (int) $gperm_itemid));
}
}
return $this->deleteAll($criteria);
}
public function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1, $webmasterAlwaysTrue=true) {
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('gperm_modid', $gperm_modid));
$criteria->add(new icms_db_criteria_Item('gperm_name', $gperm_name));
$gperm_itemid = (int) $gperm_itemid;
if ($gperm_itemid > 0) {
$criteria->add(new icms_db_criteria_Item('gperm_itemid', $gperm_itemid));
}
if (is_array($gperm_groupid)) {
if ($webmasterAlwaysTrue && in_array(ICMS_GROUP_ADMIN, $gperm_groupid)) {
return true;
}
$criteria2 = new icms_db_criteria_Compo();
foreach ( $gperm_groupid as $gid) {
$criteria2->add(new icms_db_criteria_Item('gperm_groupid', $gid), 'OR');
}
$criteria->add($criteria2);
} else {
if ($webmasterAlwaysTrue && ICMS_GROUP_ADMIN == $gperm_groupid) {
return true;
}
$criteria->add(new icms_db_criteria_Item('gperm_groupid', $gperm_groupid));
}
if ($this->getCount($criteria) > 0) {
return true;
}
return false;
}
public function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1) {
$perm =& $this->create();
$perm->setVar('gperm_name', $gperm_name);
$perm->setVar('gperm_groupid', $gperm_groupid);
$perm->setVar('gperm_itemid', $gperm_itemid);
$perm->setVar('gperm_modid', $gperm_modid);
return $this->insert($perm);
}
public function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1) {
$ret = array();
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('gperm_name', $gperm_name));
$criteria->add(new icms_db_criteria_Item('gperm_modid', (int) $gperm_modid));
if (is_array($gperm_groupid)) {
$criteria2 = new icms_db_criteria_Compo();
foreach ( $gperm_groupid as $gid) {
$criteria2->add(new icms_db_criteria_Item('gperm_groupid', $gid), 'OR');
}
$criteria->add($criteria2);
} else {
$criteria->add(new icms_db_criteria_Item('gperm_groupid', (int) $gperm_groupid));
}
$perms = $this->getObjects($criteria, true);
foreach ( array_keys($perms) as $i) {
$ret[] = $perms[$i]->getVar('gperm_itemid');
}
return array_unique($ret);
}
public function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1) {
$ret = array();
$perms = array();
if (isset(self::$_cachedRights[$gperm_name][$gperm_itemid][$gperm_modid])) {
$perms = self::$_cachedRights[$gperm_name][$gperm_itemid][$gperm_modid];
} else {
$criteria = new icms_db_criteria_Compo(new icms_db_criteria_Item('gperm_name', $gperm_name));
$criteria->add(new icms_db_criteria_Item('gperm_itemid', (int) $gperm_itemid));
$criteria->add(new icms_db_criteria_Item('gperm_modid', (int) $gperm_modid));
$perms = $this->getObjects($criteria, true);
foreach ($perms as $perm) {
self::$_cachedRights[$gperm_name][$gperm_itemid][$gperm_modid][] = $perm;
}
}
foreach (array_keys($perms) as $i) {
$ret[] = $perms[$i]->getVar('gperm_groupid');
}
return $ret;
}
}