| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Kwiki::UserName::Auth; |
|
2
|
1
|
|
|
1
|
|
1278
|
use Kwiki::UserName -Base; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use mixin 'Kwiki::Installer'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
const cgi_class => 'Kwiki::UserName::Auth::CGI'; |
|
8
|
|
|
|
|
|
|
const css_file => 'user_name_auth.css'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Forward to this URL after process form submittion. |
|
11
|
|
|
|
|
|
|
field 'forward'; |
|
12
|
|
|
|
|
|
|
field 'display_msg'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
|
15
|
|
|
|
|
|
|
my $registry = shift; |
|
16
|
|
|
|
|
|
|
$registry->add(preload => 'user_name'); |
|
17
|
|
|
|
|
|
|
$registry->add(action => 'user_name'); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
## Override Plugin.pm's render_screen() |
|
21
|
|
|
|
|
|
|
sub render_screen { |
|
22
|
|
|
|
|
|
|
if(@_) { |
|
23
|
|
|
|
|
|
|
$self->template_process($self->screen_template, @_); |
|
24
|
|
|
|
|
|
|
} else { |
|
25
|
|
|
|
|
|
|
super; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub user_name { |
|
30
|
|
|
|
|
|
|
if(my $mode = $self->cgi->run_mode) { |
|
31
|
|
|
|
|
|
|
$self->$mode; |
|
32
|
|
|
|
|
|
|
} else { |
|
33
|
|
|
|
|
|
|
$self->setup; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
$self->render_screen; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
## 5 run modes: setup,create,login,logout,mail_password |
|
39
|
|
|
|
|
|
|
sub setup { |
|
40
|
|
|
|
|
|
|
$self->display_msg("Please login or register"); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub login { |
|
44
|
|
|
|
|
|
|
my ($email,$password) = (@_,$self->cgi->email,$self->cgi->password); |
|
45
|
|
|
|
|
|
|
if(my $user = $self->db_verify($email,$password)) { |
|
46
|
|
|
|
|
|
|
my $session = $self->hub->session->load; |
|
47
|
|
|
|
|
|
|
$session->param("users_auth",{name => $user, email => $email }); |
|
48
|
|
|
|
|
|
|
if($self->cgi->forward) { |
|
49
|
|
|
|
|
|
|
return $self->redirect($self->cgi->forward); |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
$self->display_msg("Login success"); |
|
52
|
|
|
|
|
|
|
} else { |
|
53
|
|
|
|
|
|
|
$self->display_msg("Login falied: Wrong user name or password."); |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub logout { |
|
58
|
|
|
|
|
|
|
$self->hub->session->load->param("users_auth", {}); |
|
59
|
|
|
|
|
|
|
$self->display_msg("Logout success"); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub create { |
|
63
|
|
|
|
|
|
|
if($self->cgi->password) { |
|
64
|
|
|
|
|
|
|
if($self->cgi->password_verify eq $self->cgi->password) { |
|
65
|
|
|
|
|
|
|
if($self->db_add($self->cgi->all)) { |
|
66
|
|
|
|
|
|
|
$self->display_msg("Registered"); |
|
67
|
|
|
|
|
|
|
} else { |
|
68
|
|
|
|
|
|
|
$self->display_msg("Duplicated username"); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
} else { |
|
71
|
|
|
|
|
|
|
$self->display_msg("Password does not match"); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub mail_password { |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# DB subs |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
field dbi => -init => '$self->hub->dbi'; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub dbinit { |
|
84
|
|
|
|
|
|
|
my $db = shift; |
|
85
|
|
|
|
|
|
|
my $dbh = $self->dbi->connect("dbi:SQLite:dbname=$db","","", |
|
86
|
|
|
|
|
|
|
{ RaiseError => 1, AutoCommit => 1 }); |
|
87
|
|
|
|
|
|
|
$dbh->do('CREATE TABLE user_name (displayname NOT NULL,email NOT NULL,password NOT NULL,regdate DEFAULT CURRENT_TIMESTAMP)'); |
|
88
|
|
|
|
|
|
|
$dbh->disconnect; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub dbpath { |
|
92
|
|
|
|
|
|
|
my $path = $self->plugin_directory; |
|
93
|
|
|
|
|
|
|
my $filename = io->catfile($path,"user_name.sqlt")->name; |
|
94
|
|
|
|
|
|
|
$self->dbinit($filename) unless -f $filename; |
|
95
|
|
|
|
|
|
|
return $filename; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub db_connect { |
|
99
|
|
|
|
|
|
|
my $db = $self->dbpath; |
|
100
|
|
|
|
|
|
|
$self->dbi->connect("dbi:SQLite:dbname=$db","","", |
|
101
|
|
|
|
|
|
|
{ RaiseError => 1, AutoCommit => 1 }); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub db_verify { |
|
105
|
|
|
|
|
|
|
my ($email,$password) = @_; |
|
106
|
|
|
|
|
|
|
my $dbh = $self->db_connect; |
|
107
|
|
|
|
|
|
|
my $user = $dbh->selectrow_hashref('SELECT displayname,email from user_name WHERE email=? AND password=?',undef,$email,$password); |
|
108
|
|
|
|
|
|
|
$dbh->disconnect; |
|
109
|
|
|
|
|
|
|
if($user) { |
|
110
|
|
|
|
|
|
|
return $user->{displayname} unless $user->{displayname} =~ /^\s*$/; |
|
111
|
|
|
|
|
|
|
$email =~ s/(.+?)@.*$/$1/; |
|
112
|
|
|
|
|
|
|
return $email; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
return; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub db_retrieve { |
|
118
|
|
|
|
|
|
|
my $email = shift; |
|
119
|
|
|
|
|
|
|
my $dbh = $self->db_connect; |
|
120
|
|
|
|
|
|
|
my $user = $dbh->selectrow_hashref('SELECT displayname FROM user_name WHERE email = ?',undef,$email); |
|
121
|
|
|
|
|
|
|
return $self->utf8_decode($user->{displayname}) if $user; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub db_add { |
|
125
|
|
|
|
|
|
|
my %vars = @_; |
|
126
|
|
|
|
|
|
|
return 0 if $self->db_exists($vars{email}); |
|
127
|
|
|
|
|
|
|
my $dbh = $self->db_connect; |
|
128
|
|
|
|
|
|
|
my $sth = $dbh->prepare('INSERT INTO user_name (displayname, email, password) values(?,?,?)'); |
|
129
|
|
|
|
|
|
|
$sth->execute(@vars{qw(display_name email password)}, ); |
|
130
|
|
|
|
|
|
|
$sth->finish; |
|
131
|
|
|
|
|
|
|
$dbh->disconnect; |
|
132
|
|
|
|
|
|
|
return 1; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub db_exists { |
|
136
|
|
|
|
|
|
|
my $email = shift; |
|
137
|
|
|
|
|
|
|
my $dbh = $self->db_connect; |
|
138
|
|
|
|
|
|
|
my $r = $dbh->selectrow_arrayref('SELECT email FROM user_name WHERE email=?',undef,$email); |
|
139
|
|
|
|
|
|
|
$dbh->disconnect; |
|
140
|
|
|
|
|
|
|
return $r; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
package Kwiki::UserName::Auth::CGI; |
|
144
|
|
|
|
|
|
|
use base 'Kwiki::CGI'; |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# Forward to this URL after process form submittion. |
|
147
|
|
|
|
|
|
|
cgi 'forward'; |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
cgi 'run_mode'; |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
cgi 'display_name'; |
|
152
|
|
|
|
|
|
|
cgi 'email'; |
|
153
|
|
|
|
|
|
|
cgi 'password'; |
|
154
|
|
|
|
|
|
|
cgi 'password_verify'; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
package Kwiki::UserName::Auth; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
__DATA__ |