This commit is contained in:
veypi
2021-11-05 23:46:21 +08:00
parent d7aea82ced
commit bc3f5e0b0c
43 changed files with 1096 additions and 327 deletions
+21 -6
View File
@@ -8,7 +8,10 @@ import (
"gorm.io/gorm"
)
func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint) error {
func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint, status models.AUStatus) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
@@ -17,19 +20,25 @@ func AddUser(tx *gorm.DB, appID uint, userID uint, roleID uint) error {
return oerr.ResourceDuplicated
}
if errors.Is(err, gorm.ErrRecordNotFound) {
au.Status = status
err = tx.Create(au).Error
if err != nil {
return err
}
err = auth.BindUserRole(tx, userID, roleID)
if err != nil {
return err
if roleID > 0 {
err = auth.BindUserRole(tx, userID, roleID)
if err != nil {
return err
}
}
return tx.Model(&models.App{}).Where("id = ?", appID).Update("user_count", gorm.Expr("user_count + ?", 1)).Error
}
return err
}
func EnableUser(tx *gorm.DB, appID uint, userID uint) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
@@ -37,12 +46,18 @@ func EnableUser(tx *gorm.DB, appID uint, userID uint) error {
if err != nil {
return err
}
return tx.Where(au).Update("disabled", false).Error
if au.Status != models.AUOK {
return tx.Where(au).Update("status", models.AUOK).Error
}
return nil
}
func DisableUser(tx *gorm.DB, appID uint, userID uint) error {
if appID == 0 || userID == 0 {
return oerr.FuncArgsError
}
au := &models.AppUser{}
au.AppID = appID
au.UserID = userID
return tx.Where(au).Update("disabled", true).Error
return tx.Where(au).Update("status", models.AUDisable).Error
}
+39 -19
View File
@@ -3,10 +3,12 @@ package base
import (
"OneAuth/libs/oerr"
"OneAuth/libs/tools"
"errors"
"github.com/json-iterator/go"
"github.com/veypi/OneBD"
"github.com/veypi/OneBD/rfc"
"github.com/veypi/utils/log"
"gorm.io/gorm"
"strconv"
"sync"
"time"
@@ -14,6 +16,41 @@ import (
var json = jsoniter.ConfigFastest
func JSONResponse(m OneBD.Meta, data interface{}, err error) {
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
err = oerr.ResourceNotExist
}
}
if m.Method() == rfc.MethodHead {
if err != nil {
m.SetHeader("status", "0")
m.SetHeader("code", strconv.Itoa(int(oerr.OfType(err.Error()))))
m.SetHeader("err", err.Error())
} else {
m.SetHeader("status", "1")
}
return
}
res := map[string]interface{}{
"status": 1,
}
if err != nil {
res["status"] = 0
res["code"] = oerr.OfType(err.Error())
res["err"] = err.Error()
} else {
res["status"] = 1
res["content"] = data
}
p, err := json.Marshal(res)
if err != nil {
log.Warn().Err(err).Msg("encode json data error")
return
}
_, _ = m.Write(p)
}
type ApiHandler struct {
OneBD.BaseHandler
UserHandler
@@ -24,29 +61,12 @@ func (h *ApiHandler) Init(m OneBD.Meta) error {
}
func (h *ApiHandler) OnResponse(data interface{}) {
if h.Meta().Method() == rfc.MethodHead {
h.Meta().SetHeader("status", "1")
return
}
p, err := json.Marshal(map[string]interface{}{"status": 1, "content": data})
if err != nil {
log.Warn().Err(err).Msg("encode json data error")
return
}
h.Meta().Write(p)
JSONResponse(h.Meta(), data, nil)
}
func (h *ApiHandler) OnError(err error) {
log.WithNoCaller.Warn().Err(err).Msg(h.Meta().RequestPath())
msg := err.Error()
if h.Meta().Method() == rfc.MethodHead {
h.Meta().SetHeader("status", "0")
h.Meta().SetHeader("code", strconv.Itoa(int(oerr.OfType(msg))))
h.Meta().SetHeader("err", msg)
} else {
p, _ := json.Marshal(map[string]interface{}{"status": 0, "code": oerr.OfType(msg), "err": msg})
h.Meta().Write(p)
}
JSONResponse(h.Meta(), nil, err)
}
var ioNumLimit = make(map[string]time.Time)
+2 -1
View File
@@ -1,6 +1,7 @@
package base
import (
"OneAuth/cfg"
"OneAuth/libs/oerr"
"OneAuth/libs/token"
"OneAuth/models"
@@ -26,7 +27,7 @@ func (a *UserHandler) ParsePayload(m OneBD.Meta) error {
if tokenStr == "" {
return oerr.NotLogin
}
ok, err := token.ParseToken(tokenStr, a.Payload)
ok, err := token.ParseToken(tokenStr, a.Payload, cfg.CFG.APPKey)
if ok {
return nil
}
+10 -17
View File
@@ -1,7 +1,6 @@
package oerr
import (
"errors"
"gorm.io/gorm"
"strconv"
)
@@ -42,11 +41,13 @@ type Code uint
- 9 : 本不可能发生的错误,例如被人攻击导致数据异常产生的逻辑错误
*/
// Unknown error
const (
Unknown Code = 0
)
const (
// 2 数据库错误
// DBErr 2 数据库错误
// -1 系统错误
// -2 数据读写错误
DBErr Code = 20001
@@ -56,10 +57,13 @@ const (
)
const (
// 3
// LogicErr 3 系统内逻辑错误
LogicErr Code = 30000
AppNotJoin Code = 30001
)
const (
// NotLogin
// 4 权限类型错误
// 1: 登录权限
// 2: 资源操作权限
@@ -130,6 +134,8 @@ var codeMap = map[Code]string{
NoAuth: "no auth to access",
AccessErr: "access error",
AccessTooFast: "access too fast",
LogicErr: "logic error",
AppNotJoin: "not join in app",
}
func (c Code) Error() string {
@@ -144,7 +150,7 @@ func (c Code) String() string {
return codeMap[Unknown]
}
// 附加错误详细原因
// Attach 附加错误详细原因
func (c Code) Attach(errs ...error) (e error) {
e = c
for _, err := range errs {
@@ -195,16 +201,3 @@ func (w *wrapErr) Error() string {
func (w *wrapErr) UnWrap() error {
return w.err
}
func CheckMultiErr(errs ...error) error {
msg := ""
for _, e := range errs {
if e != nil {
msg += e.Error() + "\n"
}
}
if msg != "" {
return errors.New(msg)
}
return nil
}
+8 -11
View File
@@ -1,7 +1,6 @@
package token
import (
"OneAuth/libs/key"
"OneAuth/models"
"github.com/veypi/utils/jwt"
)
@@ -16,9 +15,8 @@ type simpleAuth struct {
// TODO:: roles 是否会造成token过大 ?
type PayLoad struct {
jwt.Payload
ID uint `json:"id"`
AppID uint `json:"app_id"`
Auth map[uint]*simpleAuth `json:"auth"`
ID uint `json:"id"`
Auth map[uint]*simpleAuth `json:"auth"`
}
// GetAuth resource_uuid 缺省或仅第一个有效 权限会被更高权限覆盖
@@ -49,11 +47,10 @@ func (p *PayLoad) GetAuth(ResourceID string, ResourceUUID ...string) models.Auth
return res
}
func GetToken(u *models.User, appID uint) (string, error) {
func GetToken(u *models.User, appID uint, key string) (string, error) {
payload := &PayLoad{
ID: u.ID,
AppID: appID,
Auth: map[uint]*simpleAuth{},
ID: u.ID,
Auth: map[uint]*simpleAuth{},
}
for _, a := range u.GetAuths() {
if appID == a.AppID {
@@ -64,9 +61,9 @@ func GetToken(u *models.User, appID uint) (string, error) {
}
}
}
return jwt.GetToken(payload, []byte(key.User(payload.ID, payload.AppID)))
return jwt.GetToken(payload, []byte(key))
}
func ParseToken(token string, payload *PayLoad) (bool, error) {
return jwt.ParseToken(token, payload, []byte(key.User(payload.ID, payload.AppID)))
func ParseToken(token string, payload *PayLoad, key string) (bool, error) {
return jwt.ParseToken(token, payload, []byte(key))
}