public UserViewModel From(CSPUser user, List <CSPRole> roleList) { // Set the basic fields this.UserId = user.Id; this.Firstname = user.Firstname; this.Lastname = user.Lastname; this.Email = user.Email; this.Mobile = user.MobileNumber; // Set the organization this.OrganizationId = user.Roles?.FirstOrDefault().OrganizationId; this.OrganizationName = user.Roles?.FirstOrDefault().Organization?.Name; // Set the role this.OccupationId = user.Roles?.FirstOrDefault().RoleId; this.Occupation = roleList.FirstOrDefault(r => r.Id == user.Roles?.FirstOrDefault().RoleId)?.Name; return(this); }
public async Task <ActionResult> Register(RegisterViewModel model) { // Add debug log LogService.Debug($"Getting the registration page (HTTP_POST). SessionId:{SessionId}"); if (ModelState.IsValid) { // Create the user object var user = new CSPUser { Firstname = model.Firstname, Lastname = model.Lastname, UserName = model.Email, Email = model.Email, MobileNumber = model.Mobile, RegistrationNumber = model.RegistrationNumber, Created = DateTime.UtcNow, Status = EntityStatus.Draft }; // Set the location and check email using (var ctx = new CoronaSupportPlatformDbContext()) { // Check email from db var emailTaken = ctx.Users.Where(et => et.Email == model.Email).Any(); if (emailTaken) { // Load the locations model.Organizations = ctx.Organizations.ToList(); model.Errors.Add(model.Email + "'a ait bir hesap bulunmakta"); return(View(model)); } } // Create the user at the user store var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Assign to role IdentityResult roleAssignmentResponse = null; if (model.Occupation == "2") { // Assign to role roleAssignmentResponse = UserManager.AddToRole(user.Id, "OrganizationUser"); } else { roleAssignmentResponse = UserManager.AddToRole(user.Id, "User"); } // Set the organization id using (var ctx = new CoronaSupportPlatformDbContext()) { // Load the user role var userRole = ctx.UserRoles.FirstOrDefault(ur => ur.UserId == user.Id && ur.RoleId == 2); userRole.OrganizationId = Convert.ToInt32(model.OrganizationId); ctx.SaveChanges(); } // Log in the user await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); #region [ User e-mail validation ] // Get the current domain (it can be development, staging or production) var siteRoot = String.Format("{0}://{1}{2}", System.Web.HttpContext.Current.Request.Url.Scheme, System.Web.HttpContext.Current.Request.Url.Host, System.Web.HttpContext.Current.Request.Url.Port == 80 ? string.Empty : ":" + System.Web.HttpContext.Current.Request.Url.Port); // Calculate the activation code based on the email and user id var activationCode = EncryptMessage(user.Email, user.Id.ToString()); //// Sending confirmation email //var activationEmailModel = new RegistrationNotificationViewModel() //{ // ActivationCode = activationCode, // User = user, // SiteRoot = siteRoot //}; //var activationEmail = ViewToString("~/Views/Templates/Email/RegistrationActivation.cshtml", activationEmailModel); //var activationEmailResponse = _mailgunService.Send(new EmailMessage() //{ // ChannelId = "Mailgun", // FromName = "FreelanceFrom", // FromAddress = "*****@*****.**", // Subject = "Freelancefrom Bilgilendirme", // Body = activationEmail, // IsHtml = true, // Deliveries = new List<Delivery>() // { // new Delivery() // { // RecipientType = Common.RecipientType.Primary, // RecipientName = user.Firstname + " " + user.Lastname, // RecipientAddress = user.Email // } // } //}); #endregion #region [ Slack notification ] //try //{ // var slackNotificationResponse = _slackService.SendActivity(new MessageRequest() // { // Attachments = new List<SlackAttachment>() { // new SlackAttachment() // { // Color = "#36a64f", // Title = "Yeni Üye", // TitleLink = "http://www.freelancefrom.com/users/" + user.Id, // Text = "\n", // Fields = new List<SlackField>() // { // new SlackField() // { // Title = "Ad Soyad", // Value = $"{user.Firstname + " " + user.Lastname}\n" // }, // new SlackField() // { // Title = "E-posta adresi", // Value = $"{user.Email}\n" // } // } // } // } // }); //} //catch (Exception ex) //{ // // Do nothing //} #endregion // Add debug log LogService.Debug($"User registration complete, now redirecting to home page. SessionId:{SessionId}"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } else { // Add errors model.Errors.AddRange(ModelState.SelectMany(s => s.Value.Errors.Select(e => e.ErrorMessage))); } // Add debug log LogService.Debug($"User registration failed, re-opening the registration page. SessionId:{SessionId}"); using (var ctx = new CoronaSupportPlatformDbContext()) { model.Organizations = ctx.Organizations.ToList(); } // If we got this far, something failed, redisplay form return(View(model)); }