1 package Text::Tradition::UserStore;
7 use KiokuX::User::Util qw(crypt_password);
9 extends 'KiokuX::Model';
11 use Text::Tradition::User;
12 # use Text::Tradition::Directory;
16 Text::Tradition::UserStore - KiokuDB storage management for Users
20 my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db');
21 my $newuser = $userstore->add_user({ username => 'fred',
22 password => 'somepassword' });
24 my $fetchuser = $userstore->find_user({ username => 'fred' });
25 if($fetchuser->check_password('somepassword')) {
26 ## login user or .. whatever
29 my $user = $userstore->deactivate_user({ username => 'fred' });
31 ## shouldnt be able to login etc
36 A L<KiokuX::Model> for managing the storage and creation of
37 L<Text::Tradition::User> objects. Subclass or replace this module in
38 order to use a different source for stemmaweb users.
44 Inherited from KiokuX::Model - dsn for the data store we are using.
48 Constant for the minimum password length when validating passwords,
53 has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } );
55 # has 'directory' => (
57 # isa => 'KiokuX::Model',
61 ## TODO: Some of these methods should probably optionally take $user objects
62 ## instead of hashrefs.
64 ## It also occurs to me that all these methods don't need to be named
65 ## XX_user, but leaving that way for now incase we merge this code
66 ## into ::Directory for one-store.
68 ## To die or not to die, on error, this is the question.
74 Takes a hashref of C<username>, C<password>.
76 Create a new user object, store in the KiokuDB backend, and return it.
81 my ($self, $userinfo) = @_;
82 my $username = $userinfo->{username};
83 my $password = $userinfo->{password};
85 return unless $username && $self->validate_password($password);
87 my $user = Text::Tradition::User->new(
89 password => crypt_password($password),
92 my $scope = $self->new_scope;
93 $self->store($user->kiokudb_object_id, $user);
100 Takes a hashref of C<username>.
102 Fetches the user object for the given username and returns it.
107 my ($self, $userinfo) = @_;
108 my $username = $userinfo->{username};
110 my $scope = $self->new_scope;
111 return $self->lookup(Text::Tradition::User->id_for_user($username));
117 Takes a hashref of C<username> and C<password> (same as add_user).
119 Retrieves the user, and updates it with the new information. Username
120 changing is not currently supported.
122 Returns the updated user object, or undef if not found.
127 my ($self, $userinfo) = @_;
128 my $username = $userinfo->{username};
129 my $password = $userinfo->{password};
131 return unless $username && $self->validate_password($password);
133 my $user = $self->find_user({ username => $username });
136 my $scope = $self->new_scope;
137 $user->password(crypt_password($password));
139 $self->update($user);
144 =head3 deactivate_user
146 Takes a hashref of C<username>.
148 Sets the users C<active> flag to false (0), and sets all traditions
149 assigned to them to non-public, updates the storage and returns the
152 Returns undef if user not found.
156 sub deactivate_user {
157 my ($self, $userinfo) = @_;
158 my $username = $userinfo->{username};
160 return if !$username;
162 my $user = $self->find_user({ username => $username });
166 foreach my $tradition (@{ $user->traditions }) {
167 ## Not implemented yet
168 # $tradition->public(0);
170 my $scope = $self->new_scope;
172 ## Should we be using Text::Tradition::Directory also?
173 $self->update(@{ $user->traditions });
175 $self->update($user);
180 =head3 reactivate_user
182 Takes a hashref of C<username>.
184 Returns the user object if already activated. Activates (sets the
185 active flag to true (1)), updates the storage and returns the user.
187 Returns undef if the user is not found.
191 sub reactivate_user {
192 my ($self, $userinfo) = @_;
193 my $username = $userinfo->{username};
195 return if !$username;
197 my $user = $self->find_user({ username => $username });
200 return $user if $user->active;
203 my $scope = $self->new_scope;
204 $self->update($user);
211 CAUTION: Delets actual data!
213 Takes a hashref of C<username>.
215 Returns undef if the user doesn't exist.
217 Removes the user from the store and returns 1.
222 my ($self, $userinfo) = @_;
223 my $username = $userinfo->{username};
225 return if !$username;
227 my $user = $self->find_user({ username => $username });
230 my $scope = $self->new_scope;
232 ## Should we be using Text::Tradition::Directory for this bit?
233 $self->delete( @{ $user->traditions });
236 $self->delete($user);
241 =head3 validate_password
243 Takes a password string. Returns true if it is longer than
244 L</MIN_PASS_LEN>, false otherwise.
246 Used internally by L</add_user>.
250 sub validate_password {
251 my ($self, $password) = @_;
253 return if !$password;
254 return if length($password) < $self->MIN_PASS_LEN;