The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Bowens

「Goravel 上新」用户授权模块,让你简单的对非法用户 Say No!

  •  
  •   Bowens · Nov 29, 2022 · 1859 views
    This topic created in 1286 days ago, the information mentioned may be changed or developed.

    首先,让我们定义一个规则:用户只能访问自己创建的文章。

    facades.Gate.Define("update-post", func(ctx context.Context, arguments map[string]any) *access.Response {
      user := ctx.Value("user").(models.User)
      post := arguments["post"].(models.Post)
      
      if user.ID == post.UserID {
        return access.NewAllowResponse()
      } else {
        return access.NewDenyResponse("error")
      }
    })
    

    然后判断单个权限:

    if facades.Gate.Allows("update-post", map[string]any{
      "post": post,
    }) {
      // todo
    }
    

    你也可以同时判断多个权限:

    if facades.Gate.Any([]string{"update-post", "delete-post"}, map[string]any{
      "post": post,
    }) {
      // 用户可以提交 update 或 delete...
    }
    
    if facades.Gate.None([]string{"update-post", "delete-post"}, map[string]any{
      "post": post,
    }) {
      // 用户不可以提交 update 和 delete...
    }
    

    你甚至可以定义 BeforeAfter 进行授权前后的拦截,详见文档。 Over, 就是如此简单!

    关于 Goravel

    Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框架。作为一个起始脚手架帮助 Golang 开发者快速构建自己的应用。

    框架风格与 Laravel 保持一致,让 PHPer 不用学习新的框架,也可以愉快的玩转 Golang !致敬 Laravel !

    Welcome star, PR and issues !

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3746 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 04:32 · PVG 12:32 · LAX 21:32 · JFK 00:32
    ♥ Do have faith in what you're doing.