PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/web/concrete/core/models/permission/key.php

https://github.com/patrickheck/concrete5
PHP | 379 lines | 279 code | 52 blank | 48 comment | 37 complexity | ca1333d35d7bfe132b705d522812f121 MD5 | raw file
  1. <?
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. abstract class Concrete5_Model_PermissionKey extends Object {
  4. const ACCESS_TYPE_INCLUDE = 10;
  5. const ACCESS_TYPE_EXCLUDE = -1;
  6. const ACCESS_TYPE_ALL = 0;
  7. public function getSupportedAccessTypes() {
  8. $types = array(
  9. self::ACCESS_TYPE_INCLUDE => t('Included'),
  10. self::ACCESS_TYPE_EXCLUDE => t('Excluded'),
  11. );
  12. return $types;
  13. }
  14. /**
  15. * Returns whether a permission key can start a workflow
  16. */
  17. public function canPermissionKeyTriggerWorkflow() {return $this->pkCanTriggerWorkflow;}
  18. /**
  19. * Returns whether a permission key has a custom class.
  20. */
  21. public function permissionKeyHasCustomClass() {return $this->pkHasCustomClass;}
  22. /**
  23. * Returns the name for this permission key
  24. */
  25. public function getPermissionKeyName() { return $this->pkName;}
  26. /** Returns the display name for this permission key (localized and escaped accordingly to $format)
  27. * @param string $format = 'html'
  28. * Escape the result in html format (if $format is 'html').
  29. * If $format is 'text' or any other value, the display name won't be escaped.
  30. * @return string
  31. */
  32. public function getPermissionKeyDisplayName($format = 'html') {
  33. $value = tc('PermissionKeyName', $this->getPermissionKeyName());
  34. switch($format) {
  35. case 'html':
  36. return h($value);
  37. case 'text':
  38. default:
  39. return $value;
  40. }
  41. }
  42. /**
  43. * Returns the handle for this permission key
  44. */
  45. public function getPermissionKeyHandle() { return $this->pkHandle;}
  46. /**
  47. * Returns the description for this permission key
  48. */
  49. public function getPermissionKeyDescription() { return $this->pkDescription;}
  50. /** Returns the display description for this permission key (localized and escaped accordingly to $format)
  51. * @param string $format = 'html'
  52. * Escape the result in html format (if $format is 'html').
  53. * If $format is 'text' or any other value, the display description won't be escaped.
  54. * @return string
  55. */
  56. public function getPermissionKeyDisplayDescription($format = 'html') {
  57. $value = tc('PermissionKeyDescription', $this->getPermissionKeyDescription());
  58. switch($format) {
  59. case 'html':
  60. return h($value);
  61. case 'text':
  62. default:
  63. return $value;
  64. }
  65. }
  66. /**
  67. * Returns the ID for this permission key
  68. */
  69. public function getPermissionKeyID() {return $this->pkID;}
  70. public function getPermissionKeyCategoryID() {return $this->pkCategoryID;}
  71. public function getPermissionKeyCategoryHandle() {return $this->pkCategoryHandle;}
  72. public function setPermissionObject($object) {
  73. $this->permissionObject = $object;
  74. }
  75. public function getPermissionObjectToCheck() {
  76. if (is_object($this->permissionObjectToCheck)) {
  77. return $this->permissionObjectToCheck;
  78. } else {
  79. return $this->permissionObject;
  80. }
  81. }
  82. public function getPermissionObject() {
  83. return $this->permissionObject;
  84. }
  85. public static function loadAll() {
  86. $cl = new CacheLocal();
  87. $db = Loader::db();
  88. $permissionkeys = array();
  89. $e = $db->Execute('select pkID, pkName, pkDescription, pkHandle, pkCategoryHandle, pkCanTriggerWorkflow, pkHasCustomClass, PermissionKeys.pkCategoryID, pkCategoryHandle, PermissionKeys.pkgID from PermissionKeys inner join PermissionKeyCategories on PermissionKeyCategories.pkCategoryID = PermissionKeys.pkCategoryID');
  90. while ($r = $e->FetchRow()) {
  91. $class = Loader::helper('text')->camelcase($r['pkCategoryHandle']) . 'PermissionKey';
  92. if ($r['pkHasCustomClass']) {
  93. $class = Loader::helper('text')->camelcase($r['pkHandle']) . $class;
  94. }
  95. $pk = new $class();
  96. $pk->setPropertiesFromArray($r);
  97. $permissionkeys[$r['pkHandle']] = $pk;
  98. $permissionkeys[$r['pkID']] = $pk;
  99. }
  100. CacheLocal::set('permission_keys', false, $permissionkeys);
  101. return $permissionkeys;
  102. }
  103. protected static function load($key, $loadBy = 'pkID') {
  104. $db = Loader::db();
  105. $r = $db->GetRow('select pkID, pkName, pkDescription, pkHandle, pkCategoryHandle, pkCanTriggerWorkflow, pkHasCustomClass, PermissionKeys.pkCategoryID, pkCategoryHandle, PermissionKeys.pkgID from PermissionKeys inner join PermissionKeyCategories on PermissionKeyCategories.pkCategoryID = PermissionKeys.pkCategoryID where ' . $loadBy . ' = ?', array($key));
  106. $class = Loader::helper('text')->camelcase($r['pkCategoryHandle']) . 'PermissionKey';
  107. if (!is_array($r) && (!$r['pkID'])) {
  108. return false;
  109. }
  110. if ($r['pkHasCustomClass']) {
  111. $class = Loader::helper('text')->camelcase($r['pkHandle']) . $class;
  112. }
  113. $pk = new $class();
  114. $pk->setPropertiesFromArray($r);
  115. return $pk;
  116. }
  117. public function hasCustomOptionsForm() {
  118. $env = Environment::get();
  119. $file = $env->getPath(DIRNAME_ELEMENTS . '/' . DIRNAME_PERMISSIONS . '/' . DIRNAME_KEYS . '/' . $this->pkHandle . '.php', $this->getPackageHandle());
  120. return file_exists($file);
  121. }
  122. public function getPackageID() { return $this->pkgID;}
  123. public function getPackageHandle() {
  124. return PackageList::getHandle($this->pkgID);
  125. }
  126. /**
  127. * Returns a list of all permissions of this category
  128. */
  129. public static function getList($pkCategoryHandle, $filters = array()) {
  130. $db = Loader::db();
  131. $q = 'select pkID from PermissionKeys inner join PermissionKeyCategories on PermissionKeys.pkCategoryID = PermissionKeyCategories.pkCategoryID where pkCategoryHandle = ?';
  132. foreach($filters as $key => $value) {
  133. $q .= ' and ' . $key . ' = ' . $value . ' ';
  134. }
  135. $r = $db->Execute($q, array($pkCategoryHandle));
  136. $list = array();
  137. while ($row = $r->FetchRow()) {
  138. $pk = self::load($row['pkID']);
  139. if (is_object($pk)) {
  140. $list[] = $pk;
  141. }
  142. }
  143. $r->Close();
  144. return $list;
  145. }
  146. public function export($axml) {
  147. $category = PermissionKeyCategory::getByID($this->pkCategoryID)->getPermissionKeyCategoryHandle();
  148. $pkey = $axml->addChild('permissionkey');
  149. $pkey->addAttribute('handle',$this->getPermissionKeyHandle());
  150. $pkey->addAttribute('name', $this->getPermissionKeyName());
  151. $pkey->addAttribute('description', $this->getPermissionKeyDescription());
  152. $pkey->addAttribute('package', $this->getPackageHandle());
  153. $pkey->addAttribute('category', $category);
  154. $this->exportAccess($pkey);
  155. return $pkey;
  156. }
  157. public static function exportList($xml) {
  158. $categories = PermissionKeyCategory::getList();
  159. $pxml = $xml->addChild('permissionkeys');
  160. foreach($categories as $cat) {
  161. $permissions = PermissionKey::getList($cat->getPermissionKeyCategoryHandle());
  162. foreach($permissions as $p) {
  163. $p->export($pxml);
  164. }
  165. }
  166. }
  167. /**
  168. * Note, this queries both the pkgID found on the PermissionKeys table AND any permission keys of a special type
  169. * installed by that package, and any in categories by that package.
  170. */
  171. public static function getListByPackage($pkg) {
  172. $db = Loader::db();
  173. $kina[] = '-1';
  174. $kinb = $db->GetCol('select pkCategoryID from PermissionKeyCategories where pkgID = ?', $pkg->getPackageID());
  175. if (is_array($kinb)) {
  176. $kina = array_merge($kina, $kinb);
  177. }
  178. $kinstr = implode(',', $kina);
  179. $r = $db->Execute('select pkID, pkCategoryID from PermissionKeys where (pkgID = ? or pkCategoryID in (' . $kinstr . ')) order by pkID asc', array($pkg->getPackageID()));
  180. while ($row = $r->FetchRow()) {
  181. $pkc = PermissionKeyCategory::getByID($row['pkCategoryID']);
  182. $pk = $pkc->getPermissionKeyByID($row['pkID']);
  183. $list[] = $pk;
  184. }
  185. $r->Close();
  186. return $list;
  187. }
  188. public static function import(SimpleXMLElement $pk) {
  189. $pkCategoryHandle = $pk['category'];
  190. $pkg = false;
  191. if ($pk['package']) {
  192. $pkg = Package::getByHandle($pk['package']);
  193. }
  194. $pkCanTriggerWorkflow = 0;
  195. if ($pk['can-trigger-workflow']) {
  196. $pkCanTriggerWorkflow = 1;
  197. }
  198. $pkHasCustomClass = 0;
  199. if ($pk['has-custom-class']) {
  200. $pkHasCustomClass = 1;
  201. }
  202. $pkn = self::add($pkCategoryHandle, $pk['handle'], $pk['name'], $pk['description'], $pkCanTriggerWorkflow, $pkHasCustomClass, $pkg);
  203. return $pkn;
  204. }
  205. public static function getByID($pkID) {
  206. $keys = CacheLocal::getEntry('permission_keys', false);
  207. if (!is_array($keys)) {
  208. $keys = self::loadAll();
  209. }
  210. return $keys[$pkID];
  211. }
  212. public static function getByHandle($pkHandle) {
  213. $keys = CacheLocal::getEntry('permission_keys', false);
  214. if (!is_array($keys)) {
  215. $keys = self::loadAll();
  216. }
  217. return $keys[$pkHandle];
  218. }
  219. /**
  220. * Adds an permission key.
  221. */
  222. public static function add($pkCategoryHandle, $pkHandle, $pkName, $pkDescription, $pkCanTriggerWorkflow, $pkHasCustomClass, $pkg = false) {
  223. $vn = Loader::helper('validation/numbers');
  224. $txt = Loader::helper('text');
  225. $pkgID = 0;
  226. $db = Loader::db();
  227. if (is_object($pkg)) {
  228. $pkgID = $pkg->getPackageID();
  229. }
  230. if ($pkCanTriggerWorkflow) {
  231. $pkCanTriggerWorkflow = 1;
  232. } else {
  233. $pkCanTriggerWorkflow = 0;
  234. }
  235. if ($pkHasCustomClass) {
  236. $pkHasCustomClass = 1;
  237. } else {
  238. $pkHasCustomClass = 0;
  239. }
  240. $pkCategoryID = $db->GetOne("select pkCategoryID from PermissionKeyCategories where pkCategoryHandle = ?", $pkCategoryHandle);
  241. $a = array($pkHandle, $pkName, $pkDescription, $pkCategoryID, $pkCanTriggerWorkflow, $pkHasCustomClass, $pkgID);
  242. $r = $db->query("insert into PermissionKeys (pkHandle, pkName, pkDescription, pkCategoryID, pkCanTriggerWorkflow, pkHasCustomClass, pkgID) values (?, ?, ?, ?, ?, ?, ?)", $a);
  243. if ($r) {
  244. $pkID = $db->Insert_ID();
  245. $keys = self::loadAll();
  246. return $keys[$pkID];
  247. }
  248. }
  249. /**
  250. * @access private
  251. * legacy support
  252. */
  253. public function can() {
  254. return $this->validate();
  255. }
  256. public function validate() {
  257. $u = new User();
  258. if ($u->isSuperUser()) {
  259. return true;
  260. }
  261. $r = PermissionCache::validate($this);
  262. if ($r !== -1) {
  263. return $r;
  264. }
  265. $pae = $this->getPermissionAccessObject();
  266. if (is_object($pae)) {
  267. $valid = $pae->validate();
  268. } else {
  269. $valid = false;
  270. }
  271. PermissionCache::addValidate($this, $valid);
  272. return $valid;
  273. }
  274. public function delete() {
  275. $db = Loader::db();
  276. $db->Execute('delete from PermissionKeys where pkID = ?', array($this->getPermissionKeyID()));
  277. self::loadAll();
  278. }
  279. /**
  280. * A shortcut for grabbing the current assignment and passing into that object
  281. */
  282. public function getAccessListItems() {
  283. $args = func_get_args();
  284. $obj = $this->getPermissionAccessObject();
  285. if (is_object($obj)) {
  286. return call_user_func_array(array($obj, 'getAccessListItems'), $args);
  287. } else {
  288. return array();
  289. }
  290. }
  291. public function getPermissionAssignmentObject() {
  292. if (is_object($this->permissionObject)) {
  293. if (method_exists($this->permissionObject, 'getPermissionObjectPermissionKeyCategoryHandle')) {
  294. $objectClass = Loader::helper('text')->camelcase($this->permissionObject->getPermissionObjectPermissionKeyCategoryHandle());
  295. } else {
  296. $objectClass = get_class($this->permissionObject);
  297. }
  298. $class = $objectClass . 'PermissionAssignment';
  299. if (!class_exists($class)) {
  300. if ($this->permissionObject instanceof Page) {
  301. $class = 'PagePermissionAssignment';
  302. } else if ($this->permissionObject instanceof Area) {
  303. $class = 'AreaPermissionAssignment';
  304. }
  305. }
  306. $targ = new $class();
  307. $targ->setPermissionObject($this->permissionObject);
  308. } else {
  309. $targ = new PermissionAssignment();
  310. }
  311. $targ->setPermissionKeyObject($this);
  312. return $targ;
  313. }
  314. public function getPermissionAccessObject() {
  315. $targ = $this->getPermissionAssignmentObject();
  316. return $targ->getPermissionAccessObject();
  317. }
  318. public function getPermissionAccessID() {
  319. $pa = $this->getPermissionAccessObject();
  320. if (is_object($pa)) {
  321. return $pa->getPermissionAccessID();
  322. }
  323. }
  324. public function exportAccess($pxml) {
  325. // by default we don't. but tasks do
  326. }
  327. }