<commit_before>=pod
Lenio - Web-based Facilities Management Software
Copyright (C) 2013 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package Lenio::Login;

use Dancer2 ':script';
use Dancer2::Plugin::DBIC qw(schema resultset rset);
use Dancer2::Plugin::Emailesque;
use Ouch;
use String::Random;
use Text::Autoformat qw(autoformat break_wrap);
use Crypt::SaltedHash;
use Email::Valid;
schema->storage->debug(1);

use Lenio::Schema;

sub login($)
{   my ($class, $args) = @_;
    if ($args->{id})
    {
        rset('Login')->find($args->{id});
    }
    elsif ($args->{username})
    {
        my $login = rset('Login')->search({ username => $args->{username} })
            or ouch 'dbfail', "There was a database error when retrieving the user";
        $login->count
            or ouch 'notfound', "The requested user could not be found";
        my ($l) = $login->all;
        Crypt::SaltedHash->validate($l->password, $args->{'password'})
            or ouch 'wrongpwd', "The password entered is incorrect";
        $l;
    }
    else
    {
        ouch 'badparams', "Neither a user ID nor username were supplied";
    }
}

sub new($)
{   my ($class, $login) = @_;
    my @errors;
    $login->{username}  or push @errors, "Please enter a username";
    $login->{email}     or push @errors, "Please enter an email address";
    $login->{firstname} or push @errors, "Please enter a firstname";
    $login->{surname}   or push @errors, "Please enter a surname";
    Email::Valid->address($login->{email})
        or push @errors, "Please enter a valid email address";

    ouch 'invalid', "There were some errors in the data supplied", \@errors
        if @errors;

    $login->{password} = password($login->{password}) if $login->{password};

    my $org_ids = delete $login->{org_ids};

    my $login_id;
    if ($login->{id})
    {
        my $l = rset('Login')->find($login->{id})
            or ouch 'notfound', "The requested user cannot be found";
        $l->update($login)
            or ouch 'dbfail', "There was a database error creating the user";
        $login_id = $login->{id};
    } else
    {
        ouch 'exists', "The username already exists"
            if rset('Login')->search({ username => $login->{username} })->count;
        ouch 'exists', "The email address already exists"
            if rset('Login')->search({ email => $login->{email} })->count;
        $login_id = rset('Login')->create($login)->id
            or ouch 'dbfail', "There was a database error when creating the user";
    }

    # Update organisation membership
    if ($org_ids)<commit_msg>Set default to receive emails on user create<commit_after>=pod
Lenio - Web-based Facilities Management Software
Copyright (C) 2013 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
=cut

package Lenio::Login;

use Dancer2 ':script';
use Dancer2::Plugin::DBIC qw(schema resultset rset);
use Dancer2::Plugin::Emailesque;
use Ouch;
use String::Random;
use Text::Autoformat qw(autoformat break_wrap);
use Crypt::SaltedHash;
use Email::Valid;
schema->storage->debug(1);

use Lenio::Schema;

sub login($)
{   my ($class, $args) = @_;
    if ($args->{id})
    {
        rset('Login')->find($args->{id});
    }
    elsif ($args->{username})
    {
        my $login = rset('Login')->search({ username => $args->{username} })
            or ouch 'dbfail', "There was a database error when retrieving the user";
        $login->count
            or ouch 'notfound', "The requested user could not be found";
        my ($l) = $login->all;
        Crypt::SaltedHash->validate($l->password, $args->{'password'})
            or ouch 'wrongpwd', "The password entered is incorrect";
        $l;
    }
    else
    {
        ouch 'badparams', "Neither a user ID nor username were supplied";
    }
}

sub new($)
{   my ($class, $login) = @_;
    my @errors;
    $login->{username}  or push @errors, "Please enter a username";
    $login->{email}     or push @errors, "Please enter an email address";
    $login->{firstname} or push @errors, "Please enter a firstname";
    $login->{surname}   or push @errors, "Please enter a surname";
    Email::Valid->address($login->{email})
        or push @errors, "Please enter a valid email address";

    ouch 'invalid', "There were some errors in the data supplied", \@errors
        if @errors;

    $login->{password} = password($login->{password}) if $login->{password};

    my $org_ids = delete $login->{org_ids};

    my $login_id;
    if ($login->{id})
    {
        my $l = rset('Login')->find($login->{id})
            or ouch 'notfound', "The requested user cannot be found";
        $l->update($login)
            or ouch 'dbfail', "There was a database error creating the user";
        $login_id = $login->{id};
    } else
    {
        ouch 'exists', "The username already exists"
            if rset('Login')->search({ username => $login->{username} })->count;
        ouch 'exists', "The email address already exists"
            if rset('Login')->search({ email => $login->{email} })->count;
        # Set default email settings
        $login->{email_comment} = 1;
        $login->{email_ticket} = 1;
        $login_id = rset('Login')->create($login)->id
            or ouch 'dbfail', "There was a database error when creating the user";
    }

    # Update organisation membership
    if ($org_ids)
{'fraction_non_alphanumeric': 0.1347126436781609, 'fraction_numerical': 0.0030651340996168583, 'mean_word_length': 3.1018227529855436, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5066852', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1887, 'n_words': 710}