Finished isAdmin, working on submenu

This commit is contained in:
Im_Alpha 2021-12-13 06:29:13 -08:00
parent c7815d215c
commit f67b172f8d
4 changed files with 198 additions and 123 deletions

View File

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Data.Odbc;
using TeamHobby.HobbyProjectGenerator.UserManagement; using TeamHobby.HobbyProjectGenerator.UserManagement;
using TeamHobby.HobbyProjectGenerator.Implementations; using TeamHobby.HobbyProjectGenerator.Implementations;
using TeamHobby.HobbyProjectGenerator.DataAccess;
namespace TeamHobby.HobbyProjectGenerator.UserManagement namespace TeamHobby.HobbyProjectGenerator.UserManagement
{ {
@ -51,17 +53,88 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
} }
else else
{ {
return "Invalid Input\n"; return "Invalid input\n";
} }
} }
public bool isAdmin() // Check with database if user is an admin
public bool isAdmin(UserAccount user, IDataSource<string> dbSource)
{
// select r.Role from roles r, users u where UserName = '{user.username}' and Password = '{user.password}' and r.RoleID = u.RoleID;
// string checkAdmin = $"Select * from users where username = {user.username} and password = {user.password};";
string checkAdmin = $"select r.Role from roles r, users u where " +
$"UserName = '{user.username}' and Password = '{user.password}' and r.RoleID = u.RoleID;";
Object confirmAdmin = dbSource.ReadData(checkAdmin);
//Console.WriteLine("type of Reesult:" + confirmAdmin.GetType());
OdbcDataReader reader = null;
if (confirmAdmin.GetType() == typeof(OdbcDataReader))
{
reader = (OdbcDataReader)confirmAdmin;
}
// Create String to hold sql output
string checkSql = "";
// Read Sql query results
while (reader.Read())
{
checkSql = reader.GetString(0);
}
SqlDAO sqlDS = (SqlDAO)dbSource;
Console.WriteLine("");
// Closing the connection
sqlDS.getConnection().Close();
if (checkSql == "Admin")
{
return true;
}
else
{ {
return false; return false;
} }
public void CreateUserRecord(UserAccount user) }
public string CreateUserRecord(UserAccount user, IDataSource<string> dbSource)
{ {
Console.Write(IsInputValid(user.username, user.password)); // Check Login inputs
IsInputValid(user.username, user.password);
bool Admin = isAdmin(user, dbSource);
// Give access if the user is and Admin
if (Admin is false)
{
return "Access Denied: Unauthorized\n";
}
else
{
// db.users layout (UserName, Password, RoleID, IsActive, CreatedBy, CreatedDate)
// db.roles layout (RoleID(AutoGen), Role, CreatedBy, CreatedDate)
// Menu for all UserManagement options
int menu = 0;
Console.WriteLine($"Welcome {user.username} to User Management.\n");
Console.WriteLine("What would you like to do?\n");
Console.WriteLine((menu + 1) + ". Create a new account.");
Console.WriteLine((menu + 1) + ". Edit an account.");
Console.WriteLine((menu + 1) + ". Delete an account.");
Console.WriteLine((menu + 1) + ". Disable an account.");
Console.WriteLine((menu + 1) + ". Enable an account.");
// Notify user of new credentials to be input
Console.WriteLine("Please enter the new user information:\n");
GetCredentials credentials = new GetCredentials();
string dbAction = user.NewUserName;
return dbAction;
}
} }

