首先,让咱们定义一个规定:用户只能拜访本人创立的文章。

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!