public ApplicationUserDto GetUserAccountInformation(Guid userId) { using (var db = new AdministrationDb()) { var user = db.ApplicationUsers.SingleOrDefault(x => x.UserId == userId); if (user == null) return null; var customerInfo = user.CustomerUsers.FirstOrDefault().Customer; var applicationUser = new ApplicationUserDto { ApplicationUserId = user.UserId, Email = user.EmailAddress, Active = user.IsActive, FirstName = user.FirstName, LastName = user.LastName, TenantId = customerInfo.CustomerId, TenantDomain = customerInfo.DomainName }; return applicationUser; } }
public void CustomerRegister(TenantRegisterModel customerRegistration) { string errMessage; if (!ValidModel(customerRegistration, out errMessage)) { throw new BusinessException(errMessage); } Logger.Info("Creating Membership"); MembershipCreateStatus createStatus; var membershipUser = Membership.CreateUser(customerRegistration.UserName, customerRegistration.Password, customerRegistration.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus); Logger.Info("Membership created"); var created = false; if (membershipUser != null && membershipUser.ProviderUserKey != null) { try { using (var scope = new TransactionScope()) { using (var db = new AdministrationDb()) { var userId = (Guid)membershipUser.ProviderUserKey; var appUser = new ApplicationUser { UserId = userId, FirstName = customerRegistration.FirstName, LastName = customerRegistration.LastName, EmailAddress = customerRegistration.UserName, IsEmailVerified = true, EmailVerificationToken = string.Empty, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = userId, ModifiedOn = DateTime.Now, ModifiedBy = userId }; var customer = new Customer { CompanyName = customerRegistration.DomainName, Domain = customerRegistration.Domain, DomainName = customerRegistration.DomainName, PhoneNumber = customerRegistration.Phone, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = userId, ModifiedOn = DateTime.Now, ModifiedBy = userId }; var customerUser = new CustomerUser { ApplicationUser = appUser, Customer = customer, IsCreator = true, IsAdmin = true, IsRelationActive = true }; db.ApplicationUsers.Add(appUser); Logger.InfoFormat("User {0} Created", appUser.EmailAddress); db.Customers.Add(customer); Logger.InfoFormat("Customer {0} Created", customer.CompanyName); db.SaveChanges(); Logger.Info("Register Checkpoint"); db.CustomerUsers.Add(customerUser); Logger.Info("Relationship created"); db.SaveChanges(); scope.Complete(); Logger.Info("Transacion Completed"); created = true; } } MailService.SendMail(customerRegistration.UserName, MailMessageType.Welcome); } catch (Exception ex) { if (!created) Membership.DeleteUser(customerRegistration.UserName, true); Logger.Error(ex); } } else { Logger.Error("Membership user couldn't be created"); throw new BusinessException("The user could not be created at this time. Try again soon."); } }
private bool ValidModel(TenantRegisterModel customerRegistration, out string errMessage) { errMessage = string.Empty; using (var db = new AdministrationDb()) { if (customerRegistration.Domain.EndsWith(".onmicrosoft.com")) { customerRegistration.DomainName = customerRegistration.Domain.Split('.')[0]; } else { errMessage = "Domain must be an Office 365 valid domain."; return false; } var cust = db.Customers.FirstOrDefault(x => x.DomainName == customerRegistration.DomainName); if (cust != null) { errMessage = "Customer Domain Name already in use"; return false; } var user = db.ApplicationUsers.FirstOrDefault(x => x.EmailAddress == customerRegistration.UserName); if (user != null) { errMessage = "Username already in use"; return false; } } return true; }
public void UserRegister(ApplicationContext context, RegisterModel model) { using (var db = new AdministrationDb()) { var user = db.ApplicationUsers.FirstOrDefault(x => x.EmailAddress == model.UserName); if (user != null) { throw new BusinessException(""); } Logger.Info("Creating Membership"); MembershipCreateStatus createStatus; var membershipUser = Membership.CreateUser(model.UserName, model.Password, model.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus); Logger.Info("Membership created"); var created = false; if (membershipUser != null && membershipUser.ProviderUserKey != null) { var userId = (Guid) membershipUser.ProviderUserKey; var appUser = new ApplicationUser { UserId = userId, FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.UserName, IsEmailVerified = true, EmailVerificationToken = string.Empty, IsActive = true, CreatedOn = DateTime.Now, CreatedBy = context.UserId, ModifiedOn = DateTime.Now, ModifiedBy = context.UserId, }; db.ApplicationUsers.Add(appUser); db.SaveChanges(); var customerUser = new CustomerUser { ApplicationUserId = userId, CustomerId = context.TenantId, IsCreator = false, IsAdmin = false, IsRelationActive = true }; db.CustomerUsers.Add(customerUser); db.SaveChanges(); } } }