<commit_before>package MediaWords::Controller::Admin::Users;
use Modern::Perl "2012";
use MediaWords::CommonLibs;
use MediaWords::DBI::Auth;

use strict;
use warnings;
use parent 'Catalyst::Controller';

sub index : Path : Args(0)
{
    return list( @_ );
}

# list users
sub list : Local
{
    my ( $self, $c ) = @_;

    # Fetch list of users and their roles
    my $users = MediaWords::DBI::Auth::list_of_users( $c->dbis );

    # Fetch role descriptions
    my $roles = MediaWords::DBI::Auth::all_roles( $c->dbis );
    my %role_descriptions = map { $_->{ role } => $_->{ description } } @{ $roles };

    $c->stash->{ users }             = $users;
    $c->stash->{ role_descriptions } = \%role_descriptions;
    $c->stash->{ c }                 = $c;
    $c->stash->{ template }          = 'users/list.tt2';
}

# confirm if the user has to be deleted
sub delete : Local
{
    my ( $self, $c ) = @_;

    my $email = $c->request->param( 'email' );
    if ( !$email )
    {
        $c->stash( error_msg => "Empty email." );
        $c->stash->{ c }        = $c;
        $c->stash->{ template } = 'users/delete.tt2';
        return;
    }

    # Fetch readonly information about the user
    my $userinfo = MediaWords::DBI::Auth::user_info( $c->dbis, $email );
    if ( !$userinfo )
    {
        die "Unable to find user '$email' in the database.";
    }

    $c->stash->{ users_id }  = $userinfo->{ users_id };
    $c->stash->{ email }     = $userinfo->{ email };
    $c->stash->{ full_name } = $userinfo->{ full_name };
    $c->stash->{ c }         = $c;
    $c->stash->{ template }  = 'users/delete.tt2';
}

