permission

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2025 License: BSD-3-Clause Imports: 1 Imported by: 0

README

permission

This library provides an access control system for managing permissions between entities and resources.

Static Badge

GoDoc GitHub stars Go Report Card codecov

Versions

Stable Version GitHub Release GitHub Release

Installation

To install the permission package, use the following Go command:

go get -u github.com/gouef/permission

Documentation

There are AccessControl, Entity, Permission and Resource

Contributing

Read Contributing

Contributors

JanGalek actions-user

Join our Discord Community! 🎉

Discord

Click above to join our community on Discord!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessControl

type AccessControl struct {
	Entities  []*Entity
	Resources []*Resource
}

AccessControl manages entities and resources, allowing permission assignment.

func NewAccessControl

func NewAccessControl() *AccessControl

NewAccessControl initializes a new AccessControl instance.

Example:

ac := permission.NewAccessControl()
fmt.Println(len(ac.Entities)) // Output: 0

func (*AccessControl) AddEntities

func (ac *AccessControl) AddEntities(entities ...*Entity)

AddEntities adds multiple entities at once

Example:

ac := permission.NewAccessControl()
user1 := permission.NewEntity("user1")
user2 := permission.NewEntity("user1")
ac.AddEntities(user1, user2)

func (*AccessControl) AddEntity

func (ac *AccessControl) AddEntity(entity *Entity) *AccessControl

AddEntity manually adds an entity to the access control system.

Example:

ac := permission.NewAccessControl()
user := permission.NewEntity("user1")
ac.AddEntity(user)

func (*AccessControl) AddResource

func (ac *AccessControl) AddResource(resource *Resource) *AccessControl

AddResource manually adds a resource to the access control system.

Example:

ac := permission.NewAccessControl()
doc := permission.NewResource("document")
ac.AddResource(doc)

func (*AccessControl) AddResources

func (ac *AccessControl) AddResources(resources ...*Resource)

AddResources adds multiple resources at once.

Example:

ac := permission.NewAccessControl()
res1 := permission.NewResource("res1")
res2 := permission.NewResource("res2")
ac.AddResources(res1, res2)

func (*AccessControl) Allow

func (ac *AccessControl) Allow(entity *Entity, resource *Resource, permission Permission) *AccessControl

Allow grants a specific permission to an entity for a given resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, pe

func (*AccessControl) Can

func (ac *AccessControl) Can(entity *Entity, resource *Resource, permission Permission) bool

Can checks if an entity has a specific permission for a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.Read)
fmt.Println(ac.Can(user, doc, permission.Read)) // Output: true

func (*AccessControl) CanCreate

func (ac *AccessControl) CanCreate(entity *Entity, resource *Resource) bool

CanCreate checks if an entity has permission to create a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.Create)
fmt.Println(ac.CanCreate(user, doc)) // Output: true

func (*AccessControl) CanDelete

func (ac *AccessControl) CanDelete(entity *Entity, resource *Resource) bool

CanDelete checks if an entity has permission to delete a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.DELETE)
fmt.Println(ac.CanDelete(user, doc)) // Output: true

func (*AccessControl) CanRead

func (ac *AccessControl) CanRead(entity *Entity, resource *Resource) bool

CanRead checks if an entity has permission to read a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.READ)
fmt.Println(ac.CanRead(user, doc)) // Output: true

func (*AccessControl) CanUpdate

func (ac *AccessControl) CanUpdate(entity *Entity, resource *Resource) bool

CanUpdate checks if an entity has permission to update a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.Update)
fmt.Println(ac.CanUpdate(user, doc)) // Output: true

func (*AccessControl) CreateEntity

func (ac *AccessControl) CreateEntity(id string) *Entity

CreateEntity creates a new entity and adds it to the system.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
fmt.Println(user.ID) // Output: user1

func (*AccessControl) CreateResource

func (ac *AccessControl) CreateResource(id string) *Resource

CreateResource creates a new resource and adds it to the system.

Example:

ac := permission.NewAccessControl()
res := ac.CreateResource("document")
fmt.Println(res.ID) // Output: document

func (*AccessControl) Deny

func (ac *AccessControl) Deny(entity *Entity, resource *Resource, permission Permission) *AccessControl

Deny revokes a specific permission from an entity for a given resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Deny(user, doc, permission.Read)

func (*AccessControl) HasPermission

func (ac *AccessControl) HasPermission(entity *Entity, resource *Resource, permission Permission) bool

HasPermission verifies if an entity has permission for a resource.

Example:

ac := permission.NewAccessControl()
user := ac.CreateEntity("user1")
doc := ac.CreateResource("document")
ac.Allow(user, doc, permission.Read)
fmt.Println(ac.HasPermission(user, doc, permission.Read)) // Output: true

