ASSIGN PERMISSION FOR PAGES USING CODE FIRST IN ASP.NET MVC:

Carl Pieterson

Total Post:14

Points:98
Posted by  Carl Pieterson
 1925  View(s)
Ratings:
Rate this:

Hi,

Here I have created a Model Structure in asp.net mvc:

    public class UserModel

    {

        public int UserId {  get;  set;  }

        public string UserName  {  get;   set;  }

        public string Password   {    get; set;    }

        public List<Permission> Permissions {   get;   set;  }

        public string FirstName {  get;  set;  }

        public string LastName {  get;   set;  }

    }

    public class Permission

    {

        public int PermissionID {   get;  set;   }

        public bool IsPermit   {  get;  set;  }

        public string Name  {  get;  set;  }

    }

and setting some default values in the list and while I am adding the user in the list I assign the permission for pages to that user through the UI (by checking permission checkboxes), so that user can access only the assigned pages:

    public static class Repository

    {

        public static List<UserModel> GetUsers()

        {

            List<UserModel> listUsers = new List<UserModel>

            {

                new UserModel

                {

                    UserId = 1,

                    UserName = "abc",

                    Password = "abc",

                    Permissions = new List<Permission>

                    {

                        new Permission

                        {

                            PermissionID = 1,

                            IsPermit = true,

                            Name = "Page1"

                        },

                        new Permission

                        {

                            PermissionID = 2,

                            IsPermit = false,

                            Name = "Page2"

                        },

                        new Permission

                        {

                            PermissionID = 3,

                            IsPermit = false,

                            Name = "Page3"

                        },

                        new Permission

                        {

                            PermissionID = 4,

                            IsPermit = false,

                            Name = "Page4"

                        }

                    },

                    FirstName = "Rohit",

                    LastName = "Sharma"

                },

                new UserModel

                {

                    UserId = 2,

                    UserName = "xyz",

                    Password = "xyz",

                    Permissions = new List<Permission>

                    {

                        new Permission

                        {

                            PermissionID = 1,

                            IsPermit = false,

                            Name = "Page1"

                        },

                        new Permission

                        {

                            PermissionID = 2,

                            IsPermit = true,

                            Name = "Page2"

                        },

                        new Permission

                        {

                            PermissionID = 3,

                            IsPermit = true,

                            Name = "Page3"

                        },

                        new Permission

                        {

                            PermissionID = 4,

                            IsPermit = true,

                            Name = "Page4"

                        }

                    },

                    FirstName = "Rahul",

                    LastName = "Sharma"

                }

            };

            return listUsers;

        }

    }

Now I want to do the same by using code first approach of database with the help of DbContext class.

I have a static list of page permission in a database table (Id =1, Name=Page1; Id =2, Name=Page2; Id =3, Name=Page3; Id =4, Name=Page4).

I am confused while creating model structure for database. Please guide me how to create model structure and mapping of structure with the tables.

Please reply ASAP.
Thanks.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!