From c100a73b39e418da31395aba45234bb8c44133ea Mon Sep 17 00:00:00 2001 From: Im_Alpha Date: Mon, 13 Dec 2021 20:09:07 -0800 Subject: [PATCH] fixed create account sql and edit account --- Source Code/.vscode/launch.json | 26 +++++++ Source Code/.vscode/tasks.json | 42 ++++++++++ .../AccountService.cs | 45 ++++++----- .../SystemAccountManager.cs | 77 ++----------------- .../UserAccount.cs | 46 +++++------ 5 files changed, 125 insertions(+), 111 deletions(-) create mode 100644 Source Code/.vscode/launch.json create mode 100644 Source Code/.vscode/tasks.json diff --git a/Source Code/.vscode/launch.json b/Source Code/.vscode/launch.json new file mode 100644 index 0000000..c4a836b --- /dev/null +++ b/Source Code/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/main/bin/Debug/net6.0/main.dll", + "args": [], + "cwd": "${workspaceFolder}/main", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/Source Code/.vscode/tasks.json b/Source Code/.vscode/tasks.json new file mode 100644 index 0000000..2754b32 --- /dev/null +++ b/Source Code/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/main/main.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/main/main.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/main/main.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs index 8a37d9c..198f16c 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs @@ -10,23 +10,39 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { public class AccountService { - public bool CreateUserRecord(UserAccount newUser, IDataSource dbSource) + public bool CreateUserRecord(UserAccount newUser, string CreatedBy, IDataSource dbSource) { - // string checkAdmin = $"Select * from users where username = {user.username} and password = {user.password};"; - // Insert into users table - string sqlUser = $"INSERT INTO hobby.users (UserName, Password," + - $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.NewUserName}', " + - $"'{newUser.NewPassword}','{newUser.username}', NOW(),'{newUser.NewEmail}');"; - + 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}');"; + + bool insertNewUser = dbSource.WriteData(sqlUser); + return insertNewUser; + + } + catch (Exception ex) + { + return false; + } + } + public bool EditUserRecord(UserAccount newUser, string CreatedBy, IDataSource dbSource) + { +/* // 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 - string sqlRoles = $"INSERT INTO hobby.roles (Role, CreatedBy, CreatedDate) " + - $"VALUES ('{newUser.NewRole}', '{newUser.username}', NOW());"; + string sqlRoles = $"INSERT INTO roles (Role, CreatedBy, CreatedDate) " + + $"VALUES ('{newUser.NewRole}', '{CreatedBy}', NOW());"; Object insertNewRole = dbSource.WriteData(sqlUser); // Create string for confirming user account string confirmUser = $"Select * from users where username = {newUser.NewUserName} " + $"and password = {newUser.NewPassword};"; - Object conUser = dbSource.WriteData(confirmUser); + Object conUser = dbSource.ReadData(confirmUser); //Console.WriteLine("type of Reesult:" + confirmAdmin.GetType()); OdbcDataReader reader = null; @@ -36,9 +52,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement reader = (OdbcDataReader)conUser; } - // Create String to hold sql output - string checkSql = ""; - // Read Sql query results while (reader.Read()) { @@ -49,11 +62,7 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement Console.WriteLine(""); // Closing the connection - sqlDS.getConnection().Close(); - return true; - } - public bool EditUserRecord(UserAccount newUser, IDataSource dbSource) - { + sqlDS.getConnection().Close();*/ return true; } public bool DeleteUserRecord(UserAccount newUser, IDataSource dbSource) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs index 47ab78a..28d621e 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs @@ -148,7 +148,7 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { // 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;"; + $"UserName = '{user.username}' and Password = '{user.password}' and r.Role = u.Role;"; Object confirmAdmin = dbSource.ReadData(checkAdmin); //Console.WriteLine("type of Reesult:" + confirmAdmin.GetType()); OdbcDataReader reader = null; @@ -223,14 +223,14 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement case 1: UserAccount newUser = new UserAccount(newCredentials.GetUserName(), newCredentials.GetPassword(), newCredentials.GetEmail(), - newCredentials.GetRole(), DateTime.UtcNow); - accountService.CreateUserRecord(newUser, dbSource); + newCredentials.GetRole(), DateTime.UtcNow); + accountService.CreateUserRecord(newUser,user.username, dbSource); break; // Edit account case 2: - UserAccount newEditUser = new UserAccount(newCredentials.GetUserName(), + /* UserAccount newEditUser = new UserAccount(newCredentials.GetUserName(), newCredentials.GetPassword(), DateTime.UtcNow); - accountService.EditUserRecord(newEditUser, dbSource); + accountService.EditUserRecord(newEditUser, dbSource);*/ break; // Delete account case 3: @@ -262,72 +262,9 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement } } - string dbAction = user.NewUserName; + string dbAction = user.username; return dbAction; } - } - - - - /*public NewUserName() - { - - } - public NewPassword() - { - // Create bool value for password confirm loop - bool conPsswrd = true; - // Loop until password is confirmed - while (conPsswrd == true) - { - // Confirm Password - Console.WriteLine("Please re-enter the password:"); - string checkPsswd = Console.ReadLine(); - // Check if passwords match - if (userPassword == checkPsswd) - { - // Get Security question for password reset - Console.WriteLine("Please enter a security question.\n" + - "(EX: What is your favorite food?"); - string SecQuest = Console.ReadLine(); - // Get Security question answer - Console.WriteLine("Please enter the answer for your security question:"); - String SecAnswer = Console.ReadLine(); - - // Call user manager method to create the new user - //int userCreateConfirm = new CreateUser(userName,userPassword,SecQuest,SecAnswer); - - // Check if user creation was successful - /*if (userCreateConfirm = 1) - { - - // Confirm to user that the account has been created - Console.WriteLine("Account created succesfully with the username of" + userName); - } - else - { - - }*/ - /*conPsswrd = false; - } - else - { - Console.WriteLine("Passwords did not match, please try again."); - } - } - return true; - }*/ - - public void AccountController() - { - // Create objects - //UserAccount user = new UserAccount(); - UiPrint ui = new UiPrint(); - - bool foo = true; - - - } - + } } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs index 3911cb4..4b9a38f 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs @@ -20,6 +20,11 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement string? userPassword = Console.ReadLine(); return userPassword; } + /*public string ConfirmPassword() + { + Console.WriteLine(); + string confirmPassword = newUser.NewPassword; + }*/ public string GetEmail() { Console.WriteLine("Please enter an email:"); @@ -36,40 +41,35 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement // Admin Credentials private string _userName; private string _password; - private DateTime _logginTime; + private DateTime _Time; + // New User Credentials + private string _Email; + private string _Role; public UserAccount(string un, string pwd, DateTime TimeStamp) { _userName = un; _password = pwd; - _logginTime = TimeStamp; + _Time = TimeStamp; } - 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 string _newRole; - private DateTime _newTime; public UserAccount(string newUN, string newPWD, string Email, string role, DateTime newTime) { - _newUserName = newUN; - _newPassword = newPWD; - _newRole = role; - _newEmail = Email; - _newTime = newTime; + _userName = newUN; + _password = newPWD; + _Role = role; + _Email = Email; + _Time = newTime; } public UserAccount() { - _newRole = "regular"; - _newTime= DateTime.Now; + _Role = "regular"; + _Time= DateTime.Now; } - public string NewUserName { get { return _newUserName; } } - public string NewPassword { get { return _newPassword; } } - public string NewEmail { get { return _newEmail; } } - public string NewRole { get { return _newRole; } } - public DateTime NewTime { get { return _newTime; } } + + public string username { get { return _userName; } } + public string password { get { return _password; } } + public string email { get { return _Email; } } + public string role { get { return _Role; } } + public DateTime Time { get { return _Time; } } } }