////////////////////////////////////////////////////////////////////////////// // Common Methods ////////////////////////////////////////////////////////////////////////////// private async Task <AspNetUser> AuthCreateUser(string email, string password, bool isExistingUser, bool verifyEmail, bool signUpForNews, string organizationId) { AspNetUser aspNetUser = base.identityDB.AspNetUsers.Where(u => u.Email == email).FirstOrDefault(); //auth User, if existing if (isExistingUser) { if (aspNetUser != null) { ApplicationUser user = new ApplicationUser() { UserName = email, Email = email }; user = await base.UserManager.FindAsync(user.UserName, password); if (user == null) { aspNetUser = null; ModelState.AddModelError("Password", "User Email and/or Password is not valid"); } } else { ModelState.AddModelError("IsExistingUser", "User does not exist"); } } //else create User, send out verification email else { if (aspNetUser == null) { ApplicationUser user = new ApplicationUser() { UserName = email, Email = email }; IdentityResult result = await UserManager.CreateAsync(user, password); if (result.Succeeded) { aspNetUser = identityDB.AspNetUsers.SingleOrDefault(u => u.Email == email); user = await UserManager.FindByEmailAsync(user.Email); if (organizationId != null && aspNetUser.DefaultOrganizationId == null) { aspNetUser.DefaultOrganizationId = organizationId; db.SaveChanges(); } //send email verification notification to user if (verifyEmail) { //generate callback url string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: GetURLScheme()); //send notification bool notifySuccess = new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact( "USER_ACCOUNT_CONFIRM_EMAIL", user.Email, "User:"******"InvitationUrl", callbackUrl }, { "Email", user.Email } }); } //set email as already confirmed else { string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); result = await UserManager.ConfirmEmailAsync(user.Id, token : code); } //subscribe user to news if (signUpForNews) { //Subscribe the user for news GNNotificationTopic NewsTopic = db.GNNotificationTopics.Where(a => a.Code.Equals("GENOMENEXT_NEWS_AND_PRODUCTS") && a.Status == "ACTIVE").FirstOrDefault(); GNContact UserContact = db.GNContacts.Where(a => a.Email.Equals(aspNetUser.Email)).FirstOrDefault(); if (UserContact != null) { UserContact.IsSubscribedForNewsletters = true; } db.SaveChanges(); if (NewsTopic != null && UserContact != null) { UserContact.GNNotificationTopicSubscribers.Add(new GNNotificationTopicSubscriber { AddresseeType = "TO", GNContactId = UserContact.Id, GNNotificationTopicId = NewsTopic.Id, IsSubscribed = "Y", CreateDateTime = DateTime.Now, CreatedBy = UserContact.Id }); } } } else { AddErrors(result); } } else { ModelState.AddModelError("Email", "User with this Email Address already exists"); } } return(aspNetUser); }
public bool Edit(GNNotificationTopic gnNotificationTopic) { var tx = db.Database.BeginTransaction(); Guid createdBy = Guid.Parse(gnNotificationTopic.CreatedBy.ToString()); try { if (gnNotificationTopic.RemoveToRoles != null) { foreach (var addressee in db.GNNotificationTopicAddressees.Where(a => gnNotificationTopic.RemoveToRoles.Contains(a.AspNetRoleId) && a.AddresseeType == "TO")) { db.GNNotificationTopicAddressees.Remove(addressee); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "TO" && a.GNNotificationTopicId == gnNotificationTopic.Id && a.IsSubscribed == "Y").ToList(); db.GNNotificationTopicSubscribers.RemoveRange(Subscribers); db.SaveChanges(); } } if (gnNotificationTopic.AddToRoles != null) { foreach (var role in gnNotificationTopic.AddToRoles) { db.GNNotificationTopicAddressees.Add(new GNNotificationTopicAddressee { AspNetRoleId = role, GNNotificationTopicId = gnNotificationTopic.Id, AddresseeType = "TO", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); List <GNContactRole> Contacts = db.GNContactRoles.Where(a => a.AspNetRoleId.Equals(role)).ToList(); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "TO" && a.GNNotificationTopicId == gnNotificationTopic.Id).ToList(); foreach (var contact in Contacts) { if (Subscribers.Where(a => a.GNContactId.Equals(contact.GNContactId)).Count() == 0) { db.GNNotificationTopicSubscribers.Add(new GNNotificationTopicSubscriber { AddresseeType = "TO", GNContactId = contact.GNContactId, GNNotificationTopicId = gnNotificationTopic.Id, IsSubscribed = "Y", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); } } db.SaveChanges(); } } if (gnNotificationTopic.RemoveCcRoles != null) { foreach (var addressee in db.GNNotificationTopicAddressees.Where(a => gnNotificationTopic.RemoveCcRoles.Contains(a.AspNetRoleId) && a.AddresseeType == "CC")) { db.GNNotificationTopicAddressees.Remove(addressee); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "CC" && a.GNNotificationTopicId == gnNotificationTopic.Id && a.IsSubscribed == "Y").ToList(); db.GNNotificationTopicSubscribers.RemoveRange(Subscribers); db.SaveChanges(); } } if (gnNotificationTopic.AddCcRoles != null) { foreach (var role in gnNotificationTopic.AddCcRoles) { db.GNNotificationTopicAddressees.Add(new GNNotificationTopicAddressee { AspNetRoleId = role, GNNotificationTopicId = gnNotificationTopic.Id, AddresseeType = "CC", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); List <GNContactRole> Contacts = db.GNContactRoles.Where(a => a.AspNetRoleId.Equals(role)).ToList(); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "CC" && a.GNNotificationTopicId == gnNotificationTopic.Id).ToList(); foreach (var contact in Contacts) { if (Subscribers.Where(a => a.GNContactId.Equals(contact.GNContactId)).Count() == 0) { db.GNNotificationTopicSubscribers.Add(new GNNotificationTopicSubscriber { AddresseeType = "CC", GNContactId = contact.GNContactId, GNNotificationTopicId = gnNotificationTopic.Id, IsSubscribed = "Y", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); } } db.SaveChanges(); } } if (gnNotificationTopic.RemoveBccRoles != null) { foreach (var addressee in db.GNNotificationTopicAddressees.Where(a => gnNotificationTopic.RemoveBccRoles.Contains(a.AspNetRoleId) && a.AddresseeType == "BCC")) { db.GNNotificationTopicAddressees.Remove(addressee); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "BCC" && a.GNNotificationTopicId == gnNotificationTopic.Id && a.IsSubscribed == "Y").ToList(); db.GNNotificationTopicSubscribers.RemoveRange(Subscribers); db.SaveChanges(); } } if (gnNotificationTopic.AddBccRoles != null) { foreach (var role in gnNotificationTopic.AddBccRoles) { db.GNNotificationTopicAddressees.Add(new GNNotificationTopicAddressee { AspNetRoleId = role, GNNotificationTopicId = gnNotificationTopic.Id, AddresseeType = "BCC", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); List <GNContactRole> Contacts = db.GNContactRoles.Where(a => a.AspNetRoleId.Equals(role)).ToList(); List <GNNotificationTopicSubscriber> Subscribers = db.GNNotificationTopicSubscribers.Where(a => a.AddresseeType == "BCC" && a.GNNotificationTopicId == gnNotificationTopic.Id).ToList(); foreach (var contact in Contacts) { if (Subscribers.Where(a => a.GNContactId.Equals(contact.GNContactId)).Count() == 0) { db.GNNotificationTopicSubscribers.Add(new GNNotificationTopicSubscriber { AddresseeType = "BCC", GNContactId = contact.GNContactId, GNNotificationTopicId = gnNotificationTopic.Id, IsSubscribed = "Y", CreateDateTime = DateTime.Now, CreatedBy = createdBy }); } } db.SaveChanges(); } } } catch (Exception ex) { LogUtil.LogMethod(logger, MethodBase.GetCurrentMethod()); throw; } tx.Commit(); return(true); }