00001 <?php
00002
00011 abstract class SnapPermissionObject extends SObject {
00012 protected $id;
00013 protected $type;
00014 protected $objType;
00015 protected $objId;
00016 protected $ownerType;
00017 protected $ownerId;
00018 protected $capabilities;
00019
00020 protected static $CAPABILITIES = null;
00021 protected static $CAP_DESC = null;
00022 protected static $CAP_ID = null;
00023
00035 public function __construct($objType, $objId, $ownerType, $ownerId) {
00036 parent::__construct();
00037
00038
00039 $caps = self::getCapabilities($objType);
00040
00041 $this->objType = $objType;
00042 $this->objId = $objId;
00043 $this->ownerType = $ownerType;
00044 $this->ownerId = $ownerId;
00045 $this->capabilities = array();
00046
00047 $this->id = substr(0, 1, $objType) . ':' . $objId . ';' . substr(0, 1, $ownerType) . ':' . $ownerId;
00048
00049 foreach($caps as $cap)
00050 $this->capabilities[$cap] = SnapPermission::CAP_DEFAULT;
00051
00052 $this->type = $this->objType . $this->ownerType . 'Permission';
00053
00054 $this->populate();
00055 }
00056
00064 public function getId() {
00065 return $this->id;
00066 }
00067
00075 public function getType() {
00076 return $this->type;
00077 }
00078
00086 public function getObjectId() {
00087 return $this->objId;
00088 }
00089
00097 public function getOwnerId() {
00098 return $this->ownerId;
00099 }
00100
00109 public function isCapability($cap) {
00110 return isset($this->capabilities[$cap]);
00111 }
00112
00121 public function getCapability($cap) {
00122 if(!isset($this->capabilities[$cap])) {
00123 $this->setError('No such capability \'' . $cap . '\'');
00124 return false;
00125 }
00126
00127 return $this->capabilities[$cap];
00128 }
00129
00139 public function setCapability($cap, $val) {
00140 if(!isset($this->capabilities[$cap])) {
00141 $this->setError('No such capability \'' . $cap . '\'');
00142 return false;
00143 }
00144
00145 $this->capabilities[$cap] = $val;
00146 }
00147
00156 protected function populate($row = null) {
00157 if(!$row) {
00158 $type = $this->type;
00159 $ownerIdName = strtolower(substr($this->ownerType, 0, 1)) . 'id';
00160 $objIdName = strtolower($this->objType) . 'Id';
00161 $ownerId = $this->ownerId;
00162 $objId = $this->objId;
00163
00164 $query = "SELECT id, $ownerIdName, $objIdName, LPAD(BIN(permission),32,'0') AS perm, "
00165 . "LPAD(BIN(defaultMask),32,'0') AS defMask FROM {$this->objType}{$this->ownerType}Permission "
00166 . "WHERE $ownerIdName = '$ownerId' AND $objIdName = '$objId'";
00167
00168 if(!SnapDBI::query($query)) {
00169 $this->setError('Database error during query');
00170 return false;
00171 }
00172
00173 $row = SnapDBI::getRow();
00174 SnapDBI::freeResult();
00175 }
00176
00177 $caps = self::getCapabilities($this->objType);
00178
00179 foreach($caps as $id => $cap) {
00180 if($row == false)
00181 $this->capabilities[$cap] = SnapPermission::CAP_DEFAULT;
00182 else {
00183 $i = 32 - $id;
00184 if($row['defMask'][$i] == '0') {
00185 $this->capabilities[$cap] = SnapPermission::CAP_DEFAULT;
00186 }
00187 else if($row['perm'][$i] == '1') {
00188 $this->capabilities[$cap] = SnapPermission::CAP_ALLOW;
00189 }
00190 else {
00191 $this->capabilities[$cap] = SnapPermission::CAP_DENY;
00192 }
00193 }
00194 }
00195
00196 return true;
00197 }
00198
00210 public static function warmCache($type, $parId, $ownerType, $ownerId) {
00211 $ownerIdName = strtolower(substr($ownerType, 0, 1)) . 'id';
00212 $objIdName = strtolower($type) . 'Id';
00213 $linkTable = $type == 'Directory' ? 'DirectoryLink' : 'ResourceLink';
00214 $cls = 'Snap' . $type . ucfirst($ownerType) . 'Permission';
00215 $cacheCls = $type . ucfirst($ownerType) . 'Permission';
00216
00217 $query = "SELECT id, $ownerIdName, $objIdName, LPAD(BIN(permission),32,'0') AS perm, "
00218 . "LPAD(BIN(defaultMask),32,'0') AS defMask FROM {$type}{$ownerType}Permission "
00219 . "LEFT JOIN $linkTable ON $linkTable.childId = {$type}{$ownerType}Permission.$objIdName "
00220 . "WHERE $ownerIdName = '$ownerId' AND $linkTable.parentId = '$parId'";
00221
00222 if(!SnapDBI::query($query)) {
00223 self::setStaticError('Database error during query');
00224 return false;
00225 }
00226
00227 $caps = self::getCapabilities($type);
00228
00229 while($row = SnapDBI::getRow()) {
00230 $id = ($type == 'Directory' ? 'D:' : 'R:') . $row[$objIdName] . ';'
00231 . ($ownerType == 'User' ? 'U:' : 'G:') . $ownerId;
00232 $obj = SnapCache::getById($cacheCls, $id);
00233 if($obj)
00234 continue;
00235
00236 $obj = new $cls($row[$objIdName], $ownerId);
00237 $obj->capabilities = $caps;
00238
00239 $obj->populate($row);
00240
00241 SnapCache::putById($cacheCls, $obj->getId(), $obj);
00242 }
00243
00244 SnapDBI::freeResult();
00245
00246 return true;
00247 }
00248
00257 public static function getCapabilities($type = false) {
00258 if($type !== false && $type != 'Directory' && $type != 'Resource' && $type != 'Version'
00259 && $type != 'File' && $type != 'DirOnly' && $type != 'ResOnly') {
00260 self::setStaticError('Invalid type specified: \'' . $type . '\'');
00261 return null;
00262 }
00263
00264 if(self::$CAPABILITIES === null) {
00265 self::$CAPABILITIES = array('Directory' => array(), 'Resource' => array(),
00266 'Version' => array(), 'File' => array(),
00267 'DirOnly' => array(), 'ResOnly' => array());
00268 self::$CAP_DESC = array('Directory' => array(), 'Resource' => array(),
00269 'Version' => array(), 'File' => array(),
00270 'DirOnly' => array(), 'ResOnly' => array());
00271 self::$CAP_ID = array();
00272 $query = 'SELECT id,name,description,directory,resource,version FROM Capability WHERE directory=1 OR resource=1 OR version=1';
00273 if(!SnapDBI::query($query))
00274 $this->setError('Could not load capabilities from database! Things will not work!');
00275 else {
00276 while(($row = SnapDBI::getRow())) {
00277 self::$CAPABILITIES['Directory'][$row['id']] = $row['name'];
00278 self::$CAP_DESC['Directory'][$row['name']] = $row['description'];
00279 self::$CAP_ID[$row['name']] = $row['id'];
00280 if($row['resource'] == 1 || $row['version'] == 1) {
00281 self::$CAPABILITIES['Resource'][$row['id']] = $row['name'];
00282 self::$CAP_DESC['Resource'][$row['name']] = $row['description'];
00283 if($row['version'] == 1) {
00284 self::$CAPABILITIES['Version'][$row['id']] = $row['name'];
00285 self::$CAP_DESC['Version'][$row['name']] = $row['description'];
00286 }
00287 }
00288 if($row['directory'] == 1 && $row['resource'] == 1) {
00289 self::$CAPABILITIES['File'][$row['id']] = $row['name'];
00290 self::$CAP_DESC['File'][$row['name']] = $row['description'];
00291 }
00292 if($row['directory'] == 1 && $row['resource'] == 0 && $row['version'] == 0) {
00293 self::$CAPABILITIES['DirOnly'][$row['id']] = $row['name'];
00294 self::$CAP_DESC['DirOnly'][$row['name']] = $row['description'];
00295 }
00296 if($row['directory'] == 0 && $row['resource'] == 1 && $row['version'] == 0) {
00297 self::$CAPABILITIES['ResOnly'][$row['id']] = $row['name'];
00298 self::$CAP_DESC['ResOnly'][$row['name']] = $row['description'];
00299 }
00300 }
00301 SnapDBI::freeResult();
00302 }
00303 }
00304
00305 if($type !== false)
00306 return self::$CAPABILITIES[$type];
00307 else
00308 return true;
00309 }
00310
00320 public static function isCapabilityForType($type, $cap) {
00321 if(self::getCapabilities($type) === null)
00322 return null;
00323
00324 return isset(self::$CAP_DESC[$type][$cap]);
00325 }
00326
00335 public function getCapId($cap) {
00336 if(self::getCapabilities('Directory') === null)
00337 return null;
00338
00339 return self::$CAP_ID[$cap];
00340 }
00341
00350 public static function getCapabilityDescriptions($type) {
00351 if(self::getCapabilities($type) === null)
00352 return null;
00353
00354 return self::$CAP_DESC[$type];
00355 }
00356
00366 public static function getCapDesc($type, $cap) {
00367 if(self::getCapabilities($type) === null)
00368 return null;
00369
00370 return self::$CAP_DESC[$type][$cap];
00371 }
00372 }
00373
00374 ?>