perms

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: Apache-2.0 Imports: 34 Imported by: 0

README

pkg/perms

Ideas and some code snippets have been taken from the Permify/go-role repository, which is licensed under MIT license.

Documentation

Index

Constants

View Source
const (
	BaseSubject events.Subject = "perms"

	RolePermUpdateSubject events.Type = "roleperm.update"
	RoleAttrUpdateSubject events.Type = "roleattr.update"
)
View Source
const (
	DefaultRoleJob      = "__default__"
	DefaultRoleJobGrade = int32(1)
)

Variables

View Source
var (
	ErrAttrInvalid = errors.New("invalid attributes")
)
View Source
var Module = fx.Module("perms",
	fx.Provide(
		New,
	),
	fx.Decorate(wrapLogger),
)

Functions

func AddPermsToList

func AddPermsToList(perms []*Perm)

func BuildGuard

func BuildGuard(category Category, name Name) string

func BuildGuardWithKey

func BuildGuardWithKey(category Category, name Name, key Key) string

Types

type AddPerm

type AddPerm struct {
	Id  uint64
	Val bool
}

type Attr

type Attr struct {
	ID            uint64
	Key           Key
	Type          permissions.AttributeTypes
	ValidValues   any
	DefaultValues any
}

type Category

type Category string

type JobGradeList

type JobGradeList map[string]int32

type JobList

type JobList []string

type Key

type Key string

type Name

type Name string

type Params added in v0.8.0

type Params struct {
	fx.In

	LC     fx.Lifecycle
	Logger *zap.Logger
	DB     *sql.DB
	TP     *tracesdk.TracerProvider
	Events *events.Eventus
	Config *config.Config
}

type Perm

type Perm struct {
	Category Category
	Name     Name
	Attrs    []Attr
}

type Permissions

type Permissions interface {
	GetAllPermissions(ctx context.Context) ([]*permissions.Permission, error)
	GetPermissionsByIDs(ctx context.Context, ids ...uint64) ([]*permissions.Permission, error)
	GetPermission(ctx context.Context, category Category, name Name) (*permissions.Permission, error)
	CreatePermission(ctx context.Context, category Category, name Name) (uint64, error)
	GetPermissionsOfUser(userInfo *userinfo.UserInfo) (collections.Permissions, error)

	GetRoles(ctx context.Context, excludeSystem bool) (collections.Roles, error)
	GetJobRoles(ctx context.Context, job string) (collections.Roles, error)
	GetJobRolesUpTo(ctx context.Context, job string, grade int32) (collections.Roles, error)
	GetClosestJobRole(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)
	CountRolesForJob(ctx context.Context, prefix string) (int64, error)

	GetRole(ctx context.Context, id uint64) (*model.FivenetRoles, error)
	GetRoleByJobAndGrade(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)
	GetRolePermissions(ctx context.Context, id uint64) ([]*permissions.Permission, error)

	CreateRole(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)
	DeleteRole(ctx context.Context, id uint64) error
	UpdateRolePermissions(ctx context.Context, id uint64, perms ...AddPerm) error
	RemovePermissionsFromRole(ctx context.Context, id uint64, perms ...uint64) error

	Can(userInfo *userinfo.UserInfo, category Category, name Name) bool

	LookupAttributeByID(id uint64) (*cacheAttr, bool)
	GetAttribute(category Category, name Name, key Key) (*permissions.RoleAttribute, error)
	GetAttributeByIDs(ctx context.Context, ids ...uint64) ([]*permissions.RoleAttribute, error)
	CreateAttribute(ctx context.Context, permId uint64, key Key, aType permissions.AttributeTypes, validValues any, defaultValues any) (uint64, error)
	UpdateAttribute(ctx context.Context, attributeId uint64, permId uint64, key Key, aType permissions.AttributeTypes, validValues any, defaultValues any) error
	GetRoleAttributes(job string, grade int32) ([]*permissions.RoleAttribute, error)
	GetRoleAttributeByID(roleId uint64, attrId uint64) (*permissions.RoleAttribute, bool)
	FlattenRoleAttributes(job string, grade int32) ([]string, error)
	GetAllAttributes(ctx context.Context, job string, grade int32) ([]*permissions.RoleAttribute, error)
	AddOrUpdateAttributesToRole(ctx context.Context, job string, grade int32, roleId uint64, attrs ...*permissions.RoleAttribute) error
	RemoveAttributesFromRole(ctx context.Context, roleId uint64, attrs ...*permissions.RoleAttribute) error
	UpdateRoleAttributeMaxValues(ctx context.Context, roleId uint64, attrId uint64, maxValues *permissions.AttributeValues) error
	GetClosestRoleAttrMaxVals(job string, grade int32, permId uint64, key Key) (*permissions.AttributeValues, uint64)

	Attr(userInfo *userinfo.UserInfo, category Category, name Name, key Key) (any, error)
}

func New

func New(p Params) (Permissions, error)

type Perms

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

func (*Perms) AddOrUpdateAttributesToRole

func (p *Perms) AddOrUpdateAttributesToRole(ctx context.Context, job string, grade int32, roleId uint64, attrs ...*permissions.RoleAttribute) error

func (*Perms) Attr

func (p *Perms) Attr(userInfo *userinfo.UserInfo, category Category, name Name, key Key) (any, error)

func (*Perms) Can

func (p *Perms) Can(userInfo *userinfo.UserInfo, category Category, name Name) bool

