//....// ============================================================================================= public int InsertBlankProfile(CreateBlankProfileRequest model) { int id = 0; try { DataProvider.ExecuteNonQuery(GetConnection, "UserProfile_Insert_with_UserId" , inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@userId", model.UserId); paramCollection.AddWithValue("@email", model.UserEmail); paramCollection.AddWithValue("@companyId", model.CompanyId); SqlParameter p = new SqlParameter("@id", System.Data.SqlDbType.Int); p.Direction = System.Data.ParameterDirection.Output; paramCollection.Add(p); }, returnParameters : delegate(SqlParameterCollection param) { int.TryParse(param["@id"].Value.ToString(), out id); } ); } catch (Exception ex) { throw ex; } return(id); }
public HttpResponseMessage PostRegistration(InviteRegistrationRequest model) { IdentityUser newUserRegistration; if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } // When model is valid, CreateUser will post email and pw to database try { // creating a random password for the invited user string randomPassword = Guid.NewGuid().ToString(); // setting the invited user's email and their password newUserRegistration = UserService.CreateUser(model.Email, randomPassword); //Instantiating a new blank profile CreateBlankProfileRequest newUserProfile = new CreateBlankProfileRequest(); newUserProfile.UserEmail = model.Email; newUserProfile.CompanyId = model.CompanyId; newUserProfile.UserId = newUserRegistration.Id; // Instantiating a new user role InsertNewUserRequest newUserRole = new InsertNewUserRequest(); newUserRole.UserRole = model.UserRole; newUserRole.UserId = newUserRegistration.Id; _UserProfileService.AddRoleToUser(newUserRole); //TODO: Confirm this method is not longer needed (K.H. advised he suspects code was changed to make this unecessary) // Inserting the Blank Profile data by calling on the UserService function for it. _UserProfileService.InsertBlankProfile(newUserProfile); } catch (IdentityResultException ex) // Display error code and message if user was not created { // var ExceptionError = new ErrorResponse("Failed to register new user. (server side)"); return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Result)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } // Insert new user's id into token table and generate new token string UserId = newUserRegistration.Id; // Pass new user's email and token into emailservice for activation link try { //- Pass the invite user's email data string NewUserEmail = newUserRegistration.Email; // Create inviteType token model.TokenType = Enums.TokenType.ForgotPassword; //- Instantiate a new token request InsertTokenRequest inviteUserToken = new InsertTokenRequest { UserId = newUserRegistration.Id, Email = model.Email, TokenType = TokenType.ForgotPassword }; //- Capturing user's information into the tokenRequest //inviteUserToken.UserId = newUserRegistration.Id; //inviteUserToken.Email = model.Email; //inviteUserToken.TokenType = TokenType.ForgotPassword; if (inviteUserToken.TokenType != 0) { // Do something } Guid token = _TokenService.RegisterToken(inviteUserToken); //- Create the link for setting a new password string link = "/public/setpassword/" + token.ToString(); //- Use HttpContext to generate a clickable link in the email link = HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped) + link; // Call the service for the forgot password link //- Send Link to user email EmailRequest confirm = new EmailRequest(); // Building the object's information confirm._email_link = link; confirm._CompanyName = model.CompanyName; confirm.UserEmail = newUserRegistration.Email; // sends user invitation via email and render HTML string // TemplateEmailService was created to render HTML pages to a string string renderedHTML = _TemplateEmailService.RazorViewToString("~/Views/Email_Template/Email_Template.cshtml", confirm); // Instantiating a new SendGridMessage SendGridMessage ActivateCrEmail = new SendGridMessage(); ActivateCrEmail.AddTo(confirm.UserEmail); ActivateCrEmail.From = new MailAddress("*****@*****.**"); // The Stringified HTML page is now converted by SendGridMessage with .Html ActivateCrEmail.Html = renderedHTML; ActivateCrEmail.Subject = "You've been invited! Please set your password for your account."; // Instantiating the sendGridKey var sendGridKey = new SendGrid.Web(ConfigService.GetSendGridKey()); // Delivering the email message sendGridKey.DeliverAsync(ActivateCrEmail); //UserEmailService useremailservice = new UserEmailService(); //UserEmailService.SendEmail(confirm); } catch (NotImplementedException) { var ExceptionError = new ErrorResponse("Failed to send activation email to new user"); return(Request.CreateResponse(HttpStatusCode.BadRequest, ExceptionError)); } return(Request.CreateResponse(HttpStatusCode.OK, model)); }
public HttpResponseMessage Register(UserAddRequest model) { if (!ModelState.IsValid) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } ItemResponse <bool> response = new ItemResponse <bool>(); bool success = false; IdentityUser newUser = null; // Use model input to build user Account, company, and profile in DB try { // Create User Account newUser = UserService.CreateUser(model.Email, model.Password); // Determine company role CompanyRoleType companyType; switch (model.CompanyRole) { case "Buyer": companyType = CompanyRoleType.Buyer; break; case "Supplier": companyType = CompanyRoleType.Supplier; break; case "Shipper": companyType = CompanyRoleType.Shipper; break; default: throw new Exception("Unrecognized Company Role: " + model.CompanyRole); } // Create Company Account CompanyInsertRequest company = new CompanyInsertRequest { Name = model.Company, OwnerId = newUser.Id, TypeId = companyType }; int companyId = _CompanyOptionService.InsertCompany(company); // Set User Role _AdminService.SetUserRole(newUser.Id, _AdminService.GetCompanyOwnerRoleId()); //- Create Blank User Profile CreateBlankProfileRequest newUserProfile = new CreateBlankProfileRequest { UserId = newUser.Id, UserEmail = newUser.Email, CompanyId = companyId }; _UserProfileService.InsertBlankProfile(newUserProfile); } catch (Exception ex) { throw ex; } if (newUser != null) { // Use model input to create confirmation email InsertTokenRequest insertToken = new InsertTokenRequest { Email = model.Email, UserId = newUser.Id, TokenType = Enums.TokenType.Registration }; EmailRequest ToSend = new EmailRequest(); try { ToSend = _UserEmailService.BuildAccountConfirmEmail(insertToken); } catch (Exception ex) { throw ex; } _UserEmailService.SendEmail(ToSend); success = true; } response.Item = success; return(Request.CreateResponse(HttpStatusCode.OK, response)); }