<<

NAME

Koha::Module::Policy - Base class for module-scoped policy contracts

SYNOPSIS

    # Subclass implementation
    package Koha::Module::Policy::Checkin;
    use parent 'Koha::Module::Policy';

    sub module { return 'checkin' }
    sub _build_hashref { ... }

    # Usage in a controller
    my $policy = Koha::Module::Policy::Checkin->new(
        {
            user    => $c->stash('koha.user'),
            library => $library,
        }
    );

    my $hashref = $policy->to_hashref;
    # {
    #     module  => 'checkin',
    #     global  => { audio_alerts => true, ... },
    #     checkin => { exempt_fine => false, ... },
    # }

    # Client reads the policy to know what the UI should offer.

DESCRIPTION

A module policy represents the current capabilities available to a specific user, at a specific library, for a given functional module (checkin, checkout, ERM, etc.).

It is the intersection of:

The policy is serialized as JSON, injected into the page on load and returned in the policy attribute of the error object when the API refuses a request because of it (see Koha::REST::Plugin::Policy). Both follow the module_policy API definition. The client reads it to render the appropriate UI controls. The server remains authoritative - the policy is informational, not an access token. Every action is still validated server-side.

API

Class methods

new

    my $policy = Koha::Module::Policy::Checkin->new(
        {
            user    => $patron_object,
            library => $library_id,
        }
    );

Constructor. Requires:

Optional:

Subclasses may accept and use additional parameters.

Instance methods

user

    my $user = $policy->user;

Returns the Koha::Patron object.

library

    my $library_id = $policy->library;

Returns the library branchcode.

to_hashref

    my $hashref = $policy->to_hashref;

Returns the policy as a plain hashref, suitable for JSON serialization. The result is cached for the lifetime of the object.

The returned hashref is an envelope matching the module_policy API definition:

Flags are JSON booleans (Mojo::JSON true and false).

Example structure:

    {
        module => 'checkin',
        global => {
            audio_alerts     => true,
            catalog_concerns => false,
        },
        checkin => {
            exempt_fine         => true,
            specify_return_date => true,
            ...
        },
    }

Global policy keys

The following keys are provided under the global namespace for all module policies. They are computed once at construction time from system preferences and cannot be overridden by subclasses.

audio_alerts - Audio alerts are enabled (AudioAlerts syspref)
catalog_concerns - "Report a concern" feature is available (CatalogConcerns syspref)

module

    my $module = $policy->module;

Returns the module name (e.g. 'checkin'). It is the value of the module attribute of the policy envelope, and the key the module-specific policy is placed under.

Subclasses must implement this method.

scope

    my $scope = $policy->scope;

Returns the policy scope name (e.g., 'checkin', 'checkout', 'erm'). Derived from the class name by default.

Subclasses may override this.

Abstract methods

_build_hashref

    my $hashref = $self->_build_hashref;

Subclasses must implement this method. It should return a hashref representing the current policy state for the user/library combination.

Keys should be stable identifiers (the client's contract, described in the API definition for the module policy). Values are booleans (see "_boolean"), integers, or simple strings. Never expose raw syspref names.

Internal methods

_boolean

    my $flag = $self->_boolean($value);

Returns a JSON boolean (Mojo::JSON true or false) for the truthiness of $value.

TODO

Add an env namespace to carry session/environment context that every module needs but that is not a feature flag. This would replace the template params currently injected by C4::Auth::get_template_and_user for all pages - Vue-based and TT-based alike.

While this framework was designed for Vue module policies, it is equally a refactoring of the monolithic and unmaintainable code in get_template_and_user, which today scatters 50+ individually-set template params with no structure, no contract, and no testability. The policy pattern provides all three: structured data, a stable client contract, and a unit-testable class per module.

Candidate keys for the env namespace:

    env => {
        dateformat    => 'metric',        # C4::Context->preference('dateformat')
        time_zone     => 'America/...',   # Library timezone
        language      => 'en',            # Interface language
        library_id    => 'CPL',           # Logged-in branch
        library_name  => 'Centerville',   # For display
        user_id       => 51,              # Borrowernumber
        single_branch => 0,              # Koha::Libraries->count == 1
    }

This separates concerns: global = feature flags (sysprefs), env = session context, $module = module-specific capabilities. Together they provide the full UI contract, making get_template_and_user's param injection redundant for pages that adopt this pattern.

AUTHOR

Koha Development Team <https://koha-community.org/>

<<