Documentation
¶
Index ¶
- Variables
- func IsNilOrEmpty(val any) bool
- func RequirePermission(gate *Gate, permissionName string, opts *MiddlewareOptions) echo.MiddlewareFunc
- type Config
- type DBTX
- type Gate
- func (g *Gate) Check(ctx context.Context, modelType string, modelID any, permissionName string, ...) (bool, error)
- func (g *Gate) CreatePermission(ctx context.Context, name string, guardName string) error
- func (g *Gate) CreateRole(ctx context.Context, name string, guardName string) error
- func (g *Gate) DeletePermission(ctx context.Context, name string) error
- func (g *Gate) DeleteRole(ctx context.Context, name string) error
- func (g *Gate) GetAllPermissionsMap(ctx context.Context) (map[string][]string, error)
- func (g *Gate) GetAllRolesMap(ctx context.Context) (map[string][]string, error)
- func (g *Gate) HasRolePermission(guardName, roleName, permissionName string) bool
- func (g *Gate) LoadPolicy(ctx context.Context) error
- func (g *Gate) Model(modelType string, modelID any, teamID any) *ModelRef
- func (g *Gate) Role(name string, guardName string) *RoleRef
- type MiddlewareOptions
- type ModelRef
- func (m *ModelRef) AssignRole(ctx context.Context, roleName string, guardName string) error
- func (m *ModelRef) Can(ctx context.Context, permissionName string, guardName string) (bool, error)
- func (m *ModelRef) GetAllPermissions(ctx context.Context) ([]string, error)
- func (m *ModelRef) GetDirectPermissions(ctx context.Context) ([]string, error)
- func (m *ModelRef) GetPermissionsViaRoles(ctx context.Context) ([]string, error)
- func (m *ModelRef) GetRoleNames(ctx context.Context) ([]string, error)
- func (m *ModelRef) GetRolesMap(ctx context.Context) (map[string][]string, error)
- func (m *ModelRef) GivePermissionTo(ctx context.Context, permissionName string, guardName string) error
- func (m *ModelRef) HasAllPermissions(ctx context.Context, permissionNames ...string) (bool, error)
- func (m *ModelRef) HasAllRoles(ctx context.Context, guardName string, roleNames ...string) (bool, error)
- func (m *ModelRef) HasAnyPermission(ctx context.Context, permissionNames ...string) (bool, error)
- func (m *ModelRef) HasAnyRole(ctx context.Context, guardName string, roleNames ...string) (bool, error)
- func (m *ModelRef) HasRole(ctx context.Context, roleName string, guardName string) (bool, error)
- func (m *ModelRef) RemoveRole(ctx context.Context, roleName string, guardName string) error
- func (m *ModelRef) RevokePermissionTo(ctx context.Context, permissionName string, guardName string) error
- type RoleRef
Constants ¶
This section is empty.
Variables ¶
var ( ErrRoleAlreadyExists = errors.New("wpd-gogate: role already exists") ErrPermissionAlreadyExists = errors.New("wpd-gogate: permission already exists") )
Functions ¶
func IsNilOrEmpty ¶ added in v1.1.0
IsNilOrEmpty checks if a value is nil, an empty string, a nil pointer, or a zero UUID.
func RequirePermission ¶
func RequirePermission(gate *Gate, permissionName string, opts *MiddlewareOptions) echo.MiddlewareFunc
RequirePermission returns an Echo middleware enforcing that the authenticated model has the required permission.
Types ¶
type Config ¶
type Config struct {
RolesTable string
PermissionsTable string
RoleHasPermissionsTable string
ModelHasRolesTable string
ModelHasPermissionsTable string
DefaultGuardName string
}
Config defines the table names and defaults for the RBAC gate, mirroring standard relational database RBAC conventions.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the standard config mapping to defaults.
type DBTX ¶
type DBTX interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
DBTX is the minimal database interface required by wpd-gogate. It is satisfied by *sql.DB and *sql.Tx.
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
Gate is the core engine for role-based and permission-based authorization.
func (*Gate) Check ¶
func (g *Gate) Check(ctx context.Context, modelType string, modelID any, permissionName string, guardName string, teamID any) (bool, error)
Check verifies if the model (e.g. user) has the required permission. It queries both direct permissions and roles in a single database round-trip (UNION ALL), then maps them against the in-memory cache to determine access. teamID is optional and can be nil to check global assignments.
func (*Gate) CreatePermission ¶
CreatePermission inserts a new permission into the database.
func (*Gate) CreateRole ¶
CreateRole inserts a new role into the database.
func (*Gate) DeletePermission ¶
DeletePermission deletes a permission from the database and removes it from all roles in the cache.
func (*Gate) DeleteRole ¶
DeleteRole deletes a role from the database and removes it from the cache.
func (*Gate) GetAllPermissionsMap ¶ added in v1.2.0
GetAllPermissionsMap returns all permissions in the database, grouped by guard_name.
func (*Gate) GetAllRolesMap ¶ added in v1.2.0
GetAllRolesMap returns all roles in the database, grouped by guard_name.
func (*Gate) HasRolePermission ¶
HasRolePermission performs an in-memory O(1) check of whether a role is assigned a specific permission.
func (*Gate) LoadPolicy ¶
LoadPolicy fetches all role-permission associations from the database and caches them in memory. This is thread-safe and should be run on boot or when permissions are updated.
type MiddlewareOptions ¶
type MiddlewareOptions struct {
// ModelType specifies the type of model being checked (default: "users").
ModelType string
// ExtractModelID extracts the model identifier (e.g., user UUID) from the context.
ExtractModelID func(c echo.Context) (any, error)
// ExtractTeamID extracts the team or workspace identifier (optional, e.g., workspace UUID) from the context.
ExtractTeamID func(c echo.Context) (any, error)
// OnDenied defines the response when permission is denied.
OnDenied func(c echo.Context, permissionName string) error
// OnError defines the response when an internal database error occurs.
OnError func(c echo.Context, err error) error
// GuardName specifies the guard name for this check. If empty, uses default.
GuardName string
}
MiddlewareOptions configures how the RBAC middleware behaves.
func DefaultMiddlewareOptions ¶
func DefaultMiddlewareOptions() MiddlewareOptions
DefaultMiddlewareOptions provides sensible defaults for Echo web applications.
type ModelRef ¶
type ModelRef struct {
// contains filtered or unexported fields
}
ModelRef provides a fluent, scoped API for a specific model (e.g., User, APIKey) in the context of an optional team or workspace.
func (*ModelRef) AssignRole ¶
AssignRole assigns the given role to the model.
func (*ModelRef) GetAllPermissions ¶
GetAllPermissions returns both direct and inherited permissions.
func (*ModelRef) GetDirectPermissions ¶
GetDirectPermissions returns the names of all direct permissions assigned to the model.
func (*ModelRef) GetPermissionsViaRoles ¶
GetPermissionsViaRoles returns all permissions inherited by the model's roles.
func (*ModelRef) GetRoleNames ¶
GetRoleNames returns the names of all roles assigned to the model.
func (*ModelRef) GetRolesMap ¶ added in v1.1.0
GetRolesMap returns all roles assigned to the model, grouped by guard_name.
func (*ModelRef) GivePermissionTo ¶
func (m *ModelRef) GivePermissionTo(ctx context.Context, permissionName string, guardName string) error
GivePermissionTo assigns a direct permission override to the model.
func (*ModelRef) HasAllPermissions ¶ added in v1.1.0
HasAllPermissions checks if the model has all of the specified permissions.
func (*ModelRef) HasAllRoles ¶ added in v1.1.0
func (m *ModelRef) HasAllRoles(ctx context.Context, guardName string, roleNames ...string) (bool, error)
HasAllRoles checks if the model has all of the specified roles.
func (*ModelRef) HasAnyPermission ¶ added in v1.1.0
HasAnyPermission checks if the model has any of the specified permissions.
func (*ModelRef) HasAnyRole ¶ added in v1.1.0
func (m *ModelRef) HasAnyRole(ctx context.Context, guardName string, roleNames ...string) (bool, error)
HasAnyRole checks if the model has at least one of the specified roles.
func (*ModelRef) HasRole ¶ added in v1.1.0
HasRole checks if the model has the specified role directly in the database.
func (*ModelRef) RemoveRole ¶
RemoveRole removes the given role from the model.
type RoleRef ¶
type RoleRef struct {
// contains filtered or unexported fields
}
RoleRef provides a fluent API for managing a specific Role's permissions.
func (*RoleRef) GetPermissionNames ¶
GetPermissionNames returns names of all permissions assigned to the role.
func (*RoleRef) GivePermissionTo ¶
GivePermissionTo assigns the specified permission to the role in the database and immediately updates the in-memory cache.