Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using AutoMapper;
- using Common.Extensions;
- using Domain.Entities;
- using Hangfire;
- using Infrastructure.Auth;
- using Infrastructure.Database;
- using Services.Email;
- using Services.Email.Models;
- using WebFront.Main.Models;
- using WebFront.Main.Properties;
- using Newtonsoft.Json.Linq;
- namespace WebFront.Main.Controllers {
- public class RegisterController : BaseController
- {
- protected readonly IDb Db;
- protected readonly IMapper Mapper;
- protected readonly IAppUserManager UserManager;
- protected readonly string SecretKeyReCaptcha = "6LcqqyIUAAAAAOxbuhBY494sDAPlE5f0mT9XS1wI";
- public RegisterController(IDb db, IMapper mapper, IAppUserManager userManager)
- {
- Db = db;
- Mapper = mapper;
- UserManager = userManager;
- }
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Index(FreeRegistrationData data) {
- var responseReCaptcha = Request["g-recaptcha-response"];
- var webClientReCaptcha = new WebClient();
- var resultReCaptcha = webClientReCaptcha.DownloadString($"https://www.google.com/recaptcha/api/siteverify?secret={SecretKeyReCaptcha}&response={responseReCaptcha}");
- var obj = JObject.Parse(resultReCaptcha);
- var statusReCaptcha = (bool)obj.SelectToken("success");
- if (!statusReCaptcha) {
- ModelState.AddModelError("ReCaptchaError", "Captcha invalid");
- return View(data);
- }
- if (!ModelState.IsValid) return View(data);
- if (!data.IsAgreement) {
- ModelState.AddModelError("IsAgreement", "Please indicate that you have read and agree to the eSaverPlus+™ terms.");
- return View(data);
- }
- var role = Db.Query<Role>().Single(r => r.Name == "user");
- var affiliate = Db.Query<Company>().FirstOrDefault(c => c.Id == 1);
- var user = new User
- {
- RegisteredAt = DateTime.UtcNow,
- Role = role,
- Affiliate = affiliate,
- SystemManagerId = affiliate?.SystemManagerId
- };
- if (Db.IsExists<User>(u => u.Login == data.Email))
- {
- ModelState.AddModelError("", "Login is already in use");
- return View(data);
- }
- if (!string.IsNullOrEmpty(data.Email) && Db.IsExists<User>(u => u.Email == data.Email))
- {
- ModelState.AddModelError("", "Email is already in use");
- return View(data);
- }
- using (var dbTransaction = Db.BeginTransaction())
- {
- Mapper.Map(data, user);
- user.Login = data.Email;
- user.Category = UserCategory.Active;
- user.PasswordHash = UserManager.PasswordHasher.HashPassword(data.Password);
- var result = UserManager.Create(user, data.Password);
- if (result.Succeeded == false)
- {
- var errors = string.Join("; ", result.Errors);
- ModelState.AddModelError("", "User creation errors: " + errors);
- return View(data);
- }
- Db.Commit();
- dbTransaction.Commit();
- var gluedDomain = ((affiliate?.Domain ?? string.Empty) + "." + Settings.Default.MainDomain).Trim('.');
- // send email to user
- var registrationNotificationModel = new RegistrationNotificationEmailModel
- {
- Password = data.Password,
- User = Mapper.Map<RegistrationNotificationUserEmailModel>(user),
- Domain = gluedDomain
- };
- BackgroundJob.Enqueue<IEmailService>(es => es.Send(registrationNotificationModel));
- // send email to admin
- var newCustomerBase = user.Map<NewCustomerAddedBase>(Mapper);
- newCustomerBase.Company = affiliate.Map<CompanyEmailModel>(Mapper);
- newCustomerBase.Subscription = new NewCustomerSubscriptionEmailModel();
- var newCustomerAddedNotificationModel = new NewCustomerAddedNotificationEmailModel
- {
- NewCustomer = newCustomerBase,
- IsUserAdded = false,
- EmailTo = Settings.Default.AdminEmailForNotify,
- Domain = gluedDomain
- };
- BackgroundJob.Enqueue<IEmailService>(es => es.Send(newCustomerAddedNotificationModel));
- // send email to affiliate
- var newCustomerAddedAffiliateNotification = new NewCustomerAddedAffiliateNotificationEmailModel
- {
- NewCustomer = newCustomerBase,
- EmailTo = affiliate.Email,
- Domain = gluedDomain
- };
- BackgroundJob.Enqueue<IEmailService>(es => es.Send(newCustomerAddedAffiliateNotification));
- var authKey = HttpUtility.UrlEncode(OAuthAuthorizationKey.Create(user.Id, TimeSpan.FromMinutes(5)).ToString());
- var applicationDomain = $"{affiliate.Domain}.{Settings.Default.MainDomain}";
- var uri = $"http://{applicationDomain}/#/auth/key/{authKey}/";
- return Redirect(uri);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement