Ado

 view release on metacpan or  search on metacpan

lib/Ado/Model/Users.pm  view on Meta::CPAN

                name        => $args->{ingroup},
                disabled    => 0,
                description => 'Additional group initially created for user ' . $self->login_name,
                created_by => $args->{created_by} || 1,
            );
        }

        #Link them
        Ado::Model::UserGroup->create(
            user_id  => $self->id,
            group_id => $ingroup->id
        );
        $dbix->commit;
    };
    unless ($try) {
        $dbix->rollback or croak($dbix->error);
        carp("ERROR adding user to group (rolling back):[$@]");
    }
    return $ingroup;
}

__PACKAGE__->SQL(SELECT_group_names => <<"SQL");
    SELECT name FROM groups
        WHERE id IN (SELECT group_id FROM user_group WHERE user_id=?)
SQL

sub ingroup {
    my ($self, $group) = @_;
    state $sql = __PACKAGE__->SQL('SELECT_group_names');
    my @groups = $self->dbix->query($sql, $self->id)->flat;
    if ($group) {
        return List::Util::first { $_ eq $group } @groups;
    }
    return @groups;
}

$CLASS->SQL('user_id_by_group_name' => <<"UG");
    SELECT user_id FROM user_group WHERE group_id =
        (SELECT id FROM groups  WHERE name = ?)
UG

$CLASS->SQL('by_group_name' => <<"SQL");
    SELECT id, login_name, first_name, last_name, email
    FROM ${\ $CLASS->TABLE }
    WHERE id IN(${\ $CLASS->SQL('user_id_by_group_name') })
        AND (disabled=0 AND (stop_date>? OR stop_date=0) AND start_date<?)
    ORDER BY first_name, last_name ASC

SQL

#Selects users belonging to a group only.
#returns a list of hashes
sub by_group_name {
    my ($class, $group, $limit, $offset) = @_;

    state $SQL = $class->SQL('by_group_name') . $CLASS->SQL_LIMIT('?', '?');
    $limit  //= 500;
    $offset //= 0;
    my $time = time;
    my @a = $class->query($SQL, $group, $time, $time, $limit, $offset);
    return map { +{%{$_->data}, name => $_->name} } @a;
}

1;

=pod

=encoding utf8

=head1 NAME

A class for TABLE users in schema main

=head1 SYNOPSIS


    #In a controller use the helper.
    #Find a user by login_name and change the current user
    my $user       = Ado::Model::Users->by_login_name($login_name);
    $c->user($user);

    #in a template
    <h1>Hello, <%=user->name%>!</h1>

    #Create a new user.
    my $user = Ado::Model::Users->add(login_name=>'petko'...);
    #Add the user to a group
    $user->add_to_group('cool');

=head1 DESCRIPTION

This class maps to rows in table C<users>.

=head1 ATTRIBUTES

Ado::Model::Users inherits all attributes from Ado::Model
and provides the following.

=head2 name

Readonly. Returns concatenated L</first_name> and L</last_name> of the user
or the username (in case the first two are not available).

    # Hello, Guest
    <h1>Hello, <%=user->name%>!</h1>

=head1 COLUMNS

Each column from table C<users> has an accessor method in this class.

=head2 id

=head2 group_id

=head2 login_name

=head2 login_password

=head2 first_name

=head2 last_name

=head2 email

=head2 description

=head2 created_by

=head2 changed_by

=head2 tstamp

=head2 reg_date

=head2 disabled

=head2 start_date

=head2 stop_date

=head1 ALIASES

none

=head1 METHODS

Ado::Model::Users inherits all methods from Ado::Model and provides the
following additional methods:

=head2 add

Given enough parameters creates a new user object and inserts it  into the



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