# Copyright 2003-2006, Fred Steinberg, Brown Bear Software

# Simple internal Calcium crypted pw user authentication (and enumeration)

package User::CalciumAuth;

use strict;
use Calendar::User;

# Return list of (all) user names
sub getUserNames {
    my $classname = shift;
    my @users = User->getUsers;
    return map {lc ($_->name)} @users;
}

sub exists {
    my ($classname, $username) = @_;
    my @usernames = $classname->getUserNames;
    foreach my $name (@usernames) {
        if ($name eq $username) {
            return 1;
        }
    }
    return undef;
}

# true if ok, false if bad, undef if user not found ?
sub checkPassword {
    my $classOrObject = shift;
    my ($username, $password);
    if (ref $classOrObject) {
        $password = shift;
        $username = $classOrObject->name;
    } else {
        ($username, $password) = @_;
    }

    my $storedPW = MasterDB->getPassword ($username);
    return undef unless $storedPW;
    return (crypt ($password, $storedPW) eq $storedPW);
}

sub canSetPassword {
    return 1;
}

1;
