Documentation
¶
Index ¶
- func AssignPermission(roleID, permID string) error
- func AssignRole(userID, roleID string) error
- func CreatePermission(id, name, resource string, action byte) error
- func CreateRole(id string, code byte, name, description string) error
- func DeletePermission(id string) error
- func DeleteRole(id string) error
- func GetUserRoleCodes(userID string) ([]byte, error)
- func HasPermission(userID, resource string, action byte) (bool, error)
- func Init(exec Executor) error
- func Register(handlers ...any) error
- func RevokePermission(roleID, permID string) error
- func RevokeRole(userID, roleID string) error
- func SetLog(fn func(messages ...any))
- type Executor
- type Permission
- type Role
- type Rows
- type Scanner
- type Store
- func (s *Store) AssignPermission(roleID, permID string) error
- func (s *Store) AssignRole(userID, roleID string) error
- func (s *Store) CreatePermission(id, name, resource string, action byte) error
- func (s *Store) CreateRole(id string, code byte, name, description string) error
- func (s *Store) DeletePermission(id string) error
- func (s *Store) DeleteRole(id string) error
- func (s *Store) GetPermission(id string) (*Permission, error)
- func (s *Store) GetRole(id string) (*Role, error)
- func (s *Store) GetRoleByCode(code byte) (*Role, error)
- func (s *Store) GetUserRoleCodes(userID string) ([]byte, error)
- func (s *Store) GetUserRoles(userID string) ([]Role, error)
- func (s *Store) HasPermission(userID, resource string, action byte) (bool, error)
- func (s *Store) ListPermissions() ([]Permission, error)
- func (s *Store) ListRoles() ([]Role, error)
- func (s *Store) Migrate() error
- func (s *Store) Register(handlers ...any) error
- func (s *Store) RevokePermission(roleID, permID string) error
- func (s *Store) RevokeRole(userID, roleID string) error
- func (s *Store) SetLog(fn func(messages ...any))
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignPermission ¶ added in v0.0.2
func AssignRole ¶ added in v0.0.2
func CreatePermission ¶ added in v0.0.2
func CreateRole ¶ added in v0.0.2
func DeletePermission ¶ added in v0.0.2
func DeleteRole ¶ added in v0.0.2
func GetUserRoleCodes ¶ added in v0.0.2
func HasPermission ¶ added in v0.0.2
func Init ¶ added in v0.0.2
Init initializes the package-level singleton. Safe to call multiple times; only the first call has effect (sync.Once).
func RevokePermission ¶ added in v0.0.2
func RevokeRole ¶ added in v0.0.2
func SetLog ¶ added in v0.0.2
func SetLog(fn func(messages ...any))
SetLog configures the logger used by rbac for internal events (SQL errors, cache load events, write-through mutations). Default: no-op — rbac is silent unless SetLog is called. Call before Init().
Example:
rbac.SetLog(func(msg ...any) { log.Println(msg...) })
Types ¶
type Executor ¶ added in v0.0.2
type Executor interface {
Exec(query string, args ...any) error
QueryRow(query string, args ...any) Scanner
Query(query string, args ...any) (Rows, error)
}
Executor is the primary SQL dependency interface. Satisfied by *sql.DB and *sql.Tx without importing database/sql.
type Permission ¶ added in v0.0.2
func GetPermission ¶ added in v0.0.2
func GetPermission(id string) (*Permission, error)
func ListPermissions ¶ added in v0.0.2
func ListPermissions() ([]Permission, error)
type Role ¶ added in v0.0.2
func GetRoleByCode ¶ added in v0.0.2
func GetUserRoles ¶ added in v0.0.2
type Rows ¶ added in v0.0.2
Rows is the interface for iterating multi-row results. Satisfied by *sql.Rows.
type Scanner ¶ added in v0.0.2
Scanner is the interface for scanning a single row result. Satisfied by *sql.Row.
type Store ¶ added in v0.0.2
type Store struct {
// contains filtered or unexported fields
}
func New ¶
New creates a new store instance, runs migration, and loads the cache. After New() returns successfully, all authorization reads are served from memory.
func (*Store) AssignPermission ¶ added in v0.0.2
AssignPermission assigns a permission to a role.
func (*Store) AssignRole ¶ added in v0.0.2
AssignRole assigns a role to a user.
func (*Store) CreatePermission ¶ added in v0.0.2
CreatePermission creates a new permission. Uses: INSERT INTO rbac_permissions ... ON CONFLICT (resource, action) DO NOTHING Idempotent: each domain module can register its permissions on every startup.
func (*Store) CreateRole ¶ added in v0.0.2
CreateRole creates a new role. Uses: INSERT INTO rbac_roles ... ON CONFLICT (code) DO NOTHING Idempotent: safe to call on every startup with the same code.
func (*Store) DeletePermission ¶ added in v0.0.2
DeletePermission deletes a permission by ID.
func (*Store) DeleteRole ¶ added in v0.0.2
DeleteRole deletes a role by ID.
func (*Store) GetPermission ¶ added in v0.0.2
func (s *Store) GetPermission(id string) (*Permission, error)
GetPermission returns a permission by ID from cache.
func (*Store) GetRoleByCode ¶ added in v0.0.2
GetRoleByCode returns a role by Code from cache.
func (*Store) GetUserRoleCodes ¶ added in v0.0.2
GetUserRoleCodes returns the Code byte slice for all user roles (from cache). Returns []byte{'a', 'e'} for a user who is admin and editor. Compatible with any role-based access system that uses byte role codes.
func (*Store) GetUserRoles ¶ added in v0.0.2
GetUserRoles returns all Role records assigned to the user (from cache).
func (*Store) HasPermission ¶ added in v0.0.2
HasPermission returns true if the user has any role that holds a permission matching {resource, action} (from cache, zero I/O).
func (*Store) ListPermissions ¶ added in v0.0.2
func (s *Store) ListPermissions() ([]Permission, error)
ListPermissions returns all permissions from cache.
func (*Store) Register ¶ added in v0.0.2
Register seeds permissions and role assignments into the database for each handler. Uses duck-typing — no import of crudp required. Reads HandlerName() as the resource name, AllowedRoles(action) as the role codes authorized. Handlers not implementing both interfaces are silently skipped.
Permission name format: "resource:action" (e.g. "invoice:r") — readable in admin UIs.
All operations are idempotent:
- CreatePermission uses ON CONFLICT (resource, action) DO NOTHING
- AssignPermission uses ON CONFLICT DO NOTHING
Must be called after Init() and after roles are created (CreateRole / seeding). Generates permission IDs internally via unixid.New().
func (*Store) RevokePermission ¶ added in v0.0.2
RevokePermission revokes a permission from a role.
func (*Store) RevokeRole ¶ added in v0.0.2
RevokeRole revokes a role from a user.