Models Updated
This commit is contained in:
@@ -9,55 +9,31 @@ 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)
|
||||
public CreateInitialUser(string Username, string Password)
|
||||
{
|
||||
userctl = new UserController();
|
||||
this.RoleName = RoleName;
|
||||
this.Username = Username;
|
||||
this.Password = Password;
|
||||
}
|
||||
public void CreatePermissions()
|
||||
public void CreateRoles()
|
||||
{
|
||||
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var admin = new Permission("Admin");
|
||||
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");
|
||||
context.Permission.AddRange
|
||||
(
|
||||
admin,
|
||||
roleChanger,
|
||||
userCreation,
|
||||
report,
|
||||
stock,
|
||||
sell
|
||||
);
|
||||
var Admin = new Role("Admin");
|
||||
var Manager = new Role("Manager");
|
||||
var Cashier = new Role("Cashier");
|
||||
context.Role.AddRange(Admin, Manager, Cashier);
|
||||
context.SaveChanges();
|
||||
Console.WriteLine("Permissions were intialized");
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateFirstRole()
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var AdminPermission = context.Permission.FirstOrDefault(p => p.Name == "Admin");
|
||||
userctl.CreateRole(this.RoleName , AdminPermission);
|
||||
}
|
||||
}
|
||||
public void CreateFirstUser()
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var roleToAttach = context.Role.Where(r => r.Name == this.RoleName).FirstOrDefault();
|
||||
int roleToAttach = context.Role.FirstOrDefault(r => r.Name == "Admin").Id;
|
||||
userctl.CreateUser(this.Username, this.Password, roleToAttach);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using Data.Models;
|
||||
using Models;
|
||||
using Models;
|
||||
using Models.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -12,26 +11,11 @@ namespace Business.Business.UserManagment
|
||||
public class UserController
|
||||
{
|
||||
private LuminousContext context;
|
||||
public void CreateRole(string RoleName, Permission Permission)
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var role = new Role(RoleName);
|
||||
var relationship = new RolePermission();
|
||||
relationship.Roles = role;
|
||||
relationship.Permission = Permission;
|
||||
role.Permissions.Add(relationship);
|
||||
Permission.Role.Add(relationship);
|
||||
context.RolePermission.Add(relationship);
|
||||
context.Role.Add(role);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
public void CreateUser(string Username, string Password, Role Role)
|
||||
public void CreateUser(string Username, string Password, int RoleId)
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var user = new User(Username, Password, Role);
|
||||
var user = new User(Username, Password, RoleId);
|
||||
context.User.Add(user);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
@@ -10,26 +10,24 @@ namespace Business.Business.UserManagment
|
||||
public class UserValidator
|
||||
{
|
||||
private LuminousContext context;
|
||||
public bool CheckIfUserIsCreated()
|
||||
public void CheckIfUserEverCreated()
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
if (context.User.ToList().Any())
|
||||
{
|
||||
return true;
|
||||
throw new ArgumentException("First user is already created!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool CheckPassword(string Password)
|
||||
public void CheckPassword(string Password)
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
if (context.User.ToList().Exists(user => user.Password == Password))
|
||||
{
|
||||
return true;
|
||||
throw new ArgumentException("Invalid User!");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user