View File

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\TeamHobby.HobbyProjectGenerator.DataAccess\TeamHobby.HobbyProjectGenerator.DataAccess.csproj" />
<ProjectReference Include="..\TeamHobby.HobbyProjectGenerator\TeamHobby.HobbyProjectGenerator.csproj" /> <ProjectReference Include="..\TeamHobby.HobbyProjectGenerator\TeamHobby.HobbyProjectGenerator.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -6,37 +6,52 @@ using System.Threading.Tasks;
namespace TeamHobby.HobbyProjectGenerator.UserManagement namespace TeamHobby.HobbyProjectGenerator.UserManagement
{ {
public class GetCredentials
{
public string? GetUserName()
{
Console.WriteLine("Please enter a username:");
string? userName = Console.ReadLine();
return userName;
}
public string? GetPassword()
{
Console.WriteLine("Please enter a password:");
string? userPassword = Console.ReadLine();
return userPassword;
}
}
public class UserAccount public class UserAccount
{ {
// Admin Credentials // Admin Credentials
private string UserName; private string _userName;
private string Password; private string _password;
private DateTime Time; private DateTime _logginTime;
public UserAccount(string un, string pwd, DateTime TimeStamp) public UserAccount(string un, string pwd, DateTime TimeStamp)
{ {
UserName = un; _userName = un;
Password = pwd; _password = pwd;
Time = TimeStamp; _logginTime = TimeStamp;
} }
public string username { get { return UserName; } } public string username { get { return _userName; } }
public string password { get { return Password; } } public string password { get { return _password; } }
// New User Credentials // New User Credentials
private string newusername; private string _newUserName;
private string newpassword; private string _newPassword;
private string newemail; private string _newEmail;
private DateTime newtime; private DateTime _newTime;
public void NewUser(string newUN, string newPWD, string Email, DateTime newTime) public UserAccount(string newUN, string newPWD, string Email, DateTime newTime)
{ {
newusername = newUN; _newUserName = newUN;
newpassword = newPWD; _newPassword = newPWD;
newemail = Email; _newEmail = Email;
newtime = newTime; _newTime = newTime;
} }
public string NewUserName { get { return newusername; } } public string NewUserName { get { return _newUserName; } }
public string NewPassword { get { return newpassword; } } public string NewPassword { get { return _newPassword; } }
public string NewEmail { get { return newemail; } } public string NewEmail { get { return _newEmail; } }
public DateTime Newtime { get { return newtime;} } public DateTime Newtime { get { return _newTime; } }
} }
} }

View File

@ -7,26 +7,11 @@ using TeamHobby.HobbyProjectGenerator.UserManagement;
namespace TeamHobby.HobbyProjectGenerator.Main namespace TeamHobby.HobbyProjectGenerator.Main
{ {
public class GetCredentials
{
public string? GetUserName()
{
Console.WriteLine("Please enter a username:");
string? userName = Console.ReadLine();
return userName;
}
public string? GetPassword()
{
Console.WriteLine("Please enter a password:");
string? userPassword = Console.ReadLine();
return userPassword;
}
}
public class Controller public class Controller
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
/*// Creating the Factory class // Creating the Factory class
string dbType = "sql"; string dbType = "sql";
RDSFactory factory = new RDSFactory(); RDSFactory factory = new RDSFactory();
@ -37,7 +22,7 @@ namespace TeamHobby.HobbyProjectGenerator.Main
"UID=root;" + "UID=root;" +
"PASSWORD=Teamhobby;" + "PASSWORD=Teamhobby;" +
"OPTION=3"; "OPTION=3";
IDataSource<string> datasource = factory.getDataSource(dbType, dbInfo);*/ IDataSource<string> datasource = factory.getDataSource(dbType, dbInfo);
// Create manager class from UserManagement // Create manager class from UserManagement
SystemAccountManager manager = new SystemAccountManager(); SystemAccountManager manager = new SystemAccountManager();
@ -53,7 +38,7 @@ namespace TeamHobby.HobbyProjectGenerator.Main
// Create UserAccount class // Create UserAccount class
UserAccount user = new UserAccount(username, password, TimeStamp); UserAccount user = new UserAccount(username, password, TimeStamp);
manager.CreateUserRecord(user); Console.Write(manager.CreateUserRecord(user, datasource));
//Console.WriteLine(value: $"Welcome {username}\n"); //Console.WriteLine(value: $"Welcome {username}\n");
@ -79,18 +64,19 @@ namespace TeamHobby.HobbyProjectGenerator.Main
// Closing the connection // Closing the connection
sqlDS.getConnection().Close();*/ 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
//while (true) //while (true)
//{ //{
/* string currentDate = DateTime.Now.ToString("dd"); string currentDate = DateTime.Now.ToString("dd");
string currentTime = DateTime.Now.ToString("T"); string currentTime = DateTime.Now.ToString("T");
Console.WriteLine("Current date: {0}, Current Time: {1}", currentDate, currentTime); Console.WriteLine("Current date: {0}, Current Time: {1}", currentDate, currentTime);
if (String.Equals(currentDate, "1") && String.Equals(currentTime, "00:00:00 AM")){ if (String.Equals(currentDate, "1") && String.Equals(currentTime, "00:00:00 AM"))
{
ArchiveManager archive = new ArchiveManager(datasource); ArchiveManager archive = new ArchiveManager(datasource);
archive.Controller(); archive.Controller();
}*/ }
//} //}*/