From 84031701b5f396ae1c79b4ac0ee92fed25583fc0 Mon Sep 17 00:00:00 2001
From: chuntaojun <liaochuntao@live.com>
Date: Thu, 26 Sep 2024 16:38:53 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=E9=89=B4=E6=9D=83=E8=83=BD=E5=8A=9B?=
 =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B0=83=E6=95=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 release/conf/polaris-server.yaml       | 32 +++++++++++++-------------
 store/mysql/role.go                    |  4 ++--
 store/mysql/scripts/polaris_server.sql | 12 +++++++---
 store/mysql/strategy.go                |  6 ++---
 4 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/release/conf/polaris-server.yaml b/release/conf/polaris-server.yaml
index 2d1558136..8fb71bd9c 100644
--- a/release/conf/polaris-server.yaml
+++ b/release/conf/polaris-server.yaml
@@ -448,23 +448,23 @@ maintain:
 # Storage configuration
 store:
   # # Standalone file storage plugin
-  name: boltdbStore
-  option:
-    path: ./polaris.bolt
-    loadFile: ./conf/bolt-data.yaml
-  ## Database storage plugin
-  # name: defaultStore
+  # name: boltdbStore
   # option:
-  #   master:
-  #     dbType: mysql
-  #     dbName: polaris_server
-  #     dbUser: ${MYSQL_USER} ##DB_USER##
-  #     dbPwd: ${MYSQL_PWD} ##DB_PWD##
-  #     dbAddr: ${MYSQL_HOST} ##DB_ADDR##
-  #     maxOpenConns: 300
-  #     maxIdleConns: 50
-  #     connMaxLifetime: 300 # Unit second
-  #     txIsolationLevel: 2 #LevelReadCommitted
+  #   path: ./polaris.bolt
+  #   loadFile: ./conf/bolt-data.yaml
+  # Database storage plugin
+  name: defaultStore
+  option:
+    master:
+      dbType: mysql
+      dbName: polaris_server
+      dbUser: ${MYSQL_USER} ##DB_USER##
+      dbPwd: ${MYSQL_PWD} ##DB_PWD##
+      dbAddr: ${MYSQL_HOST} ##DB_ADDR##
+      maxOpenConns: 300
+      maxIdleConns: 50
+      connMaxLifetime: 300 # Unit second
+      txIsolationLevel: 2 #LevelReadCommitted
 # polaris-server plugin settings
 plugin:
   crypto:
diff --git a/store/mysql/role.go b/store/mysql/role.go
index dfe353626..3ec6d9bf5 100644
--- a/store/mysql/role.go
+++ b/store/mysql/role.go
@@ -70,7 +70,7 @@ func (s *roleStore) savePrincipals(tx *BaseTx, role *authcommon.Role) error {
 		return err
 	}
 
