The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Jifty::Manual::AccessControl

DESCRIPTION

Out of the box Jifty-based applications have an ACL system. The system automatically validates ACLs on Jifty::Record objects by calling the method current_user_can before any create, read, update, or delete operation. In all cases, the arguments passed to the CRUD operation are passed as extra arguments to current_user_can.

On create(), we reject the operation if current_user_can('create') returns FALSE.

On _value() or somefieldname, we reject the operation if current_user_can('read') returns false.

On _set() or set_somefieldname, we reject the operation if current_user_can('write') returns false.

On delete(), we reject the operation if current_user_can('delete') returns false.

Out of the box, current_user_can returns 1. When you want to actually check ACLs, you'll need to override current_user_can() in your Jifty::Record subclass.

It's likely that at some point, you'll decide you want to ask other questions on certain types of operation. Say, you only want to let administrators update the paid_account field. In that case, you'd override check_update_rights() to look for the admin right rather than the update right, if the FIELD is paid_account.

ENABLING ACCESS CONTROL USING THE LOGIN PLUGIN

To painlessly enable the AccessControl subsystem, the Login plugin may get enabled. This is done in the etc/config.yml configuration file.

    Plugins:
      - Login: {}

Then, create an App::Model::User class that derives from Jifty::Plugin::Login::Model::User, for example like that:

    use strict;
    use warnings;

    package App::Model::User;
    use base 'Jifty::Plugin::Login::Model::User';

    # Your model-specific methods go here.

    1;

Next, create the table in your database using the jifty executable like ./bin/jifty schema --setup.

Expanding the Model

The model that manages User Records is not limited to the plugin's definition. It can be expanded by providing an additional schema definition. Every column here will be added to the plugin's columns. Simply add a schema definition block like the following:

    use Jifty::DBI::Schema;
    use App::Record schema {
        column 'extra_column_name';

        # more columns if necessary
    };

The full syntax for defining a schema can be found in Jifty::Manual::Models or in Jifty::DBI::Schema.

Defining a method _init in your App::CurrentUser class gives you a chance to add more data to the CurrentUser object. This method will automatically get called after the Plugin's _init is done.

Templates defined by the Login plugin

To avoid the need for repetitive work, the Login plugin already defines a couple of usable templates:

/login

provides a login screen with a signup option. After successful login, the current Continuation is called. If no Continuation exists, the template sitting at the base URL (/) is called.

/logout

logs out the current user.

/signup

allows a user to sign up himself. As a default behavior a confirmation mail is sent out that has to get followed by the user.

/chgpasswd

allows a user to change his password.

/passwordreminder

after entering his mail address, the user will receive a mail that contains a link to /let/reset_lost_password.

/let/confirm_email

is called in the mail and results in accepting the user.

/let/reset_lost_password

enabled by the passwordreminder template, this template allows a user to reenter a password for future use.

Doing checks at other places in your code

If you need to check more than Model-based record operations you will have to do some coding on your own. Jifty->web->current_user provides a App::CurrentUser object that can get queried about the current user. This object provides some convenience methods:

username

returns the name of the current user or undef if not logged in.

id

returns the id of the current user or undef if not logged in.

SEE ALSO

Jifty::CurrentUser, Jifty::Record, Jifty::RightsFrom