Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP MVC 5 application users

ASP MVC 5 application users

Scheduled Pinned Locked Moved ASP.NET
asp-netarchitecturetutorialquestionannouncement
8 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnCristina
    wrote on last edited by
    #1

    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 .

    V 1 Reply Last reply
    0
    • A AnCristina

      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 .

      V Offline
      V Offline
      Vi ky
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • V Vi ky

        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.

        A Offline
        A Offline
        AnCristina
        wrote on last edited by
        #3

        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 ?

        V 1 Reply Last reply
        0
        • A AnCristina

          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 ?

          V Offline
          V Offline
          Vi ky
          wrote on last edited by
          #4

          Yes :)

          A 2 Replies Last reply
          0
          • V Vi ky

            Yes :)

            A Offline
            A Offline
            AnCristina
            wrote on last edited by
            #5

            Thank you very much ! :)

            1 Reply Last reply
            0
            • V Vi ky

              Yes :)

              A Offline
              A Offline
              AnCristina
              wrote on last edited by
              #6

              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 ?

              V 1 Reply Last reply
              0
              • A AnCristina

                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 ?

                V Offline
                V Offline
                Vi ky
                wrote on last edited by
                #7

                Yes you are right. You should use this one public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } }

                A 1 Reply Last reply
                0
                • V Vi ky

                  Yes you are right. You should use this one public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } }

                  A Offline
                  A Offline
                  AnCristina
                  wrote on last edited by
                  #8

                  It worked . Thank you very much ! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups