Просмотр исходного кода

added acl fields in SystemObjects, auto-update schema to version 2

Piotr Labudda 8 лет назад
Родитель
Сommit
59e9583d02
1 измененных файлов с 111 добавлено и 78 удалено
  1. 111 78
      SE/se-lib/Schema/SystemObjectStorageAcl.php

+ 111 - 78
SE/se-lib/Schema/SystemObjectStorageAcl.php

@@ -32,87 +32,120 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
 	];
 	// public $_rootTableName = 'CRM_LISTA_ZASOBOW';
 	public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT';
-	public $_version = '1';
-
-	// public function __construct($simpleSchema = null) {
-	//	 parent::__construct($simpleSchema);
-	//	 self::updateCacheIfNeeded(array($this, 'updateCache'));
-	// }
-
-	// public static function updateCacheIfNeeded($updateCallback) {
-	//	 static $_cacheUpdated = null;
-	//	 if (null !== $_cacheUpdated) return;
-	//	 $conf = DB::getPDO()->fetchFirst("
-	//	 select (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__last_cache_update_date') as lastCacheUpdate
-	//	 ,	(select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__force_cache_update') as forceCacheUpdate
-	//	 ,	(select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__schema_version') as schemaVersion
-	//	 ,	(select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end') as lastProcesIndexer
-	//	 ,	(
-	//	 select IF('N/S;' = A_RECORD_UPDATE_DATE or A_RECORD_UPDATE_DATE is null or '' = A_RECORD_UPDATE_DATE, A_RECORD_CREATE_DATE, A_RECORD_UPDATE_DATE) as lastUpdateDate
-	//	 from CRM_LISTA_ZASOBOW_HIST
-	//	 order by ID DESC
-	//	 limit 1
-	//	 ) as lastZasobyUpdate
-	//	 ");
-	//	 if ($conf['lastZasobyUpdate']) {// fix date format - convert to Mysql date time format '2002-05-30 09:00:00'
-	//	 if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\-\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // 'Y-m-d-H:i'
-	//	 $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11) . ":00";
-	//	 } else if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\-\d\d:\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // 'Y-m-d-H:i:s'
-	//	 $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11);
-	//	 } else if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\ \d\d:\d\d:\d\d$/', $conf['lastZasobyUpdate'])) {
-	//	 $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11);
-	//	 }
-	//	 }
-	//	 // $lastProcesIndexer = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end' ");
-	//	 $updateReason = null;
-	//	 if ('1' == $conf['forceCacheUpdate']) $updateReason = 'forceCacheUpdate = 1';
-	//	 if (!$conf['lastCacheUpdate']) $updateReason = '!lastCacheUpdate';
-	//	 if (!$conf['schemaVersion']) $updateReason = '!schemaVersion';
-	//	 else if ($conf['schemaVersion'] < $this->_version) $updateReason = 'schemaVersion < ' . $this->_version;
-	//	 if ($conf['lastCacheUpdate'] && $conf['lastZasobyUpdate'] && $conf['lastCacheUpdate'] < $conf['lastZasobyUpdate']) $updateReason = 'lastZasobyUpdate';
-	//	 if ($updateReason) {
-	//	 DBG::simpleLog('schema', "SystemObject: update cache reason '{$updateReason}' '" . json_encode($conf) . "'");
-	//	 call_user_func($updateCallback);
-	//	 }
-	//	 DB::getPDO()->execSql("
-	//	 insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
-	//	 values('SystemObject__last_cache_update_date', NOW())
-	//	 on duplicate key update CONF_VAL = NOW()
-	//	 ");
-	//	 DB::getPDO()->execSql("
-	//	 insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
-	//	 values('SystemObject__schema_version', {$this->_version})
-	//	 on duplicate key update CONF_VAL = {$this->_version}
-	//	 ");
-	//	 if ('1' == $conf['forceCacheUpdate']) DB::getPDO()->execSql(" update CRM_CONFIG set CONF_VAL = '0' where CONF_KEY = 'SystemObject__force_cache_update' ");
-	//	 $_cacheUpdated = true;
-	// }
+	public $_version = '2';
+
+	public function __construct($simpleSchema = null) {
+		parent::__construct($simpleSchema);
+
+		$dbVersion = $this->getDBVersion();
+		if (!$dbVersion || $dbVersion < 2) { // version is 1 - upgrade to 2
+			DB::getPDO()->execSql("
+				create table if not exists `{$this->_rootTableName}` (
+					`idZasob` int(11) DEFAULT NULL,
+					`idDatabase` int(11) NOT NULL,
+					`namespace` varchar(255) DEFAULT '',
+					`_rootTableName` varchar(255) DEFAULT '',
+					`_type` varchar(255) DEFAULT '',
+					`hasStruct` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'has structure',
+					`isStructInstalled` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'has installed structure',
+					`isObjectActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'object is active',
+					`description` varchar(255) DEFAULT '',
+					`primaryKey` varchar(255) DEFAULT '',
+					`hasWriteGroupField` tinyint(1) NOT NULL DEFAULT 0,
+					`hasReadGroupField` tinyint(1) NOT NULL DEFAULT 0,
+					`hasOwnerField` tinyint(1) NOT NULL DEFAULT 0,
+					UNIQUE KEY `idZasob` (idZasob),
+					UNIQUE KEY `namespace` (namespace),
+					KEY `isObjectActive` (isObjectActive)
+				) ENGINE=MyISAM	DEFAULT CHARSET=latin2
+			");
+			try {
+				DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `primaryKey` VARCHAR(255) NOT NULL DEFAULT '' AFTER `description` ");
+			} catch (Exception $e) {
+				DBG::log($e);
+			}
+			try {
+				DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `hasWriteGroupField` tinyint(1) NOT NULL DEFAULT 0 ");
+			} catch (Exception $e) {
+				DBG::log($e);
+			}
+			try {
+				DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `hasReadGroupField` tinyint(1) NOT NULL DEFAULT 0 ");
+			} catch (Exception $e) {
+				DBG::log($e);
+			}
+			try {
+				DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `hasOwnerField` tinyint(1) NOT NULL DEFAULT 0 ");
+			} catch (Exception $e) {
+				DBG::log($e);
+			}
+
+			{ // Fix hasWriteGroupField, hasReadGroupField, hasOwnerField
+				// public function hasWriteGroupField() { // return $this->hasField('A_ADM_COMPANY');
+				// public function hasReadGroupField() { // return $this->hasField('A_CLASSIFIED');
+				// public function hasOwnerField() { // return $this->hasField('L_APPOITMENT_USER');
+				DB::getPDO()->execSql("
+					CREATE TABLE IF NOT EXISTS `tmp_cache_acl_fields` (
+					  `table_name` varchar(64) NOT NULL DEFAULT '',
+					  `column_name` varchar(64) NOT NULL DEFAULT '',
+					  UNIQUE KEY `table_2` (`table_name`,`column_name`),
+					  KEY `table` (`table_name`)
+					) ENGINE=MyISAM DEFAULT CHARSET=latin2
+				");
+				DB::getPDO()->execSql("
+					CREATE TABLE IF NOT EXISTS `tmp_cache_acl_group_fields` (
+					  `table_name` varchar(64) NOT NULL DEFAULT '',
+					  `has_write` tinyint(1) NOT NULL DEFAULT 0,
+					  `has_read` tinyint(1) NOT NULL DEFAULT 0,
+					  `has_owner` tinyint(1) NOT NULL DEFAULT 0,
+					  UNIQUE KEY `table_name` (`table_name`)
+					) ENGINE=MyISAM DEFAULT CHARSET=latin2
+				");
+				DB::getPDO()->execSql(" TRUNCATE TABLE `tmp_cache_acl_fields` ");
+				DB::getPDO()->execSql(" TRUNCATE TABLE `tmp_cache_acl_group_fields` ");
+				DB::getPDO()->execSql("
+					insert into tmp_cache_acl_group_fields(table_name, has_write, has_read, has_owner)
+						select c.TABLE_NAME as table_name
+							, IF(c.COLUMN_NAME = 'A_ADM_COMPANY', 1, 0) as has_write
+							, IF(c.COLUMN_NAME = 'A_CLASSIFIED', 1, 0) as has_read
+							, IF(c.COLUMN_NAME = 'L_APPOITMENT_USER', 1, 0) as has_owner
+						from information_schema.COLUMNS c
+							where c.TABLE_SCHEMA = 'SES_USERS2'
+						  and c.COLUMN_NAME in ('A_ADM_COMPANY', 'A_CLASSIFIED', 'L_APPOITMENT_USER')
+						ON DUPLICATE KEY UPDATE
+							has_write = has_write + IF(c.COLUMN_NAME = 'A_ADM_COMPANY', 1, 0),
+							has_read = has_read + IF(c.COLUMN_NAME = 'A_CLASSIFIED', 1, 0),
+							has_owner = has_owner + IF(c.COLUMN_NAME = 'L_APPOITMENT_USER', 1, 0)
+				");
+
+				DB::getPDO()->execSql("
+					update `CRM_#CACHE_ACL_OBJECT` c
+						join `tmp_cache_acl_group_fields` t on (t.table_name = c._rootTableName)
+					set
+						c.hasWriteGroupField = t.has_write,
+						c.hasReadGroupField = t.has_read,
+						c.hasOwnerField = t.has_owner
+				");
+			}
+
+			$dbVersion = 2; $this->updateDBVersion($dbVersion);
+		}
+
+		// if ($dbVersion < 3) {
+		//	// sql ...
+		// 	$dbVersion = 3; $this->updateDBVersion($dbVersion);
+		// }
+	}
+	function getDBVersion() {
+		return (int)DB::getPDO()->fetchValue(" select `CONF_VAL` from `CRM_CONFIG` where `CONF_KEY` = 'Schema_SystemObjectStorageAcl__version' ");
+	}
+	function updateDBVersion($version) {
+		DB::getPDO()->execSql(" REPLACE INTO CRM_CONFIG (`CONF_KEY`,`CONF_VAL`) VALUES ('Schema_SystemObjectStorageAcl__version', '{$version}') ");
+	}
 
 	public function updateCache($idDatabase = null) {
 		DBG::simpleLog('schema', "SystemObject::updateCache...");
 		// DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG
-		DB::getPDO()->execSql("
-			create table if not exists `{$this->_rootTableName}` (
-				`idZasob` int(11) DEFAULT NULL,
-				`idDatabase` int(11) NOT NULL,
-				`namespace` varchar(255) DEFAULT '',
-				`_rootTableName` varchar(255) DEFAULT '',
-				`_type` varchar(255) DEFAULT '',
-				`hasStruct` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'has structure',
-				`isStructInstalled` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'has installed structure',
-				`isObjectActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'object is active',
-				`description` varchar(255) DEFAULT '',
-				`primaryKey` varchar(255) DEFAULT '',
-				UNIQUE KEY `idZasob` (idZasob),
-				UNIQUE KEY `namespace` (namespace),
-				KEY `isObjectActive` (isObjectActive)
-			) ENGINE=MyISAM	DEFAULT CHARSET=latin2
-		");
-		try {
-			DB::getPDO()->execSql(" ALTER TABLE `{$this->_rootTableName}` ADD `primaryKey` VARCHAR(255) NOT NULL DEFAULT '' AFTER `description` ");
-		} catch (Exception $e) {
-			DBG::log($e);
-		}
 		DB::getPDO()->execSql(" update `{$this->_rootTableName}` set hasStruct = 0 ");
 		$idDefDB = DB::getPDO()->getZasobId();
 
@@ -162,7 +195,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
 				try {
 					$dbName = DB::getPDO($source['idZasob'])->getDatabaseName();
 					$dbType = DB::getPDO($source['idZasob'])->getType();
-					if ('mysql' == $dbType) {
+					if ('mysql' == $dbType) { // TODO: if the same database DB::getPDO($source['idZasob'])->getID === DB::getPDO()->getID
 						UI::alert('warning', "TODO: { id: {$source['idZasob']}, nsPrefix: '{$source['nsPrefix']}', dbName: '{$dbName}', dbType: '{$dbType}'} ...");
 						// TODO: if another DB split select and insert
 						DB::getPDO()->execSql("