| 1 | ## no critic | ||||||||||
| 2 | # no critic line turns off Perl::Critic, to get rid of the warning that | ||||||||||
| 3 | # Dist::Zilla::Plugin::PkgVersion puts the package version straight | ||||||||||
| 4 | package Catalyst::Authentication::Store::CouchDB::User; | ||||||||||
| 5 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | $Catalyst::Authentication::Store::CouchDB::User::VERSION = '0.001'; | |||||||||
| 7 | } | ||||||||||
| 8 | # ABSTRACT: The backing user class for the Catalyst::Authentication::Store::CouchDB storage module. | ||||||||||
| 9 | ## use critic | ||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Moose 2.000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use MooseX::NonMoose 0.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use CouchDB::Client 0.09 qw (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Catalyst::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Catalyst::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use JSON 2.17 qw (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use Try::Tiny 0.09; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 2 | extends 'Catalyst::Authentication::User'; | |||||||||
| 24 | |||||||||||
| 25 | 2 | has '_user' => (is => 'rw', isa => 'CouchDB::Client::Doc', ); | |||||||||
| 26 | 2 | has '_couchdb' => (is => 'ro', isa => 'CouchDB::Client::DB', ); | |||||||||
| 27 | 2 | has '_designdoc' => (is => 'ro', isa => 'CouchDB::Client::DesignDoc', ); | |||||||||
| 28 | 2 | has 'view' => (is => 'ro', isa => 'Str', required => 1, ); | |||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | around BUILDARGS => sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=23.6
| |||||||||||
| 31 | 1 | my ($orig, $class, $config, $c) = @_; | |||||||||
| 32 | |||||||||||
| 33 | # Allow the User Agent to be overridden - this is handy for tests to | ||||||||||
| 34 | # mock up the CouchDB interaction | ||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | my $ua_class = (exists $config->{ua} ? $config->{ua} : 'LWP::UserAgent'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1 | Catalyst::Utils::ensure_class_loaded($ua_class); | |||||||||
| 38 | 1 | my $ua = $ua_class->new(); | |||||||||
| 39 | |||||||||||
| 40 | my $couch = CouchDB::Client->new( | ||||||||||
| 41 | uri => $config->{couchdb_uri}, | ||||||||||
| 42 | 1 | ua => $ua, | |||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | if (!$couch->testConnection()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | Catalyst::Exception->throw("Could not connect to database"); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | my $couch_database = $couch->newDB($config->{dbname}); | |||||||||
| 50 | 0 | ▶ | my $couch_designdoc = try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 51 | 0 | $couch_database->newDesignDoc($config->{designdoc})->retrieve(); | |||||||||
| 52 | 0 | }; | |||||||||
| 53 | 0 | ▶ | if (!$couch_designdoc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | Catalyst::Exception->throw("Could not retrieve design document"); | |||||||||
| 55 | }; | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | if (!exists $couch_designdoc->views->{$config->{view}}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | Catalyst::Exception->throw("Design document does not contain view"); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | return $class->$orig( | ||||||||||
| 62 | _couchdb => $couch_database, | ||||||||||
| 63 | _designdoc => $couch_designdoc, | ||||||||||
| 64 | view => $config->{view}, | ||||||||||
| 65 | 0 | ); | |||||||||
| 66 | |||||||||||
| 67 | 2 | }; | |||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 71 | 0 | my ($self, $authinfo, $c) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | my $couch_data = try { $self->_designdoc->queryView( | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 74 | $self->view, | ||||||||||
| 75 | include_docs => 'true', | ||||||||||
| 76 | limit => 1, | ||||||||||
| 77 | key => $authinfo->{username}, | ||||||||||
| 78 | 0 | ); | |||||||||
| 79 | 0 | }; | |||||||||
| 80 | 0 | ▶ | if (!$couch_data) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | Catalyst::Exception->throw("Could not read view"); | |||||||||
| 82 | }; | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | return unless exists $couch_data->{rows}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | ▶ | return unless ref ( $couch_data->{rows}) eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | ▶ | return unless defined $couch_data->{rows}->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | ▶ | return unless exists $couch_data->{rows}->[0]->{doc}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | my $user_data = $couch_data->{rows}->[0]->{doc}; | |||||||||
| 89 | |||||||||||
| 90 | 0 | my $user_doc = $self->_user_doc_from_hash($user_data); | |||||||||
| 91 | 0 | $self->_user($user_doc); | |||||||||
| 92 | 0 | return $self; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub supported_features { | ||||||||
|
Subroutine
supported_features: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | |||||||||||
| 98 | return { | ||||||||||
| 99 | 0 | session => 1, | |||||||||
| 100 | roles => 1, | ||||||||||
| 101 | }; | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub id { | ||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 105 | 0 | my ($self) = @_; | |||||||||
| 106 | 0 | return $self->_user->id; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | sub roles { | ||||||||
|
Subroutine
roles: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 111 | 0 | my ($self) = shift; | |||||||||
| 112 | |||||||||||
| 113 | 0 | return @{$self->_user->data->{roles}}; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 117 | 0 | my ($self, $field) = @_; | |||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | return unless defined $self->_user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | if ($field eq 'id') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | return $self->id; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | if (exists $self->_user->data->{$field}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | return $self->_user->data->{$field}; | |||||||||
| 127 | } | ||||||||||
| 128 | 0 | return; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub get_object { | ||||||||
|
Subroutine
get_object: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 132 | 0 | my ($self, $force) = @_; | |||||||||
| 133 | |||||||||||
| 134 | 0 | return $self->_user; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 0 | ▶ | sub for_session { | ||||||||
|
Subroutine
for_session: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 138 | 0 | my ($self) = @_; | |||||||||
| 139 | |||||||||||
| 140 | 0 | my $data = $self->_user->contentForSubmit(); | |||||||||
| 141 | # Return JSON here, because it's fast, it's human readable so we can | ||||||||||
| 142 | # see what's going on in the session. We can't return the data structure, | ||||||||||
| 143 | # because something in the session handling somewhere is mangling it. | ||||||||||
| 144 | 0 | return JSON::encode_json($data); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | sub from_session { | ||||||||
|
Subroutine
from_session: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 148 | 0 | my ($self, $frozen_user) = @_; | |||||||||
| 149 | |||||||||||
| 150 | 0 | $self->_user($self->_user_doc_from_hash(JSON::decode_json($frozen_user))); | |||||||||
| 151 | 0 | return $self; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: not called CC=2 SCAR=17.9
| |||||||||||
| 155 | 0 | my ($self) = @_; | |||||||||
| 156 | |||||||||||
| 157 | 0 | (my $method) = (our $AUTOLOAD =~ /([^:]+)$/); | |||||||||
| 158 | 0 | ▶ | return if $method eq "DESTROY"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | |||||||||||
| 160 | 0 | return $self->get($method); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | sub _user_doc_from_hash { | ||||||||
|
Subroutine
_user_doc_from_hash: not called CC=1 SCAR=6.9
| |||||||||||
| 168 | 0 | my ($self, $user_data) = @_; | |||||||||
| 169 | |||||||||||
| 170 | 0 | my $id = delete($user_data->{_id}); | |||||||||
| 171 | 0 | my $rev = delete($user_data->{_rev}); | |||||||||
| 172 | 0 | my $attachments = delete($user_data->{_attachments}); | |||||||||
| 173 | |||||||||||
| 174 | 0 | return $self->_couchdb->newDoc($id, $rev, $user_data, $attachments); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | |||||||||||
| 178 | 2 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | |||||||||
| 179 | |||||||||||
| 180 | 2 | 1; | |||||||||
| 181 | |||||||||||
| 182 | |||||||||||
| 183 | |||||||||||
| 184 | |||||||||||
| 185 | =pod | ||||||||||
| 186 | |||||||||||
| 187 | =head1 NAME | ||||||||||
| 188 | |||||||||||
| 189 | Catalyst::Authentication::Store::CouchDB::User - The backing user class for the Catalyst::Authentication::Store::CouchDB storage module. | ||||||||||
| 190 | |||||||||||
| 191 | =head1 VERSION | ||||||||||
| 192 | |||||||||||
| 193 | version 0.001 | ||||||||||
| 194 | |||||||||||
| 195 | =head1 DESCRIPTION | ||||||||||
| 196 | |||||||||||
| 197 | The L<Catalyst::Authentication::Store::CouchDB:User> class | ||||||||||
| 198 | implements user storage connected to a CouchDB instance. | ||||||||||
| 199 | |||||||||||
| 200 | =head1 SYNPOSIS | ||||||||||
| 201 | |||||||||||
| 202 | Internal - not used directly. | ||||||||||
| 203 | |||||||||||
| 204 | =head1 METHODS | ||||||||||
| 205 | |||||||||||
| 206 | =head2 new | ||||||||||
| 207 | |||||||||||
| 208 | Constructor. Connects to the CouchDB instance in the configuration, and | ||||||||||
| 209 | fetches the design document that contains the configured view. | ||||||||||
| 210 | |||||||||||
| 211 | =head2 load ( $authinfo, $c ) | ||||||||||
| 212 | |||||||||||
| 213 | Retrieves a user from storage. It queries the configured view, and converts | ||||||||||
| 214 | the first document retrieved into a CouchDB document. This is then used | ||||||||||
| 215 | as the User backing object | ||||||||||
| 216 | |||||||||||
| 217 | =head2 supported_features | ||||||||||
| 218 | |||||||||||
| 219 | Indicates the features supported by this class. | ||||||||||
| 220 | |||||||||||
| 221 | =head2 roles | ||||||||||
| 222 | |||||||||||
| 223 | Returns a list of roles supported by this class. These are stored as an array | ||||||||||
| 224 | in the 'roles' field of the User document. | ||||||||||
| 225 | |||||||||||
| 226 | =head2 for_session | ||||||||||
| 227 | |||||||||||
| 228 | Returns a serialised user for storage in the session. This is a JSON | ||||||||||
| 229 | representation of the user. | ||||||||||
| 230 | |||||||||||
| 231 | =head2 from_session ( $frozen_user ) | ||||||||||
| 232 | |||||||||||
| 233 | Given the results of for_session, deserialises the user, and recreates the backing | ||||||||||
| 234 | object. | ||||||||||
| 235 | |||||||||||
| 236 | =head2 get ( $fieldname ) | ||||||||||
| 237 | |||||||||||
| 238 | Returns the field $fielname from the backing object. | ||||||||||
| 239 | |||||||||||
| 240 | =head2 AUTOLOAD | ||||||||||
| 241 | |||||||||||
| 242 | AUTOLOAD is defined so that calls to missing methods will get converted into a call to | ||||||||||
| 243 | C<get> for the field matching the method name. This is convenient for use inside templates, | ||||||||||
| 244 | as for example C<user.name> will now return the C<name> field from the user document. | ||||||||||
| 245 | |||||||||||
| 246 | =head1 NOTES | ||||||||||
| 247 | |||||||||||
| 248 | This module is heavily based on L<Catalyst::Authentication::Store::DBIx::Class::User>. | ||||||||||
| 249 | |||||||||||
| 250 | =head1 BUGS | ||||||||||
| 251 | |||||||||||
| 252 | None known, but there are bound to be some. Please email the author. | ||||||||||
| 253 | |||||||||||
| 254 | =head1 AUTHOR | ||||||||||
| 255 | |||||||||||
| 256 | Colin Bradford <cjbradford@gmail.com> | ||||||||||
| 257 | |||||||||||
| 258 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 259 | |||||||||||
| 260 | This software is copyright (c) 2011 by Colin Bradford. | ||||||||||
| 261 | |||||||||||
| 262 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 263 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 264 | |||||||||||
| 265 | =cut | ||||||||||
| 266 | |||||||||||
| 267 | |||||||||||
| 268 | __END__ | ||||||||||