Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using AutoMapper;
  7. using Common.Extensions;
  8. using Domain.Entities;
  9. using Hangfire;
  10. using Infrastructure.Auth;
  11. using Infrastructure.Database;
  12. using Services.Email;
  13. using Services.Email.Models;
  14. using WebFront.Main.Models;
  15. using WebFront.Main.Properties;
  16. using Newtonsoft.Json.Linq;
  17.  
  18. namespace WebFront.Main.Controllers {
  19.     public class RegisterController : BaseController
  20.     {
  21.         protected readonly IDb Db;
  22.         protected readonly IMapper Mapper;
  23.         protected readonly IAppUserManager UserManager;
  24.         protected readonly string SecretKeyReCaptcha = "6LcqqyIUAAAAAOxbuhBY494sDAPlE5f0mT9XS1wI";
  25.  
  26.         public RegisterController(IDb db, IMapper mapper, IAppUserManager userManager)
  27.         {
  28.             Db = db;
  29.             Mapper = mapper;
  30.             UserManager = userManager;
  31.         }
  32.  
  33.         public ActionResult Index()
  34.         {
  35.             return View();
  36.         }
  37.  
  38.         [HttpPost]
  39.         public ActionResult Index(FreeRegistrationData data) {
  40.             var responseReCaptcha = Request["g-recaptcha-response"];
  41.             var webClientReCaptcha = new WebClient();
  42.             var resultReCaptcha = webClientReCaptcha.DownloadString($"https://www.google.com/recaptcha/api/siteverify?secret={SecretKeyReCaptcha}&response={responseReCaptcha}");
  43.             var obj = JObject.Parse(resultReCaptcha);
  44.             var statusReCaptcha = (bool)obj.SelectToken("success");
  45.  
  46.             if (!statusReCaptcha) {
  47.                 ModelState.AddModelError("ReCaptchaError", "Captcha invalid");
  48.                 return View(data);
  49.             }
  50.             if (!ModelState.IsValid) return View(data);
  51.             if (!data.IsAgreement) {
  52.                 ModelState.AddModelError("IsAgreement", "Please indicate that you have read and agree to the eSaverPlus+™ terms.");
  53.                 return View(data);
  54.             }
  55.  
  56.             var role = Db.Query<Role>().Single(r => r.Name == "user");
  57.  
  58.             var affiliate = Db.Query<Company>().FirstOrDefault(c => c.Id == 1);
  59.  
  60.             var user = new User
  61.             {
  62.                 RegisteredAt = DateTime.UtcNow,
  63.                 Role = role,
  64.                 Affiliate = affiliate,
  65.                 SystemManagerId = affiliate?.SystemManagerId
  66.             };
  67.  
  68.             if (Db.IsExists<User>(u => u.Login == data.Email))
  69.             {
  70.                 ModelState.AddModelError("", "Login is already in use");
  71.                 return View(data);
  72.  
  73.             }
  74.  
  75.             if (!string.IsNullOrEmpty(data.Email) && Db.IsExists<User>(u => u.Email == data.Email))
  76.             {
  77.                 ModelState.AddModelError("", "Email is already in use");
  78.                 return View(data);
  79.  
  80.             }
  81.  
  82.             using (var dbTransaction = Db.BeginTransaction())
  83.             {
  84.                 Mapper.Map(data, user);
  85.                 user.Login = data.Email;
  86.                 user.Category = UserCategory.Active;
  87.  
  88.                 user.PasswordHash = UserManager.PasswordHasher.HashPassword(data.Password);
  89.  
  90.                 var result = UserManager.Create(user, data.Password);
  91.                 if (result.Succeeded == false)
  92.                 {
  93.                     var errors = string.Join("; ", result.Errors);
  94.                     ModelState.AddModelError("", "User creation errors: " + errors);
  95.                     return View(data);
  96.                 }
  97.  
  98.                 Db.Commit();
  99.  
  100.                 dbTransaction.Commit();
  101.                 var gluedDomain = ((affiliate?.Domain ?? string.Empty) + "." + Settings.Default.MainDomain).Trim('.');
  102.  
  103.                 // send email to user
  104.                 var registrationNotificationModel = new RegistrationNotificationEmailModel
  105.                 {
  106.                     Password = data.Password,
  107.                     User = Mapper.Map<RegistrationNotificationUserEmailModel>(user),
  108.                     Domain = gluedDomain
  109.                 };
  110.                 BackgroundJob.Enqueue<IEmailService>(es => es.Send(registrationNotificationModel));
  111.  
  112.                 // send email to admin
  113.                 var newCustomerBase = user.Map<NewCustomerAddedBase>(Mapper);
  114.                 newCustomerBase.Company = affiliate.Map<CompanyEmailModel>(Mapper);
  115.                 newCustomerBase.Subscription = new NewCustomerSubscriptionEmailModel();
  116.  
  117.                 var newCustomerAddedNotificationModel = new NewCustomerAddedNotificationEmailModel
  118.                 {
  119.                     NewCustomer = newCustomerBase,
  120.                     IsUserAdded = false,
  121.                     EmailTo = Settings.Default.AdminEmailForNotify,
  122.                     Domain = gluedDomain
  123.                 };
  124.                 BackgroundJob.Enqueue<IEmailService>(es => es.Send(newCustomerAddedNotificationModel));
  125.  
  126.                 // send email to affiliate
  127.                 var newCustomerAddedAffiliateNotification = new NewCustomerAddedAffiliateNotificationEmailModel
  128.                 {
  129.                     NewCustomer = newCustomerBase,
  130.                     EmailTo = affiliate.Email,
  131.                     Domain = gluedDomain
  132.                 };
  133.                 BackgroundJob.Enqueue<IEmailService>(es => es.Send(newCustomerAddedAffiliateNotification));
  134.  
  135.                 var authKey = HttpUtility.UrlEncode(OAuthAuthorizationKey.Create(user.Id, TimeSpan.FromMinutes(5)).ToString());
  136.                 var applicationDomain = $"{affiliate.Domain}.{Settings.Default.MainDomain}";
  137.                 var uri = $"http://{applicationDomain}/#/auth/key/{authKey}/";
  138.                 return Redirect(uri);
  139.             }
  140.  
  141.         }
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement