<commit_before>package Silki::Role::Controller::User;

use strict;
use warnings;

use Moose::Role -traits => 'MooseX::MethodAttributes::Role::Meta::Role';

requires qw( _set_user _make_user_uri );

after '_set_user' => sub
{
    my $self    = shift;
    my $c       = shift;
    my $user_id = shift;

    return unless $user_id =~ /^\d+$/;

    my $user = Silki::Schema::User->new( user_id => $user_id );

    $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) )
        unless $user;

    my $wiki = $c->stash()->{wiki};

    my %profile =
          $c->user()->user_id() == $user->user_id()
        ? ( label   => $c->loc('Your profile'),
            tooltip => $c->loc('View details about your profile'),
          )
        : ( label   => $user->best_name(),
            tooltip => $c->loc( 'Information about %1', $user->best_name() ),
          );

    $c->add_tab( { uri => $self->_make_user_uri( $c, $user ),
                   id  => 'profile',
                   %profile,
                 }
               );

    if ( $c->user()->can_edit_user($user) )
    {
        my %prefs =
              $c->user()->user_id() == $user->user_id()
            ? ( label   => $c->loc('Your preferences'),
                tooltip => $c->loc('Set your preferences'),
              )
            : ( label   => $c->loc( 'Preferences for %1', $user->best_name() ),
                tooltip => $c->loc( 'View and change preferences for %1', $user->best_name() ),
              );

        $c->add_tab( { uri => $self->_make_user_uri( $c, $user, 'preferences_form' ),
                       id  => 'preferences',
                       %prefs,
                     }
                   );
    }

    $c->stash()->{user} = $user;
};

sub user : Chained('_set_user') : PathPart('') : Args(0) : ActionClass('+Silki::Action::REST') { }

sub user_GET_html
{
    my $self = shift;
    my $c    = shift;

    $c->tab_by_id('profile')->set_is_selected(1);

    $c->stash()->{template} = '/user/profile';
}

sub user_PUT
{
    my $self = shift;
    my $c    = shift;

    my $user = $c->stash()->{user};

    $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) )
        unless $c->user()->can_edit_user($user);

    my %update = $c->request()->user_params();

    if ( defined $update{password} )
    {
        unless ( defined $update{password2} && $update{password} eq $update{password2} )
        {
            my @e = { field   => 'password',
                      message => $c->loc('The two passwords you provided did not match'),
                    };

            $self->_user_update_error( $c, \@e, \%update );
        }
    }

    eval { $user->update(%update) };

    if ( my $e = $@ )
    {
        $self->_user_update_error( $c, $e, \%update );
    }

    my $message =
          $user->user_id() == $c->user()->user_id()
        ? $c->loc('Your preferences have been updated.')
        : $c->loc( 'Preferences for ' . $user->best_name() . ' have been updated.' );

    $c->session_object()->add_message($message);

    $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) );
}

sub _user_update_error
{
    my $self      = shift;
    my $c         = shift;
    my $errors    = shift;
    my $form_data = shift;

    delete @{ $form_data }{ qw( password password2 ) };

    $c->redirect_with_error
        ( error     => $errors,
          uri       => $self->_make_user_uri( $c, $c->stash()->{user}, 'preferences_form' ),
          form_data => $form_data,
        );
}

sub preferences_form : Chained('_set_user') : PathPart('preferences_form') : Args(0)
{
    my $self = shift;
    my $c    = shift;

    $c->tab_by_id('preferences')->set_is_selected(1);

    $c->stash()->{template} = '/user/preferences_form';
}

no Moose::Role;

1;
<commit_msg>Don't try to show pref form unless user can be edited<commit_after>package Silki::Role::Controller::User;

use strict;
use warnings;

use Moose::Role -traits => 'MooseX::MethodAttributes::Role::Meta::Role';

requires qw( _set_user _make_user_uri );

after '_set_user' => sub
{
    my $self    = shift;
    my $c       = shift;
    my $user_id = shift;

    return unless $user_id =~ /^\d+$/;

    my $user = Silki::Schema::User->new( user_id => $user_id );

    $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) )
        unless $user;

    my $wiki = $c->stash()->{wiki};

    my %profile =
          $c->user()->user_id() == $user->user_id()
        ? ( label   => $c->loc('Your profile'),
            tooltip => $c->loc('View details about your profile'),
          )
        : ( label   => $user->best_name(),
            tooltip => $c->loc( 'Information about %1', $user->best_name() ),
          );

    $c->add_tab( { uri => $self->_make_user_uri( $c, $user ),
                   id  => 'profile',
                   %profile,
                 }
               );

    if ( $c->user()->can_edit_user($user) )
    {
        my %prefs =
              $c->user()->user_id() == $user->user_id()
            ? ( label   => $c->loc('Your preferences'),
                tooltip => $c->loc('Set your preferences'),
              )
            : ( label   => $c->loc( 'Preferences for %1', $user->best_name() ),
                tooltip => $c->loc( 'View and change preferences for %1', $user->best_name() ),
              );

        $c->add_tab( { uri => $self->_make_user_uri( $c, $user, 'preferences_form' ),
                       id  => 'preferences',
                       %prefs,
                     }
                   );
    }

    $c->stash()->{user} = $user;
};

sub user : Chained('_set_user') : PathPart('') : Args(0) : ActionClass('+Silki::Action::REST') { }

sub user_GET_html
{
    my $self = shift;
    my $c    = shift;

    $c->tab_by_id('profile')->set_is_selected(1);

    $c->stash()->{template} = '/user/profile';
}

sub user_PUT
{
    my $self = shift;
    my $c    = shift;

    my $user = $c->stash()->{user};

    $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) )
        unless $c->user()->can_edit_user($user);

    my %update = $c->request()->user_params();

    if ( defined $update{password} )
    {
        unless ( defined $update{password2} && $update{password} eq $update{password2} )
        {
            my @e = { field   => 'password',
                      message => $c->loc('The two passwords you provided did not match'),
                    };

            $self->_user_update_error( $c, \@e, \%update );
        }
    }

    eval { $user->update(%update) };

    if ( my $e = $@ )
    {
        $self->_user_update_error( $c, $e, \%update );
    }

    my $message =
          $user->user_id() == $c->user()->user_id()
        ? $c->loc('Your preferences have been updated.')
        : $c->loc( 'Preferences for ' . $user->best_name() . ' have been updated.' );

    $c->session_object()->add_message($message);

    $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) );
}

sub _user_update_error
{
    my $self      = shift;
    my $c         = shift;
    my $errors    = shift;
    my $form_data = shift;

    delete @{ $form_data }{ qw( password <PASSWORD> ) };

    $c->redirect_with_error
        ( error     => $errors,
          uri       => $self->_make_user_uri( $c, $c->stash()->{user}, 'preferences_form' ),
          form_data => $form_data,
        );
}

sub preferences_form : Chained('_set_user') : PathPart('preferences_form') : Args(0)
{
    my $self = shift;
    my $c    = shift;

    my $user = $c->stash()->{user};

    $c->redirect_and_detach( $self->_make_user_uri( $c, $user ) )
        unless $c->user()->can_edit_user($user);

    $c->tab_by_id('preferences')->set_is_selected(1);

    $c->stash()->{template} = '/user/preferences_form';
}

no Moose::Role;

1;

{'fraction_non_alphanumeric': 0.18968859025713916, 'fraction_numerical': 0.0031011758625145366, 'mean_word_length': 2.0484442693974008, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 184, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4102390', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2579, 'n_words': 521}