-	insertTpl := "INSERT INTO auth_role_principal(role_id, principal_id, principal_role, extend_info) VALUES (?, ?, ?)"
+	insertTpl := "INSERT INTO auth_role_principal(role_id, principal_id, principal_role, IFNULL(extend_info, '')) VALUES (?, ?, ?)"
 
 	for i := range role.Users {
 		args := []interface{}{role.ID, role.Users[i].PrincipalID, authcommon.PrincipalUser, utils.MustJson(role.Users[i].Extend)}
@@ -267,7 +267,7 @@ func (s *roleStore) GetMoreRoles(firstUpdate bool, mtime time.Time) ([]*authcomm
 }
 
 func (s *roleStore) fetchRolePrincipals(tx *BaseTx, role *authcommon.Role) error {
-	rows, err := tx.Query("SELECT role_id, principal_id, principal_role, extend_info FROM auth_role_principal WHERE rold_id = ?", role.ID)
+	rows, err := tx.Query("SELECT role_id, principal_id, principal_role, IFNULL(extend_info, '') FROM auth_role_principal WHERE rold_id = ?", role.ID)
 	if err != nil {
 		log.Error("[store][role] fetch role principals", zap.String("name", role.Name), zap.Error(err))
 		return store.Error(err)
diff --git a/store/mysql/scripts/polaris_server.sql b/store/mysql/scripts/polaris_server.sql
index 4d4afb714..f7a470a58 100644
--- a/store/mysql/scripts/polaris_server.sql
+++ b/store/mysql/scripts/polaris_server.sql
@@ -1009,6 +1009,7 @@ INSERT INTO
         `owner`,
         `comment`,
         `default`,
+        `source`,
         `revision`,
         `flag`,
         `ctime`,
@@ -1022,6 +1023,7 @@ VALUES
         '65e4789a6d5b49669adf1e9e8387549c',
         'default admin',
         1,
+        'Polaris',
         'fbca9bfa04ae4ead86e1ecf5811e32a9',
         0,
         sysdate(),
@@ -1146,6 +1148,7 @@ INSERT INTO
         `owner`,
         `comment`,
         `default`,
+        `source`,
         `revision`,
         `flag`,
         `ctime`,
@@ -1159,6 +1162,7 @@ VALUES
         '65e4789a6d5b49669adf1e9e8387549c',
         'global resources read onyly',
         1,
+        'Polaris',
         'fbca9bfa04ae4ead86e1ecf5811e32a9',
         0,
         sysdate(),
@@ -1261,15 +1265,15 @@ VALUES
 
 INSERT INTO
     auth_strategy_function (`strategy_id`, `function`) VALUES (
-        'fbca9bfa04ae4ead86e1ecf5811e32a9',
+        'bfa04ae1e32a94fbca9ead86e1ecf581',
         'Describe*'
     ),
     (
-        'fbca9bfa04ae4ead86e1ecf5811e32a9',
+        'bfa04ae1e32a94fbca9ead86e1ecf581',
         'List*'
     ),
     (
-        'fbca9bfa04ae4ead86e1ecf5811e32a9',
+        'bfa04ae1e32a94fbca9ead86e1ecf581',
         'Get*'
     );
 
@@ -1283,6 +1287,7 @@ INSERT INTO
         `owner`,
         `comment`,
         `default`,
+        `source`,
         `revision`,
         `flag`,
         `ctime`,
@@ -1296,6 +1301,7 @@ VALUES
         '65e4789a6d5b49669adf1e9e8387549c',
         'global resources read and write',
         1,
+        'Polaris',
         'fbca9bfa04ae4ead86e1ecf5811e32a9',
         0,
         sysdate(),
diff --git a/store/mysql/strategy.go b/store/mysql/strategy.go
index 23c8e096d..c55d69624 100644
--- a/store/mysql/strategy.go
+++ b/store/mysql/strategy.go
@@ -277,7 +277,7 @@ func (s *strategyStore) addPolicyPrincipals(tx *BaseTx, id string, principals []
 		return nil
 	}
 
-	savePrincipalSql := "INSERT IGNORE INTO auth_principal(strategy_id, principal_id, principal_role, extend_info) VALUES "
+	savePrincipalSql := "INSERT IGNORE INTO auth_principal(strategy_id, principal_id, principal_role, IFNULL(extend_info, '')) VALUES "
 	values := make([]string, 0)
 	args := make([]interface{}, 0)
 
@@ -613,7 +613,7 @@ func (s *strategyStore) GetStrategyResources(principalId string,
 
 func (s *strategyStore) getStrategyPrincipals(queryHander QueryHandler, id string) ([]authcommon.Principal, error) {
 
-	rows, err := queryHander("SELECT principal_id, principal_role, extend_info FROM auth_principal WHERE strategy_id = ?", id)
+	rows, err := queryHander("SELECT principal_id, principal_role, IFNULL(extend_info, '') FROM auth_principal WHERE strategy_id = ?", id)
 	if err != nil {
 		switch err {
 		case sql.ErrNoRows:
@@ -670,7 +670,7 @@ func (s *strategyStore) getStrategyConditions(queryHander QueryHandler, id strin
 
 func (s *strategyStore) getStrategyFunctions(queryHander QueryHandler, id string) ([]string, error) {
 
-	rows, err := queryHander("SELECT `function` FROM auth_strategy_label WHERE strategy_id = ?", id)
+	rows, err := queryHander("SELECT `function` FROM auth_strategy_function WHERE strategy_id = ?", id)
 	if err != nil {
 		switch err {
 		case sql.ErrNoRows: