0
votes

I created my own user permission tables, Where each user can belong to many groups and many security roles. But I have the following as part of my _layout view:-

<li class="nav-header hidden-tablet"style="background-color:#3E9BD4 ; color:white">User Section</li>
<li><a class="ajax-link" href="~/Home/Contact"><i class="icon-home"></i><span class="hidden-tablet">contact</span></a></li>

<li class="nav-header hidden-tablet"style="background-color:#3E9BD4 ; color:white">Administration Section</li>
<li><a class="ajax-link" href="~/SecurityGroup/"><i class="icon-home"></i><span class="hidden-tablet">Security Groups</span></a></li>
<li><a class="ajax-link" href="~/SecurityRole/"><i class="icon-home"></i><span class="hidden-tablet">Security Roles</span></a></li>
<li><a class="ajax-link" href="~/AuditInfo"><i class="icon-home"></i><span class="hidden-tablet">Audit</span></a></li>
<li><a class="ajax-link" href="~/SecurityGroup/ADUsers"><i class="icon-home"></i><span class="hidden-tablet">Active Directory</span></a></li>

So how I can show the administration section only if the current user belongs to a Group that is associated with a predefined security role named “Admin” or directly linked to this security role.

I am not storing any of the users info inside my application , as the users exists inside the Active Directory . So I have the following models classes:-

public partial class Group

    {

        public Group()

        {

            this.UserGroups = new HashSet<UserGroup>();

            this.SecurityRoles = new HashSet<SecurityRole>();

        }



        public int GroupID { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public byte[] timestamp { get; set; }



        public virtual ICollection<UserGroup> UserGroups { get; set; }

        public virtual ICollection<SecurityRole> SecurityRoles { get; set; }

    }

    public partial class UserGroup

    {

        public int GroupID { get; set; }

        public string UserName { get; set; }



        public virtual Group Group { get; set; }

    }

}



public partial class SecurityRole

    {

        public SecurityRole()

        {

            this.SecurityroleTypePermisions = new HashSet<SecurityroleTypePermision>();

            this.SecurityRoleUsers = new HashSet<SecurityRoleUser>();

            this.Groups = new HashSet<Group>();

        }



        public int SecurityRoleID { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public byte[] timestamp { get; set; }



        public virtual ICollection<SecurityroleTypePermision> SecurityroleTypePermisions { get; set; }

        public virtual ICollection<SecurityRoleUser> SecurityRoleUsers { get; set; }

        public virtual ICollection<Group> Groups { get; set; }

    }

public partial class SecurityRoleUser

    {

        public int SecurityRoleID { get; set; }

        public string UserName { get; set; }



        public virtual SecurityRole SecurityRole { get; set; }

    }

Baring in mind that the _layout view is not a strongly typed view , so am not able to define helper method on the model level such as public bool IsAdmin(string userName) .

Thanks in advance for any help.

1

1 Answers

0
votes

Seeing as your user information is not being dealt with in application, I don't see a way that you can access user security rights in the _layout file, as it is one of the first things loaded.

Why not move what you currently have in your _layout file into a partial view, then create another partial view that is identical, but not including whatever rights you want to hide. Then you could strongly type your views and choose to render each partial where needed. Given, you would need to render a partial every time you loaded a page, but then you would potentially be able to use your models and helpers (IsAdmin, etc.) as well to decide which partial to render.

If you are really interested in going for the _layout page, try strongly typing the entire layout for a particular model. See the "Razor View Engine" section of http://philjthorne.blogspot.com/2012/05/strongly-typing-aspnet-mvc-masterlayout.html for more information.