Guest User

Untitled

a guest
Oct 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult Create(CreateModel model) {
  3. // Retrieve and encode the user supplied arguments.
  4. string email = HttpUtility.HtmlEncode(model.Email.GetNullSafe()).Trim();
  5. string firstName = HttpUtility.HtmlEncode(model.FirstName.GetNullSafe()).Trim();
  6. string lastName = HttpUtility.HtmlEncode(model.LastName.GetNullSafe()).Trim();
  7.  
  8. // Check if the model state currently has any errors.
  9. if (ModelState.IsValid) {
  10. // Retrieve the session for the current request.
  11. var session = GlobalApplication.SessionFactory.GetCurrentSession();
  12.  
  13. // Query the system for the user that matches the supplied user name.
  14. var query =
  15. from user in session.Query<User>()
  16. where user.Email.ToLower() == email.ToLower() &&
  17. (user.Status & UserStatusTypes.Active) != 0
  18. select user;
  19.  
  20. // Check if the query returned any matching results.
  21. if (query.Any()) {
  22. // Add an error to the model state describing the failure.
  23. ModelState.AddModelError("_FORM", "Oh no! It looks like a user already exists with the same e-mail address.");
  24. } else {
  25. // Initialize the transaction for the account creation attempt.
  26. using (var transaction = session.BeginTransaction()) {
  27. // Generate the new random password for the user.
  28. string password = GeneratePassword();
  29.  
  30. // Create the new user account using the supplied details.
  31. var user = Domain.User.Create(
  32. email.ToLower(),
  33. password);
  34.  
  35. // Save the newly created user.
  36. session.Save(user);
  37.  
  38. // Create the user's profile using the supplied details.
  39. var profile = Profile.Create(
  40. user,
  41. firstName,
  42. null,
  43. lastName);
  44.  
  45. // Initialize the profile for the user.
  46. user.WithProfile = profile;
  47.  
  48. // Save the newly created user profile.
  49. session.Save(profile);
  50.  
  51. // Commit the changes for the user creation transaction.
  52. transaction.Commit();
  53.  
  54. // Return a successful completion for the creation of the new user.
  55. return Json(new {
  56. status = "OK",
  57. values = new {
  58. id = user.Id.ToString("000000"),
  59. name = user.GetName(),
  60. timestamp = user.DateLastModified.ToLocalTime().ToShortDateString()
  61. }
  62. });
  63. }
  64. }
  65. }
  66.  
  67. // Attempt to retreive the first error from the model state.
  68. var error = ModelState.Values.FirstOrDefault(s => s.Errors.Count() > 0);
  69.  
  70. // Initialize the message that was returned for the call.
  71. string message = (error == null) ?
  72. "An unexpected error has occurred. Please contact support." :
  73. error.Errors.First().ErrorMessage;
  74.  
  75. // Return data pertaining to the creation of the user.
  76. return Json(new {
  77. status = "ERROR",
  78. message = message
  79. });
  80. }
Add Comment
Please, Sign In to add comment