AntAclBase.php 12 KB

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