type Entity

type Entity struct {
	ID         string
	Parents    []*Entity
	Children   []*Entity
	Permission map[Permission]map[*Resource]bool
}

Entity represents a user, group, role (or what you want) with specific permissions.

func NewEntity

func NewEntity(id string) *Entity

NewEntity creates a new entity with default permission sets.

Example:

user := permission.NewEntity("user1")
fmt.Println(user.ID) // Output: user1

func (*Entity) AddChildren

func (e *Entity) AddChildren(children ...*Entity)

AddChildren associates child entities with the current entity.

Example:

admin := permission.NewEntity("admin")
user := permission.NewEntity("user")
admin.AddChildren(user)

func (*Entity) AddParents

func (e *Entity) AddParents(parents ...*Entity)

AddParents associates parent entities with the current entity.

Example:

admin := permission.NewEntity("admin")
user := permission.NewEntity("user")
user.AddParents(admin)

func (*Entity) AddPerm

func (e *Entity) AddPerm(permission Permission, resource *Resource, enabled bool)

AddPerm sets or removes a specific permission for a resource.

func (*Entity) AddPermAll

func (e *Entity) AddPermAll(resource *Resource, enabled bool)

func (*Entity) AddPermCreate

func (e *Entity) AddPermCreate(resource *Resource, enabled bool)

func (*Entity) AddPermDelete

func (e *Entity) AddPermDelete(resource *Resource, enabled bool)

func (*Entity) AddPermRead

func (e *Entity) AddPermRead(resource *Resource, enabled bool)

func (*Entity) AddPermUpdate

func (e *Entity) AddPermUpdate(resource *Resource, enabled bool)

func (*Entity) Allow

func (e *Entity) Allow(resource *Resource, permissions ...Permission)

Allow grants specified permissions for a resource to the entity.

Example:

user := permission.NewEntity("user")
res := permission.NewResource("file")
user.Allow(res, permission.Read, permission.Write)

func (*Entity) CreateChild

func (e *Entity) CreateChild(id string) *Entity

CreateChild creates a child entity and assigns it as a descendant.

Example:

parent := permission.NewEntity("admin")
child := parent.CreateChild("user")
fmt.Println(child.ID) // Output: user

func (*Entity) Deny

func (e *Entity) Deny(resource *Resource, permissions ...Permission)

Deny revokes specified permissions for a resource from the entity.

Example:

user := permission.NewEntity("user")
res := permission.NewResource("file")
user.Deny(res, permission.Read, permission.Write)

type Permission

type Permission string

Permission represents different levels of access control.

const (
	// Create allows an entity to create a resource.
	Create Permission = "CREATE"
	// Read allows an entity to read a resource.
	Read Permission = "READ"
	// Update allows an entity to modify a resource.
	Update Permission = "UPDATE"
	// Delete allows an entity to remove a resource.
	Delete Permission = "DELETE"
	// All grants full access to a resource.
	All Permission = "ALL"
)

type Resource

type Resource struct {
	ID           string
	Parent       *Resource
	SubResources map[string]*Resource // Podresource podle názvu
	Owners       []*Entity            // Vlastníci resource
}

Resource represents an entity that can be assigned permissions.

func NewResource

func NewResource(id string) *Resource

NewResource initializes a new resource with the given ID.

Example:

doc := permission.NewResource("document")
fmt.Println(doc.ID) // Output: document

func (*Resource) AddOwners

func (r *Resource) AddOwners(owners ...*Entity) *Resource

AddOwners assigns ownership of the resource to specific entities.

Example:

user := permission.NewEntity("user")
doc := permission.NewResource("document")
doc.AddOwners(user)

func (*Resource) AddSubs

func (r *Resource) AddSubs(resources ...*Resource) *Resource

AddSubs links additional sub-resources to the current resource.

func (*Resource) CreateSub

func (r *Resource) CreateSub(id string) *Resource

CreateSub generates a new sub-resource under the current resource.

Example:

website := permission.NewResource("website")
news := website.CreateSub("news")
fmt.Println(news.ID) // Output: news

func (*Resource) CreateSubs

func (r *Resource) CreateSubs(ids ...string) *Resource

CreateSubs generates multiple sub-resources.

Example:

website := permission.NewResource("website")
website.CreateSub("news", "comments", "reviews")

func (*Resource) GetSub

func (r *Resource) GetSub(id string) *Resource

GetSub retrieves a sub-resource by its ID.

Example:

website := permission.NewResource("website")
news := website.CreateSub("news")
fmt.Println(website.GetSub("news").ID) // Output: news

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL