finished all UM features

create account
edit account
delete account
disable account
enable account
This commit is contained in:
Im_Alpha 2021-12-14 02:01:28 -08:00
parent 484c725ba8
commit 53aaf77c6b
4 changed files with 117 additions and 69 deletions

View File

@ -29,53 +29,74 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
return false; return false;
} }
} }
public bool EditUserRecord(UserAccount newUser, string CreatedBy, IDataSource<string> dbSource) public bool EditUserRecord(UserAccount editUser, string CreatedBy, IDataSource<string> dbSource)
{
try
{ {
/* // Insert into users table
string sqlUser = $"UPDATE hobby.roles r SET r.Role = 'regular',r.CreatedBy = 'colin'WHERE r.RoleID = 5; ";
Object insertNewUser = dbSource.WriteData(sqlUser);
// Insert into users table // Insert into users table
string sqlRoles = $"INSERT INTO roles (Role, CreatedBy, CreatedDate) " + string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " +
$"VALUES ('{newUser.NewRole}', '{CreatedBy}', NOW());"; $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " +
Object insertNewRole = dbSource.WriteData(sqlUser); $"WHERE t.UserName = '{editUser.username}';";
// Create string for confirming user account
string confirmUser = $"Select * from users where username = {newUser.NewUserName} " +
$"and password = {newUser.NewPassword};";
Object conUser = dbSource.ReadData(confirmUser);
//Console.WriteLine("type of Reesult:" + confirmAdmin.GetType()); bool updateNewUser = dbSource.UpdateData(sqlUser);
OdbcDataReader reader = null; return updateNewUser;
if (conUser.GetType() == typeof(OdbcDataReader))
{
reader = (OdbcDataReader)conUser;
} }
catch (Exception ex)
{
return false;
}
}
public bool DeleteUserRecord(UserAccount deleteUser, string CreatedBy, IDataSource<string> dbSource)
{
try
{
// Insert into users table
string sqlUser = $"DELETE from users WHERE UserName = '{deleteUser.username}' " +
$"and Password = '{deleteUser.password}';";
// Read Sql query results bool deleteNewUser = dbSource.DeleteData(sqlUser);
while (reader.Read()) return deleteNewUser;
{
Console.WriteLine(reader.GetString(0));
}
SqlDAO sqlDS = (SqlDAO)dbSource; }
Console.WriteLine(""); catch (Exception ex)
{
return false;
}
}
public bool DisableUser(UserAccount disableUser, string CreatedBy, IDataSource<string> dbSource)
{
try
{
// Insert into users table
string sqlUser = $"UPDATE users u SET u.IsActive = 0 WHERE u.UserName = '{disableUser.username}'" +
$"and u.Role = '{disableUser.role}';";
bool disableNewUser = dbSource.UpdateData(sqlUser);
return disableNewUser;
// Closing the connection
sqlDS.getConnection().Close();*/
return true;
} }
public bool DeleteUserRecord(UserAccount newUser, IDataSource<string> dbSource) catch (Exception ex)
{ {
return true; return false;
} }
public bool DisableUser(UserAccount newUser, IDataSource<string> dbSource)
{
return true;
} }
public bool EnableUser(UserAccount newUser, IDataSource<string> dbSource) public bool EnableUser(UserAccount enableUser, string CreatedBy, IDataSource<string> dbSource)
{ {
return true; try
{
// Insert into users table
string sqlUser = $"UPDATE users u SET u.IsActive = 1 WHERE u.UserName = '{enableUser.username}'" +
$"and u.Role = '{enableUser.role}';";
bool disableNewUser = dbSource.UpdateData(sqlUser);
return disableNewUser;
}
catch (Exception ex)
{
return false;
}
} }
} }
} }

View File

