From b1fa836304be1a82af580bd27903431296f1479b Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sun, 20 Oct 2024 01:24:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth/auth.go | 36 ++++++++++++++++++++++++++++++++++-- auth/model.go | 6 ++++++ auth/service.go | 24 ++++++++++++++++++++++++ constants/auth.go | 8 ++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 auth/model.go create mode 100644 auth/service.go create mode 100644 constants/auth.go diff --git a/auth/auth.go b/auth/auth.go index f8ebc53..daf4b03 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -1,5 +1,37 @@ package auth -func isRootUser(qqID string) bool { - return false +import ( + "git.lxtend.com/qqbot/handler" + "git.lxtend.com/qqbot/model" + "git.lxtend.com/qqbot/sqlite3" +) + +func init() { + authTableCreateSQL := ` + CREATE TABLE IF NOT EXISTS auth ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + qqid TEXT UNIQUE, + role INTEGER + ); + ` + sqlite3.TryCreateTable(authTableCreateSQL) +} + +func HasPermission(qqID int64, level int) bool { + auth, err := getAuth(qqID) + if err != nil { + return false + } + return auth.Role <= level +} + +func TryExecHandler(msg model.Message, level int, handler handler.Handler) (reply model.Reply) { + if HasPermission(msg.UserId, level) { + return handler(msg) + } + return model.Reply{ + ReplyMsg: "权限验证失败", + ReferOriginMsg: true, + FromMsg: msg, + } } diff --git a/auth/model.go b/auth/model.go new file mode 100644 index 0000000..192296b --- /dev/null +++ b/auth/model.go @@ -0,0 +1,6 @@ +package auth + +type Auth struct { + Qqid string `json:"qqid" db:"qqid"` + Role int `json:"role" db:"role"` +} diff --git a/auth/service.go b/auth/service.go new file mode 100644 index 0000000..73b952b --- /dev/null +++ b/auth/service.go @@ -0,0 +1,24 @@ +package auth + +import ( + "database/sql" + + "git.lxtend.com/qqbot/constants" + "git.lxtend.com/qqbot/sqlite3" +) + +func getAuth(qqID int64) (auth Auth, err error) { + tx, err := sqlite3.GetTran() + if err != nil { + return Auth{}, err + } + defer tx.Rollback() + err = tx.Get(&auth, "SELECT * FROM auth WHERE qqid = ?", qqID) + if err == sql.ErrNoRows { + _, err = tx.Exec("INSERT INTO auth(qqid, role) VALUES(?, ?)", qqID, constants.LEVEL_USER) + } + if err != nil { + return Auth{}, err + } + return auth, nil +} diff --git a/constants/auth.go b/constants/auth.go new file mode 100644 index 0000000..1cd3eb6 --- /dev/null +++ b/constants/auth.go @@ -0,0 +1,8 @@ +package constants + +const ( + LEVEL_ROOT = 0 + LEVEL_ADMIN = 10 + LEVEL_USER = 20 //default + LEVEL_BANNED = 100 +)