AntAclBase.php 13 KB

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