acl

package module
v0.0.0-...-db14010 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2020 License: MIT Imports: 0 Imported by: 0

README

GoDoc Codeship Codecov Go Report Card

ACL - Access Control List

ACL is a simple but powerful Access Control List manager

Installation

go get -u github.com/txgruppi/acl-go

Example

You should not ignore the errors returned by the methods

package main

import (
  "fmt"

  "github.com/txgruppi/acl-go"
  "github.com/txgruppi/acl-go/driver/memory"
)

func main() {
  driver := memory.NewDriver()

  // Driver can be directly used as ACL managers
  var manager acl.ACL = driver

  // Set the default policy as Deny
  acl.SetDefaultPolicy(acl.Deny)

  // Get some users
  userCEO, _ := acl.GetActor("userCEO_UUID")
  userDeveloper, _ := acl.GetActor("userDeveloper_UUID")

  // Get some actions
  accessBackAccount, _ := acl.GetAction("accessBackAccount")
  accessProductionServer, _ := acl.GetAction("accessProductionServer")

  // Set rules
  acl.Set(userCEO, accessBackAccount, acl.Allow)
  acl.Set(userDeveloper, accessProductionServer, acl.Allow)

  // Check using the ACL manager
  allowed, _ := acl.IsAllowed(userCEO, accessBackAccount)
  fmt.Println(allowed) // true
  allowed, _ = acl.IsAllowed(userDeveloper, accessBackAccount)
  fmt.Println(allowed) // false

  // Check using the Actor or Action struct
  allowed, _ := userCEO.IsAllowed(accessProductionServer)
  fmt.Println(allowed) // false
  allowed, _ = accessProductionServer.Allows(userDeveloper)
  fmt.Println(allowed) // true
}

Tests

go get -u -t github.com/txgruppi/acl-go
cd $GOPATH/src/github.com/txgruppi/acl-go
go test ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ACL

type ACL interface {
	// SetDefaultPolicy - Check Driver's .SetDefaultPolicy
	SetDefaultPolicy(Policy) error

	// GetActor - Check Driver's .GetActor
	GetActor(string) (Actor, error)

	// GetAction - Check Driver's .GetAction
	GetAction(string) (Action, error)

	// Set - Check Driver's .Set
	Set(Actor, Action, Policy) error
}

ACL defines the basic methods for an ACL manager

type Action

type Action interface {
	// Allows checks if this action is allowed to an actor
	Allows(Actor) (bool, error)

	// String returns the string ID of this action
	String() string
}

Action is an action which can be allowed or denied to one or more actors

type Actor

type Actor interface {
	// IsAllowed checks if this user has access to an action
	IsAllowed(Action) (bool, error)

	// String returns the string ID of this actor
	String() string
}

Actor is an user which has access or not to one or more actions

type Driver

type Driver interface {
	// Begin the communication with the Driver's backend
	//
	// This method should be used to do any initialization before the driver can
	// be used
	Begin() error

	// End the communication with the Driver's backend
	//
	// This method should be used to do any cleanup after the driver is no more
	// needed
	End() error

	// SetDefaultPolicy defines the default access policy, whether to deny or allow
	//
	// The default policy is returned when a rule is not defined.
	// Its value should be false by default.
	SetDefaultPolicy(Policy) error

	// GetActor returns an Actor with the given ID
	GetActor(string) (Actor, error)

	// GetAction returns an Action with the given ID
	GetAction(string) (Action, error)

	// Set defines a rule for a Actor and Action, which can be Allow or Deny
	Set(Actor, Action, Policy) error

	// IsAllowed checks if an Actor has access to an Action
	IsAllowed(Actor, Action) (bool, error)
}

Driver defines the basic methods for an ACL manager driver

type Policy

type Policy bool

Policy defines the Allow and Deny policies

const (
	// Deny should be used when the actor has no access to an action
	Deny Policy = false

	// Allow should be used when the actor has access to an action
	Allow = true
)

type SimpleAction

type SimpleAction struct {
	// contains filtered or unexported fields
}

SimpleAction represents an ACL action, it implements the Action interface

func NewSimpleAction

func NewSimpleAction(driver Driver, id string) *SimpleAction

NewSimpleAction creates a new SimpleAction with the given ID and Driver

func (*SimpleAction) Allows

func (s *SimpleAction) Allows(actor Actor) (bool, error)

Allows - Check Action's Allows

func (*SimpleAction) String

func (s *SimpleAction) String() string

String - Check Action's String

type SimpleActor

type SimpleActor struct {
	// contains filtered or unexported fields
}

SimpleActor represents an ACL actor, it implements the Actor interface

func NewSimpleActor

func NewSimpleActor(driver Driver, id string) *SimpleActor

NewSimpleActor creates a new SimpleActor with the given ID and Driver

func (*SimpleActor) IsAllowed

func (s *SimpleActor) IsAllowed(action Action) (bool, error)

IsAllowed - Check Actor's IsAllowed

func (*SimpleActor) String

func (s *SimpleActor) String() string

String - Check Actor's String

Directories

Path Synopsis
driver

Jump to

Keyboard shortcuts

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