public static MvcHtmlString NavbarLink(this HtmlHelper helper, string name, string actionName, string controllerName) { var sb = new StringBuilder(); var routeElement = new RouteElement { Action = actionName, Controller = controllerName }; var currentUserRoles = new string[] { "Unregister" }; if (WebSecurity.IsAuthenticated) { var userRolesProvider = (SimpleRoleProvider)System.Web.Security.Roles.Provider; var userName = WebSecurity.CurrentUserName; currentUserRoles = userRolesProvider.GetRolesForUser(userName); } if (!SecurityStuffs.GetInstance().HasPermmisions(currentUserRoles, routeElement)) { return new MvcHtmlString(sb.ToString()); } string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"]; string currentActionName = (string)helper.ViewContext.RouteData.Values["action"]; string isActiveClass = ""; if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)) { isActiveClass = " class=\"active\""; } sb.AppendFormat("<li {0}>", isActiveClass); var url = new UrlHelper(HttpContext.Current.Request.RequestContext); sb.AppendFormat("<a href=\"{0}\">{1}</a>", url.Action(actionName, controllerName), name); sb.Append("</li>"); return new MvcHtmlString(sb.ToString()); }
public bool HasPermmisions(string[] roles, RouteElement route) { bool isRoleAuthorized = false; foreach (string role in roles) { var views = RoutesListByRole(role); if (views.Exists(x => x.Action == route.Action && x.Controller == route.Controller)) { isRoleAuthorized = true; break; } } return(isRoleAuthorized); }
public bool HasPermmisions(string[] roles, RouteElement route) { bool isRoleAuthorized = false; foreach (string role in roles) { var views = RoutesListByRole(role); if (views.Exists(x => x.Action == route.Action && x.Controller == route.Controller)) { isRoleAuthorized = true; break; } } return isRoleAuthorized; }