AntAclBase.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. Lib::loadClass('ACL');
  3. Lib::loadClass('Core_AclBase');
  4. /**
  5. * SE/schema/ant-object/default_db.{rootTableName}/{name}/build.xml
  6. */
  7. class AntAclBase extends Core_AclBase {
  8. public function __construct($zasobID = 0) {
  9. $this->_zasobID = (int)$zasobID;
  10. $this->_name = '';
  11. $this->_namespace = '';
  12. $this->_rootTableName = '';
  13. $this->_db = 0; // database id zasobu
  14. $this->_rootNamespace = '';
  15. $this->_primaryKey = '';
  16. $this->_fields = [];
  17. $this->_zasobyInfoFetched = false;
  18. }
  19. public function getDB() { return $this->_db; }
  20. public function getName() { return $this->_name; }
  21. public function getNamespace() { return $this->_namespace; }
  22. public function getRootNamespace() { return $this->_rootNamespace; }
  23. public function getSourceName() { return 'default_db'; } // TODO: ?
  24. public function getRootTableName() { return $this->_rootTableName; }
  25. public function getPrimaryKeyField() { return $this->_primaryKey; }
  26. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  27. public function getVirtualFieldListByIdZasob() { return []; }
  28. // public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
  29. public function _fetchInfoFromZasobyIfNeeded() {
  30. if (!$this->_zasobyInfoFetched) {
  31. $zasobyIds = array_filter(
  32. array_map(function ($field) {
  33. return (int)$field['idZasob'];
  34. }, $this->_fields),
  35. function ($id) { return $id > 0; }
  36. );
  37. if (!empty($zasobyIds)) {
  38. DBG::log("DBG sort fields - TODO: ids [".implode(",", $zasobyIds)."]");
  39. $zasobyInfo = DB::getPDO()->fetchAllByKey("
  40. select z.ID, z.DESC_PL, z.OPIS, z.SORT_PRIO
  41. from CRM_LISTA_ZASOBOW z
  42. where z.ID in(".implode(",", $zasobyIds).")
  43. ", 'ID');
  44. DBG::log($zasobyInfo, 'array', "DBG sort fields - zasobyInfo");
  45. $maxSortPrio = 0;
  46. array_map(function ($zInfo) use (&$maxSortPrio) {
  47. if ($zInfo['SORT_PRIO'] > 0 && $zInfo['SORT_PRIO'] > $maxSortPrio) {
  48. $maxSortPrio = $zInfo['SORT_PRIO'];
  49. }
  50. }, $zasobyInfo);
  51. foreach ($this->_fields as $idx => $field) {
  52. // $this->_fields[$idx]['name'] = $field['fieldNamespace']; // TODO: BUG query for non existing fields - check if isLocal is used
  53. if ($field['idZasob'] > 0 && array_key_exists($field['idZasob'], $zasobyInfo)) {
  54. $this->_fields[$idx]['sort_prio'] = $zasobyInfo[ $field['idZasob'] ]['SORT_PRIO'];
  55. if (!empty($zasobyInfo[ $field['idZasob'] ]['DESC_PL'])) $this->_fields[$idx]['label'] = $zasobyInfo[ $field['idZasob'] ]['DESC_PL'];
  56. if (!empty($zasobyInfo[ $field['idZasob'] ]['OPIS'])) $this->_fields[$idx]['opis'] = $zasobyInfo[ $field['idZasob'] ]['OPIS'];
  57. } else { // !$field['idZasob'] => generate sortPrio
  58. $this->_fields[$idx]['sort_prio'] = ++$maxSortPrio;
  59. }
  60. }
  61. }
  62. $this->_zasobyInfoFetched = true;
  63. }
  64. usort($this->_fields, array($this, '_sortFieldsCallback'));
  65. DBG::log($this->_fields, 'array', "DBG sort fields - sorted \$this->_fields");
  66. }
  67. public function _sortFieldsCallback($a, $b) {
  68. if ($a['name'] == 'ID') {
  69. return -1;
  70. }
  71. else if ($b['name'] == 'ID') {
  72. return 1;
  73. }
  74. else if ($a['sort_prio'] < $b['sort_prio']) {
  75. return -1;
  76. }
  77. else if ($a['sort_prio'] > $b['sort_prio']) {
  78. return 1;
  79. }
  80. else {
  81. return 0;
  82. }
  83. }
  84. public function getVisibleFieldListByIdZasob() {
  85. $this->_fetchInfoFromZasobyIfNeeded();
  86. $fields = $this->getRealFieldListByIdZasob();
  87. $pkField = $this->getPrimaryKeyField();
  88. $cols = array();
  89. foreach ($fields as $kFieldID => $fieldName) {
  90. if ($pkField === $fieldName) {
  91. $id = $kFieldID;
  92. break;
  93. }
  94. }
  95. $cols[$id] = 'ID'; // TODO: why rename primary key field to ID?
  96. foreach ($fields as $kFieldID => $fieldName) {
  97. if ($pkField === $fieldName) continue;
  98. $cols[$kFieldID] = $fieldName;
  99. }
  100. return $cols;
  101. }
  102. public function getRealFieldListByIdZasob() {
  103. $cols = array();
  104. $fakeZasobId = 1000000; // TODO: RMME
  105. foreach ($this->getFields() as $field) {
  106. $idZasobField = ($field['idZasob']) ? $field['idZasob'] : $fakeZasobId++; // TODO: RMME
  107. if (!$field['isActive']) continue;
  108. if (!$field['idZasob']) continue;
  109. $cols[$idZasobField] = $field['fieldNamespace'];
  110. }
  111. return $cols;
  112. }
  113. public function getFieldType($fieldName) { return null; }
  114. // try {
  115. // throw new Exception("TODO: AntAclBase::getFieldType({$fieldName})");
  116. // } catch (Exception $e) {
  117. // DBG::log($e);
  118. // }
  119. // $field = $this->_getField($fieldName);
  120. // return $field['xsdType'];
  121. // }
  122. public function getField($idField) {
  123. DBG::log($this->getFields(), 'array', "fields");
  124. foreach ($this->getFields() as $field) {
  125. if (!$field['isActive']) continue;
  126. if (!$field['idZasob']) continue;
  127. if ($idField == $field['idZasob']) {
  128. $field['name'] = $field['fieldNamespace'];
  129. return $field;
  130. }
  131. }
  132. return null;
  133. }
  134. public function getXsdFieldType($fieldName) {
  135. $field = $this->_getField($fieldName);
  136. return $field['xsdType'];
  137. }
  138. public function getXsdMaxOccurs($fieldName) {
  139. $field = $this->_getField($fieldName);
  140. return $field['maxOccurs'];
  141. }
  142. public function getXsdMinOccurs($fieldName) {
  143. $field = $this->_getField($fieldName);
  144. return $field['minOccurs'];
  145. }
  146. public function getAttributesFromZasoby() {
  147. return [];// TODO: ...
  148. }
  149. public function getXsdFieldParam($fieldName, $paramKey) { // TODO: fetch from db
  150. return null;
  151. }
  152. // public function getXsdFieldParam($fieldName, $paramKey) { // TableAcl
  153. // return ($this->_schemaClass)
  154. // ? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
  155. // : null
  156. // ;
  157. // }
  158. // public function getXsdFieldParam($fieldName, $paramKey) { // SimpleSchema
  159. // if (empty($this->_simpleSchema['root'][$fieldName])) return null;
  160. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
  161. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
  162. // return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
  163. // }
  164. public function isGeomField($fieldName) {
  165. return ('the_geom' === $fieldName); // TODO: check by xsdType
  166. }
  167. public function isEnumerationField($fieldName) {
  168. return false; // TODO: ...
  169. }
  170. public function canWriteField($fieldName) {
  171. return false; // TODO: $this->getAclInfo($fieldName); @see canReadField
  172. }
  173. public function canCreateField($fieldName) {
  174. try {
  175. $fieldAclInfo = $this->getAclInfo($fieldName);
  176. DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})...");
  177. return ($fieldAclInfo['PERM_C'] > 0);
  178. } catch (Exception $e) {
  179. DBG::log($e);
  180. return false;
  181. }
  182. return false;
  183. }
  184. public function canReadField($fieldName) {
  185. try {
  186. $fieldAclInfo = $this->getAclInfo($fieldName);
  187. DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})...");
  188. return ($fieldAclInfo['PERM_R'] > 0 || $fieldAclInfo['PERM_V'] > 0 || $fieldAclInfo['PERM_O'] > 0);
  189. } catch (Exception $e) {
  190. DBG::log($e);
  191. return false;
  192. }
  193. return false;
  194. }
  195. public function canReadObjectField($fieldName, $object) {
  196. return true; // TODO: $this->getAclInfo($fieldName); @see canReadField
  197. }
  198. public function canWriteObjectField($fieldName, $record) {
  199. return false; // TODO: $this->getAclInfo($fieldName); @see canReadField
  200. }
  201. public function getAclInfo($fieldName = null) {
  202. static $_aclInfo = []; // [ fieldName => [ id => {idZasob}, perms => 'RWX...' ] // TODO: , sort_prio => {SORT_PRIO}, label => {CELL_LABEL} ]
  203. if (!array_key_exists($this->getID(), $_aclInfo)) {
  204. $_aclInfo[ $this->getID() ] = [];
  205. $fieldsConfig = User::getAcl()->getPermsForTable($this->getID()); // @returns [ permInfo group by ID_CELL ]; permInfo = [ ID_CELL, CELL_NAME, CELL_LABEL, SORT_PRIO, PERM_R, PERM_W, PERM_... ]
  206. DBG::log($fieldsConfig, 'array', "AntAclBase: User::getAcl()->getPermsForTable(".$this->getID().");");
  207. // $this->initFieldsFromConfig($fieldsConfig);
  208. $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ];
  209. foreach ($fieldsConfig as $row) {
  210. $nameField = $row['CELL_NAME'];
  211. $_aclInfo[ $this->getID() ][ $nameField ] = [
  212. 'idZasob' => $row['ID_CELL'],
  213. 'label' => $row['CELL_LABEL'],
  214. 'sort_prio' => $row['SORT_PRIO'],
  215. ];
  216. foreach ($permCols as $colPerm) $_aclInfo[ $this->getID() ][ $nameField ][ $colPerm ] = (int)$row[ $colPerm ];
  217. }
  218. }
  219. if ($fieldName && !array_key_exists($fieldName, $_aclInfo[ $this->getID() ])) {
  220. throw new Exception("Field not exists or missing access '{$fieldName}'");
  221. }
  222. return ($fieldName) ? $_aclInfo[ $this->getID() ][ $fieldName ] : $_aclInfo[ $this->getID() ];
  223. }
  224. public function getFields() { // TODO: conflict return structure with TableAcl
  225. if (empty($this->_fields)) {
  226. // TODO: fetch fields from DB
  227. // Lib::loadClass('SchemaFactory');
  228. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  229. // $item = $objectStorage->getItem($namespace, [
  230. // 'propertyName' => '*,field'
  231. // ]);
  232. }
  233. return $this->_fields;
  234. }
  235. public function _getField($fieldName) {
  236. foreach ($this->getFields() as $field) {
  237. if ($fieldName === $field['fieldNamespace']) return $field;
  238. }
  239. throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
  240. }
  241. public function getSqlPrimaryKeyField() { return $this->_primaryKey; }
  242. public function getTotal($params = []) {
  243. DBG::log($params, 'array', "AntAclBase::getTotal params");
  244. $idInstance = ACL::getInstanceId($this->_namespace);
  245. $instanceTable = ACL::getInstanceTable($this->_namespace);
  246. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  247. return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
  248. select count(1)
  249. from `{$this->_rootTableName}` t
  250. join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
  251. ");
  252. }
  253. public function getItems($params = []) {
  254. DBG::log($params, 'array', "AntAclBase::getItems params");
  255. // $sql->limit = V::get('limit', 10, $params, 'int');
  256. // $sql->offset = V::get('limitstart', 0, $params, 'int');
  257. $limit = V::get('limit', 0, $params, 'int');
  258. $limit = ($limit < 0) ? 0 : $limit;
  259. $offset = V::get('limitstart', 0, $params, 'int');
  260. $offset = ($offset < 0) ? 0 : $offset;
  261. $sqlLimit = ($limit > 0)
  262. ? "limit {$limit} offset {$offset}"
  263. : '';
  264. $idInstance = ACL::getInstanceId($this->_namespace);
  265. $instanceTable = ACL::getInstanceTable($this->_namespace);
  266. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  267. {
  268. $filtrIsInstance = [$this->_namespace];
  269. $filtrIsNotInstance = [];
  270. if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
  271. if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
  272. }
  273. return ACL::query($this)
  274. ->isInstance($filtrIsInstance)
  275. ->isNotInstance($filtrIsNotInstance)
  276. ->select('*') // TODO: fields
  277. ->select(!empty($params['@instances']) ? '@instances' : '')
  278. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
  279. ->limit($limit)
  280. ->offset($offset)
  281. ->execute();
  282. }
  283. public function getItem($primaryKey, $params = []) {
  284. return $this->buildQuery($params)->getItem($primaryKey);
  285. }
  286. public static function buildInstance($idZasob, $conf = []) {
  287. static $_cache;
  288. if (!$_cache) $_cache = array();
  289. if (array_key_exists($idZasob, $_cache)) {
  290. return $_cache[$idZasob];
  291. }
  292. if (empty($conf)) throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
  293. DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
  294. $acl = new AntAclBase($idZasob);
  295. $acl->_name = $conf['name'];
  296. $acl->_rootTableName = $conf['_rootTableName'];
  297. $acl->_db = $conf['idDatabase'];
  298. $acl->_namespace = $conf['namespace'];
  299. $acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
  300. $acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions - TODO: use ACL::getObjectFields
  301. $acl->_primaryKey = (!empty($conf['primaryKey'])) ? $conf['primaryKey'] : 'ID'; // $conf['primaryKey'];
  302. $_cache[$idZasob] = $acl;
  303. return $_cache[$idZasob];
  304. }
  305. public function buildQuery($params = array()) {
  306. Lib::loadClass('AclQueryFeatures');
  307. return new AclQueryFeatures($this, $params, $legacyMode = false);
  308. }
  309. public function getInstanceList() {
  310. $rootTableName = $this->_rootTableName;
  311. return array_map(function ($row) use ($rootTableName) {
  312. return $row['name'];
  313. }, SchemaFactory::loadDefaultObject('SystemObject')->getItems([
  314. 'propertyName' => 'name', // TODO: SystemObject fix propertyName
  315. 'f__rootTableName' => "={$rootTableName}",
  316. 'f__type' => "=AntAcl",
  317. 'f_isObjectActive' => "=1",
  318. ])
  319. );
  320. }
  321. public function getLocalFieldList() {
  322. return array_map(function ($field) {
  323. return $field['fieldNamespace'];
  324. }, array_filter($this->getFields(), function ($field) {
  325. return ($field['isLocal']);
  326. }));
  327. }
  328. public function isLocalField($fieldName) {
  329. return $this->_getField($fieldName)['isLocal'];
  330. }
  331. public function init($force = false) { }
  332. public function isInitialized($force = false) { return true; }
  333. }