Models Update
This commit is contained in:
@@ -49,8 +49,8 @@ namespace Business.Business.UserManagment
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var AdminRole = context.Permission.Where(p => p.Name == "Admin").FirstOrDefault();
|
||||
userctl.CreateRole(this.RoleName , new List<Permission> { AdminRole });
|
||||
var AdminPermission = context.Permission.FirstOrDefault(p => p.Name == "Admin");
|
||||
userctl.CreateRole(this.RoleName , AdminPermission);
|
||||
}
|
||||
}
|
||||
public void CreateFirstUser()
|
||||
|
@@ -1,7 +1,9 @@
|
||||
using Models;
|
||||
using Data.Models;
|
||||
using Models;
|
||||
using Models.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -10,12 +12,18 @@ namespace Business.Business.UserManagment
|
||||
public class UserController
|
||||
{
|
||||
private LuminousContext context;
|
||||
public void CreateRole(string RoleName, ICollection<Permission> Permissions)
|
||||
public void CreateRole(string RoleName, Permission Permission)
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var firstRole = new Role(RoleName, Permissions);
|
||||
context.Role.Add(firstRole);
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -23,8 +31,8 @@ namespace Business.Business.UserManagment
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
var firstUser = new User(Username, Password, Role);
|
||||
context.User.Add(firstUser);
|
||||
var user = new User(Username, Password, Role);
|
||||
context.User.Add(user);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,36 @@
|
||||
using Models;
|
||||
using Models.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Business.Business.UserManagment
|
||||
{
|
||||
public class UserValidator
|
||||
{
|
||||
private LuminousContext context;
|
||||
public bool CheckIfUserIsCreated()
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
if (context.User.ToList().Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool CheckPassword(string Password)
|
||||
{
|
||||
using (context = new LuminousContext())
|
||||
{
|
||||
if (context.User.ToList().Exists(user => user.Password == Password))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using Business.Business.UserManagment;
|
||||
using Models;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
|
||||
namespace Display
|
||||
{
|
||||
@@ -7,10 +10,16 @@ namespace Display
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var val = new Business.Business.UserManagment.Validator();
|
||||
if ()
|
||||
var val = new UserValidator();
|
||||
if (!val.CheckIfUserIsCreated())
|
||||
{
|
||||
|
||||
var InitialCreation = new CreateInitialUser("Admin", "Admin", "pass123");
|
||||
InitialCreation.CreateFirstRole();
|
||||
InitialCreation.CreateFirstUser();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Already created");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Data;
|
||||
using Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Models.Models;
|
||||
|
||||
@@ -22,6 +23,7 @@ namespace Models
|
||||
public DbSet<Product> Product { get; set; }
|
||||
public DbSet<Deal> Deal { get; set; }
|
||||
public DbSet<Stock> Stock { get; set; }
|
||||
public DbSet<RolePermission> RolePermission { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
@@ -44,6 +46,16 @@ namespace Models
|
||||
modelBuilder.Entity<Product>()
|
||||
.HasIndex(product => new { product.Name })
|
||||
.IsUnique(true);
|
||||
modelBuilder.Entity<RolePermission>()
|
||||
.HasKey(rp => new { rp.RoleId, rp.PermisionId });
|
||||
modelBuilder.Entity<RolePermission>()
|
||||
.HasOne(rp => rp.Roles)
|
||||
.WithMany(rp => rp.Permissions)
|
||||
.HasForeignKey(rp => rp.RoleId);
|
||||
modelBuilder.Entity<RolePermission>()
|
||||
.HasOne(rp => rp.Permission)
|
||||
.WithMany(rp => rp.Role)
|
||||
.HasForeignKey(rp => rp.PermisionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,8 +10,8 @@ using Models;
|
||||
namespace Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(LuminousContext))]
|
||||
[Migration("20210314081427_IntialMigration")]
|
||||
partial class IntialMigration
|
||||
[Migration("20210317183331_InitialMigration")]
|
||||
partial class InitialMigration
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -21,6 +21,21 @@ namespace Data.Migrations
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128)
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
modelBuilder.Entity("Data.Models.RolePermission", b =>
|
||||
{
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PermisionId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("RoleId", "PermisionId");
|
||||
|
||||
b.HasIndex("PermisionId");
|
||||
|
||||
b.ToTable("RolePermission");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Deal", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@@ -55,16 +70,11 @@ namespace Data.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<int?>("RoleId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Permission");
|
||||
});
|
||||
|
||||
@@ -173,6 +183,21 @@ namespace Data.Migrations
|
||||
b.ToTable("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Data.Models.RolePermission", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Permission", "Permission")
|
||||
.WithMany("Role")
|
||||
.HasForeignKey("PermisionId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Models.Models.Role", "Roles")
|
||||
.WithMany("Permissions")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Deal", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.User", "User")
|
||||
@@ -182,13 +207,6 @@ namespace Data.Migrations
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Permission", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Role", null)
|
||||
.WithMany("Permissions")
|
||||
.HasForeignKey("RoleId");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Product", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Deal", null)
|
@@ -3,10 +3,23 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Data.Migrations
|
||||
{
|
||||
public partial class IntialMigration : Migration
|
||||
public partial class InitialMigration : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Permission",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Name = table.Column<string>(nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Permission", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Role",
|
||||
columns: table => new
|
||||
@@ -21,23 +34,27 @@ namespace Data.Migrations
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Permission",
|
||||
name: "RolePermission",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Name = table.Column<string>(nullable: false),
|
||||
RoleId = table.Column<int>(nullable: true)
|
||||
RoleId = table.Column<int>(nullable: false),
|
||||
PermisionId = table.Column<int>(nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Permission", x => x.Id);
|
||||
table.PrimaryKey("PK_RolePermission", x => new { x.RoleId, x.PermisionId });
|
||||
table.ForeignKey(
|
||||
name: "FK_Permission_Role_RoleId",
|
||||
name: "FK_RolePermission_Permission_PermisionId",
|
||||
column: x => x.PermisionId,
|
||||
principalTable: "Permission",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_RolePermission_Role_RoleId",
|
||||
column: x => x.RoleId,
|
||||
principalTable: "Role",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
@@ -141,11 +158,6 @@ namespace Data.Migrations
|
||||
column: "Name",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Permission_RoleId",
|
||||
table: "Permission",
|
||||
column: "RoleId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Product_DealId",
|
||||
table: "Product",
|
||||
@@ -168,6 +180,11 @@ namespace Data.Migrations
|
||||
column: "Name",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_RolePermission_PermisionId",
|
||||
table: "RolePermission",
|
||||
column: "PermisionId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Stock_UserId",
|
||||
table: "Stock",
|
||||
@@ -188,10 +205,10 @@ namespace Data.Migrations
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Permission");
|
||||
name: "Product");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Product");
|
||||
name: "RolePermission");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Deal");
|
||||
@@ -199,6 +216,9 @@ namespace Data.Migrations
|
||||
migrationBuilder.DropTable(
|
||||
name: "Stock");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Permission");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "User");
|
||||
|
@@ -19,6 +19,21 @@ namespace Data.Migrations
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128)
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
modelBuilder.Entity("Data.Models.RolePermission", b =>
|
||||
{
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PermisionId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("RoleId", "PermisionId");
|
||||
|
||||
b.HasIndex("PermisionId");
|
||||
|
||||
b.ToTable("RolePermission");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Deal", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@@ -53,16 +68,11 @@ namespace Data.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)");
|
||||
|
||||
b.Property<int?>("RoleId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Permission");
|
||||
});
|
||||
|
||||
@@ -171,6 +181,21 @@ namespace Data.Migrations
|
||||
b.ToTable("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Data.Models.RolePermission", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Permission", "Permission")
|
||||
.WithMany("Role")
|
||||
.HasForeignKey("PermisionId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("Models.Models.Role", "Roles")
|
||||
.WithMany("Permissions")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Deal", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.User", "User")
|
||||
@@ -180,13 +205,6 @@ namespace Data.Migrations
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Permission", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Role", null)
|
||||
.WithMany("Permissions")
|
||||
.HasForeignKey("RoleId");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Models.Models.Product", b =>
|
||||
{
|
||||
b.HasOne("Models.Models.Deal", null)
|
||||
|
@@ -1,4 +1,8 @@
|
||||
using Data.Base;
|
||||
using Data.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Models.Models
|
||||
{
|
||||
@@ -6,5 +10,7 @@ namespace Models.Models
|
||||
{
|
||||
public Permission() : base(){}
|
||||
public Permission(string Name) : base(Name){}
|
||||
[Required]
|
||||
public virtual ICollection<RolePermission> Role { get; set; }
|
||||
}
|
||||
}
|
@@ -1,17 +1,17 @@
|
||||
using Data.Base;
|
||||
using Data.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Models.Models
|
||||
{
|
||||
public class Role : BaseUserManagmentEntity
|
||||
{
|
||||
public Role() : base(){}
|
||||
public Role(string Name, ICollection<Permission> Permissions) : base(Name)
|
||||
public Role(string Name) : base(Name)
|
||||
{
|
||||
this.Permissions = Permissions;
|
||||
}
|
||||
[Required]
|
||||
public virtual ICollection<Permission> Permissions { get; set; }
|
||||
public virtual ICollection<RolePermission> Permissions { get; set; } = new List<>
|
||||
}
|
||||
}
|
15
LuminousSales/Models/Models/RolePermission.cs
Normal file
15
LuminousSales/Models/Models/RolePermission.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Models.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Data.Models
|
||||
{
|
||||
public class RolePermission
|
||||
{
|
||||
public int RoleId { get; set; }
|
||||
public virtual Role Roles { get; set; }
|
||||
public int PermisionId { get; set; }
|
||||
public virtual Permission Permission { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user