Skip to content

Commit

Permalink
update sv valid to new version
Browse files Browse the repository at this point in the history
  • Loading branch information
23233 committed May 26, 2020
1 parent 88878e4 commit db33893
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/23233/simple_valid v1.0.4
github.com/23233/sv v1.0.8
github.com/casbin/casbin/v2 v2.2.2
github.com/casbin/xorm-adapter v0.0.0-20191120030838-267478260350
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
Expand Down
26 changes: 8 additions & 18 deletions simple_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package simple_admin

import (
"fmt"
"github.com/23233/simple_valid"
"github.com/23233/sv"
"github.com/casbin/casbin/v2"
"github.com/imdario/mergo"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/pkg/errors"
"log"
"reflect"
Expand All @@ -17,7 +16,7 @@ import (

var (
NowSpAdmin *SpAdmin
SvKey = "sv"
SvKey = sv.GlobalContextKey
DefaultPrefix = "/SP_PREFIX"
)

Expand All @@ -28,7 +27,6 @@ type SpAdmin struct {
defaultMethods map[string]string // 默认权限方法
defaultRole map[string]string // 默认角色
sitePolicy map[string]string
sv simple_valid.ReqValid
prefix string
}

Expand Down Expand Up @@ -75,13 +73,6 @@ func New(c Config) (*SpAdmin, error) {
// 生成表名列表
modelTables := c.generateTables()

// 验证器初始化
sv := simple_valid.New(SvKey, func(err error, ctx context.Context) {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{"detail": err.Error()})
return
})

NowSpAdmin = &SpAdmin{
config: c,
casbinEnforcer: enforcer,
Expand All @@ -101,7 +92,6 @@ func New(c Config) (*SpAdmin, error) {
"login_site": "login_site",
"user_manage": c.getUserModelTableName(),
},
sv: sv,
prefix: DefaultPrefix,
}
// 进行视图注册绑定
Expand All @@ -127,15 +117,15 @@ func (lib *SpAdmin) Router(router iris.Party) {
// 首页
router.Get("/", Index)
// 登录
router.Post("/login", lib.sv.Run(new(UserLoginReq)), Login)
router.Post("/login", sv.Run(new(UserLoginReq)), Login)
// 注册
router.Post("/reg", lib.sv.Run(new(UserLoginReq)), Reg)
router.Post("/reg", sv.Run(new(UserLoginReq)), Reg)
router.Get("/config", Configuration)
c := router.Party("/v", CustomJwt.Serve, TokenToUserUidMiddleware)
// 获取当前用户
c.Get("/get_current_user", GetCurrentUser)
// 变更用户密码
c.Post("/change_password", lib.sv.Run(new(UserChangePasswordReq)), ChangeUserPassword)
c.Post("/change_password", sv.Run(new(UserChangePasswordReq)), ChangeUserPassword)
// 获取所有表
c.Get("/get_routers", GetRouters)
// 获取单表列信息
Expand All @@ -150,12 +140,12 @@ func (lib *SpAdmin) Router(router iris.Party) {
// 修改
c.Put("/{routerName:string}/{id:uint64}", PolicyValidMiddleware, EditRouterData)
// 删除 delete模式在某些匹配时候有问题
c.Post("/{routerName:string}/delete", PolicyValidMiddleware, lib.sv.Run(new(DeleteReq)), RemoveRouterData)
c.Post("/{routerName:string}/delete", PolicyValidMiddleware, sv.Run(new(DeleteReq)), RemoveRouterData)
// 权限相关
c.Post("/change_user_role", PolicyRequireAdminMiddleware, lib.sv.Run(new(UserChangeRolesReq)), ChangeUserRoles)
c.Post("/change_user_role", PolicyRequireAdminMiddleware, sv.Run(new(UserChangeRolesReq)), ChangeUserRoles)
// 进行自定义action绑定
for _, action := range lib.config.CustomAction {
c.Handle(action.Methods, action.Path, lib.sv.Run(action.Valid), action.Func)
c.Handle(action.Methods, action.Path, sv.Run(action.Valid), action.Func)
}
}

Expand Down

0 comments on commit db33893

Please sign in to comment.