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


KiokuX::User - A generic role for user objects stored in KiokuDB


    package MyFoo::Schema::User;
    use Moose;

    use KiokuX::User::Util qw(crypt_password);

    with qw(KiokuX::User);

    my $user = MyFoo::Schema::User->new(
        id       => $user_id,
        password => crypt_password($password),

    $user->kiokudb_object_id; # "user:$user_id"

    if ( $user->check_password($read_password) ) {
        warn "Login successful";
    } else {
        warn "Login failed";


This role provides a fairly trivial set of attributes and methods designed to ease the storage of objects representing users in a KiokuDB database.

It consumes KiokuX::User::ID which provides the id attribute and related methods as well as KiokuDB::Role::ID integration, and KiokuX::User::Password which provides an Authen::Passphrase based password attribute and a check_password method.


This role strictly implements a notion of an authenticatable identity, not of a user.

If you want to support renaming, multiple authentication methods (e.g. a password and/or an openid), it's best to create identity delegates that consume this role, and have them point at the actual user object:

    package MyFoo::Schema::Identity;
    use Moose::Role;

    has user => (
        isa => "MyFoo::Schema::User",
        is  => "ro",
        required => 1,

And here's an example username identity:

    package MyFoo::Schema::Identity::Username;
    use Moose;

    with qw(

and then point back to these identities from the user:

    has identities => (
        isa      => "ArrayRef[MyFoo::Schema::Identity]",
        is       => "rw",
        required => 1,

Since the identity is part of the objects' ID uniqueness is enforced in a portable way (you don't need to use the DBI backend and a custom unique constraint).

This also allows you to easily add additional authentication schemes, change them, provide namespacing support and so on without affecting the high level user object, which represents the actual account holder regardless of the authentication scheme they used.



Yuval Kogman <>


    Copyright (c) 2008, 2009 Yuval Kogman, Infinity Interactive. All
    rights reserved This program is free software; you can redistribute
    it and/or modify it under the same terms as Perl itself.