#################################### ### Change Username ### Wrote by shann123 3-11-04 ### ### Changes username in both user's account and on their record. ### This will only change usernames in both places if the email address matches. ### ### NOTE: You will not be able to change anything else such as username, ### password or permissions while changing the username. This is because the ### username change requires a matching email address, so I didn't write it ### to change anything other than the username. ### ### INSTRUCTIONS: To change username in record along with account username, be sure the ### email address is the same in both places. If they aren't, it'll only change the username ### for the account. ### In admin, select the username you want to change, then inquire. Type in the new username, ### then check the box "Change username" next to the new username field. DO NOT change any other info. ### Then just hit the "Update/create user" button! ### ###################################### ############################################################################### # script: db.cgi # # sub admin_display # # # # replace subroutine # # by shann123 11 March 2004 # # for Change username Mod # ############################################################################### sub admin_display { # -------------------------------------------------------- # This displays the current user list. # my ($sth, $rc, $query); my ($insert_names, $insert_values, $message, $username_q, $email, $new_username_q, $update, @lines, $line); # Let's first see if we have anything to do. # Creates new account if checkbox NOT checked. if ($in{'new_username'} && ($in{'change_username'} ne "Yes")) { $insert_names = $insert_values = ""; $in{'username'} = $in{'new_username'}; if (($in{'username'} =~ /^[\w\d]+$/) and (length($in{'username'}) < 12)) { my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/'); $in{'password'} = crypt($in{'password'}, join '', @salt_chars[rand 64, rand 64]); if ($in{'email'} eq $email) { $message .= "email address already exists."; } foreach (qw!username password email per_view per_add per_del per_mod per_admin!) { $insert_names .= "$_,"; $insert_values .= $DBH->quote($in{$_}) . ","; } chop ($insert_names); chop ($insert_values); $query = qq! INSERT INTO $db_table_user ($insert_names) VALUES ($insert_values) !; $rc = $DBH->do($query); $rc ? ($message = "User: $in{'new_username'} created.") : ($message = "Error adding user: $in{'new_username'}. Reason: $DBI::errstr"); } else { $message = "Invalid username: '$in{'username'}'. Must only contain letters and numbers and be less then 12 characters."; } } elsif ($in{'delete'}) { if ($in{'username'}) { $username_q = $DBH->quote($in{'username'}); $query = qq! DELETE FROM $db_table_user WHERE username = $username_q !; $rc = $DBH->do($query); $rc ? ($message = "User: $in{'username'} deleted.") : ($message = "Error deleting user: $in{'username'}. Reason: $DBI::errstr"); } else { $message = "No username specified!"; } } # Updates user info if checkbox is NOT checked. elsif ($in{'username'} && !$in{'inquire'} && ($in{'change_username'} ne "Yes")) { $username_q = $DBH->quote($in{'username'}); if (($in{'email'} eq $email) && ($in{'username'} ne $userid)) { $message .= "email address already exists."; } foreach (qw!per_view per_add per_del per_mod per_admin!) { $update .= $_ . "=" . $DBH->quote($in{$_}) . ","; } chop ($update); $query = qq! SELECT password FROM $db_table_user WHERE username = $username_q !; my $sth = $DBH->prepare($query); $sth->execute(); if ($sth->rows) { my ($orig_pass) = $sth->fetchrow_array(); $orig_pass =~ s/^\s*(\S*)\s*$/$1/; $in{password} =~ s/^\s*(\S*)\s*$/$1/; if ($orig_pass ne $in{password}) { my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/'); $in{'password'} = crypt($in{'password'}, join '', @salt_chars[rand 64, rand 64]); } $query = qq! UPDATE $db_table_user SET $update, password='$in{'password'}', email='$in{'email'}' WHERE username = $username_q !; $rc = $DBH->do($query); $rc ? ($message = "User: $in{'username'} updated.") : ($message = "Error updating user: $in{'username'}. Reason: $DBI::errstr"); } else { $message = "Error, user $username_q not found!"; } } # Changes username if checkbox IS checked. elsif ($in{'new_username'} && !$in{'inquire'} && ($in{'change_username'} eq "Yes")) { $email_q = $DBH->quote($in{'email'}); if ($in{'username'} ne $in{'new_username'}) { $username_q = $DBH->quote($in{'new_username'}); $query = qq! SELECT * FROM $db_table_user WHERE Email = $email_q !; my $sth = $DBH->prepare($query); $sth->execute(); if ($sth->rows) { $query = qq! UPDATE $db_table_user SET username = $username_q WHERE Email = $email_q !; $rc = $DBH->do($query); $query = qq! UPDATE $db_table SET userid = $username_q WHERE Email = $email_q !; $rc = $DBH->do($query); $rc ? # You can change the 'username' to 'new_username' in the next 2 lines # if you would rather have it display the new username ($message = "User: $in{'username'} updated.") : ($message = "Error updating user: $in{'username'}. Reason: $DBI::errstr"); } else { $message = "Error, user $username_q not found!"; } } } else {} # Now let's load the list of users. $query = qq! SELECT username, password, Email, per_view, per_add, per_del, per_mod, per_admin FROM $db_table_user ORDER BY username !; $sth = $DBH->prepare ($query) or &cgierr("Unable to query database. Reason: $DBI::errstr. Query: $query"); $sth->execute or &cgierr("Unable to query database. Reason: $DBI::errstr. Query: $query"); # If we are inquiring, let's look for the specified user. # my (@data, $user_list, $perm); my (@data, $user_list, $perm, $password, $email); $user_list = qq~ Add Delete Modify Admin |; $password = $data[1]; $email = $data[2]; } else { $user_list .= qq~\n~; } } $user_list .= ""; # Build the permissions list if we haven't inquired in someone. if (!$perm) { $perm = qq| View Add Delete Modify Admin |; } &html_admin_display ($message, $user_list, $password, $perm, $email); } ############################################################################### # script: html.pl # # sub admin_display # # # # replace line # # by shann123 11 March 2004 # # for Change username Mod # ############################################################################### ## replace: ## With:    Change Username: ## This adds the check box next to the new username field. Of course, ## you can put the checkbox wherever you want, but you will need it somewhere. ## That's it!