Finished isAdmin, working on submenu
This commit is contained in:
parent
c7815d215c
commit
f67b172f8d
@ -3,8 +3,10 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data.Odbc;
|
||||
using TeamHobby.HobbyProjectGenerator.UserManagement;
|
||||
using TeamHobby.HobbyProjectGenerator.Implementations;
|
||||
using TeamHobby.HobbyProjectGenerator.DataAccess;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.UserManagement
|
||||
{
|
||||
@ -51,17 +53,88 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
|
||||
}
|
||||
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)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
public void CreateUserRecord(UserAccount user)
|
||||
{
|
||||
Console.Write(IsInputValid(user.username, user.password));
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
public string CreateUserRecord(UserAccount user, IDataSource<string> dbSource)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TeamHobby.HobbyProjectGenerator.DataAccess\TeamHobby.HobbyProjectGenerator.DataAccess.csproj" />
|
||||
<ProjectReference Include="..\TeamHobby.HobbyProjectGenerator\TeamHobby.HobbyProjectGenerator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -6,37 +6,52 @@ using System.Threading.Tasks;
|
||||
|
||||
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
|
||||
{
|
||||
// Admin Credentials
|
||||
private string UserName;
|
||||
private string Password;
|
||||
private DateTime Time;
|
||||
private string _userName;
|
||||
private string _password;
|
||||
private DateTime _logginTime;
|
||||
|
||||
public UserAccount(string un, string pwd, DateTime TimeStamp)
|
||||
{
|
||||
UserName = un;
|
||||
Password = pwd;
|
||||
Time = TimeStamp;
|
||||
_userName = un;
|
||||
_password = pwd;
|
||||
_logginTime = TimeStamp;
|
||||
}
|
||||
public string username { get { return UserName; } }
|
||||
public string password { get { return Password; } }
|
||||
public string username { get { return _userName; } }
|
||||
public string password { get { return _password; } }
|
||||
|
||||
// New User Credentials
|
||||
private string newusername;
|
||||
private string newpassword;
|
||||
private string newemail;
|
||||
private DateTime newtime;
|
||||
public void NewUser(string newUN, string newPWD, string Email, DateTime newTime)
|
||||
private string _newUserName;
|
||||
private string _newPassword;
|
||||
private string _newEmail;
|
||||
private DateTime _newTime;
|
||||
public UserAccount(string newUN, string newPWD, string Email, DateTime newTime)
|
||||
{
|
||||
newusername = newUN;
|
||||
newpassword = newPWD;
|
||||
newemail = Email;
|
||||
newtime = newTime;
|
||||
_newUserName = newUN;
|
||||
_newPassword = newPWD;
|
||||
_newEmail = Email;
|
||||
_newTime = newTime;
|
||||
}
|
||||
public string NewUserName { get { return newusername; } }
|
||||
public string NewPassword { get { return newpassword; } }
|
||||
public string NewEmail { get { return newemail; } }
|
||||
public DateTime Newtime { get { return newtime;} }
|
||||
public string NewUserName { get { return _newUserName; } }
|
||||
public string NewPassword { get { return _newPassword; } }
|
||||
public string NewEmail { get { return _newEmail; } }
|
||||
public DateTime Newtime { get { return _newTime; } }
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,26 +7,11 @@ using TeamHobby.HobbyProjectGenerator.UserManagement;
|
||||
|
||||
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 static void Main(string[] args)
|
||||
{
|
||||
/*// Creating the Factory class
|
||||
// Creating the Factory class
|
||||
string dbType = "sql";
|
||||
RDSFactory factory = new RDSFactory();
|
||||
|
||||
@ -37,7 +22,7 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
"UID=root;" +
|
||||
"PASSWORD=Teamhobby;" +
|
||||
"OPTION=3";
|
||||
IDataSource<string> datasource = factory.getDataSource(dbType, dbInfo);*/
|
||||
IDataSource<string> datasource = factory.getDataSource(dbType, dbInfo);
|
||||
|
||||
// Create manager class from UserManagement
|
||||
SystemAccountManager manager = new SystemAccountManager();
|
||||
@ -53,11 +38,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
// Create UserAccount class
|
||||
UserAccount user = new UserAccount(username, password, TimeStamp);
|
||||
|
||||
manager.CreateUserRecord(user);
|
||||
Console.Write(manager.CreateUserRecord(user, datasource));
|
||||
|
||||
//Console.WriteLine(value: $"Welcome {username}\n");
|
||||
|
||||
/* string sqlQuery = "Select * from log;";
|
||||
/* string sqlQuery = "Select * from log;";
|
||||
Object result = datasource.ReadData(sqlQuery);
|
||||
Console.WriteLine("type of Result: " + result.GetType());
|
||||
OdbcDataReader reader = null;
|
||||
@ -79,18 +64,19 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
// Closing the connection
|
||||
sqlDS.getConnection().Close();*/
|
||||
|
||||
// While loop to keep this running forever with UserManagement
|
||||
// Testing archive Manager
|
||||
//while (true)
|
||||
//{
|
||||
/* string currentDate = DateTime.Now.ToString("dd");
|
||||
string currentTime = DateTime.Now.ToString("T");
|
||||
Console.WriteLine("Current date: {0}, Current Time: {1}", currentDate, currentTime);
|
||||
if (String.Equals(currentDate, "1") && String.Equals(currentTime, "00:00:00 AM")){
|
||||
ArchiveManager archive = new ArchiveManager(datasource);
|
||||
archive.Controller();
|
||||
}*/
|
||||
//}
|
||||
/* // While loop to keep this running forever with UserManagement
|
||||
// Testing archive Manager
|
||||
//while (true)
|
||||
//{
|
||||
string currentDate = DateTime.Now.ToString("dd");
|
||||
string currentTime = DateTime.Now.ToString("T");
|
||||
Console.WriteLine("Current date: {0}, Current Time: {1}", currentDate, currentTime);
|
||||
if (String.Equals(currentDate, "1") && String.Equals(currentTime, "00:00:00 AM"))
|
||||
{
|
||||
ArchiveManager archive = new ArchiveManager(datasource);
|
||||
archive.Controller();
|
||||
}
|
||||
//}*/
|
||||
|
||||
|
||||
|
||||
@ -139,80 +125,80 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
//datasource.WriteData(sqlRemove);
|
||||
//Console.WriteLine("Writing completed. ");
|
||||
|
||||
/* bool MainMenu = true;
|
||||
/* bool MainMenu = true;
|
||||
|
||||
// Set up menu loop
|
||||
while (MainMenu == true)
|
||||
{
|
||||
// Console customization
|
||||
// Change the look of the console
|
||||
Console.Title = "HobbyProjectGenerator";
|
||||
// Change console text color
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
// Change terminal height
|
||||
Console.WindowHeight = 40;
|
||||
// Set up menu loop
|
||||
while (MainMenu == true)
|
||||
{
|
||||
// Console customization
|
||||
// Change the look of the console
|
||||
Console.Title = "HobbyProjectGenerator";
|
||||
// Change console text color
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
// Change terminal height
|
||||
Console.WindowHeight = 40;
|
||||
|
||||
|
||||
// Create class objects
|
||||
UiPrint menu = new UiPrint();
|
||||
// Create class objects
|
||||
UiPrint menu = new UiPrint();
|
||||
|
||||
|
||||
// Print main menu
|
||||
menu.InitialMenu();
|
||||
// Print main menu
|
||||
menu.InitialMenu();
|
||||
|
||||
// Set up try-catch for invalid inputs
|
||||
try
|
||||
{
|
||||
// Get user choice
|
||||
string initialChoice = Console.ReadLine();
|
||||
// Convert to integer
|
||||
int Choice = Convert.ToInt32(initialChoice);
|
||||
// Set up try-catch for invalid inputs
|
||||
try
|
||||
{
|
||||
// Get user choice
|
||||
string initialChoice = Console.ReadLine();
|
||||
// Convert to integer
|
||||
int Choice = Convert.ToInt32(initialChoice);
|
||||
|
||||
switch (Choice)
|
||||
{
|
||||
case 0:
|
||||
MainMenu = false;
|
||||
break;
|
||||
// Create a new account
|
||||
case 1:
|
||||
user.newUser();
|
||||
break;
|
||||
// Access Admin features
|
||||
case 2:
|
||||
// Ask for Admin login credentials
|
||||
Console.WriteLine("Please enter a username:");
|
||||
string AdminUser = Console.ReadLine();
|
||||
Console.WriteLine("Please enter the password for" + AdminUser);
|
||||
string AdminPsswrd = Console.ReadLine();
|
||||
switch (Choice)
|
||||
{
|
||||
case 0:
|
||||
MainMenu = false;
|
||||
break;
|
||||
// Create a new account
|
||||
case 1:
|
||||
user.newUser();
|
||||
break;
|
||||
// Access Admin features
|
||||
case 2:
|
||||
// Ask for Admin login credentials
|
||||
Console.WriteLine("Please enter a username:");
|
||||
string AdminUser = Console.ReadLine();
|
||||
Console.WriteLine("Please enter the password for" + AdminUser);
|
||||
string AdminPsswrd = Console.ReadLine();
|
||||
|
||||
// Check if the username and password match a record within the administrators
|
||||
// Check if the username and password match a record within the administrators
|
||||
|
||||
|
||||
// Show new administrator menu
|
||||
int AdminNum = 0;
|
||||
Console.WriteLine("Welcome" + AdminUser);
|
||||
Console.WriteLine("What would you like to do?");
|
||||
Console.WriteLine("1.View normal user records.");
|
||||
Console.WriteLine("2.View Administrator user records.");
|
||||
Console.WriteLine("3.View log files.");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
// Show new administrator menu
|
||||
int AdminNum = 0;
|
||||
Console.WriteLine("Welcome" + AdminUser);
|
||||
Console.WriteLine("What would you like to do?");
|
||||
Console.WriteLine("1.View normal user records.");
|
||||
Console.WriteLine("2.View Administrator user records.");
|
||||
Console.WriteLine("3.View log files.");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("");
|
||||
|
||||
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Invalid choice, please enter a valid number.");
|
||||
break;
|
||||
};
|
||||
}
|
||||
// Catch invalid keys such as spamming enter
|
||||
catch
|
||||
{
|
||||
MainMenu = false;
|
||||
};*/
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Invalid choice, please enter a valid number.");
|
||||
break;
|
||||
};
|
||||
}
|
||||
// Catch invalid keys such as spamming enter
|
||||
catch
|
||||
{
|
||||
MainMenu = false;
|
||||
};*/
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user