// // GET: /Manage/Index public async Task<ActionResult> Index(ManageMessageId? message) { ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed." : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set." : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set." : message == ManageMessageId.Error ? "An error has occurred." : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added." : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed." : ""; var userId = User.Identity.GetUserId(); User user = null; using (var db = new StationCADDb()) { user = db.Users.Include("Profile").Where(x => x.Id == userId).FirstOrDefault(); } if (User == null) { throw new ApplicationException(string.Format("No user data. User ID: {0}", userId)); } var model = new IndexViewModel(user); model.HasPassword = HasPassword(); model.PhoneNumber = await UserManager.GetPhoneNumberAsync(userId); model.TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId); model.Logins = await UserManager.GetLoginsAsync(userId); model.BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId); return View(model); }
public void AddUser() { using (var db = new StationCADDb()) { var usr = new UserProfile(); usr.FirstName = string.Format("FirstName_{0}", DateTime.Now.Ticks); usr.LastName = string.Format("LastName_{0}", DateTime.Now.Ticks); usr.IdentificationNumber = DateTime.Now.Ticks.ToString(); //usr.UserName = string.Format("{0}.{1}", usr.FirstName, usr.LastName); usr.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); usr.OrganizationAffiliations.Add(new OrganizationUserAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); usr.NotificationEmail = "*****@*****.**"; usr.MobileDevices = new List<UserMobileDevice>(); usr.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); db.UserProfiles.Add(usr); db.SaveChanges(); Assert.IsTrue(usr.Id > 0); List<UserProfile> users = db.UserProfiles .Include("MobileDevices") .Include("OrganizationAffiliations") .Where(w => w.OrganizationAffiliations.Where(x => x.CurrentOrganization.Id == 1).Count() > 0) .ToList<UserProfile>(); var afterUser = db.UserProfiles .Include("OrganizationAffiliations") .Include("MobileDevices") .Where(x => x.IdentificationNumber == usr.IdentificationNumber) .FirstOrDefault(); db.UserProfiles.Remove(afterUser); db.SaveChanges(); } }
public void CreateIncident() { Incident inc = new Incident(); inc.OrganizationId = 1; inc.CADIdentifier = 1; inc.CallerAddress = "Caller Address 1"; inc.CallerName = "Caller Name 1"; inc.CallerPhone = "Caller Phone 1"; inc.ConsoleID = "Console 1"; inc.DispatchedDateTime = DateTime.Now; inc.IncidentIdentifier = Guid.NewGuid(); inc.Title = "Title"; IncidentAddress add = new IncidentAddress(); add.Number = "1"; add.Street = "street"; add.City = "city"; inc.LocationAddresses = new List<IncidentAddress>(); inc.LocationAddresses.Add(add); IncidentUnit unit = new IncidentUnit(); unit.UnitID = "Unit 1"; unit.EnteredDateTime = DateTime.Now; unit.Disposition = "DP"; inc.Units = new List<IncidentUnit>(); inc.Units.Add(unit); IncidentNote cmt = new IncidentNote(); cmt.Message = "Comment 1"; cmt.EnteredDateTime = DateTime.Now; cmt.Author = "Author"; inc.Notes = new List<IncidentNote>(); inc.Notes.Add(cmt); using (var db = new StationCADDb()) { Incident existing = db.Incidents.Where(x => x.Title == "Title").FirstOrDefault(); if (existing != null) { db.Incidents.Remove(existing); db.SaveChanges(); } db.Incidents.Add(inc); db.SaveChanges(); Incident saved = db.Incidents.Where(x => x.Title == "Title").FirstOrDefault(); Assert.IsNotNull(saved); db.Incidents.Remove(saved); db.SaveChanges(); } }
public ActionResult Index(int orgID) { Organization orgModel = new Organization(); using (var db = new StationCADDb()) { orgModel = db.Organizations .Include("Addresses") .Include("IncidentHistory") .Where(x => x.Id == orgID) .FirstOrDefault(); } return View(orgModel); }
public void EmailAPINotificationTest() { using (var db = new StationCADDb()) { Incident inc = db.Incidents .Include("Organization") .Include("LocationAddresses") .Include("Notes") .Include("Units") .Where(x => x.LocalIncidentID == "F16001462").FirstOrDefault(); if (inc != null) { UserProfile usr; usr = db.UserProfiles .Include("OrganizationAffiliations") .Include("MobileDevices") .Where(w => w.NotificationEmail == "*****@*****.**") .FirstOrDefault(); if (usr == null) { usr = new UserProfile(); usr.FirstName = string.Format("FirstName_{0}", DateTime.Now.Ticks); usr.LastName = string.Format("LastName_{0}", DateTime.Now.Ticks); usr.IdentificationNumber = DateTime.Now.Ticks.ToString(); //usr.UserName = string.Format("{0}.{1}", usr.FirstName, usr.LastName); usr.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); usr.OrganizationAffiliations.Add(new OrganizationUserAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); usr.NotificationEmail = "*****@*****.**"; usr.MobileDevices = new List<UserMobileDevice>(); usr.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); db.UserProfiles.Add(usr); db.SaveChanges(); } EmailNotification email = inc.GetEmailNotification(usr.OrganizationAffiliations.First()); string emailResult = Email.SendEmailMessage(email); SMSEmailNotification sms = inc.GetSMSEmailNotification(usr.OrganizationAffiliations.First()); string smsResult = Email.SendEmailMessage(sms); Console.WriteLine(string.Format("Email: {0}; SMS: {1}", emailResult, smsResult)); db.UserProfiles.Remove(usr); db.SaveChanges(); } else { Console.WriteLine("Couldn't find it."); } } }
public void LoadExistingIncident() { using (var db = new StationCADDb()) { Incident inc = db.Incidents .Include("Organization") .Include("LocationAddresses") .Include("Notes") .Include("Units") .Where(x => x.LocalIncidentID == "F16001462").FirstOrDefault(); if (inc != null) { string json = JsonUtil<Incident>.ToJson(inc); Console.WriteLine(json); } else { Console.WriteLine("Couldn't find it."); } } }
private async Task<List<UserRegistration>> ImportUsers(OrgUserRegistration orgUserReg) { List<UserRegistration> userErrors = new List<UserRegistration>(); using (var db = new StationCADDb()) { // Loop thru.... foreach (UserRegistration item in orgUserReg.Users) { User user = null; try { // 1. Check to see if the user exists user = db.Users.Include("Profile").Where(x => x.Email == item.Email).FirstOrDefault(); // 1b. if not, CreateAsync() for User if (user == null) { user = new User { Id = Guid.NewGuid().ToString(), UserName = item.Email, Email = item.Email }; DateTime now = DateTime.Now; user.Profile = new UserProfile { FirstName = item.FirstName, LastName = item.LastName, CreateUser = "", CreateDate = now, LastUpdateUser = "", LastUpdateDate = now }; var result = await UserManager.CreateAsync(user, "P@ssword1"); if (!result.Succeeded) { userErrors.Add(item); break; } } // add user-org-affiliation OrganizationUserAffiliation uoa = new OrganizationUserAffiliation(); uoa.CurrentOrganization = orgUserReg.Organization; uoa.Role = OrganizationUserRole.User; uoa.Status = OrganizationUserStatus.Active; user.Profile.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); user.Profile.OrganizationAffiliations.Add(uoa); await db.SaveChangesAsync(); } catch(Exception ex) { string msg = string.Format("", ""); base.LogException("", ex); userErrors.Add(item); break; } // 3. Create Email confirmation // 3a. If !emailConfirmed, create emailConfirmToken and send email if (!user.EmailConfirmed) { string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string body = string.Format("{1}, {0}{0} You have been added as a user to {2}. Please confirm your account by clicking <a href=\"{3}\">here</a>. You will be required to choose a new password. {0}{0} Thanks, {0}{0} The StationCAD Team", Environment.NewLine, user.Profile.FirstName, orgUserReg.Organization.Name, callbackUrl); await UserManager.SendEmailAsync(user.Id, "StationCAD - Activate your account", body); } // 3b. Just send email letting them know they have been added to the org else { string body = string.Format("{1}, {0}{0} You have been added as a user to {2}. {0}{0} Thanks, {0}{0} The StationCAD Team", Environment.NewLine, user.Profile.FirstName, orgUserReg.Organization.Name); await UserManager.SendEmailAsync(user.Id, "StationCAD - You are part of a new Organization!", body); } } } return userErrors; }
public ActionResult Process(FormCollection oColl) { HttpStatusCodeResult httpResult; string recipient = string.Empty; Organization org; string sender = string.Empty; string body = string.Empty; string attachmentData = string.Empty; string userIP = string.Empty; string userDomain = string.Empty; string json = string.Empty; string err = string.Empty; string uvFileCnt = string.Empty; string vFileCnt = string.Empty; int fileLen = 0; byte[] fileContent = new byte[0]; string attachmentName = string.Empty; StringBuilder sb = new StringBuilder(); try { recipient = Request.Unvalidated.Form["recipient"]; string[] recipientParts = recipient.Split('@'); using (var db = new StationCADDb()) { string tag = recipientParts[0]; org = db.Organizations .Include("NotificationRules") .Where(x => x.Tag == tag).FirstOrDefault(); if (org == null) throw new InvalidProgramException(string.Format("Invalid Organization tag: {0}", tag)); } sender = Request.Unvalidated.Form["sender"]; body = Request.Unvalidated.Form["body-plain"]; DateTime eventRecieved = DateTime.Now; // Validate the sender userIP = Request.UserHostAddress; userDomain = Request.UserHostName; sb.AppendLine(string.Format("User IP:{1}{0}{0}", Environment.NewLine, userIP)); sb.AppendLine(string.Format("User Domain:{1}{0}{0}", Environment.NewLine, userDomain)); var formkeys = Request.Unvalidated.Form.Keys; sb.AppendLine(string.Format("Keys:{0}", Environment.NewLine)); foreach (var item in formkeys) { string value = Request.Unvalidated.Form[item.ToString()]; sb.AppendLine(string.Format("Keys: {1}; Value: {2}{0}", Environment.NewLine, item.ToString(), value)); } vFileCnt = Request.Files.Count.ToString(); uvFileCnt = Request.Unvalidated.Files.Count.ToString(); sb.AppendLine(string.Format("Attachment Count: {1}, {2}{0}{0} ", Environment.NewLine, vFileCnt, uvFileCnt)); if (Request.Unvalidated.Files.Count > 0) { // for this example; processing just the first file HttpPostedFileBase file = Request.Unvalidated.Files[0]; fileLen = file.ContentLength; attachmentName = file.FileName; sb.AppendLine(string.Format("Length:{0}{1}{0}{0}", Environment.NewLine, fileLen)); if (fileLen >= 0) { // throw an error here if content length is not > 0 // you'll probably want to do something with file.ContentType and file.FileName fileContent = new byte[file.ContentLength]; file.InputStream.Read(fileContent, 0, file.ContentLength); sb.AppendLine(string.Format("File Content Length:{0}{1}{0}{0}", Environment.NewLine, fileContent.Length)); // fileContent now contains the byte[] of your attachment... attachmentData = System.Text.Encoding.Default.GetString(fileContent); sb.AppendLine(string.Format("Attachment Data:{0}{1}{0}{0}", Environment.NewLine, attachmentData)); } } DispatchManager dispMgr = new DispatchManager(); DispatchEvent eventMsg; if (attachmentData.Length > 0) { eventMsg = dispMgr.ProcessEvent(org, attachmentData, DispatchManager.MessageType.Html); eventMsg.FileName = attachmentName; } else eventMsg = dispMgr.ProcessEvent(org, body, DispatchManager.MessageType.Text); json = JsonUtil<DispatchEvent>.ToJson(eventMsg); sb.AppendLine(string.Format("Json Body:{0} {1}{0}{0}", Environment.NewLine, json)); httpResult = new HttpStatusCodeResult(HttpStatusCode.OK); } catch (Exception ex) { string errMsg = string.Format("An error occurred in EventController.Process(). Exception: {0}", ex.Message); base.LogException(errMsg, ex); sb.AppendLine(string.Format("Error:{0}{1}{0}{0}", Environment.NewLine, err)); httpResult = new HttpStatusCodeResult(HttpStatusCode.InternalServerError, string.Format("Error encountered processing the event. Message: {0}", ex.Message)); } finally { base.LogInfo(sb.ToString()); } return httpResult; }
public ActionResult Edit() { EditViewModel model = null; try { string id = User.Identity.GetUserId(); using (var db = new StationCADDb()) { var user = db.Users.Include("Profile").Where(x => x.Id == id).FirstOrDefault(); if (user != null) model = new Models.EditViewModel(user); } } catch(Exception ex) { string msg = string.Format("An error occured in ManageController.Edit(string id). Message: {0}", ex.Message); LogException(msg, ex); return View("Error"); } if (model == null) { ViewBag.errorMessage = "Unable to find user data."; // Log them out for safety... AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); return RedirectToAction("Index", "Home"); } else { return View(model); } }
public async Task<ActionResult> Update(EditViewModel model) { try { string id = User.Identity.GetUserId(); using (var db = new StationCADDb()) { User usrUpdate = db.Users.Include("Profile").Where(x => x.Id == id).FirstOrDefault(); usrUpdate.Profile.FirstName = model.FirstName; usrUpdate.Profile.LastName = model.LastName; usrUpdate.Profile.SecurityQuestion = model.SecurityQuestion; usrUpdate.Profile.SecurityAnswer = model.SecurityAnswer; usrUpdate.Profile.AccountEmail = model.AccountEmail; usrUpdate.Profile.IdentificationNumber = model.IdentificationNumber; usrUpdate.Profile.NotificationEmail = model.NotificationEmail; usrUpdate.Profile.NotificationCellPhone = model.NotificationCellPhone; usrUpdate.Profile.NotifcationPushMobile = model.NotifcationPushMobile; usrUpdate.Profile.NotifcationPushBrowser = model.NotifcationPushBrowser; await db.SaveChangesAsync(); } } catch (Exception ex) { string msg = string.Format("An error occured in ManageController.Update(EditViewModel model). Message: {0}", ex.Message); LogException(msg, ex); return View("Error"); } return RedirectToAction("index"); }
public void TestDispatchEventParsing() { string tag = "CC51-FWFC"; using (var db = new StationCADDb()) { string data; using (StreamReader sr = new StreamReader(@"TestData\UnitDispatchReport-F16001716.htm")) { data = sr.ReadToEnd(); } DispatchManager dispMgr = new DispatchManager(); Organization org = db.Organizations.Where(x => x.Tag == tag).FirstOrDefault(); if (org == null) { org = new Organization(); org.Name = "First West Chester Fire Company"; org.Status = OrganizationStatus.Active; org.Type = OrganizationType.Fire; org.Tag = tag; org.ContactEmail = "*****@*****.**"; org.ContactPhone = "610.883.3253"; db.Organizations.Add(org); try { db.SaveChanges(); } catch (Exception ex) { ex.ToString(); } } // Add UserProfile string email = "*****@*****.**"; User user; UserProfile usrp; user = db.Users .Include("Profile") .Include("Profile.OrganizationAffiliations") .Include("Profile.MobileDevices") .Where(w => w.Email == email) .FirstOrDefault(); if (user == null) { user = new User { Id = Guid.NewGuid().ToString(), UserName = email, Email = email }; usrp = new UserProfile(); usrp.FirstName = string.Format("FirstName_{0}", DateTime.Now.Ticks); usrp.LastName = string.Format("LastName_{0}", DateTime.Now.Ticks); usrp.AccountEmail = email; usrp.IdentificationNumber = DateTime.Now.Ticks.ToString(); //usrp.UserName = string.Format("{0}.{1}", usrp.FirstName, usrp.LastName); usrp.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); usrp.OrganizationAffiliations.Add(new OrganizationUserAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); usrp.NotificationEmail = email; usrp.MobileDevices = new List<UserMobileDevice>(); usrp.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); user.Profile = usrp; user.Profile.OrganizationAffiliations = new List<OrganizationUserAffiliation>(); OrganizationUserAffiliation uoa = new OrganizationUserAffiliation(); uoa.CurrentOrganization = org; uoa.Role = OrganizationUserRole.User; uoa.Status = OrganizationUserStatus.Active; user.Profile.OrganizationAffiliations.Add(uoa); db.Users.Add(user); } //usr2 = db.UserProfiles // .Include("OrganizationAffiliations")b // .Include("MobileDevices") // .Where(w => w.NotificationEmail == "*****@*****.**") // .FirstOrDefault(); //if (usr2 == null) //{ // usr2 = new UserProfile(); // usr2.FirstName = "Michael"; // usr2.LastName = "Lam"; // usr2.IdentificationNumber = DateTime.Now.Ticks.ToString(); //// usr2.UserName = string.Format("{0}.{1}", usr2.FirstName, usr2.LastName); // usr2.OrganizationAffiliations = new List<UserOrganizationAffiliation>(); // usr2.OrganizationAffiliations.Add(new UserOrganizationAffiliation { Status = OrganizationUserStatus.Active, Role = OrganizationUserRole.User }); // usr2.NotificationEmail = "*****@*****.**"; // usr2.MobileDevices = new List<UserMobileDevice>(); // usr2.MobileDevices.Add(new UserMobileDevice { Carrier = MobileCarrier.ATT, EnableSMS = true, MobileNumber = "6108833253" }); // db.UserProfiles.Add(usr2); //} db.SaveChanges(); dispMgr.ProcessEvent(org, data, DispatchManager.MessageType.Html); //db.UserProfiles.Remove(usrp); //db.SaveChanges(); } }
public static List<OrganizationUserNotification> CreateNotifications(Incident incident) { List<OrganizationUserNotification> results = new List<OrganizationUserNotification>(); ConcurrentBag<OrganizationUserNotification> resultsBag = new ConcurrentBag<OrganizationUserNotification>(); List<OrganizationUserAffiliation> uoas; List<OrganizationUserNotification> notifications; try { using (var db = new StationCADDb()) { uoas = db.OrganizationUserAffiliations .Include("CurrentUserProfile") .Include("CurrentUserProfile.MobileDevices") .Include("CurrentOrganization") .Where(x => x.CurrentOrganization.Id == incident.OrganizationId && x.Status == OrganizationUserStatus.Active).ToList(); if (uoas == null) throw new InvalidProgramException("Unable to find valid UserProfile-Org Affiliations."); ParallelOptions opts = new ParallelOptions(); opts.MaxDegreeOfParallelism = ParallelismFactor; ParallelLoopResult ptlseResult = Parallel.ForEach( uoas, opts, current => { OrganizationUserNotification item = new OrganizationUserNotification(); SMSEmailNotification notification = incident.GetSMSEmailNotification(current); item.NotifcationType = OrganizationUserNotifcationType.TextMessage; item.Notification = notification; item.MessageTitle = notification.MessageSubject; item.MessageBody = notification.MessageBody; item.Affilitation = current; resultsBag.Add(item); }); ParallelLoopResult ptleResult = Parallel.ForEach( uoas, opts, current => { OrganizationUserNotification item = new OrganizationUserNotification(); EmailNotification notification = incident.GetEmailNotification(current); item.NotifcationType = OrganizationUserNotifcationType.Email; item.Notification = notification; item.MessageTitle = notification.MessageSubject; item.MessageBody = notification.MessageBody; item.Affilitation = current; resultsBag.Add(item); }); notifications = resultsBag.ToList<OrganizationUserNotification>(); db.OrganizationUserNotifcations.AddRange(notifications); db.SaveChanges(); } } catch (Exception ex) { string errMsg = string.Format("An error occurred in NotificationManager.CreateNotifications(). Exception: {0}", ex.Message); LogException(errMsg, ex); throw ex; } return notifications; }
public static void NotifyUsers(ref List<OrganizationUserNotification> users) { try { // First group the notifications by notificaion Type List<NotificationGroup> groups = users .GroupBy(g => g.NotifcationType) .Select(x => new NotificationGroup { Type = x.Key, Users = x.ToList<OrganizationUserNotification>() }) .ToList<NotificationGroup>(); // Use TPL to process each type of notification if (groups.Count > 0) { ParallelOptions tplOptions = new ParallelOptions(); tplOptions.MaxDegreeOfParallelism = ParallelismFactor; Parallel.ForEach<NotificationGroup>(groups, x => ProcessNotificationGroup(ref x)); } using (var db = new StationCADDb()) { foreach (OrganizationUserNotification item in users) { db.OrganizationUserNotifcations.Attach(item); } db.SaveChanges(); } } catch (Exception ex) { string errMsg = string.Format("An error occurred in NotificationManager.NotifyUsers(). Exception: {0}", ex.Message); LogException(errMsg, ex); throw ex; } }