diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs index 6f4a942..de6c7e8 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs @@ -15,14 +15,26 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement try { // Insert into users table - string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + - $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + - $"'{newUser.password}','{newUser.role}', 1," + - $"'{CreatedBy}', NOW(),'{newUser.email}');"; + if (newUser.email == null || newUser.email == "NULL") + { + string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + + $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + + $"'{newUser.password}','{newUser.role}', 1," + + $"'{CreatedBy}', NOW(),{newUser.email});"; - bool insertNewUser = dbSource.WriteData(sqlUser); - return insertNewUser; + bool insertNewUser = dbSource.WriteData(sqlUser); + return insertNewUser; + } + else + { + string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + + $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + + $"'{newUser.password}','{newUser.role}', 1," + + $"'{CreatedBy}', NOW(),'{newUser.email}');"; + bool insertNewUser = dbSource.WriteData(sqlUser); + return insertNewUser; + } } catch (Exception ex) { @@ -34,13 +46,24 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement try { // Insert into users table - string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + - $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + - $"WHERE t.UserName = '{editUser.username}';"; + if (editUser.email == null || editUser.email == "NULL") + { + string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + + $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + + $"WHERE t.UserName = {editUser.username};"; - bool updateNewUser = dbSource.UpdateData(sqlUser); - return updateNewUser; + bool updateNewUser = dbSource.UpdateData(sqlUser); + return updateNewUser; + } + else + { + string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + + $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + + $"WHERE t.UserName = '{editUser.username}';"; + bool updateNewUser = dbSource.UpdateData(sqlUser); + return updateNewUser; + } } catch (Exception ex) { @@ -98,13 +121,181 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement return false; } } - public bool BulkOperation() + public bool BulkOperation(string signedUser, IDataSource dbSource) { - // List choices of operations - List ops = new List {"Create Account", "Edit Account", - "Delete Account", "Disable Account", "Enable Account"}; - - return true; + try + { + // Get the current directory. + string path = Directory.GetCurrentDirectory(); + // Open file reader stream + using StreamReader fileRead = new(path + "\\BulkOps\\Bulk.txt"); // path + "\\BulkOps\\{input}" + // Show location of the file being written + Console.WriteLine(path + "\\BulkOps\\Bulk.txt"); + + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + + // List to hold file operation and credentials + List fileList = new List(); + string operation = ""; + string UN = ""; + string PWD = ""; + string Role = ""; + string Email = ""; + + // Loop through file rows + while (!fileRead.EndOfStream) + { + // Create variable to hold current line + string line = fileRead.ReadLine(); + + // Check if line is not empty and contains the separator + if (line != null && line.Contains(":")) + { + // Assign value to hold current line parameter + string value = line.Split(':')[1].Trim(); + + // Check what operation is being called + if (ops.Contains(value)) + { + // Add operation into temp + //Console.WriteLine("Found operation"); + //Console.WriteLine(value); + operation = value; + } + else + { + string checkCredential = line.Split(':')[0].Trim(); + //Console.WriteLine("Found credential"); + switch (checkCredential) + { + case "Username": + UN = value; + break; + case "Password": + PWD = value; + break; + case "Role": + Role = value; + break; + case "Email": + Email = value; + break; + default: + break; + } + } + } + else if(line != null) + { + switch (operation) + { + case "Create Account": + //Console.WriteLine(operation); + if (!String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Edit Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Delete Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Disable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Role) && String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Enable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Role) && String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + default: + break; + } + } + } + fileRead.Close(); + return true; + } + catch (Exception e) + { + Console.WriteLine("Error reading file"); + Console.WriteLine(e.Message); + return false; + } } } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs index 2743dd9..4abe273 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs @@ -11,13 +11,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { public class SystemAccountManager { - public bool BulkOperation() - { - // List choices of operations - List ops = new List {"Create Account", "Edit Account", - "Delete Account", "Disable Account", "Enable Account"}; - return true; - } public string IsInputValid(string checkUN, string checkPWD) { // Create bool variables to check if username and password are valid @@ -246,7 +239,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement case 2: // State what account is being edited string userName = newCredentials.GetUserName(); - string userRole = newCredentials.GetRole(); // Notify the user of what can be edited Console.WriteLine($"\n****The following information will be used to update {userName}"); @@ -322,9 +314,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement Console.WriteLine("Please input the name of the file:(Example.txt)"); string filename = $"{path}\\BulkOps\\{Console.ReadLine()}"; - // Read file - FileInfo fileInfo = new FileInfo(filename); - // Get filesize long fileSize = filename.Length; long fileSizeGB = fileSize / (1024 * 1024); @@ -344,10 +333,17 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement else { // Begin bulk operation - bulkOP.BulkOperation(); - - - break; + Console.WriteLine("Running bulk operation..."); + bool bulkReq = bulkOP.BulkOperation(user.username, dbSource); + if (bulkReq is true) + { + Console.WriteLine("\nBulk operation complete"); + break; + } + else + { + return "Bulk operation failed"; + } } } break; diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs index 58d6b13..e048653 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs @@ -47,7 +47,25 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement } public string GetRole() { - Console.WriteLine("Please enter the role of the user:"); + while (true) + { + // List of possible roles + List roles = new List {"Admin", "regular" }; + // Get role + Console.WriteLine("Please enter the role of the user:(Admin or regular)"); + string? userRole = Console.ReadLine(); + // Check if passwords match + if (roles.Contains(userRole)) + { + return userRole; + } + else + { + Console.WriteLine("Not an available role.\n"); + } + } + + return Console.ReadLine(); } } @@ -65,26 +83,62 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { _userName = un; _password = pwd; + _Role = "regular"; + _Email = "NULL"; _Time = TimeStamp; } + public UserAccount(string un, string pwd, string role) + { + if (String.IsNullOrEmpty(role)) + { + _Role = "regular"; + } + else { _Role = role; } + _userName = un; + _password = pwd; + _Email = "NULL"; + _Time = DateTime.UtcNow; + } public UserAccount(string un, string role) { _userName = un; _Role = role; + _Email = "NULL"; _Time = DateTime.UtcNow; } + public UserAccount(string un, string pwd, string Email, DateTime TimeStamp) + { + if (String.IsNullOrEmpty(Email)) + { + _Email = "NULL"; + } + else { _Email = Email; } + _userName = un; + _password = pwd; + _Role = "regular"; + _Time = TimeStamp; + } public UserAccount(string newUN, string newPWD, string Email, string role, DateTime newTime) { + if (String.IsNullOrEmpty(role)) + { + _Role = "regular"; + } + else { _Role = role; } + if (String.IsNullOrEmpty(Email)) + { + _Email = "NULL"; + } + else { _Email = Email; } _userName = newUN; _password = newPWD; - _Role = role; - _Email = Email; _Time = newTime; } public UserAccount() { _Role = "regular"; - _Time= DateTime.Now; + _Email = "NULL"; + _Time = DateTime.Now; } public string username { get { return _userName; } } diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 5aec1be..cb526ab 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -1,12 +1,12 @@ -/*using System; +using System; using System.Data.Odbc; using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; using TeamHobby.HobbyProjectGenerator.UserManagement; namespace TeamHobby.HobbyProjectGenerator.Main -{ - public class Controller +{ + public class Controller { public static void Main(string[] args) @@ -45,8 +45,6 @@ namespace TeamHobby.HobbyProjectGenerator.Main // Get time of login attempt DateTime TimeStamp = DateTime.UtcNow; - - // String for checking query return type string dbType = "sql"; @@ -96,55 +94,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main { Console.WriteLine("******Access Denied: Unauthorized******"); } - + } - - //Creating the folder Archive - //Console.WriteLine("Creating a new folder ..."); - //archive.CreateArchiveFolder(); - //Console.WriteLine(""); - - //// Creating a file name: - //string filePath = @"C:\HobbyArchive"; - ////Console.WriteLine("Creating file name ... "); - ////string curPath = archive.CreateOutFileName(); - //string curPath = archive.CreateOutFileName(filePath); - - ////string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive"; - ////string pathTemp = "C:/Temp/oldlogs10.txt"; - ////string pathTempBack = @"C:\Temp\oldlogs10.txt"; - //Console.WriteLine("----------------"); - - ////Output SQL to a text file - //sqlDS.CopyToFile(curPath); - - //// Compress the file - //sqlDS.CompressFile(curPath); - - ////Remove output file - //sqlDS.RemoveOutputFile(curPath); - - //// Remove entries fromt the database - //sqlDS.RemoveEntries(); - - - // 2.Inserting Data into the database: - //string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " + - // "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');"; - - //Console.WriteLine("Writing to the database... "); - //datasource.WriteData(sqlWrite); - //Console.WriteLine("Writing completed. "); - - // 3. Removing from a database - //string sqlRemove = "DELETE from log where logID = 28;"; - //Console.WriteLine("Writing to the database... "); - //datasource.WriteData(sqlRemove); - //Console.WriteLine("Writing completed. "); } } } -*/ \ No newline at end of file diff --git a/Source Code/main/ExtractBulkOp.cs b/Source Code/main/ExtractBulkOp.cs index c3e18fa..e4396c5 100644 --- a/Source Code/main/ExtractBulkOp.cs +++ b/Source Code/main/ExtractBulkOp.cs @@ -11,8 +11,8 @@ namespace main { public class ExtractBulkOp { - //public void ExtractBulkOP() - public static void Main(string[] args) + public void ExtractBulkOP(string signedUser, IDataSource dbSource) + //public static void Main(string[] args) { try { @@ -27,16 +27,20 @@ namespace main List ops = new List {"Create Account", "Edit Account", "Delete Account", "Disable Account", "Enable Account"}; - // Create Account service object - AccountService serviceTest = new AccountService(); - // List to hold file operation and credentials - List fileList = new List(); + List fileList = new List(); + string operation = ""; + string UN = ""; + string PWD = ""; + string Role = ""; + string Email = ""; + // Loop through file rows while (!fileRead.EndOfStream) { // Create variable to hold current line string line = fileRead.ReadLine(); + // Check if line is not empty and contains the separator if (line != null && line.Contains(":")) { @@ -44,41 +48,110 @@ namespace main string value = line.Split(':')[1].Trim(); // Check what operation is being called - if(ops.Contains(value)) + if (ops.Contains(value)) { - // Declare empty list each time operation is found - string[] temp = new string[] { }; - // Loop until empty line is found - while (true) + // Add operation into temp + //Console.WriteLine("Found operation"); + //Console.WriteLine(value); + operation = value; + } + else + { + string checkCredential = line.Split(':')[0].Trim(); + //Console.WriteLine("Found credential"); + switch (checkCredential) { - string currLine = fileRead.ReadLine(); - // Check if line is not empty - if(currLine != null && currLine.Contains(":")) - { - currLine = currLine.Split(':')[1].Trim(); - if (ops.Contains(currLine)) - { - break; - } - else - { - temp.Append(currLine); - } - } + case "Username": + UN = value; + break; + case "Password": + PWD = value; + break; + case "Role": + Role = value; + break; + case "Email": + Email = value; + break; + default: + break; } - fileList.Add(temp); } } - } - Console.WriteLine(fileList[2]); -/* for (int i = 0; i < fileList.Count; i++) - { - for(int j = 0; j < fileList[i].Length; j++) + switch (operation) { - Console.WriteLine(fileList[i][j]); + case "Create Account": + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role)) + { + Role = null; + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //Console.WriteLine(serviceTest.CreateUserRecord(user, 'Jacob', dbSource)); + } + else if (String.IsNullOrEmpty(Email)) + { + Email = null; + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else + { + // Console.WriteLine(UN); + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + if (user.email == null) + { + Console.WriteLine("wokring correctly"); + } + else + { + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + } + break; + case "Edit Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Delete Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Disable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Enable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + default: + break; + } - }*/ - fileRead.Close(); + } + fileRead.Close(); } catch (Exception e) {