00001 <?php
00002
00011 class SnapPermissionCache extends SObject {
00012 private static $CAPABILITIES = null;
00013 private static $cacheObjects = array();
00014
00015 private $userCaps = array();
00016 private $fromDB = false;
00017 private $needsUpdate = false;
00018
00019 private $objType;
00020 private $objId;
00021 private $uid;
00022
00034 public function __construct($objType, $objId, $uid = '', $preFill = null) {
00035 parent::__construct();
00036
00037 if($preFill) {
00038 $cacheA = explode(';', $preFill);
00039 foreach($cacheA as $cap) {
00040 $capInfo = explode(':', $cap);
00041 $capReal = self::$CAPABILITIES[$objType][$capInfo[0]];
00042 $this->userCaps[$capReal] = ($capInfo[1] == 'A' ? true : false);
00043 }
00044 $this->fromDB = true;
00045 }
00046 else {
00047 self::getCapabilities();
00048
00049 if($uid == '')
00050 $uid = Snap2::getCurrentUser();
00051
00052 $query = "SELECT cache FROM ${objType}EACache WHERE " . strtolower($objType) . "Id = $objId AND uid = $uid";
00053 if(!SnapDBI::query($query)) {
00054 $this->setError('Error executing cache query');
00055 }
00056 else {
00057 $row = SnapDBI::getRow();
00058 if($row !== false) {
00059 $cacheA = explode(';', $row['cache']);
00060 foreach($cacheA as $cap) {
00061 $capInfo = explode(':', $cap);
00062 $capReal = self::$CAPABILITIES[$objType][$capInfo[0]];
00063 $this->userCaps[$capReal] = ($capInfo[1] == 'A' ? true : false);
00064 }
00065 $this->fromDB = true;
00066 }
00067 SnapDBI::freeResult();
00068 }
00069 }
00070
00071 $this->objType = $objType;
00072 $this->objId = $objId;
00073 $this->uid = $uid;
00074
00075 self::$cacheObjects["$this->objType:$this->objId:$uid"] = $this;
00076 }
00077
00089 public static function warmCache($parType, $parId, $objType, $uid = '') {
00090 self::getCapabilities();
00091
00092
00093 global $SWAT;
00094 if(!$SWAT || !$SWAT->getGlobalUser() || !$SWAT->getGlobalUser()->getAuthModule('ldap'))
00095 return true;
00096
00097 if($uid == '')
00098 $uid = Snap2::getCurrentUser();
00099
00100 $objIdName = strtolower($objType) . 'Id';
00101 $query = "SELECT $objIdName, cache FROM ${objType}EACache LEFT JOIN {$objType}Link ON {$objType}Link.childId = "
00102 . "{$objType}EACache.$objIdName WHERE {$objType}Link.parentId = $parId AND uid = $uid";
00103 if(!SnapDBI::query($query)) {
00104 $this->setError('Error executing cache query');
00105 }
00106 else {
00107 while($row = SnapDBI::getRow()) {
00108 if(!isset(self::$cacheObjects["$objType:{$row[$objIdName]}:$uid"]))
00109 $obj = new SnapPermissionCache($objType, $row[$objIdName], $uid, $row['cache']);
00110 }
00111 SnapDBI::freeResult();
00112 }
00113 }
00114
00125 public static function retrieve($objType, $objId, $uid = '') {
00126 if($uid == '')
00127 $uid = Snap2::getCurrentUser();
00128
00129 if(isset(self::$cacheObjects["$objType:$objId:$uid"]))
00130 return self::$cacheObjects["$objType:$objId:$uid"];
00131 else
00132 return new SnapPermissionCache($objType, $objId, $uid);
00133 }
00134
00144 public function setEA($cap, $access) {
00145 $this->userCaps[$cap] = $access;
00146 $this->needsUpdate = true;
00147 }
00148
00157 public function setAllEA($caps) {
00158 foreach($caps as $cap => $access)
00159 $this->userCaps[$cap] = $access;
00160 $this->needsUpdate = true;
00161 }
00162
00171 public function getEA($cap) {
00172 if(!isset($this->userCaps[$cap]))
00173 return null;
00174 return $this->userCaps[$cap];
00175 }
00176
00184 public function commit() {
00185 if(!$this->needsUpdate)
00186 return true;
00187
00188
00189 $cacheStr = "";
00190 foreach(self::$CAPABILITIES[$this->objType] as $capId => $capName)
00191 $cacheStr .= "$capId:" . ($this->userCaps[$capName] ? 'A' : 'D') . ';';
00192 $cacheStr = rtrim($cacheStr, ';');
00193 if($this->fromDB)
00194 $query = 'UPDATE ' . $this->objType . 'EACache SET cache=\'' . $cacheStr . '\' WHERE uid = ' . $this->uid
00195 . ' AND ' . strtolower($this->objType) . 'Id = ' . $this->objId;
00196 else
00197 $query = 'INSERT INTO ' . $this->objType . 'EACache SET cache=\'' . $cacheStr . '\', uid = ' . $this->uid
00198 . ', ' . strtolower($this->objType) . 'Id = ' . $this->objId;
00199
00200
00201 SnapDBI::startTransaction();
00202
00203 if(!SnapDBI::query($query)) {
00204 $this->setError('Error executing query');
00205 SnapDBI::cancelTransaction();
00206 return false;
00207 }
00208
00209 SnapDBI::commitTransaction();
00210
00211 $this->needsUpdate = false;
00212 $this->fromDB = true;
00213
00214 return true;
00215 }
00216
00224 public function delete() {
00225 $query = "DELETE FROM $this->objType WHERE " . strtolower($this->objType) . "Id = $this->objId AND uid = $this->uid";
00226
00227 SnapDBI::startTransaction();
00228 if(!SnapDBI::query($query)) {
00229 $this->setError('Error executing query');
00230 SnapDBI::cancelTransaction();
00231 return false;
00232 }
00233
00234 SnapDBI::commitTransaction();
00235
00236 unset(self::$cacheObjects["$this->objType:$this->objId:$uid"]);
00237
00238 $this->userCaps = array();
00239 $this->id = null;
00240 $this->fromDB = false;
00241 $this->needsUpdate = false;
00242
00243 return true;
00244 }
00245
00256 public static function clearCache($objType, $objId = '', $uid = '') {
00257 $query = 'DELETE FROM ' . $objType . 'EACache';
00258 if($objId != '') {
00259 $query .= ' WHERE ' . strtolower($objType) . 'Id = ' . $objId;
00260 if($uid != '')
00261 $query .= ' AND uid = ' . $uid;
00262 }
00263
00264 SnapDBI::startTransaction();
00265 if(!SnapDBI::query($query)) {
00266 self::setStaticError('Error executing query');
00267 SnapDBI::cancelTransaction();
00268 return false;
00269 }
00270
00271 SnapDBI::commitTransaction();
00272
00273 self::$cacheObjects = array();
00274
00275 return true;
00276 }
00277
00285 public static function clearAll() {
00286 self::clearCache('Directory');
00287 self::clearCache('Resource');
00288 return true;
00289 }
00290
00298 public static function getCapabilities() {
00299 if(self::$CAPABILITIES === null) {
00300 self::$CAPABILITIES = array('Directory' => array(), 'Resource' => array());
00301 $query = 'SELECT id, name, resource, version FROM Capability WHERE directory = 1 OR resource = 1 OR version = 1';
00302 if(!SnapDBI::query($query)) {
00303 self::setStaticError('Error retrieving capabilities! Snap2 is probably dead now :(');
00304 return array();
00305 }
00306 while($row = SnapDBI::getRow()) {
00307 self::$CAPABILITIES['Directory'][$row['id']] = $row['name'];
00308 if($row['resource'] == 1 || $row['version'] == 1)
00309 self::$CAPABILITIES['Resource'][$row['id']] = $row['name'];
00310 }
00311 SnapDBI::freeResult();
00312 }
00313
00314 return self::$CAPABILITIES;
00315 }
00316 }
00317
00318 ?>