using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.Xml; using System.Web; using IronIntel.Contractor.Users; using Foresight.ServiceModel; using Foresight.Fleet.Services.Styles; using Foresight.Fleet.Services; using Foresight.Fleet.Services.User; using Foresight; using IronIntel.Contractor.Machines; using Foresight.Fleet.Services.JobSite; using Foresight.Fleet.Services.Asset; using System.Web.UI.WebControls; using FI.FIC.Contracts.DataObjects.BaseObject; using System.Security.Cryptography; namespace IronIntel.Contractor.Site.SystemSettings { public class SystemSettingsBasePage : ContractorBasePage { protected bool IsDev { get { var session = GetCurrentLoginSession(); if (session != null) { if (Helper.DevAccounts.Contains(session.User.ID, StringComparer.OrdinalIgnoreCase)) return true; } return false; } } protected void ProcessRequest(string methodName) { object result = null; try { if (methodName != null) { switch (methodName.ToUpper()) { case "GETCOMPANYUISTYLES": result = GetCompanyUIStyles(); break; case "SETCOMPANYUISTYLE": result = SetCompanyUIStyle(); break; case "DELETECOMPANYUISTYLE": result = DeleteCompanyUIStyle(); break; case "GETUSEROPTIONS": result = GetUserOptions(); break; case "SAVEUSEROPTIONS": result = SaveUserOptions(); break; case "GETTIMEZONES": result = GetTimeZones(); break; case "GETSYSTEMOPTIONS": result = GetSystemOptions(); break; case "SAVESYSTEMOPTIONS": result = SaveSystemOptions(); break; case "GETLANGUAGES": result = GetLanguages(); break; case "GETSYSTEMUNITOFODOMETER": result = GetSystemUnitOfOdometer(); break; case "GETASSETGROUPLIST": result = GetAssetGroupList(); break; case "GETJOBSITELIST": result = GetJobsiteList(); break; case "GETASSETTYPES": result = GetAssetTypes(); break; case "GETFILTERTEMPLATES": result = GetFilterTemplates(); break; case "GETFILTERTEMPLATEITEM": result = GetFilterTemplateItem(); break; case "GETUSERASSIGNEDASSETGROUPS": result = GetUserAssignedAssetGroups(); break; case "GETUSERASSIGNEDJOBSITES": result = GetUserAssignedJobsites(); break; case "GETUSERASSIGNEDASSETTYPES": result = GetUserAssignedAssetTypes(); break; case "SAVEASSETALERTFILTER": result = SaveAssetAlertFilter(); break; } } } catch (Exception ex) { result = ex.Message; SystemParams.WriteLog("Error", "Settingcolor.ProcessRequest", ex.Message, ex.ToString()); } string json = JsonConvert.SerializeObject(result); Response.Write(json); Response.End(); } private object GetCompanyUIStyles() { try { var session = GetCurrentLoginSession(); if (session != null) { CustUIStyleList uis = CreateClient().GetCompanyUIStyles(SystemParams.CompanyID); List list = new List(); foreach (CustUIStyle ui in uis) { CustUIStyleItem item = new CustUIStyleItem(); Helper.CloneProperty(item, ui); list.Add(item); } if (list.Count() > 0) return list.ToArray(); else return new CustUIStyleItem[0]; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private string SetCompanyUIStyle() { try { var session = GetCurrentLoginSession(); if (session != null) { string uidata = Request.Params["ClientData"]; CustUIStyleItem item = JsonConvert.DeserializeObject(uidata); item.UIStyleName = HttpUtility.HtmlDecode(item.UIStyleName); CustUIStyle ui = new CustUIStyle(); Helper.CloneProperty(ui, item); CreateClient().SetCompanyUIStyle(SystemParams.CompanyID, ui); return "OK"; } else { return "User being not logged in."; } } catch (Exception ex) { return ex.Message; } } private string DeleteCompanyUIStyle() { try { var session = GetCurrentLoginSession(); if (session != null) { string uistyleid = HttpUtility.HtmlDecode(Request.Params["ClientData"]); int styleid = -1; int.TryParse(uistyleid, out styleid); CreateClient().DeleteCompanyUIStyle(SystemParams.CompanyID, styleid); return "OK"; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object GetUserOptions() { try { var session = GetCurrentLoginSession(); if (session != null) { var up = UserParams.GetUserParams(session.SessionID, session.User.UID); return up; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object SaveUserOptions() { try { var session = GetCurrentLoginSession(); if (session != null) { string content = HttpUtility.HtmlDecode(Request.Params["ClientData"]); var user = JsonConvert.DeserializeObject(content); var uid = session.User.UID; UserParamInfo upi = user.UserParam; UserParams.SetUserParams(uid, upi); var uc = CreateClient(); uc.UpdateUserPreferredLanguage(uid, upi.PreferredLanguage); uc.UpdateUserTimeZone(uid, upi.TimeZone); // save subscribe message if (user.Subscribe != null) { user.Subscribe.UserIID = uid; List filters = null; if (!string.IsNullOrEmpty(user.UserAlertFilter)) { filters = JsonConvert.DeserializeObject>(user.UserAlertFilter); } FI.FIC.Models.WorkspaceManager.SaveSubscribeMessageByEmail(user.Subscribe, filters, uid); } //FIC Alert Filter Templates var userfilterclient = CreateClient(); if (user.FilterTemplates != null && user.FilterTemplates.Length > 0) { foreach (UserFilterTemplateItem filter in user.FilterTemplates) { if (filter.Id < 0) filter.Id = userfilterclient.AddFilterTemplate(SystemParams.CompanyID, uid, filter.Name); else userfilterclient.ChangeFilterName(SystemParams.CompanyID, filter.Id, filter.Name); long[] assetids = null; if (filter.Assets != null && filter.Assets.Length > 0) assetids = filter.Assets.Select(m => m.Id).ToArray(); userfilterclient.SetAssetFilter(SystemParams.CompanyID, filter.Id, assetids); string[] assetgroups = null; if (filter.AssetGroups != null && filter.AssetGroups.Length > 0) assetgroups = filter.AssetGroups.Select(m => m.GroupID).ToArray(); userfilterclient.SetAssetGroupFilter(SystemParams.CompanyID, filter.Id, assetgroups); int[] assettypes = null; if (filter.AssetTypes != null && filter.AssetTypes.Length > 0) assettypes = filter.AssetTypes.Select(m => m.ID).ToArray(); userfilterclient.SetAssetTypeFilter(SystemParams.CompanyID, filter.Id, assettypes); long[] jobsites = null; if (filter.Jobsites != null && filter.Jobsites.Length > 0) jobsites = filter.Jobsites.Select(m => m.ID).ToArray(); userfilterclient.SetJobSiteFilter(SystemParams.CompanyID, filter.Id, jobsites); } } if (user.DeleteFilterTemplates != null && user.DeleteFilterTemplates.Length > 0) { foreach (int id in user.DeleteFilterTemplates) { userfilterclient.DeleteFilterTemplate(SystemParams.CompanyID, id); } } return "OK"; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object SaveAssetAlertFilter() { try { var session = GetCurrentLoginSession(); if (session != null) { string content = HttpUtility.HtmlDecode(Request.Params["ClientData"]); var user = JsonConvert.DeserializeObject(content); var uid = session.User.UID; var userfilterclient = CreateClient(); if (user.FilterTemplates != null && user.FilterTemplates.Length > 0) { foreach (UserFilterTemplateItem filter in user.FilterTemplates) { if (filter.Id < 0) filter.Id = userfilterclient.AddFilterTemplate(SystemParams.CompanyID, uid, filter.Name); else userfilterclient.ChangeFilterName(SystemParams.CompanyID, filter.Id, filter.Name); long[] assetids = null; if (filter.Assets != null && filter.Assets.Length > 0) assetids = filter.Assets.Select(m => m.Id).ToArray(); userfilterclient.SetAssetFilter(SystemParams.CompanyID, filter.Id, assetids); string[] assetgroups = null; if (filter.AssetGroups != null && filter.AssetGroups.Length > 0) assetgroups = filter.AssetGroups.Select(m => m.GroupID).ToArray(); userfilterclient.SetAssetGroupFilter(SystemParams.CompanyID, filter.Id, assetgroups); int[] assettypes = null; if (filter.AssetTypes != null && filter.AssetTypes.Length > 0) assettypes = filter.AssetTypes.Select(m => m.ID).ToArray(); userfilterclient.SetAssetTypeFilter(SystemParams.CompanyID, filter.Id, assettypes); long[] jobsites = null; if (filter.Jobsites != null && filter.Jobsites.Length > 0) jobsites = filter.Jobsites.Select(m => m.ID).ToArray(); userfilterclient.SetJobSiteFilter(SystemParams.CompanyID, filter.Id, jobsites); } } if (user.DeleteFilterTemplates != null && user.DeleteFilterTemplates.Length > 0) { foreach (int id in user.DeleteFilterTemplates) { userfilterclient.DeleteFilterTemplate(SystemParams.CompanyID, id); } } return "OK"; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object GetTimeZones() { try { var session = GetCurrentLoginSession(); if (session != null) { var timezones = SystemParams.GetTimeZones(); return timezones; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object GetSystemUnitOfOdometer() { try { var session = GetCurrentLoginSession(); if (session != null) { return SystemParams.GetStringParam("UnitOfOdometer"); } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object GetLanguages() { try { var session = GetCurrentLoginSession(); if (session != null) { return CountryAndRegionCode.SupportedLanguages; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object GetSystemOptions() { try { var session = GetCurrentLoginSession(); if (session != null) { string af = SystemParams.GetStringParam("AccuracyFilter"); double accuracyfilter = 0; double.TryParse(af, out accuracyfilter); string au = SystemParams.GetStringParam("AreaUnits"); int areaunits = 0; int.TryParse(au, out areaunits); string vu = SystemParams.GetStringParam("VolumnUnits"); int volumeunits = 0; int.TryParse(vu, out volumeunits); string wu = SystemParams.GetStringParam("WeightUnits"); int weightunits = 0; int.TryParse(wu, out weightunits); string rmd = SystemParams.GetStringParam("MFARememberMeDays"); int remembermedays = 0; if (!int.TryParse(rmd, out remembermedays)) remembermedays = 30; SystemOptionInfo soi = new SystemOptionInfo(); soi.TimeZone = SystemParams.GetStringParam("CustomerTimeZone", false); soi.AccuracyFilter = accuracyfilter; soi.UnitOfOdometer = SystemParams.GetStringParam("UnitOfOdometer"); soi.AcknowledgingAlerts = SystemParams.GetStringParam("AcknowledgingAlerts"); soi.LoginVerifyType = SystemParams.GetStringParam("LoginVerifyType"); soi.AreaUnits = areaunits; soi.VolumeUnits = volumeunits; soi.WeightUnits = weightunits; soi.MFARememberMeDays = remembermedays; string connectorxml = SystemParams.GetStringParam("Connector"); soi.Connectors = ConnectorHelper.FromXML(connectorxml); return soi; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } private object SaveSystemOptions() { try { var session = GetCurrentLoginSession(); if (session != null) { string options = HttpUtility.HtmlDecode(Request.Params["ClientData"]); SystemOptionInfo upi = JsonConvert.DeserializeObject(options); SystemParams.SetStringParam("CustomerTimeZone", upi.TimeZone); SystemParams.SetStringParam("CustomerTimeZoneOffset", upi.Offset.ToString()); SystemParams.SetStringParam("AccuracyFilter", upi.AccuracyFilter.ToString()); SystemParams.SetStringParam("UnitOfOdometer", upi.UnitOfOdometer); SystemParams.SetStringParam("AcknowledgingAlerts", upi.AcknowledgingAlerts); SystemParams.SetStringParam("AreaUnits", upi.AreaUnits.ToString()); SystemParams.SetStringParam("VolumnUnits", upi.VolumeUnits.ToString()); SystemParams.SetStringParam("WeightUnits", upi.WeightUnits.ToString()); SystemParams.SetStringParam("LoginVerifyType", upi.LoginVerifyType); SystemParams.SetStringParam("MFARememberMeDays", upi.MFARememberMeDays.ToString()); XmlDocument doc = ConnectorHelper.ToXml(upi.Connectors); SystemParams.SetStringParam("Connector", doc.InnerXml); return "OK"; } else { throw new Exception("not login."); } } catch (Exception ex) { return ex.Message; } } #region user filter private object GetFilterTemplates() { try { var session = GetCurrentLoginSession(); if (session != null) { var client = CreateClient(); KeyValuePair[] filters = client.GetFilterTemplates(SystemParams.CompanyID, session.User.UID); if (filters == null && filters.Length == 0) return new KeyValuePair[0]; return filters; } else { return new KeyValuePair[0]; } } catch (Exception ex) { return ex.Message; } } private object GetFilterTemplateItem() { try { var clientdata = Request.Form["ClientData"]; string filteridstr = HttpUtility.HtmlDecode(clientdata); int filterid = Convert.ToInt32(filteridstr); var session = GetCurrentLoginSession(); if (session != null) { var client = CreateClient(); string useriid = session.User.UID; bool accessallassets = false; Foresight.Fleet.Services.User.UserAdditionalAttribute attrs = CreateClient(SystemParams.CompanyID).GetUserAdditionalAttribute(useriid); if (attrs != null) accessallassets = attrs.AccessAllAssets; UserFilterTemplateItem filter = new UserFilterTemplateItem(); List lsasset = new List(); var fassets = client.GetAssetFilter(SystemParams.CompanyID, filterid); if (fassets != null && fassets.Length > 0) { var selitems = CreateClient(SystemParams.CompanyID).GetAssetsAssignedToUser(SystemParams.CompanyID, useriid); foreach (var ai in fassets) { AssetListItemInfo item = new AssetListItemInfo(); item.Id = ai.Id; item.Name = ai.Name; item.VIN = ai.VIN; item.MakeName = ai.MakeName; item.ModelName = ai.ModelName; item.TypeName = ai.TypeName; if (accessallassets) lsasset.Add(item); else { if (selitems != null && selitems.Length > 0 && selitems.Select(m => m.Id).Contains(ai.Id)) lsasset.Add(item); } } } filter.Assets = lsasset.ToArray(); List lsag = new List(); var fassetgroups = client.GetAssetGroupFilter(SystemParams.CompanyID, filterid); if (fassetgroups != null && fassetgroups.Length > 0) { var selitems = MachineManagement.GetMachineGroupByUser(useriid).OrderBy((m) => m.GroupName).ToArray(); foreach (var kv in fassetgroups) { MachineGroup item = new MachineGroup(); item.GroupID = kv.Key; item.GroupName = kv.Value; if (accessallassets) lsag.Add(item); else { if (selitems != null && selitems.Length > 0 && selitems.Select(m => m.GroupID).Contains(kv.Key)) lsag.Add(item); } } } filter.AssetGroups = lsag.ToArray(); List lstp = new List(); var fassettypes = client.GetAssetTypeFilter(SystemParams.CompanyID, filterid); if (fassettypes != null && fassettypes.Length > 0) { var selitems = MachineManagement.GetMachineTypesByUser(useriid); foreach (var kv in fassettypes) { AssetType item = new AssetType(); item.ID = kv.Key; item.Name = kv.Value; if (accessallassets) lstp.Add(item); else { if (selitems != null && selitems.Length > 0 && selitems.Select(m => m.ID).Contains(kv.Key)) lstp.Add(item); } } } filter.AssetTypes = lstp.ToArray(); List lsjs = new List(); var fjobsites = client.GetJobsiteFilter(SystemParams.CompanyID, filterid); if (fjobsites != null && fjobsites.Length > 0) { var selitems = CreateClient().GetUserJobsites(SystemParams.CompanyID, useriid); foreach (var kv in fjobsites) { JobSiteItem item = new JobSiteItem(); item.ID = kv.Key; item.Name = kv.Value; if (accessallassets) lsjs.Add(item); else { if (selitems != null && selitems.Length > 0 && selitems.Select(m => m.ID).Contains(kv.Key)) lsjs.Add(item); } } } filter.Jobsites = lsjs.ToArray(); return filter; } else { return null; } } catch (Exception ex) { return ex.Message; } } private object GetAssetGroupList() { try { MachineGroup[] items = null; if (GetCurrentLoginSession() != null) { items = MachineManagement.GetMachineGroups("").OrderBy((m) => m.GroupName).ToArray(); } else { items = new MachineGroup[0]; } return items; } catch (Exception ex) { return ex.Message; } } private object GetJobsiteList() { try { JobSiteItem[] items = null; if (GetCurrentLoginSession() != null) { var s = Request.Form["ClientData"]; s = HttpUtility.HtmlDecode(s); items = CreateClient().GetJobSiteItems(SystemParams.CompanyID, "", false); } else { items = new JobSiteItem[0]; } return items; } catch (Exception ex) { return ex.Message; } } private object GetAssetTypes() { try { var session = GetCurrentLoginSession(); if (session != null) { AssetType[] types = CreateClient().GetAssetTypes(SystemParams.CompanyID); return types.OrderBy((t) => t.Name).ToArray(); } else return new AssetType[0]; } catch (Exception ex) { return ex.Message; } } private object GetUserAssignedAssetGroups() { try { var clientdata = Request.Form["ClientData"]; var uid = HttpUtility.HtmlDecode(clientdata); MachineGroup[] items = null; if (GetCurrentLoginSession() != null) { var ui = UserManagement.GetUserByIID(uid); if (ui.UserType < IronIntel.Contractor.Users.UserTypes.Admin) { bool accessallassets = GetAccessAllAssets(uid); if (accessallassets) return GetAssetGroupList(); else items = MachineManagement.GetMachineGroupByUser(uid).OrderBy((m) => m.GroupName).ToArray(); } else return GetAssetGroupList(); } else { items = new MachineGroup[0]; } return items; } catch (Exception ex) { return ex.Message; } } private object GetUserAssignedAssetTypes() { try { var clientdata = Request.Form["ClientData"]; var uid = HttpUtility.HtmlDecode(clientdata); AssetType[] items = null; if (GetCurrentLoginSession() != null) { var ui = UserManagement.GetUserByIID(uid); if (ui.UserType < IronIntel.Contractor.Users.UserTypes.Admin) { bool accessallassets = GetAccessAllAssets(uid); if (accessallassets) return GetAssetTypes(); else items = MachineManagement.GetMachineTypesByUser(uid); } else return GetAssetTypes(); } else { items = new AssetType[0]; } return items; } catch (Exception ex) { return ex.Message; } } private object GetUserAssignedJobsites() { try { JobSiteItem[] items = null; if (GetCurrentLoginSession() != null) { var uid = Request.Form["ClientData"]; uid = HttpUtility.HtmlDecode(uid); var ui = UserManagement.GetUserByIID(uid); if (ui.UserType < IronIntel.Contractor.Users.UserTypes.Admin) { bool accessallassets = GetAccessAllAssets(uid); if (accessallassets) return GetJobsiteList(); else items = CreateClient().GetUserJobsites(SystemParams.CompanyID, uid); } else return GetJobsiteList(); } else { items = new JobSiteItem[0]; } return items; } catch (Exception ex) { return ex.Message; } } private bool GetAccessAllAssets(string uid) { bool accessallassets = false; var attrs = CreateClient(SystemParams.CompanyID).GetUserAdditionalAttribute(uid); if (attrs != null) accessallassets = attrs.AccessAllAssets; return accessallassets; } #endregion private class CustUIStyleItem { public int UIStyleID { get; set; } public string UIStyleName { get; set; } public bool IsDefault { get; set; } public string TitleBarColor { get; set; } public string MenuBackgroundColor { get; set; } public string ChartTitleBackgroundColor { get; set; } public string ChartBorderColor { get; set; } } private class SystemOptionInfo { public string TimeZone { get; set; } public int Offset { get; set; } public double AccuracyFilter { get; set; } public string UnitOfOdometer { get; set; } public string AcknowledgingAlerts { get; set; } public int AreaUnits { get; set; } public int VolumeUnits { get; set; } public int WeightUnits { get; set; } public string LoginVerifyType { get; set; } public StringKeyValue[] Connectors { get; set; } public int MFARememberMeDays { get; set; } } private class UserOptionObject { public UserParamInfo UserParam { get; set; } public SubscribeMessageByEmail Subscribe { get; set; } public string UserAlertFilter { get; set; } public UserFilterTemplateItem[] FilterTemplates { get; set; } public int[] DeleteFilterTemplates { get; set; } } } }