ASP MVC 5 application users
-
Hello ! I am building a MVC 5 app in which there will be 4 types of users . (Admin , HR Member , Managers , Employee) . I already created the views and the controllers for each type . I want to give each manager access to certain departments , and the employee access to data according to his employeeID value . After reading several articles on the Internet about this topic , I suppose I have to assign each user type a role and each department/employeeID a permission , but I have no clue on how to do that . Can someone explain to me how to implement this ? I mention that the Identity Framework installed on the project is at version 1.0 .
-
Hello ! I am building a MVC 5 app in which there will be 4 types of users . (Admin , HR Member , Managers , Employee) . I already created the views and the controllers for each type . I want to give each manager access to certain departments , and the employee access to data according to his employeeID value . After reading several articles on the Internet about this topic , I suppose I have to assign each user type a role and each department/employeeID a permission , but I have no clue on how to do that . Can someone explain to me how to implement this ? I mention that the Identity Framework installed on the project is at version 1.0 .
You need to create Roles & Permissions. You need to create a table (like "Roles") that store the Roles In your app there is 4 Roles 1- Admin 2- HR Member 3- Managers 4- Employee And need to store all the permissions in a Table (like "Permission" table). for each Role you need to map the permissions means to specify what are the permission available for Admin, HR Member, Managers & Employee. To do this you need to create a table (like RolesPermission) that contains the Id of Role and also the Ids of Permission that "One To Many" mapping. like ID RoleId PermissionId 1 1 1 2 1 2 3 1 4 4 2 4 5 2 5 ... ... ... After that you just need to assign the Role to the users. And where the user can access resources or not You just need to get Permissions of that user and check the permission that available to access the resources. If available then give access otherwise don't give access.
-
You need to create Roles & Permissions. You need to create a table (like "Roles") that store the Roles In your app there is 4 Roles 1- Admin 2- HR Member 3- Managers 4- Employee And need to store all the permissions in a Table (like "Permission" table). for each Role you need to map the permissions means to specify what are the permission available for Admin, HR Member, Managers & Employee. To do this you need to create a table (like RolesPermission) that contains the Id of Role and also the Ids of Permission that "One To Many" mapping. like ID RoleId PermissionId 1 1 1 2 1 2 3 1 4 4 2 4 5 2 5 ... ... ... After that you just need to assign the Role to the users. And where the user can access resources or not You just need to get Permissions of that user and check the permission that available to access the resources. If available then give access otherwise don't give access.
Ok , in other words I should have three classes : Role Permission RolePermission(in which I should include two objects , one of type Role , and one of type Permission) And I should link the RolePermission to the ApplicationUser , then apply the migration . Is that correct ?
-
Ok , in other words I should have three classes : Role Permission RolePermission(in which I should include two objects , one of type Role , and one of type Permission) And I should link the RolePermission to the ApplicationUser , then apply the migration . Is that correct ?
-
Thank you very much ! :)
-
Ok , I got stuck again . So I created the classes : Role :
public class Role { public Role() { } public Role(string name, int id):this() { this.RoleId = id; this.Name = name; } \[Key\] \[Required\] public virtual int RoleId { get; set; } public virtual string Name { get; set; } }
Permission :
public Permission() { } public Permission(string name,int id):this() { this.Name=name; this.Id = id; } \[Key\] \[Required\] public virtual int Id { get; set; } public virtual string Name { get; set; } }
RolePermission :
public class RolePermission { public Role Role { get; set; } public Permission Group { get; set; } \[Required\] public int RoleGroupId { get; set; } public RolePermission() { } }
I updated the ApplicationUser class
public class ApplicationUser : IdentityUser { public ApplicationUser() : base() { this.Permission=new HashSet(); } \[Required\] public string Name { get; set; } public virtual ICollection Permission { get; set; } }
Is this correct up to this point ? I suppose I should modify the ApplicationDbContext , which , up to this point , looks like this :
public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } }
What changes should I do ?
-
Ok , I got stuck again . So I created the classes : Role :
public class Role { public Role() { } public Role(string name, int id):this() { this.RoleId = id; this.Name = name; } \[Key\] \[Required\] public virtual int RoleId { get; set; } public virtual string Name { get; set; } }
Permission :
public Permission() { } public Permission(string name,int id):this() { this.Name=name; this.Id = id; } \[Key\] \[Required\] public virtual int Id { get; set; } public virtual string Name { get; set; } }
RolePermission :
public class RolePermission { public Role Role { get; set; } public Permission Group { get; set; } \[Required\] public int RoleGroupId { get; set; } public RolePermission() { } }
I updated the ApplicationUser class
public class ApplicationUser : IdentityUser { public ApplicationUser() : base() { this.Permission=new HashSet(); } \[Required\] public string Name { get; set; } public virtual ICollection Permission { get; set; } }
Is this correct up to this point ? I suppose I should modify the ApplicationDbContext , which , up to this point , looks like this :
public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } }
What changes should I do ?
-
Yes you are right. You should use this one public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } }
It worked . Thank you very much ! :)