GRABC Beego Frame RABC plug-in

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently want to use the Beego framework to develop a scheduled task management system, found that user rights here needs a RABC function. I have been PHP development, with Yii has been 4 years, so based on the YII framework rabc function, with Go also wrote a Beego framework plugin. Currently just finished, welcome to use the spit groove, later in the 2.0 version of the added menu and Permissions cache function. Not much to say, to see the current function ~ ~

GRABC plug-ins include the ability to route, permissions, roles, and user role bindings. The routing feature automatically scans the method inside the controller, then assigns the route to the permissions as needed, assigns the permissions to the role, and assigns the role to the user. GRABC function is simple and practical, in the Beego framework to install only less than 20 lines of code, you can achieve the function of RABC.

Installation:

go get github.com/codyi/grabc

Configuration:

The first step: import Rabc.sql into the database in your project, generate the corresponding data table, to save the routing, permissions, role of the data

Step two: Introduce the GRABC library in the project (can be introduced in Main.go or Router.go in the project)

In Router.go or MAIN.GO, configure the following:

//引入grabc库import "github.com/codyi/grabc"func init() {//将路由注册到grabc,用于反射出对应的网址grabc.RegisterController(&controllers.SiteController{})grabc.RegisterController(&controllers.UserController{})//注册用户系统模型到grabc,用于用户ID和grabc插件绑定//注意:注册的这个用户模型,需要实现IUserModel中的方法grabc.RegisterUserModel(&models.User{})//增加忽律权限检查的页面grabc.AppendIgnoreRoute("site", "login")//403页面地址注册到grabc中,用于grabc插件禁止权限的页面跳转grabc.Http_403("/site/nopermission")//设置模板,为了让grabc更具有通用性,可以设置模板//目前设置模板只支持传入模板的内容,也可以使用默认的模板grabc.SetLayout(libs.Grabc_layout, nil)}

After adding the above configuration, the rest is to increase the authority in the controller to be sentenced, personal advice to do a basecontroller, and then each controller inherits this base, Then add the GRABC permission check in the Prepare method in Basecontroller ~ ~

The Prepare method in Basecontroller.go is configured as follows

//注册当前登录的用户,注意:user需要继承IUserIdentify接口grabc.RegisterIdentify(user)if !grabc.CheckAccess(this.controllerName, this.actionName) {this.redirect(this.URLFor("SiteController.NoPermission"))}

To this GRABC function all add up, is not very simple ~ ~ ~

Attention:

After adding the permission to judge, you will find a lot of pages are inaccessible, then add the following code in the law permission

grabc.AppendIgnoreRoute("*", "*")

The above configuration will be all rights checks, this time need to go to/route/index to increase the route, and then add permissions, roles and user assignment, are configured, you can be grabc. Appendignoreroute ("*", "*") code Delete, and then restart the project ~ ~ Permission worked

Interface Description:

IUserModel接口//用于定义用户modeltype IUserModel interface {//用户列表返回可用用户的id和姓名//参数:pageIndex 分页的页数//参数:pageCount 每页显示的用户数量//返回值:userList [用户ID]用户姓名,用户列表展示//返回值:totalNum 全部的用户数目,用于计算分页的数量//返回值:errUserList(pageIndex, pageCount int) (userList map[int]string, totalNum int, err error)//根据用户ID获取用户姓名FindNameById(id int) string }IUserIdentify接口type IUserIdentify interface {GetId() int //返回当前登录用户的ID}

Problem with incomplete route scanning:

GRABC reflects on the registered controller, and then gets the name of each director and the public method within the controller, since each controller inherits the Beego. Controller, when obtaining the method name under controller, will be beego. Controller inherited methods will be all obtained, so at present can not distinguish between the method name is Beego and the user's own definition, so GRABC will Beego inherited methods are all the law, if in the route scan, did not find a custom method, You can add the following method to the controller to return the method ~ ~

func (this *SiteController) RABCMethods() []string {return []string{"Get", "Post"}}

Project Address:

Https://github.com/codyi/grabc

GRABC Example Address:

Https://github.com/codyi/grabc_example

263 reads ∙1 likes
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.