#---------------------------------------------------------------------#
# This source code is (c)1999 Dave Belfer-Shevett - shevett@pobox.com #
# see http://www.stonekeep.com/license.html for licensing information #
#---------------------------------------------------------------------#

# ----------------------------------------------------------------------
#  Written by David H. Silber for Stonekeep Consulting.
#  david@SilberSoft.com -- http://www.SilberSoft.com/
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
#  People.pm
#  $Id:
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
package People;

# ----------------------------------------------------------------------
#  To keep me honest.
use strict;


=head1 NAME

People - The Keystone database C<people> table

=head1 SYNOPSIS 

	require People;

	$accesspermission = People::usermatch();

=head1 DESCRIPTION

People provides methods for working with the data in the C<people> table.

=head1 METHODS

=cut # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

=head2 usermatch() -- Does this user match a Keystone user?

Check the current user against the list of Keystone users.  If there
is a match return TRUE.  Otherwise return FALSE.

If the keystone.conf variable ``$sys_usermatch'' is set to `0', this
will always fail.

=over 4

=item Use:

  require People;

  $accesspermission = People::usermatch();

=item Returns:

True if a match is found, False otherwise.

=back

=cut # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

sub usermatch {
  my $package = shift;   #  Package name.

  $main::debug && print STDERR "Entering " . $package . "::usermatch().\n";


  my $query;   #  SQL command to check if user is a registered Keystone
               #  user.
  my $sth;     #  Statement handle.

  $main::debug && print "sys_usermatch: " .
      $main::keystone->config_var( 'usermatch' ) .  ".\n";

  #  If $sys_usermatch is `0', we're done.
  if (! $main::keystone->config_var( 'usermatch' )) {
    $main::debug && print STDERR "Returning from " . $package .
	"::usermatch().  \$sys_usermatch not set.\n";
    return 0;
  }

  #  Build a query to see if $main::username is listed in the people
  #  table.
  $query = "SELECT * FROM people WHERE login = '" . $main::username .
      "'";

  #  Query the database.
  $sth = $main::keystone->{dbh}->prepare( $query );
  $sth->execute() || die "execute('$query'): $DBI::errstr";

  $main::debug && print STDERR "Returning from " . $package .
      "::usermatch().  " . $sth->rows . " matches found.\n";

  return $sth->rows;
}

1;

__END__

=head1 CAVEATS

The main part of the program must define $main::username to be the name
of the current user.
If this method ends up being used by programs other than keycli, perhaps
the user's name should be extracted by the usermatch() method directly
from the environment, to avoid confusing bugs.

=head1 AUTHOR

David H. Silber
of Silber Software Engineering
F<E<lt>david@SilberSoft.comE<gt>>
wrote this for StoneKeep Consulting
as part of the B<keycli> development effort.

=head1 SEE ALSO

L<Keystone>

=cut
