private async Task<Configurations.Role> GetUserRole(ApplicationUser user) { IList<string> roles = await _userManager.GetRolesAsync(user); string role = roles.FirstOrDefault(x => Configurations.GetRoles().Contains(x)); return (Configurations.Role)Enum.Parse(typeof(Configurations.Role), role); }
public async Task<IActionResult> Create(ProducerViewModel vmProducer, IFormFile uploadFile) { if (ModelState.IsValid) { #region Creating Producer string fileName = Configurations.DefaultFileName; if (uploadFile != null) { //Image uploading string uploads = Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath); fileName = Guid.NewGuid().ToString() + "_" + ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition).FileName.Trim('"'); await uploadFile.SaveAsAsync(Path.Combine(uploads, fileName)); } //Setting value for creation vmProducer.Producer.Avatar = Path.Combine(Configurations.UserAvatarStockagePath, fileName); vmProducer.Producer.RegistrationDate = DateTime.Now; _context.Producers.Add(vmProducer.Producer); #endregion Creating Producer #region Creating linked application data var producerAppUser = new ApplicationUser { UserName = vmProducer.Producer.Email, Email = vmProducer.Producer.Email }; producerAppUser.User = vmProducer.Producer; var result = await _userManager.CreateAsync(producerAppUser, vmProducer.Producer.Email); if (result.Succeeded) { //Add user role result = await _userManager.AddToRoleAsync(producerAppUser, vmProducer.UserRole.ToString()); //Add user type result = await _userManager.AddToRoleAsync(producerAppUser, Configurations.UserType.Producer.ToString()); } #endregion Creating linked application data _context.SaveChanges(); //Send confirmation mail Services.AuthMessageSender.SendEmail(vmProducer.Producer.Email, vmProducer.Producer.Name, "Creation de votre compte", base.RenderPartialViewToString("ProducerCreatedConfirmationMail", vmProducer)); return RedirectToAction("Index"); } return View(vmProducer); }
private async Task CreateAcount(ApplicationDbContext context, UserManager<ApplicationUser> userManager, string name, string surname, string email, string password, Configurations.Role role, Configurations.UserType userType) { if (context.Consumers.Any(x => x.Email == email) || context.Producers.Any(x => x.Email == email)) return; User user; switch(userType) { case Configurations.UserType.Producer: user = new Producer(); break; case Configurations.UserType.Consumer: user = new Consumer(); break; default: user = new Consumer(); break; } user.Name = name; user.Surname = surname; user.Email = email; user.Avatar = Path.Combine(Configurations.UserAvatarStockagePath, Configurations.DefaultFileName); user.RegistrationDate = DateTime.Now; user.Enable = true; switch (userType) { case Configurations.UserType.Producer: Producer producer = user as Producer; producer.CompanyName = "La ferme de " + producer.Name; producer.Latitude = 44.7354673; producer.Longitude = 4.601407399999971; context.Producers.Add(producer); break; case Configurations.UserType.Consumer: context.Consumers.Add(user as Consumer); break; default: context.Consumers.Add(user as Consumer); break; } #region Creating linked application data var appUser = new ApplicationUser { UserName = user.Email, Email = user.Email }; appUser.User = user; var result = await userManager.CreateAsync(appUser, password); if (result.Succeeded) { //Add user role result = await userManager.AddToRoleAsync(appUser, role.ToString()); //Add user type result = await userManager.AddToRoleAsync(appUser, userType.ToString()); } #endregion Creating linked application data context.SaveChanges(); }