AntAclBase.php 12 KB

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