File Coverage

blib/lib/Rubric/WebApp/Login.pm
Criterion Covered Total %
statement 21 24 87.5
branch 6 10 60.0
condition 1 3 33.3
subroutine 7 10 70.0
pod 8 8 100.0
total 43 55 78.1


line stmt bran cond sub pod time code
1 2     2   1816 use strict;
  2         3  
  2         51  
2 2     2   10 use warnings;
  2         13  
  2         603  
3             package Rubric::WebApp::Login;
4             # ABSTRACT: web login processing
5             $Rubric::WebApp::Login::VERSION = '0.156';
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod This module checks for information needed to confirm that a user is logged into
9             #pod the Rubric.
10             #pod
11             #pod =head1 METHODS
12             #pod
13             #pod =head2 Rubric::WebApp::Login->check_for_login($webapp)
14             #pod
15             #pod This method is called by the WebApp's C, and checks for a login
16             #pod attempt in the submitted request.
17             #pod
18             #pod It looks for a login username by calling C, then converts
19             #pod the login name to a Rubric name by calling C and returns
20             #pod immediately if the name can't be shown valid by calling C.
21             #pod
22             #pod It retrieves the User object by calling C or, if needed,
23             #pod C, and returns if it can't get a User object. It tries to
24             #pod authenticate by calling C. If the user is authorized but
25             #pod isn't verified, he won't be logged in and the C parameter will be
26             #pod set on the Rubric::WebApp object. Otherwise, he will be logged in with
27             #pod C.
28             #pod
29             #pod Most of the methods above are virtual methods in this class, and should be
30             #pod implemented in subclasses. The bundled L (the
31             #pod default) and L serve as examples.
32             #pod
33             #pod =cut
34              
35             sub check_for_login {
36 48     48 1 142 my ($self, $webapp) = @_;
37              
38 48 100       296 return unless my $username = $self->get_login_username($webapp);
39              
40 17         164 $username = $self->map_username($username);
41 17 50       92 return unless $self->valid_username($username);
42 17 50 33     82 return unless my $user =
43             $self->get_login_user($username) || $self->autocreate_user($username);
44 17 50       23524 return unless $self->authenticate_login($webapp, $user);
45 17 50       4822 if ($user->verification_code) {
46 0         0 $webapp->param('user_pending', 1);
47             } else {
48 17         16676 $self->set_current_user($webapp, $user);
49             }
50             }
51              
52             #pod =head2 get_login_username($webapp)
53             #pod
54             #pod This method returns the login username taken from the request. It is not
55             #pod necessarily the name of a Rubric user (see C).
56             #pod
57             #pod This must be implemented by the login subclass.
58             #pod
59             #pod =cut
60              
61 0     0 1 0 sub get_login_username { die "get_login_username unimplemented" }
62              
63             #pod =head2 map_username($username)
64             #pod
65             #pod This method returns the Rubric username to which the login name maps. By
66             #pod default, it returns the C<$username> verbatim.
67             #pod
68             #pod =cut
69              
70 17     17 1 55 sub map_username { $_[1] }
71              
72             #pod =head2 valid_username($username)
73             #pod
74             #pod Returns a true or false value, depending on whether the given username string
75             #pod is a valid username.
76             #pod
77             #pod =cut
78              
79             sub valid_username {
80 17     17 1 55 my ($self, $username) = @_;
81 17         116 $username =~ /^[\pL\d_]+$/;
82             }
83              
84             #pod =head2 get_login_user($username)
85             #pod
86             #pod Given a username, this method returns the Rubric::User object for the user.
87             #pod
88             #pod =cut
89              
90             sub get_login_user {
91 17     17 1 43 my ($self, $username) = @_;
92 17         205 Rubric::User->retrieve($username);
93             }
94              
95             #pod =head2 autocreate_user($username)
96             #pod
97             #pod If C can't find a user, this method is called to try to create
98             #pod the user automatically. By default, it always returns nothing. It may be
99             #pod subclassed for implementation. (For example, one could create domain users
100             #pod from a directory.)
101             #pod
102             #pod =cut
103              
104       0 1   sub autocreate_user { }
105              
106             #pod =head2 authenticate_login($webapp, $user)
107             #pod
108             #pod This method attempts to authenticate the user's login, checking the given
109             #pod password or performing any other needed check. It returns true or false.
110             #pod
111             #pod This must be implemented by the login subclass.
112             #pod
113             #pod =cut
114              
115 0     0 1 0 sub authenticate_login { die "authenticate_login unimplemented" }
116              
117             #pod =head2 set_current_user($webapp, $user)
118             #pod
119             #pod This method sets the current user on the WebApp by setting the WebApp's
120             #pod "current_user" attribute to the Rubric::User object.
121             #pod
122             #pod =cut
123              
124             sub set_current_user {
125 17     17 1 48 my ($self, $webapp, $user) = @_;
126              
127 17         84 $webapp->param(current_user => $user);
128             }
129              
130             1;
131              
132             __END__