Documentation ¶
Overview ¶
Package permission is a low-level Go package that allows to easily manage permissions
Example ¶
package main import ( "encoding/json" "fmt" "github.com/asdine/permission" ) func main() { p := permission.Permission{ Name: "user", Sub: "edit", } data, _ := json.Marshal(p) fmt.Printf("%s\n", data) p = permission.Permission{} json.Unmarshal(data, &p) fmt.Println(p.Name) fmt.Println(p.Sub) def := permission.Definition{ Name: "user", Subset: []string{"edit", "profile", "email", "friends", "about"}, DefaultSubset: []string{"profile", "about"}, } required, _ := permission.Parse("user.edit") allowed := def.Allowed(required, p) fmt.Println(allowed) }
Output: "user.edit" user edit true
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyName = errors.New("The permission name is empty") ErrEmptyInput = errors.New("The given input is an empty string") ErrBadFormat = errors.New("The given input is not in the correct format") )
Errors
Functions ¶
func Delimiter ¶
func Delimiter(delim string)
Delimiter is a thread-safe function that sets a global delimiter for Permissions. Defaults to "."
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { permission.Delimiter(":") defer permission.Delimiter(".") perm := permission.Permission{Name: "user", Sub: "edit"} fmt.Println(perm) }
Output: user:edit
func InStringSlice ¶
InStringSlice checks if the given string is in the given slice of string
Types ¶
type Definition ¶
type Definition struct { // Name is the name of the Permission Name string // Subset is a list of all allowed sub permissions Subset []string // DefaultSubset is a list of sub permissions allowed when only the name of the permission is specified DefaultSubset []string }
Definition defines a Permission and its subset. It allows to explicitly define the rules of a permission and to test permissions against the definition.
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { def := permission.Definition{ Name: "file", Subset: []string{"read", "write", "execute"}, DefaultSubset: []string{"read", "execute"}, } required, _ := permission.Parse("file.read") p, _ := permission.Parse("file.read") fmt.Println(def.Allowed(required, p)) p, _ = permission.Parse("file.execute") fmt.Println(def.Allowed(required, p)) p, _ = permission.Parse("file") // file = file.read,file.execute fmt.Println(def.Allowed(required, p)) required, _ = permission.Parse("file.write") p, _ = permission.Parse("file") // file = file.read,file.execute fmt.Println(def.Allowed(required, p)) }
Output: true false true false
func (*Definition) Allowed ¶
func (def *Definition) Allowed(required, given Permission) bool
Allowed checks wether given respects required and the definition
func (*Definition) Match ¶
func (def *Definition) Match(perm Permission) bool
Match detects if the given permission matches the Definition
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { defs := []permission.Definition{ permission.Definition{ Name: "repo", Subset: []string{"read", "write"}, DefaultSubset: []string{"read"}, }, permission.Definition{ Name: "user", Subset: []string{"profile", "edit", "friends", "email"}, DefaultSubset: []string{"profile", "friends"}, }, permission.Definition{ Name: "playlist", Subset: []string{"edit", "share", "read"}, DefaultSubset: []string{"read", "share"}, }, } p, _ := permission.Parse("user.edit") for _, def := range defs { if def.Match(p) { fmt.Println(def.Name) } } }
Output: user
type Definitions ¶
type Definitions []Definition
Definitions are a group of Definition
func (Definitions) Definition ¶
func (d Definitions) Definition(p Permission) *Definition
Definition returns the Definition that matches the Permission
func (Definitions) Require ¶
func (d Definitions) Require(required, scope string) bool
Require checks wether the given scope matches the required permission and is listed in the definitions. Returns false if the parsing fails
type Permission ¶
type Permission struct { // Name of the permission Name string // Sub permission is optional Sub string }
Permission is a simple permission structure. It is meant to describe a single specific permission. A Sub permission can be specified. It can safely be converted back and forth to json
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { // Simple Permission perm := permission.Permission{Name: "read"} fmt.Println(perm) }
Output: read
Example (SubPermission) ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { // Sub Permission perm := permission.Permission{Name: "user", Sub: "edit"} fmt.Println(perm) }
Output: user.edit
func Parse ¶
func Parse(repr string) (Permission, error)
Parse takes a string representation and returns the corresponding Permission
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { perm, _ := permission.Parse("user.edit") fmt.Println(perm.Name) fmt.Println(perm.Sub) fmt.Println(perm) }
Output: user edit user.edit
func (Permission) Equal ¶
func (p Permission) Equal(q Permission) bool
Equal reports whether p and q represents the same permission
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { p, _ := permission.Parse("user.edit") q, _ := permission.Parse("user.edit") fmt.Println(p.Equal(q)) }
Output: true
func (Permission) IsZero ¶
func (p Permission) IsZero() bool
IsZero reports wether the permission is a zero value
func (Permission) MarshalText ¶
func (p Permission) MarshalText() (text []byte, err error)
MarshalText implements the encoding.TextMarshaler interface
func (Permission) String ¶
func (p Permission) String() string
String returns the string representation of the Permission
func (*Permission) UnmarshalText ¶
func (p *Permission) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface
type Scope ¶
type Scope []Permission
Scope is a set of Permissions. It can safely be converted back and forth to json
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { perms := permission.Scope{ permission.Permission{Name: "user", Sub: "edit"}, permission.Permission{Name: "profile"}, permission.Permission{Name: "friends"}, } text, _ := perms.MarshalText() fmt.Println(string(text)) }
Output: user.edit,profile,friends
func ParseScope ¶
ParseScope takes a string representation and returns the corresponding Scope
Example ¶
package main import ( "fmt" "github.com/asdine/permission" ) func main() { perms, _ := permission.ParseScope("user.edit,profile,friends") fmt.Println(len(perms)) fmt.Println(perms[0].Name) fmt.Println(perms[0].Sub) fmt.Println(perms[1].Name) fmt.Println(perms[2].Name) }
Output: 3 user edit profile friends
func (*Scope) HasPermission ¶
func (s *Scope) HasPermission(p Permission) bool
HasPermission checks if the scope has the given permission
func (Scope) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface
func (*Scope) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface