| 1 | package Miril::UserManager::XMLTPP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | |||
| 7 | use XML::TreePP; | ||
| 8 | use Digest::MD5; | ||
| 9 | use List::Util qw(first); | ||
| 10 | use Data::AsObject qw(dao); | ||
| 11 | use Ref::List qw(list); | ||
| 12 | use Try::Tiny qw(try catch); | ||
| 13 | use Miril::Exception; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my $miril = shift; | ||
| 18 | |||
| 19 | my $self = bless {}, $class; | ||
| 20 | |||
| 21 | $self->{miril} = $miril; | ||
| 22 | my $cfg = $miril->cfg; | ||
| 23 | |||
| 24 | |||
| 25 | my $tpp = XML::TreePP->new(); | ||
| 26 | $tpp->set( indent => 2 ); | ||
| 27 | $tpp->set( force_array => ['user'] ); | ||
| 28 | my $tree; | ||
| 29 | |||
| 30 | try | ||
| 31 | { | ||
| 32 | $tree = $tpp->parsefile( $cfg->users_data ); | ||
| 33 | } | ||
| 34 | catch | ||
| 35 | { | ||
| 36 | Miril::Exception->throw( | ||
| 37 | message => "Could not parse users file", | ||
| 38 | errorvar => $_, | ||
| 39 | ); | ||
| 40 | }; | ||
| 41 | |||
| 42 | my @users = dao list $tree->{xml}{user}; | ||
| 43 | $self->{users} = \@users; | ||
| 44 | |||
| 45 | $self->{tree} = $tree; | ||
| 46 | $self->{tpp} = $tpp; | ||
| 47 | $self->{xml_file} = $cfg->users_data; | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub verification_callback { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | return sub { | ||
| 56 | my ($username, $password) = @_; | ||
| 57 | my $user = $self->get_user($username); | ||
| 58 | |||
| 59 | my $encrypted = $self->encrypt($password); | ||
| 60 | |||
| 61 | if ( | ||
| 62 | ( $encrypted eq $user->{password} ) | ||
| 63 | or ( $password eq $user->{password} ) | ||
| 64 | ) { | ||
| 65 | return $username; | ||
| 66 | } else { | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub get_user { | ||
| 73 | my $self = shift; | ||
| 74 | my $username = shift; | ||
| 75 | |||
| 76 | my $user = first {$_->username eq $username} $self->users; | ||
| 77 | return $user; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub set_user { | ||
| 81 | my $self = shift; | ||
| 82 | my $user = shift; | ||
| 83 | |||
| 84 | my $miril = $self->miril; | ||
| 85 | |||
| 86 | my @users = $self->users; | ||
| 87 | |||
| 88 | my $found = undef; | ||
| 89 | |||
| 90 | # try update | ||
| 91 | for my $u (@users) | ||
| 92 | { | ||
| 93 | if ($u->username eq $user->username) | ||
| 94 | { | ||
| 95 | $u->{password} = $user->password; | ||
| 96 | $u->{name} = $user->name; | ||
| 97 | $found++; | ||
| 98 | last; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # try create | ||
| 103 | if (!$found) { | ||
| 104 | my $new_user = dao { | ||
| 105 | username => $user->username, | ||
| 106 | password => $user->password, | ||
| 107 | name => $user->name, | ||
| 108 | }; | ||
| 109 | |||
| 110 | push @users, $new_user; | ||
| 111 | } | ||
| 112 | |||
| 113 | # update the xml file | ||
| 114 | my $new_tree = $self->tree; | ||
| 115 | $new_tree->{xml}->{user} = \@users; | ||
| 116 | $self->{tree} = $new_tree; | ||
| 117 | try | ||
| 118 | { | ||
| 119 | $self->tpp->writefile($self->xml_file, $new_tree); | ||
| 120 | } | ||
| 121 | catch | ||
| 122 | { | ||
| 123 | Miril::Exception->throw( | ||
| 124 | message => "Could not update user info", | ||
| 125 | errorvar => $_, | ||
| 126 | ); | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub delete_user { | ||
| 131 | my $self = shift; | ||
| 132 | my $username = shift; | ||
| 133 | |||
| 134 | my $miril = $self->miril; | ||
| 135 | |||
| 136 | my @users = $self->users; | ||
| 137 | |||
| 138 | my $i = -1; | ||
| 139 | for (@users) { | ||
| 140 | $i++; | ||
| 141 | last if $_->username eq $username; | ||
| 142 | } | ||
| 143 | |||
| 144 | if ($i != -1) { | ||
| 145 | splice(@users, $i, 1); | ||
| 146 | } | ||
| 147 | |||
| 148 | my $new_tree = $self->tree; | ||
| 149 | $new_tree->{xml}{user} = \@users; | ||
| 150 | $self->{tree} = $new_tree; | ||
| 151 | try | ||
| 152 | { | ||
| 153 | $self->tpp->writefile($self->xml_file, $new_tree); | ||
| 154 | } | ||
| 155 | catch | ||
| 156 | { | ||
| 157 | Miril::Exception->throw( | ||
| 158 | message => "Could not delete user", | ||
| 159 | errorvar => $_, | ||
| 160 | ); | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub encrypt | ||
| 165 | { | ||
| 166 | my $self = shift; | ||
| 167 | my $password = shift; | ||
| 168 | |||
| 169 | # only Digest::MD5 is available in core perl 5.8 | ||
| 170 | my $md5 = Digest::MD5->new; | ||
| 171 | $md5->add($password); | ||
| 172 | my $digest = $md5->b64digest; | ||
| 173 | return $digest; | ||
| 174 | } | ||
| 175 | |||
| 176 | ### ACCESSORS ### | ||
| 177 | |||
| 178 | sub users { @{ shift->{users} }; } | ||
| 179 | sub tree { shift->{tree}; } | ||
| 180 | sub tpp { shift->{tpp}; } | ||
| 181 | sub xml_file { shift->{xml_file}; } | ||
| 182 | sub miril { shift->{miril}; } | ||
| 183 | |||
| 184 | 1; |