@ -224,7 +224,7 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
UserAccount newUser = new UserAccount(newCredentials.GetUserName(), UserAccount newUser = new UserAccount(newCredentials.GetUserName(),
newCredentials.GetPassword(), newCredentials.GetEmail(), newCredentials.GetPassword(), newCredentials.GetEmail(),
newCredentials.GetRole(), DateTime.UtcNow); newCredentials.GetRole(), DateTime.UtcNow);
bool accountValid = accountService.CreateUserRecord(newUser,user.username, dbSource); bool accountValid = accountService.CreateUserRecord(newUser, user.username, dbSource);
if (accountValid is true) if (accountValid is true)
{ {
Console.WriteLine("\nAccount created Successfully"); Console.WriteLine("\nAccount created Successfully");
@ -234,30 +234,74 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
{ {
return "Database Timed out"; return "Database Timed out";
} }
//break;
// Edit account // Edit account
case 2: case 2:
/* UserAccount newEditUser = new UserAccount(newCredentials.GetUserName(), // State what account is being edited
newCredentials.GetPassword(), DateTime.UtcNow); string userName = newCredentials.GetUserName();
accountService.EditUserRecord(newEditUser, dbSource);*/ string userRole = newCredentials.GetRole();
// Notify the user of what can be edited
Console.WriteLine($"\n****The following information will be used to update {userName}");
// Get updated parameters
UserAccount newEditUser = new UserAccount(userName,
newCredentials.GetPassword(), newCredentials.GetEmail(),
newCredentials.GetRole(), DateTime.UtcNow);
bool editValid = accountService.EditUserRecord(newEditUser, user.username, dbSource);
if (editValid is true)
{
Console.WriteLine("\nAccount updated Successfully");
break;
}
else
{
return "Database Timed out";
}
break; break;
// Delete account // Delete account
case 3: case 3:
UserAccount newDeleteUser = new UserAccount(newCredentials.GetUserName(), UserAccount newDeleteUser = new UserAccount(newCredentials.GetUserName(),
newCredentials.GetPassword(), DateTime.UtcNow); newCredentials.GetPassword(), DateTime.UtcNow);
accountService.DeleteUserRecord(newDeleteUser, dbSource); bool deleteValid = accountService.DeleteUserRecord(newDeleteUser, user.username , dbSource);
if (deleteValid is true)
{
Console.WriteLine("\nAccount deleted Successfully");
break;
}
else
{
return "User does not exist";
}
break; break;
// Disable account // Disable account
case 4: case 4:
UserAccount newDisableUser = new UserAccount(newCredentials.GetUserName(), UserAccount newDisableUser = new UserAccount(newCredentials.GetUserName(),
newCredentials.GetPassword(), DateTime.UtcNow); newCredentials.GetRole());
accountService.DisableUser(newDisableUser, dbSource); bool disableValid = accountService.DisableUser(newDisableUser, user.username , dbSource);
if (disableValid is true)
{
Console.WriteLine("\nAccount disabled Successfully");
break;
}
else
{
return "Database Timed out";
}
break; break;
// Enable account // Enable account
case 5: case 5:
UserAccount newEnableUser = new UserAccount(newCredentials.GetUserName(), UserAccount newEnableUser = new UserAccount(newCredentials.GetUserName(),
newCredentials.GetPassword(), DateTime.UtcNow); newCredentials.GetRole());
accountService.EnableUser(newEnableUser, dbSource); bool enableValid = accountService.EnableUser(newEnableUser, user.username , dbSource);
if (enableValid is true)
{
Console.WriteLine("\nAccount enabled Successfully");
break;
}
else
{
return "Database Timed out";
}
break; break;
// View logs // View logs
case 6: case 6:

View File

@ -52,6 +52,12 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
_password = pwd; _password = pwd;
_Time = TimeStamp; _Time = TimeStamp;
} }
public UserAccount(string un, string role)
{
_userName = un;
_Role = role;
_Time = DateTime.UtcNow;
}
public UserAccount(string newUN, string newPWD, string Email, string role, DateTime newTime) public UserAccount(string newUN, string newPWD, string Email, string role, DateTime newTime)
{ {
_userName = newUN; _userName = newUN;

View File

@ -95,29 +95,6 @@ namespace TeamHobby.HobbyProjectGenerator.Main
} }
} }
//Console.WriteLine(value: $"Welcome {username}\n");
/* string sqlQuery = "Select * from log;";
Object result = datasource.ReadData(sqlQuery);
Console.WriteLine("type of Result: " + result.GetType());
OdbcDataReader reader = null;
if (result.GetType() == typeof(OdbcDataReader))
{
reader = (OdbcDataReader)result;
}
Console.WriteLine("Reading from the database");
while (reader.Read())
{
Console.WriteLine("Date={0} {1} {2} {3} {4} {5}", reader[0], reader[1], reader[2], reader[3], reader[4], reader[5]);
}
SqlDAO sqlDS = (SqlDAO)datasource;
Console.WriteLine("");
// Closing the connection
sqlDS.getConnection().Close();*/
/* // While loop to keep this running forever with UserManagement /* // While loop to keep this running forever with UserManagement
// Testing archive Manager // Testing archive Manager