Documentation
¶
Overview ¶
Package goncierge holds the capabilities each role carries, as granted by named sources.
Index ¶
- Variables
- func Declare(r *Registry, rules Rules, source string, roles []Role) error
- type Registry
- func (r *Registry) Can(role, capability string) bool
- func (r *Registry) Capabilities() []string
- func (r *Registry) CapabilitiesOf(role string) []string
- func (r *Registry) Grant(source, role string, capabilities ...string) error
- func (r *Registry) Grantable(caller string) []string
- func (r *Registry) HoldersOf(capability string) []string
- func (r *Registry) Known(role string) bool
- func (r *Registry) Outranks(caller, target string) bool
- func (r *Registry) Replace(source string, roles []Role) error
- func (r *Registry) Revoke(source, role string, capabilities ...string)
- func (r *Registry) Roles() []string
- func (r *Registry) Withdraw(source string)
- type Role
- type Rules
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptySource = errors.New("goncierge: empty source")
ErrEmptySource reports that a grant names no source.
var ErrInvalidName = errors.New("goncierge: invalid name")
ErrInvalidName reports that a role or capability name breaks the name rule.
var ErrOutsidePrefix = errors.New("goncierge: new capability outside the source's prefix")
ErrOutsidePrefix reports a new capability whose name does not start with its source and a dot.
var ErrUnknownAdmin = errors.New("goncierge: unknown admin role")
ErrUnknownAdmin reports an administrator role that no other source created.
Functions ¶
func Declare ¶
Declare replaces the source's grants with its roles under the rules, refusing a new capability outside its prefix.
Example ¶
package main
import (
"fmt"
"github.com/gopherium/framework/goncierge"
)
func main() {
registry := goncierge.New()
if err := registry.Grant("core", "admin", "manage_users"); err != nil {
return
}
rules := goncierge.Rules{Admin: "admin"}
fmt.Println(goncierge.Declare(registry, rules, "events", []goncierge.Role{
{Name: "organizer", Capabilities: []string{"events.manage"}},
}))
fmt.Println(registry.CapabilitiesOf("admin"))
fmt.Println(goncierge.Declare(registry, rules, "tickets", []goncierge.Role{
{Name: "seller", Capabilities: []string{"sell_tickets"}},
}))
}
Output: <nil> [events.manage manage_users] goncierge: new capability outside the source's prefix "tickets.": "sell_tickets"
Types ¶
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the capabilities each role carries, as granted by named sources.
Example ¶
package main
import (
"fmt"
"github.com/gopherium/framework/goncierge"
)
func main() {
registry := goncierge.New()
if err := registry.Grant("core", "admin", "users.manage", "events.manage"); err != nil {
return
}
if err := registry.Grant("core", "member"); err != nil {
return
}
if err := registry.Grant("events", "organizer", "events.manage"); err != nil {
return
}
fmt.Println(registry.Can("organizer", "events.manage"))
fmt.Println(registry.Grantable("organizer"))
fmt.Println(registry.HoldersOf("users.manage"))
registry.Withdraw("events")
fmt.Println(registry.Roles())
}
Output: true [organizer member] [admin] [admin member]
func (*Registry) Can ¶
Can reports whether the role carries the capability, an empty role never carrying one.
func (*Registry) Capabilities ¶
Capabilities returns every capability any source granted, sorted.
func (*Registry) CapabilitiesOf ¶
CapabilitiesOf returns the capabilities the role carries, sorted, from every source.
func (*Registry) Grant ¶
Grant records that the source gives the role the capabilities, creating the role when it is new.
Example ¶
package main
import (
"fmt"
"github.com/gopherium/framework/goncierge"
)
func main() {
err := goncierge.New().Grant("core", "Shop manager", "orders.manage")
fmt.Println(err)
}
Output: goncierge: invalid name for a role: "Shop manager"
func (*Registry) Grantable ¶
Grantable returns the roles the caller outranks, the one carrying most capabilities first.
func (*Registry) Outranks ¶
Outranks reports whether the caller carries every capability the target carries.
func (*Registry) Replace ¶
Replace swaps every grant the source made for the roles given, in one step.
func (*Registry) Revoke ¶
Revoke removes the capabilities from the source's grant to the role, or the whole grant when none is named.