WWW-Suffit-AuthDB

 view release on metacpan or  search on metacpan

lib/WWW/Suffit/AuthDB/Role/CRUD.pm  view on Meta::CPAN

        iat         => time,
        exp         => time + 3600,
        address     => '127.0.0.1',
    ) or die($authdb->error);

Updates token's data by id

=head2 user_del

    $authdb->user_del( "admin" ) or die $authdb->error;

Delete user by username

=head2 user_edit

    $authdb->user_edit(
        username    => $username,
        comment     => $comment,
        email       => $email,
        name        => $name,
        role        => $role,
    ) or die($authdb->error);

Edit general user data only

=head2 user_get

    my %data = $authdb->user_get( "admin" );
    my @users = $authdb->user_get;

This method returns user's data or returns all users as array of hashes

=head2 user_groups

    my @groups = $authdb->user_groups( "admin" );

This method returns all groups of the user

=head2 user_passwd

    $authdb->user_passwd(
            username => "admin",
            password => "password",
        ) or die $authdb->error;

This method sets password for user

=head2 user_pset

    $authdb->user_pset(
            username => "foo",
            name => "Test User",
            email       => 'test@localhost',
            password    => "098f6bcd4621d373cade4e832627b4f6",
            algorithm   => "MD5",
            role        => "Test user",
            flags       => 0,
            not_before  => time(),
            not_after   => undef,
            public_key  => "",
            private_key => "",
            attributes  => qq/{"disabled": 0}/,
            comment     => "This user added for test",
        ) or die $authdb->error;

This method adds new user or doing update data of existing user in pure mode

=head2 user_pure_set

This method is deprecated! See L</user_pset>

=head2 user_search

    my @users = $authdb->user_search( $text );

This method performs search user by name fragment

=head2 user_set

    $authdb->user_set(
            username    => "foo",
            name        => "Test User",
            email       => 'test@localhost',
            password    => "MyPassword", # Unsafe password
            algorithm   => "SHA256",
            role        => "Test user",
            flags       => 0,
            not_before  => time(),
            not_after   => undef,
            public_key  => "",
            private_key => "",
            attributes  => qq/{"disabled": 0}/,
            comment     => "This user added for test",
        ) or die $authdb->error;

This method adds new user or doing update data of existing user

=head2 user_setkeys

    $authdb->user_setkeys(
            username => "foo",
            public_key => $public_key,
            private_key => $private_key,
        ) or die $authdb->error;

This method sets keys for user

=head2 user_tokens

    my @tokens = $authdb->user_tokens( $username );

This method returns all tokens of specified user

=head2 ERROR CODES

List of error codes describes in L<WWW::Suffit::AuthDB>

=head1 HISTORY

See C<Changes> file

=head1 TO DO

See C<TODO> file

=head1 SEE ALSO

L<WWW::Suffit::AuthDB>, L<Mojolicious>, L<Role::Tiny>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2026 D&D Corporation

=head1 LICENSE

This program is distributed under the terms of the Artistic License Version 2.0

See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details

=cut

use Mojo::Base -role;

use Acrux::RefUtil qw/is_hash_ref is_array_ref is_true_flag/;

use Mojo::Util qw/deprecated/;

# Meta CRUD
sub meta {
    my $self = shift;
    my $i = scalar(@_);
    my $key = shift // '';
    my $val = shift;
    $self->clean; # Flush error

    # No key specified
    return $self->raise(400 => "E1330: No key specified") unless length($key);

    # Get model

lib/WWW/Suffit/AuthDB/Role/CRUD.pm  view on Meta::CPAN

    return 1;
}
sub token_check {
    my $self = shift;
    my $username = shift // '';
    my $jti = shift // '';
    $self->clean; # Flush error

    # Get model
    my $model = $self->model;

    # Get data from model
    my %data = $model->token_get_cond('api', username => $username, jti => $jti);
    return $self->raise(500 => "E1381: %s", $model->error) // 0 if $model->error;

    # Check
    return 1 if $data{id};
    return 0;
}

# Working with dumps
sub import_data {
    my $self = shift;
    my $file = shift;
    $self->clean; # Flush error
    my $model = $self->model;
    my $now = time();

    # Get data struct from file
    if ($file) {
        $self->load($file);
        if ($self->error) {
            $self->code(500);
            return;
        }
    }
    my $data = $self->data; # Perl struct expected!

    # Get users
    my $users_array = $data->{"users"} // [];
       $users_array = [] unless is_array_ref($users_array);
    my %grpsusrs = ();
    foreach my $user (@$users_array) {
        next unless is_hash_ref($user);
        my $username = $user->{"username"} // '';
        next unless length($username);

        # Add user
        $self->user_pset(
            username    => $username,
            name        => $user->{"name"} // '',
            email       => $user->{"email"} // '',
            password    => $user->{"password"} // '',
            algorithm   => $user->{"algorithm"} // '',
            role        => $user->{"role"} // '',
            flags       => $user->{"flags"} || 0,
            created     => $now,
            not_before  => $now,
            not_after   => is_true_flag($user->{"disabled"}) ? $now : undef,
            public_key  => $user->{"public_key"} // '',
            private_key => $user->{"private_key"} // '',
            attributes  => $user->{"attributes"} // '',
            comment     => $user->{"comment"} // '',
        ) or return;

        # Add groups to grpsusrs
        my $groups = $user->{"groups"} || [];
           $groups = [] unless is_array_ref($groups);
        foreach my $g (@$groups) {
            $grpsusrs{"$g:$username"} = {
                groupname => $g,
                username  => $username,
            };
        }
    }

    # Get groups
    my $groups_array = $data->{"groups"} // [];
       $groups_array = [] unless is_array_ref($groups_array);
    foreach my $group (@$groups_array) {
        next unless is_hash_ref($group);
        my $groupname = $group->{"groupname"} // '';
        next unless length($groupname);

        # Add group
        $self->group_pset(
            groupname   => $groupname,
            description => $group->{"description"} // '',
        ) or return;

        # Add users to grpsusrs
        my $users = $group->{"users"} || [];
           $users = [] unless is_array_ref($users);
        foreach my $u (@$users) {
            $grpsusrs{"$groupname:$u"} = {
                groupname => $groupname,
                username  => $u,
            };
        }
    }

    # Add members to group
    foreach my $member (values %grpsusrs) {
        $self->group_enroll(%$member) or return;
    }

    # Get realms
    my $realms_array = $data->{"realms"} // [];
       $realms_array = [] unless is_array_ref($realms_array);
    foreach my $realm (@$realms_array) {
        next unless is_hash_ref($realm);
        my $realmname = $realm->{"realmname"} // '';
        next unless length($realmname);

        # Add realm
        $self->realm_pset(
            realmname   => $realmname,
            realm       => $realm->{"realm"} // '',
            satisfy     => $realm->{"satisfy"} // '',
            description => $realm->{"description"} // '',
        ) or return;



( run in 0.886 second using v1.01-cache-2.11-cpan-39bf76dae61 )