| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Quiki::Users; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 860 | use Gravatar::URL; | 
|  | 1 |  |  |  |  | 2108 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 1050 | use Text::Password::Pronounceable; | 
|  | 1 |  |  |  |  | 1916 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 6 | 1 |  |  | 1 |  | 1041 | use Email::Sender::Simple 'sendmail'; | 
|  | 1 |  |  |  |  | 202009 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 7 | 1 |  |  | 1 |  | 320 | use Email::Simple; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 8 | 1 |  |  | 1 |  | 6 | use Email::Simple::Creator; | 
|  | 1 |  |  |  |  | 15 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 11 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 1 |  |  | 1 |  | 4364 | use DBI; | 
|  | 1 |  |  |  |  | 19950 |  | 
|  | 1 |  |  |  |  | 71 |  | 
| 14 | 1 |  |  | 1 |  | 13 | use Digest::MD5 'md5_hex'; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 1173 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub _connect { | 
| 17 | 0 |  |  | 0 |  |  | return DBI->connect("dbi:SQLite:dbname=data/users.sqlite","",""); | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub list { | 
| 21 | 0 |  |  | 0 | 1 |  | my $dbh = _connect; | 
| 22 | 0 |  |  |  |  |  | my @list = (); | 
| 23 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("SELECT username, email, perm_group FROM auth;"); | 
| 24 | 0 |  |  |  |  |  | $sth->execute; | 
| 25 | 0 |  |  |  |  |  | my $row; | 
| 26 | 0 |  |  |  |  |  | while ($row = $sth->fetchrow_hashref) { | 
| 27 | 0 |  |  |  |  |  | $row->{gravatar} = Quiki::Users->gravatar($row->{username}); | 
| 28 | 0 |  |  |  |  |  | push @list, $row; | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 31 | 0 |  |  |  |  |  | return \@list; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub gravatar { | 
| 35 | 0 |  |  | 0 | 1 |  | my ($class, $username) = @_; | 
| 36 | 0 |  |  |  |  |  | my $email; | 
| 37 | 0 | 0 | 0 |  |  |  | if ($username && ($email = $class->email($username))) { | 
| 38 | 0 |  |  |  |  |  | return gravatar_url(email => $email ); | 
| 39 |  |  |  |  |  |  | } else { | 
| 40 | 0 |  |  |  |  |  | return gravatar_url(email => "default"); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub update { | 
| 45 | 0 |  |  | 0 | 1 |  | my ($class, $username, %info) = @_; | 
| 46 | 0 |  |  |  |  |  | my @valid_fields = qw.password email perm_group.; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 | 0 |  |  |  |  | $info{password} = md5_hex($info{password}) if exists($info{password}); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | my @sql; | 
| 51 | 0 |  |  |  |  |  | for my $key (keys %info) { | 
| 52 | 0 | 0 |  |  |  |  | if (grep {$_ eq $key}  @valid_fields) { | 
|  | 0 |  |  |  |  |  |  | 
| 53 | 0 |  |  |  |  |  | push @sql, "$key = '$info{$key}'" | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 58 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("UPDATE auth SET ".join(", ",@sql)." WHERE username = ?"); | 
| 59 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 60 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub role { | 
| 64 | 0 |  |  | 0 | 1 |  | my ($class, $username) = @_; | 
| 65 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 66 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("SELECT perm_group FROM auth WHERE username = ?;"); | 
| 67 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 68 | 0 |  |  |  |  |  | my @row = $sth->fetchrow_array; | 
| 69 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 70 | 0 | 0 |  |  |  |  | return @row ? $row[0] : undef ; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub email { | 
| 74 | 0 |  |  | 0 | 1 |  | my ($class, $username) = @_; | 
| 75 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 76 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("SELECT email FROM auth WHERE username = ?;"); | 
| 77 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 78 | 0 |  |  |  |  |  | my @row = $sth->fetchrow_array; | 
| 79 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 80 | 0 | 0 |  |  |  |  | return @row ? $row[0] : undef ; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub delete { | 
| 84 | 0 |  |  | 0 | 1 |  | my ($class, $username) = @_; | 
| 85 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 86 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("DELETE FROM auth WHERE username = ?;"); | 
| 87 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub create { | 
| 91 | 0 |  |  | 0 | 1 |  | my ($class, $quiki, $username, $email) = @_; | 
| 92 | 0 |  |  |  |  |  | my $password = Text::Password::Pronounceable->generate(6, 10); | 
| 93 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 94 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("INSERT INTO auth VALUES (?,?,?,'user');"); | 
| 95 | 0 |  |  |  |  |  | $sth->execute($username, md5_hex($password), $email); | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  |  |  |  | my $servername = "http://$quiki->{SERVER_NAME}$quiki->{SCRIPT_NAME}"; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | my $from = "admin\@$quiki->{SERVER_NAME}"; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | my $message = Email::Simple->create | 
| 102 |  |  |  |  |  |  | ( | 
| 103 |  |  |  |  |  |  | header => [ | 
| 104 |  |  |  |  |  |  | To => $email, | 
| 105 |  |  |  |  |  |  | From => $from, | 
| 106 |  |  |  |  |  |  | Subject => "Your registration at $quiki->{name}", | 
| 107 |  |  |  |  |  |  | ], | 
| 108 |  |  |  |  |  |  | body => <<"EOEMAIL"); | 
| 109 |  |  |  |  |  |  | Hello, $username. | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Your password for $quiki->{name} at $servername is: $password | 
| 112 |  |  |  |  |  |  | Thank you. | 
| 113 |  |  |  |  |  |  | EOEMAIL | 
| 114 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 115 | 0 |  |  |  |  |  | sendmail($message); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | sub exists { | 
| 119 | 0 |  |  | 0 | 1 |  | my ($class, $username) = @_; | 
| 120 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 121 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("SELECT username FROM auth WHERE username = ?"); | 
| 122 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | my @row = $sth->fetchrow_array; | 
| 125 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 126 | 0 | 0 |  |  |  |  | return (@row)?1:0; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub auth { | 
| 130 | 0 |  |  | 0 | 1 |  | my ($class, $username, $password) = @_; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 0 |  |  |  |  |  | my $dbh = _connect; | 
| 133 | 0 |  |  |  |  |  | my $sth = $dbh->prepare("SELECT password FROM auth WHERE username = ?"); | 
| 134 | 0 |  |  |  |  |  | $sth->execute($username); | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 0 |  |  |  |  |  | my @row = $sth->fetchrow_array; | 
| 137 | 0 |  |  |  |  |  | $dbh->disconnect; | 
| 138 | 0 | 0 |  |  |  |  | if (@row) { | 
| 139 | 0 |  |  |  |  |  | return (md5_hex($password) eq $row[0]); | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  | else { | 
| 142 | 0 |  |  |  |  |  | return 0; | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | '\o/'; | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =encoding UTF-8 | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 NAME | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Quiki::Users - Quiki users manager | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | use Quiki::Users; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | # authenticate user | 
| 159 |  |  |  |  |  |  | if (Quiki::Users -> auth($username, $passwod)) { ... } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | # check user availability | 
| 162 |  |  |  |  |  |  | if (not Quiki::Users -> exists($username)) { ... } | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | Handles Quiki users management and permissions. | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =head2 auth | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | This function verifies an user credentials given an username and a password. | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head2 exists | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | This function verifies if a username already exists. | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =head2 gravatar | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | Returns the gravatar URL for that user. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head2 create | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | This function creates a new user given an username and an e-mail address. | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =head2 email | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | This function retrieves the e-mail address for a given username. | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =head2 role | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | This function retrieves the user role for a given username. | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | =head2 delete | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | Delestes information for a specific user. No questions. Just does it. | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =head2 update | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | This function is used to update user's information. | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =head2 list | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | Returns a list of all users. | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | Quiki, perl(1) | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =head1 AUTHOR | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | Alberto Simões, Eambs@cpan.orgE | 
| 211 |  |  |  |  |  |  | Nuno Carvalho, Esmash@cpan.orgE | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | Copyright 2009-2010 Alberto Simoes and Nuno Carvalho. | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 218 |  |  |  |  |  |  | under the terms of either: the GNU General Public License as published | 
| 219 |  |  |  |  |  |  | by the Free Software Foundation; or the Artistic License. | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =cut | 
| 224 |  |  |  |  |  |  |  |