rbac

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package rbac provides Role-Based Access Control (RBAC) constants, types, and utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRolePermissions

func DefaultRolePermissions() map[RoleName][]PermissionName

DefaultRolePermissions returns the default permission assignments for each role

func GetRoleDescription

func GetRoleDescription(name RoleName) string

GetRoleDescription returns the description for a predefined role

func IsValidPermissionName

func IsValidPermissionName(name string) bool

IsValidPermissionName checks if a permission name is a predefined permission

func IsValidRoleName

func IsValidRoleName(name string) bool

IsValidRoleName checks if a role name is a predefined role

Types

type PermissionDefinition

type PermissionDefinition struct {
	Name        PermissionName
	Resource    string
	Action      string
	Description string
}

PermissionDefinition defines a permission with its metadata

func DefaultPermissions

func DefaultPermissions() []PermissionDefinition

DefaultPermissions returns the predefined permissions for the system

func GetPermissionDefinition

func GetPermissionDefinition(name PermissionName) *PermissionDefinition

GetPermissionDefinition returns the definition for a predefined permission

type PermissionName

type PermissionName string

PermissionName represents a type-safe permission name

const (
	PermProductCreate PermissionName = "product:create"
	PermProductRead   PermissionName = "product:read"
	PermProductUpdate PermissionName = "product:update"
	PermProductDelete PermissionName = "product:delete"
)

Product permissions

const (
	PermOrderCreate  PermissionName = "order:create"
	PermOrderRead    PermissionName = "order:read"
	PermOrderUpdate  PermissionName = "order:update"
	PermOrderProcess PermissionName = "order:process"
)

Order permissions

const (
	PermUserCreate    PermissionName = "user:create"
	PermUserRead      PermissionName = "user:read"
	PermUserUpdate    PermissionName = "user:update"
	PermUserDelete    PermissionName = "user:delete"
	PermUserUpdateOwn PermissionName = "user:update_own"
)

User permissions

const (
	PermCustomerView         PermissionName = "customer:view"
	PermCustomerOrderHistory PermissionName = "customer:order_history"
)

Customer support permissions

const (
	PermReportView PermissionName = "report:view"
)

Report permissions

func AllPermissionNames

func AllPermissionNames() []PermissionName

AllPermissionNames returns all predefined permission names

func (PermissionName) String

func (p PermissionName) String() string

String returns the string representation of the permission name

type RoleDefinition

type RoleDefinition struct {
	Name        RoleName
	Description string
}

RoleDefinition defines a role with its metadata

func DefaultRoles

func DefaultRoles() []RoleDefinition

DefaultRoles returns the predefined roles for the system

type RoleName

type RoleName string

RoleName represents a type-safe role name

const (
	// RoleAdmin has full access to all resources
	RoleAdmin RoleName = "admin"

	// RoleManager has access to manage products, orders, and view reports
	RoleManager RoleName = "manager"

	// RoleCustomerExperience has access to view customer information and order history
	RoleCustomerExperience RoleName = "customer_experience"

	// RoleCustomer has access to their own resources and basic product/order operations
	RoleCustomer RoleName = "customer"
)

Predefined role names

func AllRoleNames

func AllRoleNames() []RoleName

AllRoleNames returns all predefined role names

func DefaultRole

func DefaultRole() RoleName

DefaultRole returns the default role to assign to new users

func (RoleName) String

func (r RoleName) String() string

String returns the string representation of the role name

type Seeder

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

Seeder provides functionality to seed the database with predefined roles and permissions

func NewSeeder

func NewSeeder(store store.Store) *Seeder

NewSeeder creates a new Seeder instance

func (*Seeder) AssignDefaultRoleToUser

func (s *Seeder) AssignDefaultRoleToUser(ctx context.Context, userID string) error

AssignDefaultRoleToUser assigns the default role (customer) to a user if they have no roles

func (*Seeder) AssignDefaultRoleToUsersWithoutRoles

func (s *Seeder) AssignDefaultRoleToUsersWithoutRoles(ctx context.Context) (int, error)

AssignDefaultRoleToUsersWithoutRoles assigns the default role to all users who don't have any roles Returns the number of users updated

func (*Seeder) AssignRoleToUser

func (s *Seeder) AssignRoleToUser(ctx context.Context, userID string, roleName RoleName) error

AssignRoleToUser assigns a specific role to a user by role name

func (*Seeder) GetPermissionID

func (s *Seeder) GetPermissionID(ctx context.Context, permName PermissionName) (string, error)

GetPermissionID returns the database ID for a permission name

func (*Seeder) GetRoleID

func (s *Seeder) GetRoleID(ctx context.Context, roleName RoleName) (string, error)

GetRoleID returns the database ID for a role name

func (*Seeder) RemoveRoleFromUser

func (s *Seeder) RemoveRoleFromUser(ctx context.Context, userID string, roleName RoleName) error

RemoveRoleFromUser removes a specific role from a user by role name

func (*Seeder) SeedAll

func (s *Seeder) SeedAll(ctx context.Context) error

SeedAll seeds all predefined roles, permissions, and their relationships

func (*Seeder) SeedPermissions

func (s *Seeder) SeedPermissions(ctx context.Context) error

SeedPermissions seeds all predefined permissions into the database

func (*Seeder) SeedRolePermissions

func (s *Seeder) SeedRolePermissions(ctx context.Context) error

SeedRolePermissions seeds the role-permission relationships

func (*Seeder) SeedRoles

func (s *Seeder) SeedRoles(ctx context.Context) error

SeedRoles seeds all predefined roles into the database

Jump to

Keyboard shortcuts

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