Resource access manangment for different roles
go get github.com/Focinfi/roles
- roles.Read
- roles.Create
- roles.Update
- roles.Delete
- roles.CURD
Implement the Roler interface for you model struct, like User.
type Roler interface {
Roles() []string
}
Implement the Rescource interface for your Resource struct, like Book.
type Resourcer interface {
TableName() string
}
visitorRole := roles.Add("visitor")
// Here you have a Book struct
type Book struct{}
// Implement the Resourcer interface
func (b Book) TableName() string {
return "books"
}
// Then you can allow visitors can only read and create books
visitorRole.Allow(Book{}, roles.Read, roles.Create)
// Here you have a User struct
type User struct{}
// Implement the Roler interface
func (u User) Roles() []string {
// Query from database and get this user's roles array
return roles
}
if roels.Can(User{}, Book{}, roles.Read) {
// do something when this user can read a book
} else {
// do something when this user can not read a book
}