From cbb3b404c0af0f423ceb5438d979755c8f5c2c8a Mon Sep 17 00:00:00 2001 From: batgo6o Date: Mon, 15 Mar 2021 16:31:45 +0200 Subject: [PATCH] Added Validator and User Controller --- .../UserManagment/CreateInitialUser.cs | 30 +++++++++++------ .../Business/UserManagment/UserController.cs | 32 +++++++++++++++++++ .../{CredentialChecker.cs => Validator.cs} | 0 LuminousSales/Display/Program.cs | 2 +- 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 LuminousSales/Business/Business/UserManagment/UserController.cs rename LuminousSales/Business/Business/UserManagment/{CredentialChecker.cs => Validator.cs} (100%) diff --git a/LuminousSales/Business/Business/UserManagment/CreateInitialUser.cs b/LuminousSales/Business/Business/UserManagment/CreateInitialUser.cs index 75d6a8d..48533e8 100644 --- a/LuminousSales/Business/Business/UserManagment/CreateInitialUser.cs +++ b/LuminousSales/Business/Business/UserManagment/CreateInitialUser.cs @@ -9,13 +9,25 @@ namespace Business.Business.UserManagment public class CreateInitialUser { private LuminousContext context; + private string RoleName; + private string Username; + private string Password; + private UserController userctl; + public CreateInitialUser(string RoleName, string Username, string Password) + { + userctl = new UserController(); + this.RoleName = RoleName; + this.Username = Username; + this.Password = Password; + } public void CreatePermissions() { + using (context = new LuminousContext()) { var admin = new Permission("Admin"); - var roleChanger = new Permission("Role Changer"); - var userCreation = new Permission("User Creation"); + var roleChanger = new Permission("Role Creator"); + var userCreation = new Permission("User Creator"); var report = new Permission("Report"); var stock = new Permission("Stock"); var sell = new Permission("Sell"); @@ -33,22 +45,20 @@ namespace Business.Business.UserManagment } } - public void CreateFirstRole(string Name, ICollection Permissions) + public void CreateFirstRole() { using (context = new LuminousContext()) { - var firstRole = new Role(Name, Permissions); - context.Role.Add(firstRole); - context.SaveChanges(); + var AdminRole = context.Permission.Where(p => p.Name == "Admin").FirstOrDefault(); + userctl.CreateRole(this.RoleName , new List { AdminRole }); } } - public void CreateFirstUser(string Name, string Password, Role Role) + public void CreateFirstUser() { using (context = new LuminousContext()) { - var firstUser = new User(Name, Password, Role); - context.User.Add(firstUser); - context.SaveChanges(); + var roleToAttach = context.Role.Where(r => r.Name == this.RoleName).FirstOrDefault(); + userctl.CreateUser(this.Username, this.Password, roleToAttach); } } diff --git a/LuminousSales/Business/Business/UserManagment/UserController.cs b/LuminousSales/Business/Business/UserManagment/UserController.cs new file mode 100644 index 0000000..360c89b --- /dev/null +++ b/LuminousSales/Business/Business/UserManagment/UserController.cs @@ -0,0 +1,32 @@ +using Models; +using Models.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Business.Business.UserManagment +{ + public class UserController + { + private LuminousContext context; + public void CreateRole(string RoleName, ICollection Permissions) + { + using (context = new LuminousContext()) + { + var firstRole = new Role(RoleName, Permissions); + context.Role.Add(firstRole); + context.SaveChanges(); + } + } + public void CreateUser(string Username, string Password, Role Role) + { + using (context = new LuminousContext()) + { + var firstUser = new User(Username, Password, Role); + context.User.Add(firstUser); + context.SaveChanges(); + } + } + } +} diff --git a/LuminousSales/Business/Business/UserManagment/CredentialChecker.cs b/LuminousSales/Business/Business/UserManagment/Validator.cs similarity index 100% rename from LuminousSales/Business/Business/UserManagment/CredentialChecker.cs rename to LuminousSales/Business/Business/UserManagment/Validator.cs diff --git a/LuminousSales/Display/Program.cs b/LuminousSales/Display/Program.cs index 724979c..13bdff3 100644 --- a/LuminousSales/Display/Program.cs +++ b/LuminousSales/Display/Program.cs @@ -7,7 +7,7 @@ namespace Display { static void Main(string[] args) { - var InitialCreation = new CreateInitialUser(); + var InitialCreation = new CreateInitialUser("Admin", "Admin", "pass123"); } } }