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.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)
|
||||||
{
|
{
|
||||||
return false;
|
// 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};";
|
||||||
public void CreateUserRecord(UserAccount user)
|
string checkAdmin = $"select r.Role from roles r, users u where " +
|
||||||
{
|
$"UserName = '{user.username}' and Password = '{user.password}' and r.RoleID = u.RoleID;";
|
||||||
Console.Write(IsInputValid(user.username, user.password));
|
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>
|
</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>
|
||||||
|
|
||||||
|
|||||||
@ -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; } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,11 +38,11 @@ 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");
|
||||||
|
|
||||||
/* string sqlQuery = "Select * from log;";
|
/* string sqlQuery = "Select * from log;";
|
||||||
Object result = datasource.ReadData(sqlQuery);
|
Object result = datasource.ReadData(sqlQuery);
|
||||||
Console.WriteLine("type of Result: " + result.GetType());
|
Console.WriteLine("type of Result: " + result.GetType());
|
||||||
OdbcDataReader reader = null;
|
OdbcDataReader reader = null;
|
||||||
@ -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);
|
{
|
||||||
archive.Controller();
|
ArchiveManager archive = new ArchiveManager(datasource);
|
||||||
}*/
|
archive.Controller();
|
||||||
//}
|
}
|
||||||
|
//}*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -122,7 +108,7 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
|||||||
//// Remove entries fromt the database
|
//// Remove entries fromt the database
|
||||||
//sqlDS.RemoveEntries();
|
//sqlDS.RemoveEntries();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 2.Inserting Data into the database:
|
// 2.Inserting Data into the database:
|
||||||
@ -139,80 +125,80 @@ namespace TeamHobby.HobbyProjectGenerator.Main
|
|||||||
//datasource.WriteData(sqlRemove);
|
//datasource.WriteData(sqlRemove);
|
||||||
//Console.WriteLine("Writing completed. ");
|
//Console.WriteLine("Writing completed. ");
|
||||||
|
|
||||||
/* bool MainMenu = true;
|
/* bool MainMenu = true;
|
||||||
|
|
||||||
// Set up menu loop
|
// Set up menu loop
|
||||||
while (MainMenu == true)
|
while (MainMenu == true)
|
||||||
{
|
{
|
||||||
// Console customization
|
// Console customization
|
||||||
// Change the look of the console
|
// Change the look of the console
|
||||||
Console.Title = "HobbyProjectGenerator";
|
Console.Title = "HobbyProjectGenerator";
|
||||||
// Change console text color
|
// Change console text color
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
// Change terminal height
|
// Change terminal height
|
||||||
Console.WindowHeight = 40;
|
Console.WindowHeight = 40;
|
||||||
|
|
||||||
|
|
||||||
// Create class objects
|
// Create class objects
|
||||||
UiPrint menu = new UiPrint();
|
UiPrint menu = new UiPrint();
|
||||||
|
|
||||||
|
|
||||||
// Print main menu
|
// Print main menu
|
||||||
menu.InitialMenu();
|
menu.InitialMenu();
|
||||||
|
|
||||||
// Set up try-catch for invalid inputs
|
// Set up try-catch for invalid inputs
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get user choice
|
// Get user choice
|
||||||
string initialChoice = Console.ReadLine();
|
string initialChoice = Console.ReadLine();
|
||||||
// Convert to integer
|
// Convert to integer
|
||||||
int Choice = Convert.ToInt32(initialChoice);
|
int Choice = Convert.ToInt32(initialChoice);
|
||||||
|
|
||||||
switch (Choice)
|
switch (Choice)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
MainMenu = false;
|
MainMenu = false;
|
||||||
break;
|
break;
|
||||||
// Create a new account
|
// Create a new account
|
||||||
case 1:
|
case 1:
|
||||||
user.newUser();
|
user.newUser();
|
||||||
break;
|
break;
|
||||||
// Access Admin features
|
// Access Admin features
|
||||||
case 2:
|
case 2:
|
||||||
// Ask for Admin login credentials
|
// Ask for Admin login credentials
|
||||||
Console.WriteLine("Please enter a username:");
|
Console.WriteLine("Please enter a username:");
|
||||||
string AdminUser = Console.ReadLine();
|
string AdminUser = Console.ReadLine();
|
||||||
Console.WriteLine("Please enter the password for" + AdminUser);
|
Console.WriteLine("Please enter the password for" + AdminUser);
|
||||||
string AdminPsswrd = Console.ReadLine();
|
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
|
// Show new administrator menu
|
||||||
int AdminNum = 0;
|
int AdminNum = 0;
|
||||||
Console.WriteLine("Welcome" + AdminUser);
|
Console.WriteLine("Welcome" + AdminUser);
|
||||||
Console.WriteLine("What would you like to do?");
|
Console.WriteLine("What would you like to do?");
|
||||||
Console.WriteLine("1.View normal user records.");
|
Console.WriteLine("1.View normal user records.");
|
||||||
Console.WriteLine("2.View Administrator user records.");
|
Console.WriteLine("2.View Administrator user records.");
|
||||||
Console.WriteLine("3.View log files.");
|
Console.WriteLine("3.View log files.");
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
Console.WriteLine("");
|
Console.WriteLine("");
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Console.WriteLine("Invalid choice, please enter a valid number.");
|
Console.WriteLine("Invalid choice, please enter a valid number.");
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Catch invalid keys such as spamming enter
|
// Catch invalid keys such as spamming enter
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
MainMenu = false;
|
MainMenu = false;
|
||||||
};*/
|
};*/
|
||||||
//}
|
//}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user