And now for the changes to the
addUser() method.
ArrayList errors = new ArrayList();
if (getVariable("option", var.POST) == "Save New User")
{
//save a new user
userUsername = sanitize(getVariable("username", var.POST), clean.DB);
userDisplayname = sanitize(getVariable("displayName", var.POST), clean.DB);
userEmail = sanitize(getVariable("email", var.POST), clean.DB);
userShowEmail = (getVariable("showEmail", var.POST) == "1") ? 1 : 0;
userAccessLevel = Convert.ToInt16(getVariable("userAccessLevel", var.POST));
string password1 = getVariable("password1", var.POST);
string password2 = getVariable("password2", var.POST);
if (password1 != password2)
{
errors.Add("Passwords don't match.");
}//checking to see if passwords match
else if (password1 == null || password1.Length < 6)
{
errors.Add("Password needs to be at least 6 characters long.");
}
if (userAccessLevel != 0 && userAccessLevel != 100 && userAccessLevel != 255)
{
errors.Add("Invalid access level");
}//verify the access level is a valid value
//verify the info doesn't already exist
string select = @"SELECT count(*)
FROM users
WHERE displayName='" + userDisplayname + @"'";
SqlDataReader dr = query(select);
dr.Read();
if (Convert.ToInt16(dr.GetValue(0)) > 0)
{
errors.Add("Display name already exists.");
}
dr.Dispose();
select = @"SELECT count(*)
FROM users
WHERE username='" + userUsername + @"'";
dr = query(select);
dr.Read();
if (Convert.ToInt16(dr.GetValue(0)) > 0)
{
errors.Add("User name already exists.");
}
dr.Dispose();
select = @"SELECT count(*)
FROM users
WHERE email='" + userEmail + @"'";
dr = query(select);
dr.Read();
if (Convert.ToInt16(dr.GetValue(0)) > 0)
{
errors.Add("Email already exists.");
}
dr.Dispose();
if (errors.Count == 0)
{
//no errors, save new user
select = @"INSERT INTO users
(username, password, displayName, email, showEmail, createDate, accessLevel)
VALUES
('" + userUsername + "', '" + FormsAuthentication.HashPasswordForStoringInConfigFile(password1, "MD5") + "', '" + userDisplayname + "', '" + userEmail + "', " + userShowEmail + ", getDate(), " + userAccessLevel + ")";
dr = query(select);
if (dr.RecordsAffected > 0)
{
errors.Add("User Added");
userUsername = null;
userDisplayname = null;
userEmail = null;
}
else
{
errors.Add("There has been an error. No records were added.");
}
dr.Dispose();
}
}//end save new user section
This section of code goes directly below the variable initializiation. Please notice that I decided to change the string
error into an arraylist called
errors. An arraylist is a type of variable, like string and int and bool, but it can hold a series of any other type of variable or object. In this case, I'm having it hold one or more strings. You will need to add the System.Collections namespace to your using list to access the arraylisy type. We'll see how I spit out the results in a moment, but first let's go over the code above. I've removed all references to the error string since we no longer use it.
I create an if block looking to see if
option == "Save New User". This lets me execute the new user code when appropriate. I then grab all the passed variables, sanitizing as appropriate. Once I've grabbed the passwords, I immediately make sure they match... if they do not, I add an error to my arraylist. If they do match, I then check to make sure it's not equal to null and that it's at least 6 characters long. I had to check if it's null because I can't use the
.Length property on a null string. If I had, the page would throw an exception. If the password is null or too short, I add an error.
Next I make sure the
accessLevel is a valid number, in this case 0, 100 or 255. If not I create another error. Following this, I do a series of queries against the user table to verify that the
userDisplayname,
userUsername and
userEmail are not already in use. In each case I add a different error.
After all the error checking, I see if the
errors arraylist has a count of 0 (meaning no errors were added) and if so I insert the new user. Once more I use
RecordsAffected to make sure the insert worked. There's no reason it shouldn't, of course. If it did succeed, I put a success message into the
errors arraylist (because it was convenient) and then wipe out the
userUsername,
userDisplayname and
userEmail fields. By wiping out those three fields, when the add user form is displayed again, it's immediately ready for another new user to be added. That could be very nice if you're trying to add a bunch of users at once.