Gossamer Forum
Home : Products : DBMan : Customization :

Add $email to password file

Quote Reply
Add $email to password file
Is there any way to add a field "$email" to the password file? Visitors would enter their email address on the form (or admin would) and it would store in the password file?
Thanks.

Quote Reply
Re: Add $email to password file In reply to
umm, tried to sent a private message to you but seems like you don't have an email address filled in. Please tell me if this is getting to the wrong person. This is what you have to do, replace your sub admin_display in db.cgi with the sub below. A few things to take note of are: Are you using the mod that allows you to add more permissions to your password file. If yes, you will need modify the codes below to correspond to your password file. Secondly, if your email address is not added to the last field of your password file, you will alos need to make the necessary changes to the codes below as well. Thirdly, $in('email'} is use in the sub html_admin_display in html.pl as a field. Therefore, if you define the field differently you must change it accordingly as well. I'll indicate in red those lines you may need to modify to suit your database configuration. The codes only allows you to see the email in the amdin display. It will not retieve the email from the password file and insert that into the form fields. That is a separate thing.


sub admin_display {
# --------------------------------------------------------
# Let's an admin add/update/remove users from the authorization file.
#
my ($message, @lines, $line);

# Do we have anything to do?
CASE: {
# If we've been passed in new_username, then we are adding a new user. Do
# some basic error checking and then add him into the password file.
$in{'new_username'} and do {
unless ((length($in{'new_username'}) >= 3) and (length($in{'new_username'}) <= 12) and ($in{'new_username'} =~ /^[a-zA-Z0-9]+$/)) {
$message = "Invalid username: $in{'new_username'}. Must only contain letters and numbers and be less then 12 and greater then 3 characters.";
last CASE;
}
unless ((length($in{'password'}) >= 3) and (length($in{'password'}) <= 12)) {
$message = "Invalid password: '$in{'password'}'. Must be less then 12 and greater then 3 characters.";
last CASE;
}
unless ($in{'email'} =~ /.+\@.+\..+/) {
$message = "Invalid email address: '$in{'email'}'.";
last CASE;

}

open (PASSWD, "<$auth_pw_file") || &cgierr("unable to open password file. Reason: $!\n");
@passwds = <PASSWD>;
close PASSWD;

foreach $pass (@passwds) { # Go through each pass and see if we match..
next if ($pass =~ /^$/); # Skip blank lines.
next if ($pass =~ /^#/); # Skip Comment lines.
chomp ($pass);

($userid, $pw, $view, $add, $del, $mod, $per_mem, $admin, $email) = split (/:/, $pass);

if ($in{'new_username'} eq $userid) {
$message = "userid already exists. Please try another.";
last CASE;
}

if ($in{'email'} eq $email) {
$message .= "email address already exists.";

last CASE;
}
}
open (PASS, ">>$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
my $encrypted = crypt($in{'password'}, $salt);
print PASS "$in{'new_username'}:$encrypted:$in{'per_view'}:$in{'per_add'}:$in{'per_del'}:$in{'per_mod'}:$in{'per_mem'}:$in{'per_admin'}:$in{'email'}\n";
close PASS;
$message = "User: $in{'new_username'} created.";
last CASE;
};
# If we've been passed in delete, then we are removing a user. Check
# to make sure a user was selected then try and remove him.
$in{'delete'} and do {
unless ($in{'username'}) {
$message = "No username selected to delete.";
last CASE;
}
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1) }
@lines = <PASS>;
close PASS;

open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my $found = 0;
foreach $line (@lines) {
($line =~ /^$in{'username'}:/) ?
($found = 1) :
print PASS $line;
}
close PASS;
$found ?
($message = "User: $in{'username'} deleted.") :
($message = "Unable to find userid: $in{'username'} in password file.");
last CASE;
};
# If we have a username, and the admin didn't press inquire, then
# we are updating a user.
($in{'username'} && !$in{'inquire'}) and do {
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

foreach $pass (@passwds) { # Go through each pass and see if we match..
next if ($pass =~ /^$/); # Skip blank lines.
next if ($pass =~ /^#/); # Skip Comment lines.
chomp ($pass);
($userid, $pw, $view, $add, $del, $mod, $admin, $email) = split (/:/, $pass);
if (($in{'email'} eq $email) && ($in{'username'} ne $userid)) {
$message .= "email address already exists.";
last CASE;
}

}
if ($auth_email_field >= 0) {
&change_email_in_records($in{'username'});

}
open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
my $found = 0;
foreach $line (@lines) {
if ($line =~ /^$in{'username'}:/) {
my $password = (split (/:/, $line))[1];
unless ($password eq $in{'password'}) {
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
$password = crypt($in{'password'}, $salt);
}
print PASS "$in{'username'}:$password:$in{'per_view'}:$in{'per_add'}:$in{'per_del'}:$in{'per_mod'}:$in{'per_mem'}:$in{'per_admin'}:$in{'email'}\n";
$found = 1;
}
else {
print PASS $line;
}
}
close PASS;
$in{'inquire'} = $in{'username'};
$found ?
($message = "User: $in{'username'} updated.") :
($message = "Unable to find user: '$in{'username'}' in the password file.");
last CASE;
};
};

# Now let's load the list of users.
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;

# If we are inquiring, let's look for the specified user.
my (@data, $user_list, $perm, $password, $email);
$user_list = qq~<select name="username"><option> </option>~;
LINE: foreach $line (@lines) {
$line =~ /^#/ and next LINE;
$line =~ /^\s*$/ and next LINE;
chomp $line;
@data = split (/:/, $line);
push (@users,$data[0]);
if ($in{'inquire'} and ($in{'username'} eq $data[0])) {
$password = $data[1];
$email = $data[8];#Change the $data[number] to the number which is actually your email field in your PASSWORD file
$perm = qq|
View <input type=checkbox name="per_view" value="1" |;
($data[2] and $perm .= "CHECKED"); $perm .= qq|>
Add <input type=checkbox name="per_add" value="1" |;
($data[3] and $perm .= "CHECKED"); $perm .= qq|>
Delete <input type=checkbox name="per_del" value="1" |;
($data[4] and $perm .= "CHECKED"); $perm .= qq|>
Modify <input type=checkbox name="per_mod" value="1" |;
($data[5] and $perm .= "CHECKED"); $perm .= qq|>
Member <input type=checkbox name="per_mem" value="1" |;
($data[6] and $perm .= "CHECKED"); $perm .= qq|>
Admin <input type=checkbox name="per_admin" value="1" |;
($data[7] and $perm .= "CHECKED"); $perm .= qq|>|;
}
}
foreach $user (sort @users) {
if ($in{'inquire'} and ($in{'username'} eq $user)) {
$user_list .= qq~<option value="$user" SELECTED>$user</option>\n~;
}
else {
$user_list .= qq~<option value="$user">$user</option>\n~;
}
}
$user_list .= "</select>";

# Build the permissions list if we haven't inquired in someone.
if (!$perm) {
$perm = qq|
View <input type=checkbox name="per_view" value="1" |; ($auth_default_perm[0] and $perm .= "CHECKED"); $perm .= qq|>
Add <input type=checkbox name="per_add" value="1" |; ($auth_default_perm[1] and $perm .= "CHECKED"); $perm .= qq|>
Delete <input type=checkbox name="per_del" value="1" |; ($auth_default_perm[2] and $perm .= "CHECKED"); $perm .= qq|>
Modify <input type=checkbox name="per_mod" value="1" |; ($auth_default_perm[3] and $perm .= "CHECKED"); $perm .= qq|>
Member <input type=checkbox name="per_mem" value="1" |; ($auth_default_perm[4] and $perm .= "CHECKED"); $perm .= qq|>
Admin <input type=checkbox name="per_admin" value="1" |; ($auth_default_perm[5] and $perm .= "CHECKED"); $perm .= qq|>|;
}
&html_admin_display ($message, $user_list, $password, $perm, $email);
}

You will also need to add one filed to your html_admin_display in html.pl file.

Basically it is a tet inout field with the Name in my case set to Name="email" so that i can use $in{'email'} if you want to use a different name eg. Name="emailaddress" then change all references of $in{'email'} to $in{'emailaddress'}.

Ths is only the first part. I'll tell you the seocnd part once you verify that this is working.

Julian
Quote Reply
Re: Add $email to password file In reply to
Thank you for the time you've spent on this. I copied this as you said and here are the problems I have:

If modifying an existing record, I receive this error: "Fatal Error: Undefined subroutine &main::change_email_in_records called at db.cgi"

Adding a record with email address works fine.

Upon inquiring on a user, the email address does not show up on the html page (even though there is a field for email).

Thanks again!

Quote Reply
Re: Add $email to password file In reply to
Opps.. I forgot that you have not install that additional mod yet. As you can see the codes I gave are highly modified. Okay this should fix the problem:

Uncomment the line below:

if ($auth_email_field >= 0) {
&change_email_in_records($in{'username'});
}

Julian
Quote Reply
Re: Add $email to password file In reply to
Ok, I got everything to work. The HTML problems was my problem. I also noticed you had a "member" flag which I removed. Thanks a bunch, I really appreciate it.

Quote Reply
Re: Add $email to password file In reply to
This highlights a minor typo. "greater then" should, of course, read "greater than". There are a number of similar typos in the password validation at sign_up

--
Nigel Horne. Arranger, Composer, Conductor, Typesetter.