# show the user edit form
sub edit : Local
{
    my ( $self, $c ) = @_;

    my $form = $c->create_form(
        {
            load_config_file => $c->path_to() . '/root/forms/users/edit.yml',
            method           => 'POST',
            action           => $c->uri_for( '/admin/users/edit' )
        }
    );

    my $user_email = $c->request->param( 'email' );
    if ( !$user_email )
    {
        $c->stash( error_msg => "Empty email." );
        $c->stash->{ c }        = $c;
        $c->stash->{ form }     = $form;
        $c->stash->{ template } = 'users/edit.tt2';
        return;
    }

    # Fetch information about the user and roles
    my $userinfo = MediaWords::DBI::Auth::user_info( $c->dbis, $user_email );
    my $roles = MediaWords::DBI::Auth::user_auth( $c->dbis, $user_email );
    unless ( $userinfo and $roles )
    {
        die "Unable to find user '$user_email' in the database.";
    }

    my %user_roles = map { $_ => 1 } @{ $roles->{ roles } };

    $form->process( $c->request );

    if ( !$form->submitted_and_valid() )
    {

        # Fetch list of available roles
        my $available_roles = MediaWords::DBI::Auth::all_roles( $c->dbis );
        my @roles_options;
        for my $role ( @{ $available_roles } )
        {
            my $html_role_attributes = {};
            if ( exists( $user_roles{ $role->{ role } } ) )
            {
                $html_role_attributes = { checked => 'checked' };
            }

            push(
                @roles_options,
                {
                    value      => $role->{ roles_id },
                    label      => $role->{ role } . ': ' . $role->{ description },
                    attributes => $html_role_attributes
                }
            );
        }

        my $el_roles = $form->get_element( { name => 'roles', type => 'Checkboxgroup' } );
        $el_roles->options( \@roles_options );

        $form->default_values(
            {
                email     => $user_email,
                full_name => $userinfo->{ full_name },
                notes     => $userinfo->{ notes },
                active    => $userinfo->{ active }
            }
        );

        # Re-process the form
        $form->process( $c->request );

        # Show the form
        $c->stash->{ users_id }  = $userinfo->{ users_id };
        $c->stash->{ email }     = $userinfo->{ email };
        $c->stash->{ full_name } = $userinfo->{ full_name };
        $c->stash->{ notes }     = $userinfo->{ notes };
        $c->stash->{ active }    = $userinfo->{ active };
        $c->stash->{ c }         = $c;
        $c->stash->{ form }      = $form;
        $c->stash->{ template }  = 'users/edit.tt2';

        return;
    }

    # Form has been submitted

    my $user_full_name       = $form->param_value( 'full_name' );
    my $user_notes           = $form->param_value( 'notes' );
    my $user_roles           = $form->param_array( 'roles' );
    my $user_is_active       = $form->param_value( 'active' );
    my $user_password        = $form->param_value( 'password' );           # Might be empty
    my $user_password_repeat = $form->param_value( 'password_repeat' );    # Might be empty

    # Update user
    my $update_user_error_message =
      MediaWords::DBI::Auth::update_user( $c->dbis, $user_email, $user_full_name, $user_notes, $user_roles, $user_is_active,
        $user_password, $user_password_repeat );
    if ( $update_user_error_message )
    {
        $c->stash->{ users_id }  = $userinfo->{ users_id };
        $c->stash->{ email }     = $userinfo->{ email };
        $c->stash->{ full_name } = $userinfo->{ full_name };
        $c->stash->{ notes }     = $userinfo->{ notes };
        $c->stash->{ active }    = $userinfo->{ active };
        $c->stash->{ c }         = $c;
        $c->stash->{ form }      = $form;
        $c->stash->{ template }  = 'users/edit.tt2';
        $c->stash( error_msg => $update_user_error_message );
        return;
    }

    my $status_msg = "User information for user '$user_email' has been saved.";
    if ( $user_password )
    {
        $status_msg .= " Additionaly, the user's password has been changed.";
    }

    $c->response->redirect( $c->uri_for( '/admin/users/list', { status_msg => $status_msg } ) );<commit_msg>check if user is trying to deactivate him/herself<commit_after>package MediaWords::Controller::Admin::Users;
use Modern::Perl "2012";
use MediaWords::CommonLibs;
use MediaWords::DBI::Auth;

use strict;
use warnings;
use parent 'Catalyst::Controller';

sub index : Path : Args(0)
{
    return list( @_ );
}

# list users
sub list : Local
{
    my ( $self, $c ) = @_;

    # Fetch list of users and their roles
    my $users = MediaWords::DBI::Auth::list_of_users( $c->dbis );

    # Fetch role descriptions
    my $roles = MediaWords::DBI::Auth::all_roles( $c->dbis );
    my %role_descriptions = map { $_->{ role } => $_->{ description } } @{ $roles };

    $c->stash->{ users }             = $users;
    $c->stash->{ role_descriptions } = \%role_descriptions;
    $c->stash->{ c }                 = $c;
    $c->stash->{ template }          = 'users/list.tt2';
}

# confirm if the user has to be deleted
sub delete : Local
{
    my ( $self, $c ) = @_;

    my $email = $c->request->param( 'email' );
    if ( !$email )
    {
        $c->stash( error_msg => "Empty email." );
        $c->stash->{ c }        = $c;
        $c->stash->{ template } = 'users/delete.tt2';
        return;
    }

    # Fetch readonly information about the user
    my $userinfo = MediaWords::DBI::Auth::user_info( $c->dbis, $email );
    if ( !$userinfo )
    {
        die "Unable to find user '$email' in the database.";
    }

    $c->stash->{ users_id }  = $userinfo->{ users_id };
    $c->stash->{ email }     = $userinfo->{ email };
    $c->stash->{ full_name } = $userinfo->{ full_name };
    $c->stash->{ c }         = $c;
    $c->stash->{ template }  = 'users/delete.tt2';
}

# show the user edit form
sub edit : Local
{
    my ( $self, $c ) = @_;

    my $form = $c->create_form(
        {
            load_config_file => $c->path_to() . '/root/forms/users/edit.yml',
            method           => 'POST',
            action           => $c->uri_for( '/admin/users/edit' )
        }
    );

    my $user_email = $c->request->param( 'email' );
    if ( !$user_email )
    {
        $c->stash( error_msg => "Empty email." );
        $c->stash->{ c }        = $c;
        $c->stash->{ form }     = $form;
        $c->stash->{ template } = 'users/edit.tt2';
        return;
    }

    # Fetch information about the user and roles
    my $userinfo = MediaWords::DBI::Auth::user_info( $c->dbis, $user_email );
    my $roles = MediaWords::DBI::Auth::user_auth( $c->dbis, $user_email );
    unless ( $userinfo and $roles )
    {
        die "Unable to find user '$user_email' in the database.";
    }

    my %user_roles = map { $_ => 1 } @{ $roles->{ roles } };

    $form->process( $c->request );

    if ( !$form->submitted_and_valid() )
    {

        # Fetch list of available roles
        my $available_roles = MediaWords::DBI::Auth::all_roles( $c->dbis );
        my @roles_options;
        for my $role ( @{ $available_roles } )
        {
            my $html_role_attributes = {};
            if ( exists( $user_roles{ $role->{ role } } ) )
            {
                $html_role_attributes = { checked => 'checked' };
            }

            push(
                @roles_options,
                {
                    value      => $role->{ roles_id },
                    label      => $role->{ role } . ': ' . $role->{ description },
                    attributes => $html_role_attributes
                }
            );
        }

        my $el_roles = $form->get_element( { name => 'roles', type => 'Checkboxgroup' } );
        $el_roles->options( \@roles_options );

        $form->default_values(
            {
                email     => $user_email,
                full_name => $userinfo->{ full_name },
                notes     => $userinfo->{ notes },
                active    => $userinfo->{ active }
            }
        );

        # Re-process the form
        $form->process( $c->request );

        # Show the form
        $c->stash->{ users_id }  = $userinfo->{ users_id };
        $c->stash->{ email }     = $userinfo->{ email };
        $c->stash->{ full_name } = $userinfo->{ full_name };
        $c->stash->{ notes }     = $userinfo->{ notes };
        $c->stash->{ active }    = $userinfo->{ active };
        $c->stash->{ c }         = $c;
        $c->stash->{ form }      = $form;
        $c->stash->{ template }  = 'users/edit.tt2';

        return;
    }

    # Form has been submitted

    my $user_full_name       = $form->param_value( 'full_name' );
    my $user_notes           = $form->param_value( 'notes' );
    my $user_roles           = $form->param_array( 'roles' );
    my $user_is_active       = $form->param_value( 'active' );
    my $user_password        = $form->param_value( 'password' );           # Might be empty
    my $user_password_repeat = $form->param_value( 'password_repeat' );    # Might be empty

    # Check if user is trying to deactivate oneself
    if ( $userinfo->{ email } eq $c->user->username and ( !$user_is_active ) )
    {
        $c->stash->{ users_id }  = $userinfo->{ users_id };
        $c->stash->{ email }     = $userinfo->{ email };
        $c->stash->{ full_name } = $userinfo->{ full_name };
        $c->stash->{ notes }     = $userinfo->{ notes };
        $c->stash->{ active }    = $userinfo->{ active };
        $c->stash->{ c }         = $c;
        $c->stash->{ form }      = $form;
        $c->stash->{ template }  = 'users/edit.tt2';
        $c->stash( error_msg => "You're trying to deactivate yourself!" );
        return;
    }

    # Update user
    my $update_user_error_message =
      MediaWords::DBI::Auth::update_user( $c->dbis, $user_email, $user_full_name, $user_notes, $user_roles, $user_is_active,
        $user_password, $user_password_repeat );
    if ( $update_user_error_message )
    {
        $c->stash->{ users_id }  = $userinfo->{ users_id };
        $c->stash->{ email }     = $userinfo->{ email };
        $c->stash->{ full_name } = $userinfo->{ full_name };
        $c->stash->{ notes }     = $userinfo->{ notes };
        $c->stash->{ active }    = $userinfo->{ active };
        $c->stash->{ c }         = $c;
        $c->stash->{ form }      = $form;
        $c->stash->{ template }  = 'users/edit.tt2';
        $c->stash( error_msg => $update_user_error_message );
        return;
    }

    my $status_msg = "User information for user '$user_email' has been saved.";
    if ( $user_password )
    {
        $status_msg .= " Additionaly, the user's password has been changed.";
    }

    $c->response->redirect( $c->uri_for( '/admin/users/list', { status_msg => $status_msg } ) );
{'fraction_non_alphanumeric': 0.17579250720461095, 'fraction_numerical': 0.002001280819724624, 'mean_word_length': 1.9094084769445738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 301, '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': '6706521', 'n_tokens_mistral': 4307, 'n_tokens_neox': 4196, 'n_words': 903}