using System; using System.Reflection; using System.Security.Principal; using System.Web; using System.Web.Mvc; using System.Web.Security; using Sleis.Models; using Sleis.ViewModels; using System.Collections.Generic; namespace Sleis.Utility { public static class ViewUtility { public static UserView GetCurrentUser() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView; } public static SelectedFacilityView GetSelectedFacility() { SelectedFacilityView facView = null; UserView userView = GetCurrentUser(); if (userView != null) { facView = userView.SelectedFacility; } return facView; } public static T Get(IDictionary dict, string key) { T result = default(T); if (dict != null && !String.IsNullOrEmpty(key) && dict.ContainsKey(key)) { result = (T)dict[key]; } return result; } public static string GetViewBool(bool val) { return val.ToString().ToLower(); } public static string AgencyRoleNames() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView.User.AssignedRoles; } public static bool IsAgencyUser() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Type == SleisUserTypeType.Agency; } public static bool IsAgencyAdmin() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Roles.Contains(AppUserRoleType.AgencyAdmin); } public static bool IsAgencyAccepter() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Roles.Contains(AppUserRoleType.AgencyAcceptor); } public static bool IsAgencyEditor() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Roles.Contains(AppUserRoleType.AgencyEditor); } public static bool IsAgencySubmitter() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Roles.Contains(AppUserRoleType.AgencySubmitter); } public static bool IsAgencyViewer() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && userView.User.Roles.Contains(AppUserRoleType.AgencyViewer); } public static Dictionary PageFields(TempDataDictionary tempData) { return tempData[Constants.PageFieldsKey] as Dictionary; } public static bool IsEditor() { UserView userView = HttpContext.Current.Session[UserModel.SESSION_USER_KEY] as UserView; return userView != null && userView.User != null && (userView.User.Roles.Contains(AppUserRoleType.AgencyEditor) || userView.User.Roles.Contains(AppUserRoleType.FacilityEditor)); } public static bool ReportInProcess(AgencyReport report) { if (SessionUtility.CurrentUser.IsAgencyUser()) { return report.Status == ReportStatus.UnderReview; } return report.IsInProcess; } public static string LastEditedBy(BaseAuthoredModel obj) { if (obj.LastUpdatedBy == null || obj.LastUpdated == null) { return ""; } return obj.LastUpdatedBy.FullName + "
" + obj.LastUpdated.Value.ToString("yyyy-MM-dd hh:mm tt"); } public static IEnumerable GetUserStatusSelectItems() { List items = new List(); items.Add(new SelectListItem { Text = SleisUserStatusType.Active.ToString(), Value = SleisUserStatusType.Active.ToString() }); items.Add(new SelectListItem { Text = SleisUserStatusType.Inactive.ToString(), Value = SleisUserStatusType.Inactive.ToString() }); items.Add(new SelectListItem { Text = SleisUserStatusType.Locked.ToString(), Value = SleisUserStatusType.Locked.ToString() }); return items; } } }