AntAclBase.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. return $field;
  128. }
  129. }
  130. return null;
  131. }
  132. public function getXsdFieldType($fieldName) {
  133. $field = $this->_getField($fieldName);
  134. return $field['xsdType'];
  135. }
  136. public function getXsdMaxOccurs($fieldName) {
  137. $field = $this->_getField($fieldName);
  138. return $field['maxOccurs'];
  139. }
  140. public function getXsdMinOccurs($fieldName) {
  141. $field = $this->_getField($fieldName);
  142. return $field['minOccurs'];
  143. }
  144. public function getAttributesFromZasoby() {
  145. return [];// TODO: ...
  146. }
  147. public function getXsdFieldParam($fieldName, $paramKey) { // TODO: fetch from db
  148. return null;
  149. }
  150. // public function getXsdFieldParam($fieldName, $paramKey) { // TableAcl
  151. // return ($this->_schemaClass)
  152. // ? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
  153. // : null
  154. // ;
  155. // }
  156. // public function getXsdFieldParam($fieldName, $paramKey) { // SimpleSchema
  157. // if (empty($this->_simpleSchema['root'][$fieldName])) return null;
  158. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
  159. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
  160. // return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
  161. // }
  162. public function isGeomField($fieldName) {
  163. return ('the_geom' === $fieldName); // TODO: check by xsdType
  164. }
  165. public function isEnumerationField($fieldName) {
  166. return false; // TODO: ...
  167. }
  168. public function canWriteField($fieldName) {
  169. return false; // TODO: $this->getAclInfo($fieldName); @see canReadField
  170. }
  171. public function canCreateField($fieldName) {
  172. try {
  173. $fieldAclInfo = $this->getAclInfo($fieldName);
  174. DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})...");
  175. return ($fieldAclInfo['PERM_C'] > 0);
  176. } catch (Exception $e) {
  177. DBG::log($e);
  178. return false;
  179. }
  180. return false;
  181. }
  182. public function canReadField($fieldName) {
  183. try {
  184. $fieldAclInfo = $this->getAclInfo($fieldName);
  185. DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})...");
  186. return ($fieldAclInfo['PERM_R'] > 0 || $fieldAclInfo['PERM_V'] > 0 || $fieldAclInfo['PERM_O'] > 0);
  187. } catch (Exception $e) {
  188. DBG::log($e);
  189. return false;
  190. }
  191. return false;
  192. }
  193. public function canReadObjectField($fieldName, $object) {
  194. return true; // TODO: $this->getAclInfo($fieldName); @see canReadField
  195. }
  196. public function canWriteObjectField($fieldName, $record) {
  197. return false; // TODO: $this->getAclInfo($fieldName); @see canReadField
  198. }
  199. public function getAclInfo($fieldName = null) {
  200. static $_aclInfo = []; // [ fieldName => [ id => {idZasob}, perms => 'RWX...' ] // TODO: , sort_prio => {SORT_PRIO}, label => {CELL_LABEL} ]
  201. if (!array_key_exists($this->getID(), $_aclInfo)) {
  202. $_aclInfo[ $this->getID() ] = [];
  203. $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_... ]
  204. DBG::log($fieldsConfig, 'array', "AntAclBase: User::getAcl()->getPermsForTable(".$this->getID().");");
  205. // $this->initFieldsFromConfig($fieldsConfig);
  206. $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ];
  207. foreach ($fieldsConfig as $row) {
  208. $nameField = $row['CELL_NAME'];
  209. $_aclInfo[ $this->getID() ][ $nameField ] = [
  210. 'idZasob' => $row['ID_CELL'],
  211. 'label' => $row['CELL_LABEL'],
  212. 'sort_prio' => $row['SORT_PRIO'],
  213. ];
  214. foreach ($permCols as $colPerm) $_aclInfo[ $this->getID() ][ $nameField ][ $colPerm ] = (int)$row[ $colPerm ];
  215. }
  216. }
  217. if ($fieldName && !array_key_exists($fieldName, $_aclInfo[ $this->getID() ])) {
  218. throw new Exception("Field not exists or missing access '{$fieldName}'");
  219. }
  220. return ($fieldName) ? $_aclInfo[ $this->getID() ][ $fieldName ] : $_aclInfo[ $this->getID() ];
  221. }
  222. public function getFields() { // TODO: conflict return structure with TableAcl
  223. if (empty($this->_fields)) {
  224. // TODO: fetch fields from DB
  225. // Lib::loadClass('SchemaFactory');
  226. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  227. // $item = $objectStorage->getItem($namespace, [
  228. // 'propertyName' => '*,field'
  229. // ]);
  230. }
  231. $fields = array_filter($this->_fields, function ($field) {
  232. return ($field['isActive']);
  233. });
  234. return array_map(function ($field) {
  235. $field['name'] = $field['fieldNamespace'];
  236. return $field;
  237. }, $fields);
  238. }
  239. public function _getField($fieldName) {
  240. foreach ($this->getFields() as $field) {
  241. if ($fieldName === $field['fieldNamespace']) return $field;
  242. }
  243. throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
  244. }
  245. public function getSqlPrimaryKeyField() { return $this->_primaryKey; }
  246. public function getTotal($params = []) {
  247. DBG::log($params, 'array', "AntAclBase::getTotal params");
  248. $idInstance = ACL::getInstanceId($this->_namespace);
  249. $instanceTable = ACL::getInstanceTable($this->_namespace);
  250. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  251. return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
  252. select count(1)
  253. from `{$this->_rootTableName}` t
  254. join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
  255. ");
  256. }
  257. public function getItems($params = []) {
  258. DBG::log($params, 'array', "AntAclBase::getItems params");
  259. // $sql->limit = V::get('limit', 10, $params, 'int');
  260. // $sql->offset = V::get('limitstart', 0, $params, 'int');
  261. $limit = V::get('limit', 0, $params, 'int');
  262. $limit = ($limit < 0) ? 0 : $limit;
  263. $offset = V::get('limitstart', 0, $params, 'int');
  264. $offset = ($offset < 0) ? 0 : $offset;
  265. $sqlLimit = ($limit > 0)
  266. ? "limit {$limit} offset {$offset}"
  267. : '';
  268. $idInstance = ACL::getInstanceId($this->_namespace);
  269. $instanceTable = ACL::getInstanceTable($this->_namespace);
  270. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  271. {
  272. $filtrIsInstance = [$this->_namespace];
  273. $filtrIsNotInstance = [];
  274. if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
  275. if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
  276. }
  277. return ACL::query($this)
  278. ->isInstance($filtrIsInstance)
  279. ->isNotInstance($filtrIsNotInstance)
  280. ->select('*') // TODO: fields
  281. ->select(!empty($params['@instances']) ? '@instances' : '')
  282. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
  283. ->limit($limit)
  284. ->offset($offset)
  285. ->execute();
  286. }
  287. public function getItem($primaryKey, $params = []) {
  288. return $this->buildQuery($params)->getItem($primaryKey);
  289. }
  290. public static function buildInstance($idZasob, $conf = []) {
  291. static $_cache;
  292. if (!$_cache) $_cache = array();
  293. if (array_key_exists($idZasob, $_cache)) {
  294. return $_cache[$idZasob];
  295. }
  296. if (empty($conf)) throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
  297. DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
  298. $acl = new AntAclBase($idZasob);
  299. $acl->_name = $conf['name'];
  300. $acl->_rootTableName = $conf['_rootTableName'];
  301. $acl->_db = $conf['idDatabase'];
  302. $acl->_namespace = $conf['namespace'];
  303. $acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
  304. $acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions - TODO: use ACL::getObjectFields
  305. $acl->_primaryKey = (!empty($conf['primaryKey'])) ? $conf['primaryKey'] : 'ID'; // $conf['primaryKey'];
  306. $_cache[$idZasob] = $acl;
  307. return $_cache[$idZasob];
  308. }
  309. public function buildQuery($params = array()) {
  310. Lib::loadClass('AclQueryFeatures');
  311. return new AclQueryFeatures($this, $params, $legacyMode = false);
  312. }
  313. public function getInstanceList() {
  314. $rootTableName = $this->_rootTableName;
  315. return array_map(function ($row) use ($rootTableName) {
  316. return $row['name'];
  317. }, SchemaFactory::loadDefaultObject('SystemObject')->getItems([
  318. 'propertyName' => 'name', // TODO: SystemObject fix propertyName
  319. 'f__rootTableName' => "={$rootTableName}",
  320. 'f__type' => "=AntAcl",
  321. 'f_isObjectActive' => "=1",
  322. ])
  323. );
  324. }
  325. public function getLocalFieldList() {
  326. return array_map(function ($field) {
  327. return $field['fieldNamespace'];
  328. }, array_filter($this->getFields(), function ($field) {
  329. return ($field['isLocal']);
  330. }));
  331. }
  332. public function isLocalField($fieldName) {
  333. return $this->_getField($fieldName)['isLocal'];
  334. }
  335. public function init($force = false) { }
  336. public function isInitialized($force = false) { return true; }
  337. }