805 lines
37 KiB
C#
805 lines
37 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 struct RoleAccess {
|
|
public Guid RoleId;
|
|
public Areas SecurityObject;
|
|
public Guid ProjectId;
|
|
public int Read;
|
|
public int Write;
|
|
}
|
|
|
|
public static MvcHtmlString GetProjectTree(this HtmlHelper html, UrlHelper url, AspNetUser principal,
|
|
Guid? roleId)
|
|
{
|
|
const string attrChecked = "checked";
|
|
const string attrOverriden = "overriden";
|
|
var context = new EnVisageEntities();
|
|
var companies = (from c in context.Companies
|
|
select new {c.Id, c.Name}).ToList();
|
|
//TODO: I've commented where clause above as we now have a bunch of projects assigned directly to parent company, e.g. system ones (capacity, etc.)
|
|
|
|
var clients = (from c in context.Clients
|
|
select new {c.Id, c.Name}).ToList();
|
|
|
|
var rolePermissions = new List<RoleAccess>();
|
|
var userPermissions = new List<ProjectAccess>();
|
|
|
|
if (principal != null)
|
|
{
|
|
#region User
|
|
|
|
foreach (var role in principal.AspNetRoles)
|
|
{
|
|
var accessForRoles = (from pr in context.ProjectAccesses
|
|
where pr.PrincipalId == new Guid(role.Id)
|
|
select new
|
|
{
|
|
PrincipalId = pr.PrincipalId,
|
|
ProjectId = pr.ProjectId,
|
|
Read = pr.Read,
|
|
Write = pr.Write
|
|
}).ToList();
|
|
|
|
foreach (var project in accessForRoles)
|
|
{
|
|
var new_ra = new RoleAccess();
|
|
new_ra.RoleId = project.PrincipalId;
|
|
new_ra.ProjectId = project.ProjectId;
|
|
new_ra.Read = project.Read;
|
|
new_ra.Write = project.Write;
|
|
rolePermissions.Add(new_ra);
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(principal.Id))
|
|
userPermissions = (from pr in context.ProjectAccesses where pr.PrincipalId == new Guid(principal.Id) select pr)
|
|
.ToList();
|
|
|
|
#endregion
|
|
}
|
|
else
|
|
{
|
|
#region Roles
|
|
var accessForRoles = (from pr in context.ProjectAccesses
|
|
where pr.PrincipalId == roleId
|
|
select pr).ToList();
|
|
|
|
foreach (var project in accessForRoles)
|
|
{
|
|
var newRa = new RoleAccess
|
|
{
|
|
RoleId = project.PrincipalId,
|
|
ProjectId = project.ProjectId,
|
|
Read = project.Read,
|
|
Write = project.Write
|
|
};
|
|
rolePermissions.Add(newRa);
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
|
|
var projects = (from pr in context.Projects
|
|
orderby pr.Name
|
|
select new {Id = pr.Id, Name = pr.Name, CompanyId = pr.CompanyId, ClientId = pr.ClientId})
|
|
.ToList();
|
|
|
|
var projlist = new TagBuilder("div");
|
|
projlist.InnerHtml = @"<tr class=""treegrid-1"" style=""border-top:0;"">
|
|
<td style=""font-weight:600;border-top-width:0"">Projects</td>
|
|
<td style=""border-top-width:0;text-align: center;""> R W</td>
|
|
</tr>";
|
|
|
|
foreach (var company in companies)
|
|
{
|
|
#region Company
|
|
|
|
var tr = new TagBuilder("tr");
|
|
var td = new TagBuilder("td");
|
|
var span = new TagBuilder("span");
|
|
var td1 = new TagBuilder("td");
|
|
td1.Attributes.Add("nowrap", string.Empty);
|
|
|
|
var cbr = new TagBuilder("input");
|
|
cbr.Attributes["value"] = company.Id.ToString();
|
|
cbr.Attributes["type"] = "checkbox";
|
|
cbr.Attributes["name"] = "companyread";
|
|
cbr.AddCssClass("custominput");
|
|
cbr.AddCssClass("custom");
|
|
|
|
var cbw = new TagBuilder("input");
|
|
cbw.Attributes["value"] = company.Id.ToString();
|
|
cbw.Attributes["type"] = "checkbox";
|
|
cbw.Attributes["name"] = "companywrite";
|
|
cbw.AddCssClass("custominput");
|
|
cbw.AddCssClass("custom");
|
|
|
|
var companyProjects = projects.Where(x => x.CompanyId == company.Id).Select(x => x.Id).ToArray();
|
|
|
|
//AK: looks like here we set checkboxes for lowest level permissions
|
|
if (companyProjects.Length > 0 &&
|
|
(principal != null && userPermissions.Count(x => companyProjects.Contains(x.ProjectId)) > 0) ||
|
|
(principal == null && rolePermissions.Count(x => companyProjects.Contains(x.ProjectId)) > 0))
|
|
{
|
|
if ((principal != null && companyProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Read == (int)Permission.Allow))) ||
|
|
(principal == null && companyProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Read == (int)Permission.Allow))))
|
|
cbr.Attributes["checked"] = attrChecked;
|
|
if ((principal != null && companyProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Write == (int)Permission.Allow))) ||
|
|
(principal == null && companyProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Write == (int)Permission.Allow))))
|
|
cbw.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
td1.InnerHtml = string.Format("{0}{1}", cbr, cbw);
|
|
tr.Attributes["class"] = string.Format("treegrid-{0}", company.Id);
|
|
span.Attributes["class"] = "treegrid-expander glyphicon glyphicon-chevron-right";
|
|
td.InnerHtml = span.ToString();
|
|
td.SetInnerText(company.Name);
|
|
tr.InnerHtml = string.Format("{0}{1}", td, td1);
|
|
projlist.InnerHtml += tr;
|
|
|
|
#endregion
|
|
|
|
#region Clients
|
|
|
|
foreach (
|
|
var client in
|
|
clients.Where(
|
|
c => projects.Where(x => x.CompanyId == company.Id).Select(x => x.ClientId).Contains(c.Id)))
|
|
{
|
|
var client_tr = new TagBuilder("tr");
|
|
var client_td = new TagBuilder("td");
|
|
var client_span = new TagBuilder("span");
|
|
var client_td1 = new TagBuilder("td");
|
|
client_td1.Attributes.Add("nowrap", string.Empty);
|
|
var client_cbr = new TagBuilder("input");
|
|
client_cbr.Attributes["value"] = client.Id.ToString();
|
|
client_cbr.Attributes["companyr"] = company.Id.ToString();
|
|
client_cbr.Attributes["type"] = "checkbox";
|
|
client_cbr.Attributes["name"] = "clientread";
|
|
client_cbr.AddCssClass("custominput");
|
|
|
|
var client_cbw = new TagBuilder("input");
|
|
client_cbw.Attributes["companyw"] = company.Id.ToString();
|
|
client_cbw.Attributes["value"] = client.Id.ToString();
|
|
client_cbw.Attributes["type"] = "checkbox";
|
|
client_cbw.Attributes["name"] = "clientwrite";
|
|
client_cbw.AddCssClass("custominput");
|
|
|
|
var clientProjects = projects.Where(x => x.CompanyId == company.Id && x.ClientId == client.Id).Select(x => x.Id).ToArray();
|
|
|
|
if (clientProjects.Length > 0 &&
|
|
(principal != null && userPermissions.Count(x => clientProjects.Contains(x.ProjectId)) > 0) ||
|
|
(principal == null && rolePermissions.Count(x => clientProjects.Contains(x.ProjectId)) > 0))
|
|
{
|
|
if ((principal != null && clientProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Read == (int)Permission.Allow))) ||
|
|
(principal == null && clientProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Read == (int)Permission.Allow))))
|
|
client_cbr.Attributes["checked"] = attrChecked;
|
|
if ((principal != null && clientProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Write == (int)Permission.Allow))) ||
|
|
(principal == null && clientProjects.All(prj => rolePermissions.Exists(prm => prm.ProjectId == prj && prm.Write == (int)Permission.Allow))))
|
|
client_cbw.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
client_td1.InnerHtml = string.Format("{0}{1}", client_cbr, client_cbw);
|
|
|
|
client_tr.Attributes["class"] = string.Format("treegrid-{0}{1} treegrid-parent-{0}", company.Id,
|
|
client.Id);
|
|
client_span.Attributes["class"] = "treegrid-expander glyphicon glyphicon-chevron-right";
|
|
client_td.InnerHtml = client_span.ToString();
|
|
client_td.SetInnerText(client.Name);
|
|
client_tr.InnerHtml = string.Format("{0}{1}", client_td, client_td1);
|
|
projlist.InnerHtml += client_tr;
|
|
|
|
#region Projects
|
|
|
|
foreach (var project in projects.Where(x => x.CompanyId == company.Id && x.ClientId == client.Id))
|
|
{
|
|
var project_tr = new TagBuilder("tr");
|
|
var project_td = new TagBuilder("td");
|
|
var project_span = new TagBuilder("span");
|
|
var project_td1 = new TagBuilder("td");
|
|
project_td1.Attributes.Add("nowrap", string.Empty);
|
|
var project_cbr = new TagBuilder("input");
|
|
project_cbr.Attributes["value"] = project.Id.ToString();
|
|
project_cbr.Attributes["type"] = "checkbox";
|
|
project_cbr.Attributes["name"] = "projectlistread";
|
|
project_cbr.Attributes["companyr"] = company.Id.ToString();
|
|
project_cbr.Attributes["clientr"] = client.Id.ToString();
|
|
|
|
var project_cbw = new TagBuilder("input");
|
|
project_cbw.Attributes["value"] = project.Id.ToString();
|
|
project_cbw.Attributes["type"] = "checkbox";
|
|
project_cbw.Attributes["name"] = "projectlistwrite";
|
|
project_cbw.Attributes["companyw"] = company.Id.ToString();
|
|
project_cbw.Attributes["clientw"] = client.Id.ToString();
|
|
|
|
project_cbr.AddCssClass("custominput");
|
|
project_cbw.AddCssClass("custominput");
|
|
|
|
if (principal != null)
|
|
{
|
|
var isUserPermissionReadFound = false;
|
|
var isUserPermissionWriteFound = false;
|
|
|
|
foreach (var selected in userPermissions)
|
|
{
|
|
if (project.Id == selected.ProjectId)
|
|
{
|
|
if (selected.Read == Permission.Allow.GetHashCode())
|
|
{
|
|
project_cbr.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
if (selected.Write == Permission.Allow.GetHashCode())
|
|
{
|
|
project_cbw.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
isUserPermissionReadFound = selected.Read != Permission.Inherited.GetHashCode();
|
|
isUserPermissionWriteFound = selected.Write != Permission.Inherited.GetHashCode();
|
|
}
|
|
}
|
|
|
|
var isFound = rolePermissions.Any(t => t.ProjectId == project.Id);
|
|
var isRead = rolePermissions.Any(t => t.ProjectId == project.Id && (t.Read == (int) Permission.Allow));
|
|
var isWrite = rolePermissions.Any(t => t.ProjectId == project.Id && (t.Write == (int)Permission.Allow));
|
|
if (isFound)
|
|
{
|
|
project_cbr.Attributes["role"] = ((int)(isRead ? Permission.Allow : Permission.Deny)).ToString(CultureInfo.InvariantCulture);
|
|
project_cbw.Attributes["role"] = ((int)(isWrite ? Permission.Allow : Permission.Deny)).ToString(CultureInfo.InvariantCulture);
|
|
|
|
if (!isUserPermissionReadFound)
|
|
{
|
|
project_cbr.Attributes["overriden"] = attrOverriden;
|
|
if (isRead)
|
|
project_cbr.Attributes["checked"] = "checked";
|
|
}
|
|
|
|
if (!isUserPermissionWriteFound)
|
|
{
|
|
project_cbw.Attributes["overriden"] = attrOverriden;
|
|
if (isWrite)
|
|
project_cbw.Attributes["checked"] = "checked";
|
|
}
|
|
}
|
|
|
|
if (!isFound)
|
|
{
|
|
project_cbr.Attributes["role"] = ((int)Permission.Deny).ToString(CultureInfo.InvariantCulture);
|
|
project_cbw.Attributes["role"] = ((int)Permission.Deny).ToString(CultureInfo.InvariantCulture);
|
|
|
|
if (!isUserPermissionReadFound)
|
|
{
|
|
project_cbr.Attributes["overriden"] = attrOverriden;
|
|
}
|
|
|
|
if (!isUserPermissionWriteFound)
|
|
{
|
|
project_cbw.Attributes["overriden"] = attrOverriden;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
foreach (var selected in rolePermissions)
|
|
{
|
|
if (project.Id == selected.ProjectId)
|
|
{
|
|
if (selected.Read == Permission.Allow.GetHashCode())
|
|
{
|
|
project_cbr.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
if (selected.Write == Permission.Allow.GetHashCode())
|
|
{
|
|
project_cbw.Attributes["checked"] = attrChecked;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
project_td1.InnerHtml = string.Format("{0}{1}", project_cbr, project_cbw);
|
|
|
|
project_tr.Attributes["class"] = string.Format("treegrid-{0} treegrid-parent-{1}{2}", project.Id,
|
|
company.Id, client.Id);
|
|
project_span.Attributes["class"] = "treegrid-indent";
|
|
project_td.InnerHtml = project_span.ToString();
|
|
project_td.SetInnerText(project.Name);
|
|
project_tr.InnerHtml = string.Format("{0}{1}", project_td, project_td1);
|
|
projlist.InnerHtml += project_tr;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#region non-company and non-client projects
|
|
|
|
//Also show non-company and non-client projects
|
|
foreach (var project in projects.Where(x => !x.CompanyId.HasValue || !x.ClientId.HasValue))
|
|
{
|
|
var projectTr = new TagBuilder("tr");
|
|
var projectTd = new TagBuilder("td");
|
|
var projectSpan = new TagBuilder("span");
|
|
var projectTd1 = new TagBuilder("td");
|
|
var projectCbr = new TagBuilder("input");
|
|
projectCbr.Attributes["value"] = project.Id.ToString();
|
|
projectCbr.Attributes["type"] = "checkbox";
|
|
projectCbr.Attributes["name"] = "projectlistread";
|
|
projectCbr.AddCssClass("custominput");
|
|
|
|
var projectCbw = new TagBuilder("input");
|
|
projectCbw.Attributes["value"] = project.Id.ToString();
|
|
projectCbw.Attributes["type"] = "checkbox";
|
|
projectCbw.Attributes["name"] = "projectlistwrite";
|
|
projectCbw.AddCssClass("custominput");
|
|
|
|
foreach (var selected in rolePermissions)
|
|
{
|
|
if (project.Id == selected.ProjectId)
|
|
{
|
|
if (selected.Read == (int)Permission.Allow)
|
|
projectCbr.Attributes["checked"] = attrChecked;
|
|
if (selected.Write == (int)Permission.Allow)
|
|
projectCbw.Attributes["checked"] = attrChecked;
|
|
}
|
|
}
|
|
|
|
projectTd1.InnerHtml = string.Format("{0}{1}", projectCbr, projectCbw);
|
|
projectTr.Attributes["class"] = string.Format("treegrid-{0} treegrid-parent-{1}{2}", project.Id, -1, -1);
|
|
projectSpan.Attributes["class"] = "treegrid-indent";
|
|
projectTd.InnerHtml = projectSpan.ToString();
|
|
projectTd.SetInnerText(project.Name);
|
|
projectTr.InnerHtml = string.Format("{0}{1}", projectTd, projectTd1);
|
|
projlist.InnerHtml += projectTr;
|
|
}
|
|
|
|
#endregion
|
|
|
|
return new MvcHtmlString(projlist.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString GetAreaItemsList(this HtmlHelper html, UrlHelper url, AspNetUser 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(!string.IsNullOrEmpty(principal.Id))
|
|
userPermissions = (from pr in context.Securities where pr.PrincipalId == new Guid(principal.Id) select pr).ToList();
|
|
|
|
var menuItems = Enum.GetValues(typeof(Areas)).Cast<Areas>().OrderBy(i => i.ToString());
|
|
|
|
var menulist = new TagBuilder("div") {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 span = new TagBuilder("span");
|
|
var td1 = new TagBuilder("td");
|
|
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;
|
|
}
|
|
}
|
|
|
|
td1.InnerHtml = string.Format("<nobr>{0}{1}</nobr>", cbr, cbw);
|
|
td.InnerHtml = span.ToString();
|
|
td.SetInnerText(AddSpacesToSentence(menuItem.ToString(),false));
|
|
tr.InnerHtml = string.Format("{0}{1}", td, td1);
|
|
menulist.InnerHtml += tr;
|
|
}
|
|
return new MvcHtmlString(menulist.ToString());
|
|
}
|
|
|
|
public static string AddSpacesToSentence(string text, bool preserveAcronyms)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return string.Empty;
|
|
StringBuilder newText = new StringBuilder(text.Length * 2);
|
|
newText.Append(text[0]);
|
|
for (int i = 1; i < text.Length; i++)
|
|
{
|
|
if (char.IsUpper(text[i]))
|
|
if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||
|
|
(preserveAcronyms && char.IsUpper(text[i - 1]) &&
|
|
i < text.Length - 1 && !char.IsUpper(text[i + 1])))
|
|
newText.Append(' ');
|
|
newText.Append(text[i]);
|
|
}
|
|
return newText.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>().OrderBy(i => i.ToString());
|
|
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 span = new TagBuilder("span");
|
|
var td1 = new TagBuilder("td");
|
|
td1.Attributes.Add("nowrap", string.Empty);
|
|
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";
|
|
}
|
|
//cbw.InnerHtml = " " + menuItem.ToDisplayValue();
|
|
//menulist.InnerHtml += cbr + " " + cbw + "</br>";
|
|
|
|
td1.InnerHtml = cbr + " " + cbw;
|
|
td.InnerHtml = span.ToString();
|
|
td.SetInnerText(menuItem.ToString());
|
|
tr.InnerHtml = td + "" + td1;
|
|
menulist.InnerHtml += tr;
|
|
}
|
|
|
|
return new MvcHtmlString(menulist.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString GetRolesList(this HtmlHelper html, UrlHelper url, AspNetUser 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());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if passed Area is allowed to be accessed by passed Principal with desired Type of access
|
|
/// </summary>
|
|
public static bool CheckSecurityObjectPermission(this HtmlHelper html, Areas area, AccessLevel type)
|
|
{
|
|
List<Areas> areas = new List<Areas>();
|
|
areas.Add(area);
|
|
return CheckAnySecurityObjectPermission(html, areas, type);
|
|
}
|
|
|
|
public static List<Expenditure> GetExpendirureCats(ScenarioDetailModel scenario)
|
|
{
|
|
EnVisageEntities context = new EnVisageEntities();
|
|
var exp_cats = (from c in context.ScenarioDetail
|
|
join o in context.ExpenditureCategory on c.ExpenditureCategoryId equals o.Id
|
|
where c.ParentID == scenario.Id
|
|
select o.Expenditure).Distinct().ToList();
|
|
|
|
return exp_cats;
|
|
}
|
|
public static MvcHtmlString GetRatesList(this HtmlHelper html, ScenarioDetailModel scenario)
|
|
{
|
|
return new MvcHtmlString("");
|
|
//var rates = GetExpendirureCatsList(scenario);
|
|
//var result = new TagBuilder("ul");
|
|
//result.Attributes["class"] = "nav nav-pills nav-stacked";
|
|
//foreach (var exp in exp_cats)
|
|
//{
|
|
// var li = new TagBuilder("li");
|
|
// li.Attributes["id"] = exp.Id.ToString();
|
|
// var a = new TagBuilder("a");
|
|
// a.Attributes["href"] = "javascript:void(0);";
|
|
// a.Attributes["onClick"] = "javascript:LoadRate(\"" + exp.Id.ToString() + "\", \"" + scenario.Id.ToString() + "\", this);";
|
|
// a.InnerHtml = exp.Expenditure.Name;
|
|
// li.InnerHtml += a;
|
|
// result.InnerHtml += li.ToString();
|
|
//}
|
|
|
|
//return new MvcHtmlString(result.ToString());
|
|
}
|
|
|
|
|
|
public static MvcHtmlString GetExpendirureCatsDropdown(this HtmlHelper html, ScenarioDetailModel scenario)
|
|
{
|
|
var dropdown = new TagBuilder("input");
|
|
dropdown.Attributes["Type"] = "select";
|
|
var exp_cats = GetExpendirureCats(scenario);
|
|
foreach (var ec in exp_cats)
|
|
{
|
|
var option = new TagBuilder("option");
|
|
option.Attributes["value"] = ec.Id.ToString();
|
|
option.InnerHtml = ec.Name;
|
|
dropdown.InnerHtml += option.ToString();
|
|
}
|
|
|
|
return new MvcHtmlString(dropdown.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if any of passed Areas is allowed to be accessed by passed Principal with desired Type of access
|
|
/// </summary>
|
|
public static bool CheckAnySecurityObjectPermission(this HtmlHelper html, List<Areas> areas, AccessLevel type)
|
|
{
|
|
List<string> stringAreas = new List<string>();
|
|
if (areas != null && areas.Count > 0)
|
|
areas.ForEach(a => stringAreas.Add(a.ToString()));
|
|
|
|
var principalId = HttpContext.Current.User.Identity.GetUserId();
|
|
|
|
if (principalId == null) return false;
|
|
//EnVisageEntities context = new EnVisageEntities();
|
|
SecurityAreasCache securityAreaCache = new SecurityAreasCache();
|
|
/*var user = (from pr in context.AspNetUsers
|
|
where pr.Id == principalId
|
|
select pr).FirstOrDefault();*/
|
|
|
|
List<UserAreaAccess> s = new List<UserAreaAccess>();
|
|
foreach(var area in stringAreas)
|
|
s.AddRange(securityAreaCache.Value.Where(x => x.PrincipalId == new Guid(principalId) && x.SecurityObject == area));
|
|
|
|
if (s.Any())
|
|
{
|
|
if (type == AccessLevel.Write)
|
|
{
|
|
return s.Any(x => x.Write == 1);
|
|
}
|
|
if (type == AccessLevel.Read)
|
|
{
|
|
return s.Any(x => (x.Read == 1 || x.Write == 1));
|
|
}
|
|
}
|
|
|
|
//var roles = user.AspNetRoles.Select(x => new Guid(x.Id));
|
|
var roles = new UsersCache().Value.FirstOrDefault(x => x.Id == new Guid(principalId)).Roles;
|
|
s = new List<UserAreaAccess>();
|
|
foreach(var role in roles)
|
|
{
|
|
foreach (var area in stringAreas)
|
|
s.AddRange(securityAreaCache.Value.Where(x => x.PrincipalId == role && x.SecurityObject == area));
|
|
}
|
|
|
|
if (s.Any())
|
|
{
|
|
if (type == AccessLevel.Write)
|
|
return s.Any(x => x.Write == 1);
|
|
if (type == AccessLevel.Read)
|
|
return s.Any(x => (x.Read == 1 || x.Write == 1));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static bool CheckProjectPermission(this HtmlHelper html, Guid ProjectId, AccessLevel Type)
|
|
{
|
|
var PrincipalId = HttpContext.Current.User.Identity.GetUserId();
|
|
if (PrincipalId == null || ProjectId == null) return false;
|
|
|
|
//EnVisageEntities context = new EnVisageEntities();
|
|
ProjectAccessCache projectAccessCache = new ProjectAccessCache();
|
|
|
|
var projectAccess = projectAccessCache.Value
|
|
.FirstOrDefault(x => x.PrincipalId == new Guid(PrincipalId) && x.ProjectId == ProjectId);
|
|
|
|
if (projectAccess != null)
|
|
{
|
|
if (Type == AccessLevel.Write && projectAccess.Write == 1)
|
|
return true;
|
|
else if (Type == AccessLevel.Read && (projectAccess.Read == 1 || projectAccess.Write == 1))
|
|
return true;
|
|
}
|
|
|
|
//var roles = context.AspNetUsers.FirstOrDefault(x => x.Id == PrincipalId).AspNetRoles.Select(x => new Guid(x.Id));
|
|
var roles = new UsersCache().Value.FirstOrDefault(x => x.Id == new Guid(PrincipalId)).Roles;
|
|
|
|
List<UserProjectAccess> projectAccesses = new List<UserProjectAccess>();
|
|
|
|
foreach(var role in roles)
|
|
{
|
|
projectAccesses.AddRange(projectAccessCache.Value
|
|
.Where(x => x.PrincipalId == role && x.ProjectId == ProjectId && (x.Read == 1 || x.Write == 1)));
|
|
}
|
|
|
|
if (projectAccesses == null || !projectAccesses.Any())
|
|
return false;
|
|
|
|
if (Type == AccessLevel.Write && projectAccesses.Any(x => x.Write == 1))
|
|
return true;
|
|
if (Type == AccessLevel.Read && projectAccesses.Any(x => (x.Read == 1 || x.Write == 1)))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |