| 1 | package Silki::Role::Controller::User; | ||
| 2 | { | ||
| 3 | $Silki::Role::Controller::User::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use File::MimeInfo qw( mimetype ); | ||
| 11 | use Silki::I18N qw( loc ); | ||
| 12 | |||
| 13 | use Moose::Role -traits => 'MooseX::MethodAttributes::Role::Meta::Role'; | ||
| 14 | |||
| 15 | requires qw( _set_user _make_user_uri ); | ||
| 16 | |||
| 17 | after '_set_user' => sub { | ||
| 18 | my $self = shift; | ||
| 19 | my $c = shift; | ||
| 20 | my $user_id = shift; | ||
| 21 | |||
| 22 | my $user; | ||
| 23 | |||
| 24 | if ( $user_id eq 'guest' ) { | ||
| 25 | $user = Silki::Schema::User->GuestUser(); | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | $user = Silki::Schema::User->new( user_id => $user_id ); | ||
| 29 | } | ||
| 30 | |||
| 31 | $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) ) | ||
| 32 | unless $user; | ||
| 33 | |||
| 34 | my $wiki = $c->stash()->{wiki}; | ||
| 35 | |||
| 36 | my %profile | ||
| 37 | = $c->user()->user_id() == $user->user_id() | ||
| 38 | ? ( | ||
| 39 | label => loc('Your profile'), | ||
| 40 | tooltip => loc('View details about your profile'), | ||
| 41 | ) | ||
| 42 | : ( | ||
| 43 | label => $user->best_name(), | ||
| 44 | tooltip => loc( 'Information about %1', $user->best_name() ), | ||
| 45 | ); | ||
| 46 | |||
| 47 | $c->add_tab( | ||
| 48 | { | ||
| 49 | uri => $self->_make_user_uri( $c, $user ), | ||
| 50 | id => 'profile', | ||
| 51 | %profile, | ||
| 52 | } | ||
| 53 | ); | ||
| 54 | |||
| 55 | $c->stash()->{user} = $user; | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub user : Chained('_set_user') : PathPart('') : Args(0) : ActionClass('+Silki::Action::REST') { | ||
| 59 | } | ||
| 60 | |||
| 61 | sub user_GET_html { | ||
| 62 | my $self = shift; | ||
| 63 | my $c = shift; | ||
| 64 | |||
| 65 | $c->tab_by_id('profile')->set_is_selected(1); | ||
| 66 | |||
| 67 | my $user = $c->stash()->{user}; | ||
| 68 | |||
| 69 | unless ( $user->is_system_user() ) { | ||
| 70 | if ( $user->user_id() == $c->user()->user_id() ) { | ||
| 71 | $c->stash()->{user_wikis} = $user->all_wikis() | ||
| 72 | if $user->all_wiki_count(); | ||
| 73 | } | ||
| 74 | elsif ( !$c->user()->is_system_user() ) { | ||
| 75 | $c->stash()->{shared_wikis} | ||
| 76 | = $user->wikis_shared_with( $c->user() ); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | $c->stash()->{template} = '/user/profile'; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub user_PUT { | ||
| 84 | my $self = shift; | ||
| 85 | my $c = shift; | ||
| 86 | |||
| 87 | my $params = $c->request()->params(); | ||
| 88 | |||
| 89 | my $user = $c->stash()->{user}; | ||
| 90 | |||
| 91 | my $can_edit = 0; | ||
| 92 | my $key = $params->{confirmation_key}; | ||
| 93 | if ($key) { | ||
| 94 | $can_edit = 1 | ||
| 95 | if ( $user->confirmation_key() || q{} ) eq $key; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $can_edit = $c->user()->can_edit_user($user); | ||
| 99 | } | ||
| 100 | |||
| 101 | $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) ) | ||
| 102 | unless $can_edit; | ||
| 103 | |||
| 104 | my $message; | ||
| 105 | my $uri; | ||
| 106 | |||
| 107 | if ( exists $params->{is_disabled} | ||
| 108 | && $c->user()->is_admin() | ||
| 109 | && $c->user()->user_id() != $user->user_id() ) { | ||
| 110 | |||
| 111 | $user->update( | ||
| 112 | is_disabled => $params->{is_disabled}, | ||
| 113 | user => $c->user(), | ||
| 114 | ); | ||
| 115 | |||
| 116 | $message | ||
| 117 | = $params->{is_disabled} | ||
| 118 | ? loc( | ||
| 119 | 'The account for %1 has been disabled.', | ||
| 120 | $user->best_name() | ||
| 121 | ) | ||
| 122 | : loc( | ||
| 123 | 'The account for %1 has been enabled.', | ||
| 124 | $user->best_name() | ||
| 125 | ); | ||
| 126 | |||
| 127 | $uri = $c->domain()->application_uri( path => '/users' ); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | my %update = $c->request()->user_params(); | ||
| 131 | $update{confirmation_key} = undef | ||
| 132 | if defined $key; | ||
| 133 | $update{preserve_password} = 1; | ||
| 134 | |||
| 135 | my @errors = $self->_check_passwords_match( \%update ); | ||
| 136 | |||
| 137 | my $required_activation = $user->requires_activation(); | ||
| 138 | |||
| 139 | unless (@errors) { | ||
| 140 | my $upload = $c->request()->upload('image'); | ||
| 141 | |||
| 142 | eval { | ||
| 143 | $user->update( %update, user => $c->user(), ); | ||
| 144 | |||
| 145 | if ($upload) { | ||
| 146 | if ( my $image = $user->image() ) { | ||
| 147 | $image->delete(); | ||
| 148 | } | ||
| 149 | |||
| 150 | Silki::Schema::UserImage->insert( | ||
| 151 | user_id => $user->user_id(), | ||
| 152 | mime_type => mimetype( $upload->tempname() ), | ||
| 153 | file_size => $upload->size(), | ||
| 154 | contents => | ||
| 155 | do { my $fh = $upload->fh(); local $/; <$fh> }, | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | }; | ||
| 159 | |||
| 160 | my $e = $@; | ||
| 161 | die $e if $e && !ref $e; | ||
| 162 | |||
| 163 | push @errors, @{ $e->errors() } if $e; | ||
| 164 | } | ||
| 165 | |||
| 166 | $self->_user_update_error( $c, \@errors, \%update ) | ||
| 167 | if @errors; | ||
| 168 | |||
| 169 | $c->set_authen_cookie( value => { user_id => $user->user_id() } ) | ||
| 170 | if $c->user()->is_guest(); | ||
| 171 | |||
| 172 | $message | ||
| 173 | = $required_activation ? loc( | ||
| 174 | 'Your account has been activated. Welcome to the site, %1', | ||
| 175 | $user->best_name() | ||
| 176 | ) | ||
| 177 | : $key || $user->user_id() == $c->user()->user_id() | ||
| 178 | ? loc('Your preferences have been updated.') | ||
| 179 | : loc( | ||
| 180 | 'Preferences for ' . $user->best_name() . ' have been updated.' ); | ||
| 181 | |||
| 182 | $uri = $self->_make_user_uri( $c, $user ); | ||
| 183 | } | ||
| 184 | |||
| 185 | $c->session_object()->add_message($message); | ||
| 186 | |||
| 187 | $c->redirect_and_detach($uri); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _check_passwords_match { | ||
| 191 | my $self = shift; | ||
| 192 | my $params = shift; | ||
| 193 | |||
| 194 | return unless defined $params->{password}; | ||
| 195 | |||
| 196 | my $pw2 = delete $params->{password2}; | ||
| 197 | return | ||
| 198 | if defined $pw2 && $params->{password} eq $pw2; | ||
| 199 | |||
| 200 | # Deleting both passwords ensures that any update we attempt after this | ||
| 201 | # will fail, unless the user also provided an openid, in which case we | ||
| 202 | # might as well let it succeed. | ||
| 203 | delete @{$params}{qw( password password2 )}; | ||
| 204 | |||
| 205 | return { | ||
| 206 | field => 'password', | ||
| 207 | message => | ||
| 208 | loc('The two passwords you provided did not match'), | ||
| 209 | }; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub _user_update_error { | ||
| 213 | my $self = shift; | ||
| 214 | my $c = shift; | ||
| 215 | my $errors = shift; | ||
| 216 | my $form_data = shift; | ||
| 217 | |||
| 218 | delete @{$form_data}{qw( password password2 )}; | ||
| 219 | |||
| 220 | my $uri | ||
| 221 | = $c->request()->params()->{confirmation_key} | ||
| 222 | ? $c->stash()->{user}->confirmation_uri( | ||
| 223 | view => 'preferences_form', | ||
| 224 | host => $c->domain()->web_hostname(), | ||
| 225 | ) | ||
| 226 | : $self->_make_user_uri( | ||
| 227 | $c, | ||
| 228 | $c->stash()->{user}, | ||
| 229 | 'preferences_form', | ||
| 230 | ); | ||
| 231 | |||
| 232 | $c->redirect_with_error( | ||
| 233 | error => $errors, | ||
| 234 | uri => $uri, | ||
| 235 | form_data => $form_data, | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub preferences_form : Chained('_set_user') : PathPart('preferences_form') : Args(0) { | ||
| 240 | my $self = shift; | ||
| 241 | my $c = shift; | ||
| 242 | |||
| 243 | my $user = $c->stash()->{user}; | ||
| 244 | |||
| 245 | $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) ) | ||
| 246 | unless $c->user()->can_edit_user($user); | ||
| 247 | |||
| 248 | $c->stash()->{template} = '/user/preferences-form'; | ||
| 249 | } | ||
| 250 | |||
| 251 | 1; | ||
| 252 | |||
| 253 | # ABSTRACT: Provides user-related methods and actions | ||
| 254 | |||
| 255 | __END__ |