362 lines
15 KiB
C#
362 lines
15 KiB
C#
using System.Globalization;
|
|
using EnVisage.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using Microsoft.AspNet.Identity;
|
|
using EnVisage.Code;
|
|
using EnVisage.Code.Cache;
|
|
using EnVisage.Models.Cache;
|
|
using System.Text;
|
|
|
|
namespace EnVisage.Code.HtmlHelpers
|
|
{
|
|
public static class HtmlHelpers
|
|
{
|
|
public const int C_ITEM_LEVEL_INDENT_PIXELS = 16;
|
|
|
|
public struct RoleAccess {
|
|
public Guid RoleId;
|
|
public Areas SecurityObject;
|
|
public Guid ProjectId;
|
|
public int Read;
|
|
public int Write;
|
|
}
|
|
|
|
//public static MvcHtmlString GetAreaItemsList(this HtmlHelper html, UrlHelper url, AspNetUser principal)
|
|
public static MvcHtmlString GetAreaItemsList(this HtmlHelper html, UrlHelper url, UserModel principal)
|
|
{
|
|
const string attrChecked = "checked";
|
|
const string attrInherited = "inherited";
|
|
|
|
var context = new EnVisageEntities();
|
|
var roleIds = principal.AspNetRoles.Select(t => new Guid (t.Id)).ToArray();
|
|
var rolePermissions = (from pr in context.Securities
|
|
where roleIds.Contains(pr.PrincipalId)
|
|
select pr).ToArray().Select(area => new RoleAccess
|
|
{
|
|
RoleId = area.PrincipalId,
|
|
SecurityObject = (Areas) Enum.Parse(typeof (Areas), area.SecurityObject),
|
|
Read = area.Read,
|
|
Write = area.Write
|
|
}).ToList();
|
|
|
|
var userPermissions = new List<Security>();
|
|
|
|
if (principal.Id != Guid.Empty)
|
|
userPermissions = (from pr in context.Securities where pr.PrincipalId == principal.Id select pr).ToList();
|
|
|
|
var menuItems = Enum.GetValues(typeof(Areas)).Cast<Areas>().ToList();
|
|
|
|
//temporary remove Scheduling Board and Portfolio from permission list
|
|
menuItems.Remove(Areas.ScheduleBoard);
|
|
menuItems.Remove(Areas.Portfolio);
|
|
if (!context.MenuNavigations.Any(x => x.Value == (int) Areas.CustomReports))
|
|
menuItems.Remove(Areas.CustomReports);
|
|
var menulist = new TagBuilder("div");
|
|
menulist.InnerHtml = @"<tr style=""border-top:0;"">
|
|
<td style=""font-weight:600;border-top-width:0"">Areas</td>
|
|
<td style=""border-top-width:0;text-align: center;""> R W</td>
|
|
</tr>";
|
|
|
|
foreach (var menuItem in menuItems)
|
|
{
|
|
var tr = new TagBuilder("tr");
|
|
var td = new TagBuilder("td");
|
|
var td1 = new TagBuilder("td");
|
|
|
|
var permissionLabeDiv = new TagBuilder("div");
|
|
permissionLabeDiv.AddCssClass("permission-area-label");
|
|
|
|
var cbr = new TagBuilder("input");
|
|
cbr.Attributes["value"] = menuItem.ToString();
|
|
cbr.Attributes["type"] = "checkbox";
|
|
cbr.Attributes["name"] = "areasread";
|
|
cbr.AddCssClass("custominput");
|
|
|
|
var cbw = new TagBuilder("input");
|
|
cbw.Attributes["value"] = menuItem.ToString();
|
|
cbw.Attributes["type"] = "checkbox";
|
|
cbw.Attributes["name"] = "areaswrite";
|
|
cbw.AddCssClass("custominput");
|
|
|
|
var isExplicitReadFound = false;
|
|
var isExplicitWriteFound = false;
|
|
|
|
foreach (var pa in userPermissions)
|
|
{
|
|
if (!menuItem.ToString().Equals(pa.SecurityObject))
|
|
continue;
|
|
if (pa.Read == (int)Permission.Allow)
|
|
cbr.Attributes["checked"] = attrChecked;
|
|
if (pa.Write == (int)Permission.Allow)
|
|
cbw.Attributes["checked"] = attrChecked;
|
|
|
|
isExplicitReadFound = pa.Read != (int)Permission.Inherited;
|
|
isExplicitWriteFound = pa.Write != (int)Permission.Inherited;
|
|
break;
|
|
}
|
|
|
|
var isRolePermissionFound = rolePermissions.Any(t => t.SecurityObject == menuItem);
|
|
var isRead = rolePermissions.Any(t => t.SecurityObject == menuItem && (t.Read == (int)Permission.Allow));
|
|
var isWrite = rolePermissions.Any(t => t.SecurityObject == menuItem && (t.Write == (int)Permission.Allow));
|
|
if (isRolePermissionFound)
|
|
{
|
|
cbr.Attributes["role"] = ((int)(isRead ? Permission.Allow : Permission.Deny)).ToString(CultureInfo.InvariantCulture);
|
|
cbw.Attributes["role"] = ((int)(isWrite ? Permission.Allow : Permission.Deny)).ToString(CultureInfo.InvariantCulture);
|
|
if (!isExplicitReadFound)
|
|
{
|
|
cbr.Attributes["inherited"] = attrInherited;
|
|
if (isRead)
|
|
cbr.Attributes["checked"] = "checked";
|
|
}
|
|
if (!isExplicitWriteFound)
|
|
{
|
|
cbw.Attributes["inherited"] = attrInherited;
|
|
if (isWrite)
|
|
cbw.Attributes["checked"] = "checked";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cbr.Attributes["role"] = ((int)Permission.Deny).ToString(CultureInfo.InvariantCulture);
|
|
cbw.Attributes["role"] = ((int)Permission.Deny).ToString(CultureInfo.InvariantCulture);
|
|
if (!isExplicitReadFound)
|
|
{
|
|
cbr.Attributes["inherited"] = attrInherited;
|
|
}
|
|
if (!isExplicitWriteFound)
|
|
{
|
|
cbw.Attributes["inherited"] = attrInherited;
|
|
}
|
|
}
|
|
|
|
string permissionTitle = menuItem.ToDisplayValue();
|
|
permissionLabeDiv.SetInnerText(permissionTitle);
|
|
permissionLabeDiv.Attributes.Add("title", permissionTitle);
|
|
|
|
if (menuItem.DisplayAsBold())
|
|
// Customize display of the group permission item
|
|
permissionLabeDiv.AddCssClass("permission-group");
|
|
|
|
// Set item indent int the permission Tree
|
|
var menuItemDisplayLevel = menuItem.GetLevelInTree();
|
|
int menuItemDisplayIndentPx = C_ITEM_LEVEL_INDENT_PIXELS * ((int)menuItemDisplayLevel - 1);
|
|
|
|
if (menuItemDisplayIndentPx > 0)
|
|
{
|
|
string inlineCssStyle =
|
|
permissionLabeDiv.Attributes.ContainsKey("style") ?
|
|
permissionLabeDiv.Attributes["style"] :
|
|
String.Empty;
|
|
|
|
inlineCssStyle += String.Format(" padding-left:{0}px;", menuItemDisplayIndentPx);
|
|
permissionLabeDiv.Attributes["style"] = inlineCssStyle.Trim();
|
|
|
|
var parentItem = menuItem.GetParentItem();
|
|
if (parentItem.HasValue)
|
|
{
|
|
cbr.Attributes.Add("data-parent-item", parentItem.Value.ToString());
|
|
cbw.Attributes.Add("data-parent-item", parentItem.Value.ToString());
|
|
}
|
|
}
|
|
|
|
td.InnerHtml = permissionLabeDiv.ToString();
|
|
td1.InnerHtml = string.Format("<nobr>{0}{1}</nobr>", cbr, cbw);
|
|
|
|
tr.InnerHtml = string.Format("{0}{1}", td, td1);
|
|
menulist.InnerHtml += tr;
|
|
}
|
|
return new MvcHtmlString(menulist.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString GetAreaItemsList(this HtmlHelper html, UrlHelper url, RoleModel role)
|
|
{
|
|
EnVisageEntities context = new EnVisageEntities();
|
|
var selecteds = (from pr in context.Securities where pr.PrincipalId == role.Id select pr).ToList();
|
|
var menuItems = Enum.GetValues(typeof(Areas)).Cast<Areas>().ToList();
|
|
|
|
//temporary remove Scheduling Board and Portfolio from permission list
|
|
menuItems.Remove(Areas.ScheduleBoard);
|
|
menuItems.Remove(Areas.Portfolio);
|
|
if (!context.MenuNavigations.Any(x=>x.Value == (int) Areas.CustomReports))
|
|
menuItems.Remove(Areas.CustomReports);
|
|
var menulist = new TagBuilder("div");
|
|
menulist.InnerHtml = @"<tr style=""border-top:0;"">
|
|
<td style=""font-weight:600;border-top-width:0"">Areas</td>
|
|
<td style=""border-top-width:0;text-align: center;"">R W</td>
|
|
</tr>";
|
|
|
|
foreach (var menuItem in menuItems)
|
|
{
|
|
var tr = new TagBuilder("tr");
|
|
var td = new TagBuilder("td");
|
|
var td1 = new TagBuilder("td");
|
|
td1.Attributes.Add("nowrap", string.Empty);
|
|
|
|
var permissionLabeDiv = new TagBuilder("div");
|
|
permissionLabeDiv.AddCssClass("permission-area-label");
|
|
|
|
var cbr = new TagBuilder("input");
|
|
cbr.Attributes["value"] = menuItem.ToString();
|
|
cbr.Attributes["type"] = "checkbox";
|
|
cbr.Attributes["name"] = "areasread";
|
|
var cbw = new TagBuilder("input");
|
|
cbw.Attributes["value"] = menuItem.ToString();
|
|
cbw.Attributes["type"] = "checkbox";
|
|
cbw.Attributes["name"] = "areaswrite";
|
|
|
|
cbr.AddCssClass("custominput");
|
|
cbw.AddCssClass("custominput");
|
|
|
|
foreach (var selected in selecteds)
|
|
{
|
|
if (menuItem.ToString() == selected.SecurityObject && selected.Read == 1)
|
|
cbr.Attributes["checked"] = "checked";
|
|
if (menuItem.ToString() == selected.SecurityObject && selected.Write == 1)
|
|
cbw.Attributes["checked"] = "checked";
|
|
}
|
|
|
|
string permissionTitle = menuItem.ToDisplayValue();
|
|
permissionLabeDiv.SetInnerText(permissionTitle);
|
|
permissionLabeDiv.Attributes.Add("title", permissionTitle);
|
|
|
|
if (menuItem.DisplayAsBold())
|
|
// Customize display of the group permission item
|
|
permissionLabeDiv.AddCssClass("permission-group");
|
|
|
|
// Set item indent int the permission Tree
|
|
var menuItemDisplayLevel = menuItem.GetLevelInTree();
|
|
int menuItemDisplayIndentPx = C_ITEM_LEVEL_INDENT_PIXELS * ((int)menuItemDisplayLevel - 1);
|
|
|
|
if (menuItemDisplayIndentPx > 0)
|
|
{
|
|
string inlineCssStyle =
|
|
permissionLabeDiv.Attributes.ContainsKey("style") ?
|
|
permissionLabeDiv.Attributes["style"] :
|
|
String.Empty;
|
|
|
|
inlineCssStyle += String.Format(" padding-left:{0}px;", menuItemDisplayIndentPx);
|
|
permissionLabeDiv.Attributes["style"] = inlineCssStyle.Trim();
|
|
|
|
var parentItem = menuItem.GetParentItem();
|
|
if (parentItem.HasValue)
|
|
{
|
|
cbr.Attributes.Add("data-parent-item", parentItem.Value.ToString());
|
|
cbw.Attributes.Add("data-parent-item", parentItem.Value.ToString());
|
|
}
|
|
}
|
|
|
|
td.InnerHtml = permissionLabeDiv.ToString();
|
|
td1.InnerHtml = cbr + " " + cbw;
|
|
|
|
tr.InnerHtml = td + "" + td1;
|
|
menulist.InnerHtml += tr;
|
|
}
|
|
|
|
return new MvcHtmlString(menulist.ToString());
|
|
}
|
|
|
|
//public static MvcHtmlString GetRolesList(this HtmlHelper html, UrlHelper url, AspNetUser Principal)
|
|
public static MvcHtmlString GetRolesList(this HtmlHelper html, UrlHelper url, UserModel Principal)
|
|
{
|
|
EnVisageEntities context = new EnVisageEntities();
|
|
var selecteds = Principal.AspNetRoles.ToList();
|
|
var roleItems = (from pr in context.AspNetRoles
|
|
orderby pr.Name
|
|
select pr).ToList();
|
|
var rolelist = new TagBuilder("div");
|
|
foreach (var roleItem in roleItems)
|
|
{
|
|
var cb = new TagBuilder("input");
|
|
cb.Attributes["value"] = roleItem.Id.ToString();
|
|
cb.Attributes["type"] = "checkbox";
|
|
cb.Attributes["name"] = "roleitems";
|
|
foreach (var selected in selecteds)
|
|
{
|
|
if (roleItem.Id == selected.Id)
|
|
cb.Attributes["checked"] = "checked";
|
|
}
|
|
cb.InnerHtml = " " + roleItem.Name;
|
|
rolelist.InnerHtml += cb + " ";
|
|
}
|
|
|
|
return new MvcHtmlString(rolelist.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString GetProjectStatusDropdown(this HtmlHelper html, UrlHelper url)
|
|
{
|
|
EnVisageEntities context = new EnVisageEntities();
|
|
var statuses = (from pr in context.Status
|
|
orderby pr.Name
|
|
select pr).ToList();
|
|
var statusDropDown = new TagBuilder("select");
|
|
statusDropDown.Attributes["name"] = "statuses";
|
|
statusDropDown.Attributes["class"] = "form-control";
|
|
var opAll = new TagBuilder("option");
|
|
opAll.Attributes["value"] = "All";
|
|
opAll.InnerHtml = "All";
|
|
statusDropDown.InnerHtml += opAll.ToString();
|
|
foreach (var status in statuses)
|
|
{
|
|
var op = new TagBuilder("option");
|
|
op.Attributes["value"] = status.Name;
|
|
op.InnerHtml = status.Name;
|
|
statusDropDown.InnerHtml += op.ToString();
|
|
}
|
|
|
|
return new MvcHtmlString(statusDropDown.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString GetProjectName(this HtmlHelper html, Guid ProjectId)
|
|
{
|
|
EnVisageEntities context = new EnVisageEntities();
|
|
var projName = (from pr in context.Projects
|
|
where pr.Id == ProjectId
|
|
select pr.Name).FirstOrDefault();
|
|
if (projName == null) return new MvcHtmlString(string.Empty);
|
|
else return new MvcHtmlString(projName.ToString());
|
|
}
|
|
|
|
|
|
public static IEnumerable<SelectListItem> GetProjectStatusDropdown(EnVisage.Models.ForecastDashboardModel input)
|
|
{
|
|
var options = new List<SelectListItem>();
|
|
using (var dbContext = new EnVisageEntities())
|
|
{
|
|
var projectStatuses = dbContext.Status.ToList();
|
|
var allItem = new SelectListItem();
|
|
allItem.Text = "All";
|
|
allItem.Value = "All";
|
|
options.Add(allItem);
|
|
options.AddRange(projectStatuses.Select(creditDepartment => new SelectListItem()
|
|
{
|
|
Value = creditDepartment.Id.ToString(),
|
|
Text = creditDepartment.Name
|
|
}));
|
|
}
|
|
return options;
|
|
}
|
|
|
|
public static IEnumerable<SelectListItem> GetProjectClassificationDropdown(EnVisage.Models.ForecastDashboardModel input)
|
|
{
|
|
var options = new List<SelectListItem>();
|
|
using (var dbContext = new EnVisageEntities())
|
|
{
|
|
var projectClassifications = dbContext.Types.ToList();
|
|
var allItem = new SelectListItem();
|
|
allItem.Text = "All";
|
|
allItem.Value = "All";
|
|
options.Add(allItem);
|
|
options.AddRange(projectClassifications.Select(creditDepartment => new SelectListItem()
|
|
{
|
|
Value = creditDepartment.Id.ToString(),
|
|
Text = creditDepartment.Name
|
|
}));
|
|
}
|
|
return options;
|
|
}
|
|
}
|
|
} |