foreach my $id ( $d->traditions ) {
print $d->tradition( $id )->name;
}
+
+ ## Users:
+ my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db');
+ my $newuser = $userstore->add_user({ username => 'fred',
+ password => 'somepassword' });
+
+ my $fetchuser = $userstore->find_user({ username => 'fred' });
+ if($fetchuser->check_password('somepassword')) {
+ ## login user or .. whatever
+ }
+
+ my $user = $userstore->deactivate_user({ username => 'fred' });
+ if(!$user->active) {
+ ## shouldnt be able to login etc
+ }
=head1 DESCRIPTION
Text::Tradition::Directory is an interface for storing and retrieving text traditions and all their data, including an associated stemma hypothesis. It is an instantiation of a KiokuDB::Model, storing traditions and associated stemmas by UUID.
+=head1 ATTRIBUTES
+
+=head2 MIN_PASS_LEN
+
+Constant for the minimum password length when validating passwords,
+defaults to "8".
+
+=cut
+
+has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } );
+
=head1 METHODS
=head2 new
=end testing
=cut
+use Text::Tradition::TypeMap::Entry;
has +typemap => (
- is => 'rw',
- isa => 'KiokuDB::TypeMap',
- default => sub {
- KiokuDB::TypeMap->new(
- isa_entries => {
- "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
- "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
- }
- );
- },
+ is => 'rw',
+ isa => 'KiokuDB::TypeMap',
+ default => sub {
+ KiokuDB::TypeMap->new(
+ isa_entries => {
+ "Text::Tradition" =>
+ KiokuDB::TypeMap::Entry::Naive->new(),
+ "Graph" => Text::Tradition::TypeMap::Entry->new(),
+ "Graph::AdjacencyMap" => Text::Tradition::TypeMap::Entry->new(),
+ }
+ );
+ },
);
# Push some columns into the extra_args
return $class->$orig( $args );
};
-before [ qw/ store update insert delete / ] => sub {
+## These checks don't cover store($id, $obj)
+# before [ qw/ store update insert delete / ] => sub {
+before [ qw/ delete / ] => sub {
my $self = shift;
my @nontrad;
foreach my $obj ( @_ ) {
-# if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
-
if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition'
&& ref ($obj) ne 'Text::Tradition::User' ) {
# Is it an id => Tradition hash?
# TODO Garbage collection doesn't work. Suck it up and live with the
# inflated DB.
-# after delete => sub {
-# my $self = shift;
-# my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
-# $self->directory->backend->delete( $gc->garbage->members );
-# };
+after delete => sub {
+ my $self = shift;
+ my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
+ $self->directory->backend->delete( $gc->garbage->members );
+};
sub save {
my $self = shift;
return $obj;
}
+sub user_traditionlist {
+ my ($self, $user) = @_;
+
+ my @tlist;
+ if(ref $user && $user->is_admin) {
+ ## Admin sees all
+ return $self->traditionlist();
+ } elsif(ref $user) {
+ ## We have a user object already, so just fetch its traditions and use tose
+ foreach my $t (@{ $user->traditions }) {
+ push( @tlist, { 'id' => $self->object_to_id( $t ),
+ 'name' => $t->name } );
+ }
+ return @tlist;
+ } elsif($user ne 'public') {
+ die "Passed neither a user object nor 'public' to user_traditionlist";
+ }
+
+ ## Search for all traditions which allow public viewing
+ ## When they exist!
+## This needs to be more sophisticated, probably needs Search::GIN
+# my $list = $self->search({ public => 1 });
+
+ ## For now, just fetch all
+ ## (could use all_objects or grep down there?)
+ return $self->traditionlist();
+}
+
sub traditionlist {
my $self = shift;
+ my ($user) = @_;
+
+ return $self->user_traditionlist($user) if($user);
+
+ my @tlist;
# If we are using DBI, we can do it the easy way; if not, the hard way.
# Easy way still involves making a separate DBI connection. Ew.
- my @tlist;
if( $self->dsn =~ /^dbi:(\w+):/ ) {
my $dbtype = $1;
my @connection = @{$self->directory->backend->connect_info};
);
}
-=head1 NAME
-
-Text::Tradition::UserStore - KiokuDB storage management for Users
-
-=head1 SYNOPSIS
-
- my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db');
- my $newuser = $userstore->add_user({ username => 'fred',
- password => 'somepassword' });
-
- my $fetchuser = $userstore->find_user({ username => 'fred' });
- if($fetchuser->check_password('somepassword')) {
- ## login user or .. whatever
- }
-
- my $user = $userstore->deactivate_user({ username => 'fred' });
- if(!$user->active) {
- ## shouldnt be able to login etc
- }
-
-=head1 DESCRIPTION
-
-A L<KiokuX::Model> for managing the storage and creation of
-L<Text::Tradition::User> objects. Subclass or replace this module in
-order to use a different source for stemmaweb users.
-
-=head2 ATTRIBUTES
-
-=head3 dsn
-
-Inherited from KiokuX::Model - dsn for the data store we are using.
-
-=head3 MIN_PASS_LEN
-
-Constant for the minimum password length when validating passwords,
-defaults to "8".
-
-=cut
-
-has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } );
# has 'directory' => (
# is => 'rw',
## To die or not to die, on error, this is the question.
-=head2 METHODS
-
-=head3 add_user
+=head2 add_user
Takes a hashref of C<username>, C<password>.
sub add_user {
my ($self, $userinfo) = @_;
- my $username = $userinfo->{url} || $userinfo->{username};
+
+ my $username = $userinfo->{username};
my $password = $userinfo->{password};
+ my $role = $userinfo->{role} || 'user';
- return unless ($username =~ /^https?:/
- || ($username && $self->validate_password($password))) ;
+ throw( "No username given" ) unless $username;
+ throw( "Invalid password - must be at least " . $self->MIN_PASS_LEN
+ . " characters long" )
+ unless ( $self->validate_password($password) || $username =~ /^https?:/ );
my $user = Text::Tradition::User->new(
id => $username,
password => ($password ? crypt_password($password) : ''),
+ email => ($userinfo->{email} ? $userinfo->{email} : $username),
+ role => $role,
);
- my $scope = $self->new_scope;
$self->store($user->kiokudb_object_id, $user);
return $user;
}
sub create_user {
- my $self = shift;
- return $self->add_user(@_);
+ my ($self, $userinfo) = @_;
+
+ ## No username means probably an OpenID based user
+ if(!exists $userinfo->{username}) {
+ extract_openid_data($userinfo);
+ }
+
+ return $self->add_user($userinfo);
+}
+
+## Not quite sure where this method should be.. Auth /
+## Credential::OpenID just pass us back the chunk of extension data
+sub extract_openid_data {
+ my ($userinfo) = @_;
+
+ ## Spec says SHOULD use url as identifier
+ $userinfo->{username} = $userinfo->{url};
+
+ ## Use email addy as display if available
+ if(exists $userinfo->{extensions} &&
+ exists $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'} &&
+ defined $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'}) {
+ ## Somewhat ugly attribute extension reponse, contains
+ ## google-email string which we can use as the id
+
+ $userinfo->{email} = $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'};
+ }
+
+ return;
}
-=head3 find_user
+=head2 find_user
-Takes a hashref of C<username>, optionally C<openid_identifier>.
+Takes a hashref of C<username>, and possibly openIDish results from
+L<Net::OpenID::Consumer>.
Fetches the user object for the given username and returns it.
sub find_user {
my ($self, $userinfo) = @_;
- ## url or display?
- # 'display' => 'castaway.myopenid.com',
- # 'url' => 'http://castaway.myopenid.com/',
- my $username = $userinfo->{url} || $userinfo->{username};
- return $self->lookup(Text::Tradition::User->id_for_user($username));
-
+ ## No username means probably an OpenID based user
+ if(!exists $userinfo->{username}) {
+ extract_openid_data($userinfo);
+ }
+
+ my $username = $userinfo->{username};
+
+ ## No logins if user is deactivated (use lookup to fetch to re-activate)
+ my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
+ return if(!$user || !$user->active);
+
+ print STDERR "Found user, $username, email is :", $user->email, ":\n";
+
+ return $user;
}
-=head3 modify_user
+=head2 modify_user
Takes a hashref of C<username> and C<password> (same as add_user).
my ($self, $userinfo) = @_;
my $username = $userinfo->{username};
my $password = $userinfo->{password};
+ my $role = $userinfo->{role};
- return unless $username && $self->validate_password($password);
+ throw( "Missing username or bad password" )
+ unless $username && $self->validate_password($password);
- my $scope = $self->new_scope;
my $user = $self->find_user({ username => $username });
- return unless $user;
+ throw( "Could not find user $username" ) unless $user;
- $user->password(crypt_password($password));
+ if($password) {
+ $user->password(crypt_password($password));
+ }
+ if($role) {
+ $user->role($role);
+ }
$self->update($user);
return $user;
}
-=head3 deactivate_user
+=head2 deactivate_user
Takes a hashref of C<username>.
my ($self, $userinfo) = @_;
my $username = $userinfo->{username};
- return if !$username;
+ throw( "Need to specify a username for deactivation" ) unless $username;
my $user = $self->find_user({ username => $username });
- return if !$user;
+ throw( "User $username not found" ) unless $user;
$user->active(0);
foreach my $tradition (@{ $user->traditions }) {
## Not implemented yet
# $tradition->public(0);
}
- my $scope = $self->new_scope;
## Should we be using Text::Tradition::Directory also?
$self->update(@{ $user->traditions });
return $user;
}
-=head3 reactivate_user
+=head2 reactivate_user
Takes a hashref of C<username>.
my ($self, $userinfo) = @_;
my $username = $userinfo->{username};
- return if !$username;
+ throw( "Need to specify a username for reactivation" ) unless $username;
- my $scope = $self->new_scope;
- my $user = $self->find_user({ username => $username });
- return if !$user;
+ my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
+ throw( "User $username not found" ) unless $user;
return $user if $user->active;
return $user;
}
-=head3 delete_user
+=head2 delete_user
-CAUTION: Delets actual data!
+CAUTION: Deletes actual data!
Takes a hashref of C<username>.
my ($self, $userinfo) = @_;
my $username = $userinfo->{username};
- return if !$username;
+ throw( "Need to specify a username for deletion" ) unless $username;
- my $scope = $self->new_scope;
my $user = $self->find_user({ username => $username });
- return if !$user;
+ throw( "User $username not found" ) unless $user;
## Should we be using Text::Tradition::Directory for this bit?
$self->delete( @{ $user->traditions });
return 1;
}
-=head3 validate_password
+=head2 validate_password
Takes a password string. Returns true if it is longer than
L</MIN_PASS_LEN>, false otherwise.