func (*Perms) CountRolesForJob

func (p *Perms) CountRolesForJob(ctx context.Context, job string) (int64, error)

func (*Perms) CreateAttribute

func (p *Perms) CreateAttribute(ctx context.Context, permId uint64, key Key, aType permissions.AttributeTypes, validValues any, defaultValues any) (uint64, error)

func (*Perms) CreatePermission

func (p *Perms) CreatePermission(ctx context.Context, category Category, name Name) (uint64, error)

func (*Perms) CreateRole

func (p *Perms) CreateRole(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)

func (*Perms) DeleteRole

func (p *Perms) DeleteRole(ctx context.Context, id uint64) error

func (*Perms) FlattenRoleAttributes

func (p *Perms) FlattenRoleAttributes(job string, grade int32) ([]string, error)

func (*Perms) GetAllAttributes

func (p *Perms) GetAllAttributes(ctx context.Context, job string, grade int32) ([]*permissions.RoleAttribute, error)

func (*Perms) GetAllPermissions

func (p *Perms) GetAllPermissions(ctx context.Context) ([]*permissions.Permission, error)

func (*Perms) GetAttribute

func (p *Perms) GetAttribute(category Category, name Name, key Key) (*permissions.RoleAttribute, error)

func (*Perms) GetAttributeByIDs

func (p *Perms) GetAttributeByIDs(ctx context.Context, attrIds ...uint64) ([]*permissions.RoleAttribute, error)

func (*Perms) GetClosestJobRole

func (p *Perms) GetClosestJobRole(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)

func (*Perms) GetClosestRoleAttrMaxVals added in v0.7.0

func (p *Perms) GetClosestRoleAttrMaxVals(job string, grade int32, permId uint64, key Key) (*permissions.AttributeValues, uint64)

func (*Perms) GetJobRoles

func (p *Perms) GetJobRoles(ctx context.Context, job string) (collections.Roles, error)

func (*Perms) GetJobRolesUpTo

func (p *Perms) GetJobRolesUpTo(ctx context.Context, job string, grade int32) (collections.Roles, error)

func (*Perms) GetPermission added in v0.7.0

func (p *Perms) GetPermission(ctx context.Context, category Category, name Name) (*permissions.Permission, error)

func (*Perms) GetPermissionsByIDs

func (p *Perms) GetPermissionsByIDs(ctx context.Context, ids ...uint64) ([]*permissions.Permission, error)

func (*Perms) GetPermissionsOfUser

func (p *Perms) GetPermissionsOfUser(userInfo *userinfo.UserInfo) (collections.Permissions, error)

func (*Perms) GetRole

func (p *Perms) GetRole(ctx context.Context, id uint64) (*model.FivenetRoles, error)

func (*Perms) GetRoleAttributeByID added in v0.7.0

func (p *Perms) GetRoleAttributeByID(roleId uint64, attrId uint64) (*permissions.RoleAttribute, bool)

func (*Perms) GetRoleAttributes

func (p *Perms) GetRoleAttributes(job string, grade int32) ([]*permissions.RoleAttribute, error)

func (*Perms) GetRoleByJobAndGrade

func (p *Perms) GetRoleByJobAndGrade(ctx context.Context, job string, grade int32) (*model.FivenetRoles, error)

func (*Perms) GetRolePermissions

func (p *Perms) GetRolePermissions(ctx context.Context, id uint64) ([]*permissions.Permission, error)

func (*Perms) GetRoles added in v0.7.0

func (p *Perms) GetRoles(ctx context.Context, excludeSystem bool) (collections.Roles, error)

func (*Perms) LookupAttributeByID added in v0.7.0

func (p *Perms) LookupAttributeByID(id uint64) (*cacheAttr, bool)

func (*Perms) Register

func (p *Perms) Register(defaultRolePerms []string) error

func (*Perms) RemoveAttributesFromRole

func (p *Perms) RemoveAttributesFromRole(ctx context.Context, roleId uint64, attrs ...*permissions.RoleAttribute) error

func (*Perms) RemovePermissionsByIDs

func (p *Perms) RemovePermissionsByIDs(ctx context.Context, ids ...uint64) error

func (*Perms) RemovePermissionsFromRole

func (p *Perms) RemovePermissionsFromRole(ctx context.Context, roleId uint64, perms ...uint64) error

func (*Perms) UpdateAttribute

func (p *Perms) UpdateAttribute(ctx context.Context, attrId uint64, permId uint64, key Key, aType permissions.AttributeTypes, validValues any, defaultValues any) error

func (*Perms) UpdatePermission

func (p *Perms) UpdatePermission(ctx context.Context, id uint64, category Category, name Name) error

func (*Perms) UpdateRoleAttributeMaxValues added in v0.7.0

func (p *Perms) UpdateRoleAttributeMaxValues(ctx context.Context, roleId uint64, attrId uint64, maxValues *permissions.AttributeValues) error

func (*Perms) UpdateRolePermissions

func (p *Perms) UpdateRolePermissions(ctx context.Context, roleId uint64, perms ...AddPerm) error

type RoleAttrUpdateEvent added in v0.6.0

type RoleAttrUpdateEvent struct {
	RoleID uint64
}

type RolePermUpdateEvent added in v0.6.0

type RolePermUpdateEvent struct {
	RoleID uint64
}

type StringList

type StringList []string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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