| 1 | package Mojolicious::Plugin::RoutesAuthDBI::Guest; | ||
| 2 | use Mojo::Base -base;#'Mojolicious::Plugin::Authentication' | ||
| 3 | use Mojolicious::Plugin::RoutesAuthDBI::Util qw(json_enc json_dec); | ||
| 4 | |||
| 5 | has [qw(session_key stash_key app plugin model)]; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | state $self = shift->SUPER::new(@_); | ||
| 9 | } | ||
| 10 | |||
| 11 | sub current {# Fetch the current guest object from the stash - loading it if not already loaded | ||
| 12 | my ($self, $c) = @_; | ||
| 13 | |||
| 14 | my $stash_key = $self->stash_key; | ||
| 15 | |||
| 16 | $self->_loader($c) | ||
| 17 | unless | ||
| 18 | defined($c->stash($stash_key)) | ||
| 19 | && ($c->stash($stash_key)->{no_guest} | ||
| 20 | || defined($c->stash($stash_key)->{guest})); | ||
| 21 | |||
| 22 | my $guest_def = defined($c->stash($stash_key)) | ||
| 23 | && defined($c->stash($stash_key)->{guest}); | ||
| 24 | |||
| 25 | return $guest_def ? $c->stash($stash_key)->{guest} : undef; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Unconditionally load the guest based on id in session | ||
| 29 | sub _loader { | ||
| 30 | my ($self, $c) = @_; | ||
| 31 | my $gid = $c->session($self->session_key); | ||
| 32 | |||
| 33 | my $guest = defined($gid) && $self->load($gid); | ||
| 34 | #~ if defined $gid; | ||
| 35 | |||
| 36 | if ($guest) { | ||
| 37 | $c->stash($self->stash_key => { guest => $guest }); | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | # cache result that guest does not exist | ||
| 41 | $c->stash($self->stash_key => { no_guest => 1 }); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub load { | ||
| 46 | my ($self, $gid) = @_; | ||
| 47 | |||
| 48 | my $guest = $self->model->get_guest($gid); | ||
| 49 | |||
| 50 | if ( $guest && $guest->{id}) { | ||
| 51 | my $json = $guest->{data} && json_dec(delete $guest->{data}); | ||
| 52 | |||
| 53 | @$guest{ keys %$json } = values %$json | ||
| 54 | if $json; | ||
| 55 | |||
| 56 | $self->app->log->debug("Success loading guest by id=$gid"); | ||
| 57 | return $guest; | ||
| 58 | } | ||
| 59 | $self->app->log->debug("Failed loading guest by id=$gid"); | ||
| 60 | |||
| 61 | return undef; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub reload { | ||
| 65 | my ($self, $c) = @_; | ||
| 66 | # Clear stash to force a reload of the guest object | ||
| 67 | delete $c->stash->{$self->stash_key}; | ||
| 68 | return $self->current($c); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub is_guest { | ||
| 72 | my ($self, $c) = @_; | ||
| 73 | return defined($self->current($c)) ? 1 : 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub logout { | ||
| 77 | my ($self, $c) = @_; | ||
| 78 | delete $c->stash->{$self->stash_key}; | ||
| 79 | delete $c->session->{$self->session_key}; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub store {# new guest | ||
| 83 | my ($self, $c, $data) = @_; | ||
| 84 | |||
| 85 | $data ||= {}; | ||
| 86 | $data->{headers} = $c->req->headers->to_hash(1); | ||
| 87 | $data->{IP} = $c->tx->remote_address; | ||
| 88 | |||
| 89 | my $guest = $self->model->store(json_enc($data)); | ||
| 90 | $c->session($self->session_key => $guest->{id}); | ||
| 91 | $c->stash($self->stash_key => { guest => $guest }); | ||
| 92 | |||
| 93 | return $guest; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | =pod | ||
| 100 | |||
| 101 | =encoding utf8 | ||
| 102 | |||
| 103 | Доброго всем | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | =head1 Mojolicious::Plugin::RoutesAuthDBI::Guest | ||
| 108 | |||
| 109 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | Mojolicious::Plugin::RoutesAuthDBI::Guest - session for guests. Store guests in separate DBI table. | ||
| 114 | |||
| 115 | =head1 SYNOPSIS | ||
| 116 | |||
| 117 | $app->plugin('RoutesAuthDBI', | ||
| 118 | ... | ||
| 119 | guest => {< hashref options list below >}, | ||
| 120 | ... | ||
| 121 | ); | ||
| 122 | |||
| 123 | =head1 OPTIONS | ||
| 124 | |||
| 125 | =head2 namespace | ||
| 126 | |||
| 127 | String, default to 'Mojolicious::Plugin::RoutesAuthDBI'. | ||
| 128 | |||
| 129 | =head2 module | ||
| 130 | |||
| 131 | String, default to 'Guest' (this module). | ||
| 132 | |||
| 133 | =head2 session_key | ||
| 134 | |||
| 135 | String, session storage of guest data. Default to 'guest_data'. | ||
| 136 | |||
| 137 | =head2 tables | ||
| 138 | |||
| 139 | Hashref, any DB tables names. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>. | ||
| 140 | |||
| 141 | =head2 table | ||
| 142 | |||
| 143 | String, DB table B<guests> name. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>. | ||
| 144 | |||
| 145 | |||
| 146 | =head1 METHODS | ||
| 147 | |||
| 148 | =head2 current($controller) | ||
| 149 | |||
| 150 | Get current guest hashref by session and undef overwise. | ||
| 151 | |||
| 152 | my $guest = $c->access->plugin->guest->current($c); | ||
| 153 | |||
| 154 | =head2 store($controller, $data) | ||
| 155 | |||
| 156 | Store guest data in DB table and set session_key. Headers of request save in "data" column. | ||
| 157 | |||
| 158 | $c->access->plugin->guest->store($c, {"Glory"=>"is ♥ for me"}); | ||
| 159 | |||
| 160 | =head2 is_guest($controller) | ||
| 161 | |||
| 162 | True if current session of guest. | ||
| 163 | |||
| 164 | if( $c->access->plugin->guest->is_guest($c) ) {...} | ||
| 165 | |||
| 166 | =head2 load($id) | ||
| 167 | |||
| 168 | Loads guest data from DB table by its ID row. JSON column "data" will expand. | ||
| 169 | |||
| 170 | my $data = $c->access->plugin->guest->load($id); | ||
| 171 | |||
| 172 | =head2 reload($controller) | ||
| 173 | |||
| 174 | Cleanup stash and reload guest data. | ||
| 175 | |||
| 176 | my $guest = $c->access->plugin->guest->reload($c); | ||
| 177 | |||
| 178 | =head1 SEE ALSO | ||
| 179 | |||
| 180 | L<Mojolicious::Plugin::Authentication> | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Михаил Че (Mikhail Che), C<< <mche [on] cpan.org> >> | ||
| 185 | |||
| 186 | =head1 BUGS / CONTRIBUTING | ||
| 187 | |||
| 188 | Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues>. Pull requests welcome also. | ||
| 189 | |||
| 190 | =head1 COPYRIGHT | ||
| 191 | |||
| 192 | Copyright 2016+ Mikhail Che. | ||
| 193 | |||
| 194 | This library is free software; you can redistribute it and/or modify | ||
| 195 | it under the same terms as Perl itself. | ||
| 196 | |||
| 197 | =cut |