<commit_before>package MediaWords::DBI::Auth::Roles;

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

import_python_module( __PACKAGE__, 'mediawords.dbi.auth.roles' );

1;<commit_msg>Revert "Make ::DBI::Auth::Roles use Python implementation"<commit_after>package MediaWords::DBI::Auth::Roles;

#
# Authentication role helpers
#

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;

use Readonly;

use MediaWords::DBI::Auth::Roles::List;

# Fetch a list of available user roles
sub all_user_roles($)
{
    my ( $db ) = @_;

    my $roles = $db->query(
        <<"SQL"
        SELECT auth_roles_id,
               role,
               description
        FROM auth_roles
        ORDER BY auth_roles_id
SQL
    )->hashes;

    return $roles;
}

# Fetch a user role's ID for a role; die()s if no such role was found
sub role_id_for_role($$)
{
    my ( $db, $role ) = @_;

    if ( !$role )
    {
        LOGCONFESS "Role is empty.";
    }

    my $auth_roles_id = $db->query(
        <<"SQL",
        SELECT auth_roles_id
        FROM auth_roles
        WHERE role = ?
        LIMIT 1
SQL
        $role
    )->hash;
    if ( !( ref( $auth_roles_id ) eq ref( {} ) and $auth_roles_id->{ auth_roles_id } ) )
    {
        LOGCONFESS "Role '$role' was not found.";
    }

    return $auth_roles_id->{ auth_roles_id };
}

# List of role IDs to apply to new users
sub default_role_ids($)
{
    my ( $db ) = @_;

    my $default_role_ids = $db->query(
        <<SQL,
        SELECT auth_roles_id
        FROM auth_roles
        WHERE role = ?
SQL
        $MediaWords::DBI::Auth::Roles::List::SEARCH
    )->flat;

    unless ( scalar( @{ $default_role_ids } ) )
    {
        die 'Unable to find default role IDs';
    }

    return $default_role_ids;
}

1;
{'fraction_non_alphanumeric': 0.12956810631229235, 'fraction_numerical': 0.006090808416389812, 'mean_word_length': 2.5570866141732282, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5809486', 'n_tokens_mistral': 673, 'n_tokens_neox': 641, 'n_words': 162}