From 5fd2e7bbdd9faebc08ee0970aec50e83a631fc2b Mon Sep 17 00:00:00 2001 From: Thun_Ann <2865813065@qq.com> Date: Wed, 5 Mar 2025 16:27:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(database):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=8F=98=E9=87=8F=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=92=8C=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=8D=E5=AE=89=E5=85=A8=E6=96=B9=E6=B3=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=BC=80=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=9A=84=E5=88=9B=E5=BB=BA=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/api.go | 107 +++++++++++++++++++++----- core/app_admin.go | 20 +++-- database/database.go | 26 +++++-- wba/wind.go | 178 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 282 insertions(+), 49 deletions(-) diff --git a/core/api.go b/core/api.go index 3b1e3d9..34f34dc 100644 --- a/core/api.go +++ b/core/api.go @@ -821,33 +821,65 @@ func (a *apiInfo) Log(content string, args ...interface{}) { //database模块 // //数据库部分允许字符串变量的读写操作,允许读取配置项操作 -type databaseInfo struct {} +type databaseInfo struct{} func (dbi *databaseInfo) varSet(app wba.AppInfo, datamap string, unit string, id string, key string, value string) { database.Set(app.Name, datamap, unit, id, key, value) } -func (dbi *databaseInfo) SetUserVariable(app wba.AppInfo, id string, key string, value string) { +func (dbi *databaseInfo) SetUserVariable(app wba.AppInfo, msg wba.MessageEventInfo, key string, value string) { + id := fmt.Sprintf("%d", msg.UserId) dbi.varSet(app, app.Name, "user", id, key, value) } -func (dbi *databaseInfo) SetGroupVariable(app wba.AppInfo, id string, key string, value string) { +func (dbi *databaseInfo) SetGroupVariable(app wba.AppInfo, msg wba.MessageEventInfo, key string, value string) { + var id string + if msg.MessageType == "group" { + id = "group_" + fmt.Sprintf("%d", msg.GroupId) + } + if msg.MessageType == "private" { + id = "user_" + fmt.Sprintf("%d", msg.UserId) + } dbi.varSet(app, app.Name, "group", id, key, value) } -func (dbi *databaseInfo) SetGlobalVariable(app wba.AppInfo, id string, key string, value string) { - dbi.varSet(app, app.Name, "global", id, key, value) -} - -func (dbi *databaseInfo) SetOutUserVariable(app wba.AppInfo, datamap string, id string, key string, value string) { +func (dbi *databaseInfo) SetOutUserVariable(app wba.AppInfo, datamap string, msg wba.MessageEventInfo, key string, value string) { + id := fmt.Sprintf("%d", msg.UserId) dbi.varSet(app, datamap, "user", id, key, value) } -func (dbi *databaseInfo) SetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string, value string) { +func (dbi *databaseInfo) SetOutGroupVariable(app wba.AppInfo, datamap string, msg wba.MessageEventInfo, key string, value string) { + var id string + if msg.MessageType == "group" { + id = "group_" + fmt.Sprintf("%d", msg.GroupId) + } + if msg.MessageType == "private" { + id = "user_" + fmt.Sprintf("%d", msg.UserId) + } dbi.varSet(app, datamap, "group", id, key, value) } -func (dbi *databaseInfo) SetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string, value string) { +func (dbi *databaseInfo) UnsafelySetUserVariable(app wba.AppInfo, id string, key string, value string) { + dbi.varSet(app, app.Name, "user", id, key, value) +} + +func (dbi *databaseInfo) UnsafelySetGroupVariable(app wba.AppInfo, id string, key string, value string) { + dbi.varSet(app, app.Name, "group", id, key, value) +} + +func (dbi *databaseInfo) UnsafelySetGlobalVariable(app wba.AppInfo, id string, key string, value string) { + dbi.varSet(app, app.Name, "global", id, key, value) +} + +func (dbi *databaseInfo) UnsafelySetOutUserVariable(app wba.AppInfo, datamap string, id string, key string, value string) { + dbi.varSet(app, datamap, "user", id, key, value) +} + +func (dbi *databaseInfo) UnsafelySetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string, value string) { + dbi.varSet(app, datamap, "group", id, key, value) +} + +func (dbi *databaseInfo) UnsafelySetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string, value string) { dbi.varSet(app, datamap, "global", id, key, value) } @@ -863,27 +895,59 @@ func (dbi *databaseInfo) varGet(app wba.AppInfo, datamap string, unit string, id return resStr, true } -func (dbi *databaseInfo) GetUserVariable(app wba.AppInfo, id string, key string) (string, bool) { +func (dbi *databaseInfo) GetUserVariable(app wba.AppInfo, msg wba.MessageEventInfo, key string) (string, bool) { + id := fmt.Sprintf("%d", msg.UserId) return dbi.varGet(app, app.Name, "user", id, key) } -func (dbi *databaseInfo) GetGroupVariable(app wba.AppInfo, id string, key string) (string, bool) { +func (dbi *databaseInfo) GetGroupVariable(app wba.AppInfo, msg wba.MessageEventInfo, key string) (string, bool) { + var id string + if msg.MessageType == "group" { + id = "group_" + fmt.Sprintf("%d", msg.GroupId) + } + if msg.MessageType == "private" { + id = "user_" + fmt.Sprintf("%d", msg.UserId) + } return dbi.varGet(app, app.Name, "group", id, key) } -func (dbi *databaseInfo) GetGlobalVariable(app wba.AppInfo, id string, key string) (string, bool) { - return dbi.varGet(app, app.Name, "global", id, key) -} - -func (dbi *databaseInfo) GetOutUserVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { +func (dbi *databaseInfo) GetOutUserVariable(app wba.AppInfo, datamap string, msg wba.MessageEventInfo, key string) (string, bool) { + id := fmt.Sprintf("%d", msg.UserId) return dbi.varGet(app, datamap, "user", id, key) } -func (dbi *databaseInfo) GetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { +func (dbi *databaseInfo) GetOutGroupVariable(app wba.AppInfo, datamap string, msg wba.MessageEventInfo, key string) (string, bool) { + var id string + if msg.MessageType == "group" { + id = "group_" + fmt.Sprintf("%d", msg.GroupId) + } + if msg.MessageType == "private" { + id = "user_" + fmt.Sprintf("%d", msg.UserId) + } return dbi.varGet(app, datamap, "group", id, key) } -func (dbi *databaseInfo) GetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { +func (dbi *databaseInfo) UnsafelyGetUserVariable(app wba.AppInfo, id string, key string) (string, bool) { + return dbi.varGet(app, app.Name, "user", id, key) +} + +func (dbi *databaseInfo) UnsafelyGetGroupVariable(app wba.AppInfo, id string, key string) (string, bool) { + return dbi.varGet(app, app.Name, "group", id, key) +} + +func (dbi *databaseInfo) UnsafelyGetGlobalVariable(app wba.AppInfo, id string, key string) (string, bool) { + return dbi.varGet(app, app.Name, "global", id, key) +} + +func (dbi *databaseInfo) UnsafelyGetOutUserVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { + return dbi.varGet(app, datamap, "user", id, key) +} + +func (dbi *databaseInfo) UnsafelyGetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { + return dbi.varGet(app, datamap, "group", id, key) +} + +func (dbi *databaseInfo) UnsafelyGetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) { return dbi.varGet(app, datamap, "global", id, key) } @@ -947,6 +1011,11 @@ func (dbi *databaseInfo) GetStringSliceConfig(app wba.AppInfo, datamap string, k return resSlice, true } +func (dbi *databaseInfo) UnsafelyCreatePublicDatamap(app wba.AppInfo, datamapId string) { + appName := app.Name + database.CreatePublicDatamap(appName, datamapId) +} + // 文件管理模块 //TODO: 文件管理模块待实现 diff --git a/core/app_admin.go b/core/app_admin.go index fbe99d8..0d6d2a7 100644 --- a/core/app_admin.go +++ b/core/app_admin.go @@ -4,10 +4,11 @@ import ( "ProjectWIND/LOG" "ProjectWIND/typed" "ProjectWIND/wba" - "github.com/dop251/goja" "os" "path/filepath" "strings" + + "github.com/dop251/goja" ) var CmdMap = make([]map[string]wba.Cmd, 4) @@ -116,21 +117,30 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i _ = wsp.Set("GetVersionInfo", AppApi.GetVersionInfo) _ = wsd.Set("SetUserVariable", DatabaseApi.SetUserVariable) _ = wsd.Set("SetGroupVariable", DatabaseApi.SetGroupVariable) - _ = wsd.Set("SetGlobalVariable", DatabaseApi.SetGlobalVariable) _ = wsd.Set("SetOutUserVariable", DatabaseApi.SetOutUserVariable) _ = wsd.Set("SetOutGroupVariable", DatabaseApi.SetOutGroupVariable) - _ = wsd.Set("SetOutGlobalVariable", DatabaseApi.SetOutGlobalVariable) + _ = wsd.Set("UnsafelySetUserVariable", DatabaseApi.UnsafelySetUserVariable) + _ = wsd.Set("UnsafelySetGroupVariable", DatabaseApi.UnsafelySetGroupVariable) + _ = wsd.Set("UnsafelySetGlobalVariable", DatabaseApi.UnsafelySetGlobalVariable) + _ = wsd.Set("UnsafelySetOutUserVariable", DatabaseApi.UnsafelySetOutUserVariable) + _ = wsd.Set("UnsafelySetOutGroupVariable", DatabaseApi.UnsafelySetOutGroupVariable) + _ = wsd.Set("UnsafelySetOutGlobalVariable", DatabaseApi.UnsafelySetOutGlobalVariable) _ = wsd.Set("GetUserVariable", DatabaseApi.GetUserVariable) _ = wsd.Set("GetGroupVariable", DatabaseApi.GetGroupVariable) - _ = wsd.Set("GetGlobalVariable", DatabaseApi.GetGlobalVariable) _ = wsd.Set("GetOutUserVariable", DatabaseApi.GetOutUserVariable) _ = wsd.Set("GetOutGroupVariable", DatabaseApi.GetOutGroupVariable) - _ = wsd.Set("GetOutGlobalVariable", DatabaseApi.GetOutGlobalVariable) + _ = wsd.Set("UnsafelyGetUserVariable", DatabaseApi.UnsafelyGetUserVariable) + _ = wsd.Set("UnsafelyGetGroupVariable", DatabaseApi.UnsafelyGetGroupVariable) + _ = wsd.Set("UnsafelyGetGlobalVariable", DatabaseApi.UnsafelyGetGlobalVariable) + _ = wsd.Set("UnsafelyGetOutUserVariable", DatabaseApi.UnsafelyGetOutUserVariable) + _ = wsd.Set("UnsafelyGetOutGroupVariable", DatabaseApi.UnsafelyGetOutGroupVariable) + _ = wsd.Set("UnsafelyGetOutGlobalVariable", DatabaseApi.UnsafelyGetOutGlobalVariable) _ = wsd.Set("GetIntConfig", DatabaseApi.GetIntConfig) _ = wsd.Set("GetFloatConfig", DatabaseApi.GetFloatConfig) _ = wsd.Set("GetStringConfig", DatabaseApi.GetStringConfig) _ = wsd.Set("GetIntSliceConfig", DatabaseApi.GetIntSliceConfig) _ = wsd.Set("GetStringSliceConfig", DatabaseApi.GetStringSliceConfig) + _ = wsd.Set("UnsafelyCreatePublicDatamap", DatabaseApi.UnsafelyCreatePublicDatamap) // 获取AppInit函数 appInitVal := runtime.Get("AppInit") diff --git a/database/database.go b/database/database.go index 8b4c446..1270a98 100644 --- a/database/database.go +++ b/database/database.go @@ -481,7 +481,23 @@ func Start() { select {} // 阻塞 } -func CreatePublicDatamap(id string) { +func CreatePublicDatamap(appName string, id string) { + // 查询权限 + hash := getCorePassword() + if hash == "" { + // 删除数据表哈希 + dataSet(appName, "config", "hash", "", "", true, true) + } + datahash, ok := dataGet(appName, "config", "hash", "", true, true) + if !ok { + LOG.Error("[Error]:Error while get hash of %s", appName) + return + } + if hash != datahash { + LOG.Warn("[Warning]:App %s is not allowed to create public datamap", appName) + return + } + // 创建公开数据表 db, ok := DB.Datamaps[id] if !ok { @@ -532,9 +548,9 @@ func Get(appName string, datamap string, unit string, id string, key string, isG hash := getCorePassword() if hash == "" { // 删除数据表哈希 - dataSet(appName, "config", "hash", "", "", false, false) + dataSet(appName, "config", "hash", "", "", true, true) } - datahash, ok := dataGet(appName, "config", "hash", "", false, false) + datahash, ok := dataGet(appName, "config", "hash", "", true, true) if !ok { LOG.Error("[Error]:Error while get hash of %s", appName) } @@ -559,9 +575,9 @@ func Set(appName string, datamap string, unit string, id string, key string, val hash := getCorePassword() if hash == "" { // 删除数据表哈希 - dataSet(appName, "config", "hash", "", "", false, false) + dataSet(appName, "config", "hash", "", "", true, true) } - datahash, ok := dataGet(appName, "config", "hash", "", false, false) + datahash, ok := dataGet(appName, "config", "hash", "", true, true) if !ok { LOG.Error("[Error]:Error while get hash of %s", appName) } diff --git a/wba/wind.go b/wba/wind.go index 7606833..0deecd7 100644 --- a/wba/wind.go +++ b/wba/wind.go @@ -268,42 +268,174 @@ type WindStandardProtocolAPI interface { type WindStandardDataBaseAPI interface { // SetUserVariable 设置用户变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息事件信息。 + // - key: 变量名称。 + // - value: 变量值。 + SetUserVariable(app AppInfo, msg MessageEventInfo, key string, value string) + // SetGroupVariable 设置群组变量 - // SetGlobalVariable 设置全局变量 - // SetOutUserVarialbe 设置其他数据库中的用户变量(需要权限) - // SetOutGroupVarialbe 设置其他数据库中的群组变量(需要权限) - // SetOutGlobalVarialbe 设置其他数据库中的全局变量(需要权限) + // 参数: + // - app: 应用信息。 + // - msg: 消息事件信息。 + // - key: 变量名称。 + // - value: 变量值。 + SetGroupVariable(app AppInfo, msg MessageEventInfo, key string, value string) + + // SetOutUserVarialbe [需要master权限]设置其他数据库中的用户变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息事件信息。 + // - key: 变量名称。 + // - value: 变量值。 + // - datamap: 数据表名称。 + SetOutUserVariable(app AppInfo, datamap string, msg MessageEventInfo, key string, value string) + + // SetOutGroupVarialbe [需要master权限]设置其他数据库中的群组变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息事件信息。 + // - key: 变量名称。 + // - value: 变量值。 + // - datamap: 数据表名称。 + SetOutGroupVariable(app AppInfo, datamap string, msg MessageEventInfo, key string, value string) + + // UnsafelySetUserVariable [不安全][需要master权限]设置用户变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - value: 变量值。 + UnsafelySetUserVariable(app AppInfo, id string, key string, value string) + + // UnsafelySetGroupVariable [不安全][需要master权限]设置群组变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - value: 变量值。 + UnsafelySetGroupVariable(app AppInfo, id string, key string, value string) + + // UnsafelySetGlobalVariable [不安全][需要master权限]设置全局变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - value: 变量值。 + UnsafelySetGlobalVariable(app AppInfo, id string, key string, value string) + + // UnsafelySetOutUserVariable [不安全][需要master权限]设置其他数据库中的用户变量 // 参数: // - app: 应用信息。 // - id: 数据单元 ID。 // - key: 变量名称。 // - value: 变量值。 // - datamap: 数据表名称。 - SetUserVariable(app AppInfo, id string, key string, value string) - SetGroupVariable(app AppInfo, id string, key string, value string) - SetGlobalVariable(app AppInfo, id string, key string, value string) - SetOutUserVariable(app AppInfo, datamap string, id string, key string, value string) - SetOutGroupVariable(app AppInfo, datamap string, id string, key string, value string) - SetOutGlobalVariable(app AppInfo, datamap string, id string, key string, value string) + UnsafelySetOutUserVariable(app AppInfo, datamap string, id string, key string, value string) + + // UnsafelySetOutGroupVariable [不安全][需要master权限]设置其他数据库中的群组变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - value: 变量值。 + // - datamap: 数据表名称。 + UnsafelySetOutGroupVariable(app AppInfo, datamap string, id string, key string, value string) + + // UnsafelySetOutGlobalVariable [不安全][需要master权限]设置其他数据库中的全局变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - value: 变量值。 + // - datamap: 数据表名称。 + UnsafelySetOutGlobalVariable(app AppInfo, datamap string, id string, key string, value string) // GetUserVariable 获取用户变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息变量名称。 + // - key: 变量名称。 + // 返回: 变量值,是否存在。 + GetUserVariable(app AppInfo, msg MessageEventInfo, key string) (string, bool) + // GetGroupVariable 获取群组变量 - // GetGlobalVariable 获取全局变量 - // GetOutUserVariable 获取其他数据库中的用户变量(需要权限) - // GetOutGroupVariable 获取其他数据库中的群组变量(需要权限) - // GetOutGlobalVariable 获取其他数据库中的全局变量(需要权限) + // 参数: + // - app: 应用信息。 + // - msg: 消息变量名称。 + // - key: 变量名称。 + // 返回: 变量值,是否存在。 + GetGroupVariable(app AppInfo, msg MessageEventInfo, key string) (string, bool) + + // GetOutUserVariable [需要master权限]获取其他数据库中的用户变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息变量名称。 + // - key: 变量名称。 + // - datamap:数据表名称。 + // 返回: 变量值,是否存在。 + GetOutUserVariable(app AppInfo, datamap string, msg MessageEventInfo, key string) (string, bool) + + // GetOutGroupVariable [需要master权限]获取其他数据库中的群组变量 + // 参数: + // - app: 应用信息。 + // - msg: 消息变量名称。 + // - key: 变量名称。 + // - datamap:数据表名称。 + // 返回: 变量值,是否存在。 + GetOutGroupVariable(app AppInfo, datamap string, msg MessageEventInfo, key string) (string, bool) + + // UnsafelyGetUserVariable [不安全][需要master权限]获取用户变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // 返回: 变量值,是否存在。 + UnsafelyGetUserVariable(app AppInfo, id string, key string) (string, bool) + + // UnsafelyGetGroupVariable [不安全][需要master权限]获取群组变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // 返回: 变量值,是否存在。 + UnsafelyGetGroupVariable(app AppInfo, id string, key string) (string, bool) + + // UnsafelyGetGlobalVariable [不安全][需要master权限]获取全局变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // 返回: 变量值,是否存在。 + UnsafelyGetGlobalVariable(app AppInfo, id string, key string) (string, bool) + + // UnsafelyGetOutUserVariable [不安全][需要master权限]获取其他数据库中的用户变量 // 参数: // - app: 应用信息。 // - id: 数据单元 ID。 // - key: 变量名称。 // - datamap:数据表名称。 // 返回: 变量值,是否存在。 - GetUserVariable(app AppInfo, id string, key string) (string, bool) - GetGroupVariable(app AppInfo, id string, key string) (string, bool) - GetGlobalVariable(app AppInfo, id string, key string) (string, bool) - GetOutUserVariable(app AppInfo, datamap string, id string, key string) (string, bool) - GetOutGroupVariable(app AppInfo, datamap string, id string, key string) (string, bool) - GetOutGlobalVariable(app AppInfo, datamap string, id string, key string) (string, bool) + UnsafelyGetOutUserVariable(app AppInfo, datamap string, id string, key string) (string, bool) + + // UnsafelyGetOutGroupVariable [不安全][需要master权限]获取其他数据库中的群组变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - datamap:数据表名称。 + // 返回: 变量值,是否存在。 + UnsafelyGetOutGroupVariable(app AppInfo, datamap string, id string, key string) (string, bool) + + // UnsafelyGetOutGlobalVariable [不安全][需要master权限]获取其他数据库中的全局变量 + // 参数: + // - app: 应用信息。 + // - id: 数据单元 ID。 + // - key: 变量名称。 + // - datamap:数据表名称。 + // 返回: 变量值,是否存在。 + UnsafelyGetOutGlobalVariable(app AppInfo, datamap string, id string, key string) (string, bool) // GetIntConfig 获取指定数据单元的整数型配置。 // 参数: @@ -344,6 +476,12 @@ type WindStandardDataBaseAPI interface { // - key: 配置名称。 // 返回: 配置值,是否存在。 GetStringSliceConfig(app AppInfo, datamap string, key string) ([]string, bool) + + // CreatePublicDatamap [不安全][需要master权限]创建公共数据表 + // 参数: + // - app: 应用信息。 + // - datamapId: 数据表名称。 + UnsafelyCreatePublicDatamap(app AppInfo, datamapId string) } type AppInfo struct {