Utilities

This section shows general objects and helper functions that are used with this package.

Permission Test Utilities

Resource

class balderhub.auth.lib.utils.BaseResource

Bases: ABC

This is the base class for all resource types.

exception DoesNotExistError

Bases: ResourceEnterError

Exception that is raised if a resource does not exist.

exception NoPermissionError

Bases: ResourceEnterError

Exception that is raised if the user has no permission to access a resource.

exception ResourceEnterError

Bases: Exception

Base exception for all errors that occur when entering a resource.

exception UnauthorizedError

Bases: ResourceEnterError

Exception that is raised if the user is not authorized to access a resource.

class balderhub.auth.lib.utils.Resource

Bases: BaseResource

This class represents a resolved resource.

class balderhub.auth.lib.utils.UnresolvedResource(**kwargs)

Bases: BaseResource, ABC

This is the base class for all unresolved resource types.

class Parameter

Bases: object

Base class for parameters that are used to resolve an unresolved resource.

abstract get_resolved_resource(param: Parameter) Resource

Resolves the resource with the given parameter.

Parameters:

param – the parameter for the unresolved resource

Returns:

the resolved resource

Operation

class balderhub.auth.lib.utils.Operation(resource: Resource, action: Action)

Bases: object

This class represents an operation, which is a combination of a resource and an action.

exception DoesNotExistError

Bases: OperationEnterError

Exception that is raised if the peration does not exist.

exception NoPermissionError

Bases: OperationEnterError

Exception that is raised if the user has no permission to perform the action on the resource.

exception OperationEnterError

Bases: Exception

Base exception for all errors that occur when executing an operation.

exception UnauthorizedError

Bases: OperationEnterError

Exception that is raised if the user is not authorized to perform the action on the resource.

property action: Action

returns the action of this operation

property resource: Resource

returns the resource of this operation

class balderhub.auth.lib.utils.OperationList(iterable: Iterable[OperationT] | None = None)

Bases: object

This class represents a list of operations.

append(operation: OperationT)

Appends an operation to the list.

Parameters:

operation – the operation to append

extend(other: OperationList)

Extends the list with the operations from another OperationList.

Parameters:

other – the other OperationList to extend this list with

Action

class balderhub.auth.lib.utils.Action

Bases: object

base class for actions

Rules

class balderhub.auth.lib.utils.ResourceRule(resource: Resource | UnresolvedResource, actions: list[balderhub.auth.lib.utils.action.Action], rule: Callable[[Parameter], bool] | None = None)

Bases: object

This class represents a rule for a resource and a list of actions that are allowed for this resource.

property actions: list[balderhub.auth.lib.utils.action.Action]

returns a copy of the list of actions of this rule

property cb_rule: Callable[[Parameter], bool] | None

returns the rule callback of this rule

copy() ResourceRule

creates a deep copy of this rule

flatten() ResourceRuleList

Converts this rule into a ResourceRuleList where every ResourceRule has exactly one Action.

Returns:

a ResourceRuleList containing the flattened rules

resolve(parameter: Parameter | None = None) OperationList | None

Resolves the rule with the given parameter.

If the resource is a resolved resource, the parameter must be None. If the resource is an unresolved resource, the parameter can be provided. If a parameter is provided, the rule function is called with the parameter. If the rule function returns False, None is returned. Otherwise, the unresolved resource is resolved with the parameter and an OperationList containing the resolved resource and the actions is returned.

Parameters:

parameter – the parameter for the unresolved resource

Returns:

an OperationList containing the resolved operations or None if the rule does not match

property resource: Resource | UnresolvedResource

returns the resource of this rule

update_rule(new_rule: Callable[[Parameter], bool] | None) None

updates the rule callback of this rule

class balderhub.auth.lib.utils.ResourceRuleList(iterable: Iterable[ResourceRule] | None = None)

Bases: object

This class represents a list of resource rules.

append(rule: ResourceRule)

Appends a resource rule to the list.

Parameters:

rule – the resource rule to append

extend(other: ResourceRuleList)

Extends the list with the rules from another ResourceRuleList.

Parameters:

other – the other ResourceRuleList to extend this list with

filter_for_resolved_only()

Filters the list for rules with resolved resources only.

Returns:

a new ResourceRuleList containing only the rules with resolved resources

filter_for_unresolved_only()

Filters the list for rules with unresolved resources only.

Returns:

a new ResourceRuleList containing only the rules with unresolved resources

flatten() ResourceRuleList

Goes through every list item and converts it to a list of ResourceRules with exactly one Action :return: a flatten ResourceRuleList, where every ResourceRule has exactly one Action

flatten_and_group_by_resource_and_action() dict[balderhub.auth.lib.utils.resource.Resource, dict[balderhub.auth.lib.utils.action.Action, balderhub.auth.lib.utils.resource_rule.ResourceRule]]

Flattens the rules and groups them by resource and action.

Returns:

a dictionary where the keys are resources and the values are dictionaries where the keys are actions and the values are the corresponding resource rules

property rules: list[balderhub.auth.lib.utils.resource_rule.ResourceRule]

returns the list of rules