| 1 | package Catalyst::Authentication::Store::KiokuDB; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.03'; | ||
| 7 | |||
| 8 | use Catalyst::Authentication::User::KiokuDB; | ||
| 9 | use Search::GIN::Extract::Class; | ||
| 10 | use Search::GIN::Extract::Attributes; | ||
| 11 | use Search::GIN::Extract::Multiplex; | ||
| 12 | use Search::GIN::Query::Attributes; | ||
| 13 | use KiokuDB; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ($class, $conf, $app, $realm) = @_; | ||
| 17 | |||
| 18 | my %self; | ||
| 19 | if ($conf->{kiokuObject}) { | ||
| 20 | $self{kioku} = $conf->{kiokuObject}; | ||
| 21 | } | ||
| 22 | elsif ($conf->{kiokuDir}) { | ||
| 23 | my $ks = $conf->{kiokuBackend} || 'bdb'; | ||
| 24 | my $extract = Search::GIN::Extract::Multiplex->new({ | ||
| 25 | extractors => [ | ||
| 26 | Search::GIN::Extract::Class->new, | ||
| 27 | Search::GIN::Extract::Attributes->new, | ||
| 28 | ] | ||
| 29 | }); | ||
| 30 | if ($ks eq 'bdb') { | ||
| 31 | eval { | ||
| 32 | require KiokuDB::Backend::BDB::GIN; | ||
| 33 | KiokuDB::Backend::BDB::GIN->import(); | ||
| 34 | }; | ||
| 35 | Catalyst::Exception->throw( | ||
| 36 | message => "Failed to load BDB backend: $@." | ||
| 37 | ) if $@; | ||
| 38 | $self{kioku} = KiokuDB->new( | ||
| 39 | backend => KiokuDB::Backend::BDB::GIN->new( | ||
| 40 | manager => { home => $conf->{kiokuDir}, create => 1 }, | ||
| 41 | extract => $extract, | ||
| 42 | ), | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | elsif ($ks eq 'sqlite') { | ||
| 46 | eval { | ||
| 47 | require KiokuDB::Backend::SQLite::GIN; | ||
| 48 | KiokuDB::Backend::SQLite::GIN->import(); | ||
| 49 | }; | ||
| 50 | Catalyst::Exception->throw( | ||
| 51 | message => "Failed to load SQLite backend: $@." | ||
| 52 | ) if $@; | ||
| 53 | $self{kioku} = KiokuDB->new( | ||
| 54 | backend => KiokuDB::Backend::SQLite::GIN->new( | ||
| 55 | dir => $conf->{kiokuDir}, | ||
| 56 | extract => $extract, | ||
| 57 | ), | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | Catalyst::Exception->throw( | ||
| 62 | message => "Unknown kioku backend '$ks'\n" | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | Catalyst::Exception->throw( | ||
| 68 | message => "KiokuDB requires at least 'kiokuObject' or 'kiokuDir' to be set." | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | |||
| 72 | $self{userClass} = $conf->{userClass} || 'Catalyst::Authentication::User::KiokuDB'; | ||
| 73 | $self{kiokuScope} = $self{kioku}->new_scope(); | ||
| 74 | return bless \%self, $class; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub from_session { | ||
| 78 | my ($self, $c, $id) = @_; | ||
| 79 | |||
| 80 | return $id if ref $id; | ||
| 81 | return $self->find_user({ id => $id }); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub find_user { | ||
| 85 | my ($self, $userinfo, $c) = @_; | ||
| 86 | |||
| 87 | return $self->{kioku}->lookup($userinfo->{id}) if $userinfo->{id}; | ||
| 88 | my $q = Search::GIN::Query::Attributes->new({ attributes => $userinfo }); | ||
| 89 | my @res = $self->{kioku}->search($q)->all; # XXX can't we just get the first? | ||
| 90 | return $res[0]; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub user_supports { | ||
| 94 | my $self = shift; | ||
| 95 | return Catalyst::Authentication::User::KiokuDB->supports(@_); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub get_user { | ||
| 99 | my ($self, $id) = @_; | ||
| 100 | $self->find_user({id => $id}); | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =head1 NAME | ||
| 108 | |||
| 109 | Catalyst::Authentication::Store::KiokuDB - KiokuDB store for auth - THIS MODULE IS DEPRECATED, USE Catalyst::Authentication::Store::Model::KiokuDB INSTEAD. | ||
| 110 | |||
| 111 | =head1 SYNOPSIS | ||
| 112 | |||
| 113 | use Catalyst qw/ | ||
| 114 | Authentication | ||
| 115 | /; | ||
| 116 | |||
| 117 | __PACKAGE__->config->{'authentication'} = | ||
| 118 | { | ||
| 119 | default_realm => 'admin', | ||
| 120 | realms => { | ||
| 121 | admin => { | ||
| 122 | credential => { | ||
| 123 | class => 'Password', | ||
| 124 | password_field => 'password', | ||
| 125 | password_type => 'clear' | ||
| 126 | }, | ||
| 127 | store => { | ||
| 128 | class => 'KiokuDB', | ||
| 129 | kiokuDir => '/path/to/some/dir', | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | }; | ||
| 134 | |||
| 135 | |||
| 136 | =head1 DESCRIPTION | ||
| 137 | |||
| 138 | This is a simple authentication store that uses KiokuDB as its backend storage. | ||
| 139 | It is recommended if you are already using KiokuDB and wish to centralise all | ||
| 140 | of your storage there. | ||
| 141 | |||
| 142 | =head1 CONFIGURATION | ||
| 143 | |||
| 144 | Only one of the following should be specified. | ||
| 145 | |||
| 146 | =over 4 | ||
| 147 | |||
| 148 | =item kiokuDir | ||
| 149 | |||
| 150 | This is the path to the directory in which you tell KiokuDB to store its data. | ||
| 151 | |||
| 152 | =item kiokuObject | ||
| 153 | |||
| 154 | This is an already instantiated KiokuDB object which you wish to reuse to | ||
| 155 | store you data with. This object needs to support searching based on | ||
| 156 | attributes. | ||
| 157 | |||
| 158 | =item kiokuBackend | ||
| 159 | |||
| 160 | Specifies which backend type to use with Kioku, if you're not passing your | ||
| 161 | own Kioku object. Supported types are 'bdb' (the default) and 'sqlite'. | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 METHODS | ||
| 166 | |||
| 167 | This store implements no methods outside of those required by its base class. | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | Robin Berjon, <robin@berjon.com>, L<http://robineko.com/> | ||
| 172 | |||
| 173 | =head1 COPYRIGHT | ||
| 174 | |||
| 175 | This program is free software, you can redistribute it and/or modify it | ||
| 176 | under the same terms as Perl itself. | ||
| 177 | |||
| 178 | =cut |