public async Task<IActionResult> Edit(ProducerViewModel vmProducer, IFormFile uploadFile, Configurations.Role UserRole)
 {
     if (ModelState.IsValid)
     {
         if (uploadFile != null)
         {
             string uploads = Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath);
             //Deleting old image
             string oldImage = Path.Combine(uploads, vmProducer.Producer.Avatar);
             if (System.IO.File.Exists(oldImage) && vmProducer.Producer.Avatar != Path.Combine(Configurations.UserAvatarStockagePath, Configurations.DefaultFileName))
                 System.IO.File.Delete(Path.Combine(uploads, vmProducer.Producer.Avatar));
             //Image uploading
             string fileName = Guid.NewGuid().ToString() + "_" + ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition).FileName.Trim('"');
             await uploadFile.SaveAsAsync(Path.Combine(uploads, fileName));
             //Setting new value, saving
             vmProducer.Producer.Avatar = Path.Combine(Configurations.UserAvatarStockagePath, fileName);
         }
         ApplicationUser producerAppUser = _context.Users.First(x => x.Email == vmProducer.OriginalEmail);
         producerAppUser.Email = vmProducer.Producer.Email;
         _context.Update(producerAppUser);
         //Getting actual roles
         IList<string> roles = await _userManager.GetRolesAsync(producerAppUser);
         if (!roles.Contains(UserRole.ToString()))
         {
             string roleToRemove = roles.FirstOrDefault(x => Configurations.GetRoles().Contains(x));
             await _userManager.RemoveFromRoleAsync(producerAppUser, roleToRemove);
             //Add user role
             await _userManager.AddToRoleAsync(producerAppUser, UserRole.ToString());
         }
         _context.Update(vmProducer.Producer);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(vmProducer);
 }
        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);
        }
示例#3
0
 public async Task<IActionResult> ChangeProducerInformations(ProducerViewModel producerVm, IFormFile uploadFile, Configurations.Role UserRole)
 {
     if (ModelState.IsValid)
     {
         if (uploadFile != null)
         {
             //Deleting old image
             string oldImage = Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath, producerVm.Producer.Avatar);
             if (System.IO.File.Exists(oldImage) && producerVm.Producer.Avatar != Path.Combine(Configurations.UserAvatarStockagePath, Configurations.DefaultFileName))
                 System.IO.File.Delete(Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath, producerVm.Producer.Avatar));
             //Image uploading
             string fileName = await Configurations.UploadAndResizeImageFile(_environment, uploadFile, Configurations.UserAvatarStockagePath);
             //Setting new value, saving
             producerVm.Producer.Avatar = Path.Combine( fileName);
         }
         ApplicationUser appUser = _context.Users.First(x => x.Email == producerVm.OriginalEmail);
         appUser.Email = producerVm.Producer.Email;
         _context.Update(appUser);
         //Getting actual roles
         IList<string> roles = await _userManager.GetRolesAsync(appUser);
         if (!roles.Contains(UserRole.ToString()))
         {
             string roleToRemove = roles.FirstOrDefault(x => Configurations.GetRoles().Contains(x));
             await _userManager.RemoveFromRoleAsync(appUser, roleToRemove);
             //Add user role
             await _userManager.AddToRoleAsync(appUser, UserRole.ToString());
         }
         _context.Update(producerVm.Producer);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(producerVm);
 }