コード例 #1
0
        public async Task<IActionResult> Create(UserStolonViewModel vmConsumer, IFormFile uploadFile)
        {
            if (ModelState.IsValid)
            {
                #region Creating Consumer
                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
                vmConsumer.Consumer.Avatar = Path.Combine(Configurations.UserAvatarStockagePath, fileName);
                vmConsumer.Consumer.RegistrationDate = DateTime.Now;
                vmConsumer.Consumer.Name = vmConsumer.Consumer.Name.ToUpper();
                _context.Consumers.Add(vmConsumer.Consumer);
                #endregion Creating Consumer

                #region Creating linked application data
                var appUser = new ApplicationUser { UserName = vmConsumer.Consumer.Email, Email = vmConsumer.Consumer.Email };
                appUser.User = vmConsumer.Consumer;
                
                var result = await _userManager.CreateAsync(appUser, vmConsumer.Consumer.Email);
                if (result.Succeeded)
                {
                    //Add user role
                    result = await _userManager.AddToRoleAsync(appUser, vmConsumer.UserRole.ToString());
                    //Add user type
                    result = await _userManager.AddToRoleAsync(appUser, Configurations.UserType.Consumer.ToString());
                }
                #endregion Creating linked application data

                _context.SaveChanges();
                //Send confirmation mail
                Services.AuthMessageSender.SendEmail(vmConsumer.Consumer.Email, vmConsumer.Consumer.Name, "Creation de votre compte", base.RenderPartialViewToString("UserCreatedConfirmationMail", vmConsumer));

                return RedirectToAction("Index");
            }
            return View(vmConsumer);
        }
コード例 #2
0
 public async Task<IActionResult> Edit(UserStolonViewModel consumerVm, 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, consumerVm.Consumer.Avatar);
             if (System.IO.File.Exists(oldImage) && consumerVm.Consumer.Avatar != Path.Combine(Configurations.UserAvatarStockagePath, Configurations.DefaultFileName))
                 System.IO.File.Delete(Path.Combine(uploads, consumerVm.Consumer.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
             consumerVm.Consumer.Avatar = Path.Combine(Configurations.UserAvatarStockagePath, fileName);
         }
         ApplicationUser appUser = _context.Users.First(x => x.Email == consumerVm.OriginalEmail);
         appUser.Email = consumerVm.Consumer.Email;
         consumerVm.Consumer.Name = consumerVm.Consumer.Name.ToUpper();
         _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(consumerVm.Consumer);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(consumerVm);
 }
コード例 #3
0
 public async Task<IActionResult> ChangeUserInformations(UserStolonViewModel userStolonVm, IFormFile uploadFile, Configurations.Role UserRole)
 {
     if (ModelState.IsValid)
     {
         if (uploadFile != null)
         {
             //Deleting old image
             string oldImage = Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath, userStolonVm.Consumer.Avatar);
             if (System.IO.File.Exists(oldImage) && userStolonVm.Consumer.Avatar != Path.Combine(Configurations.UserAvatarStockagePath, Configurations.DefaultFileName))
                 System.IO.File.Delete(Path.Combine(_environment.WebRootPath, Configurations.UserAvatarStockagePath, userStolonVm.Consumer.Avatar));
             //Image uploading
             string fileName = await Configurations.UploadAndResizeImageFile(_environment, uploadFile, Configurations.UserAvatarStockagePath);
             //Setting new value, saving
             userStolonVm.Consumer.Avatar =  fileName;
         }
         ApplicationUser appUser = _context.Users.First(x => x.Email == userStolonVm.OriginalEmail);
         appUser.Email = userStolonVm.Consumer.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(userStolonVm.Consumer);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(userStolonVm);
 }