예제 #1
0
        public async Task <IActionResult> Edit(int id, PostFormViewModel postFormViewModel)
        {
            if (id != postFormViewModel.Id)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var user = await _unitOfWork.UserRepository.GetByClaimsPrincipal(HttpContext.User);

                if (!await _unitOfWork.UserRepository
                    .IsUserAllowedForOperation(user, postFormViewModel.UserId, ConstantUserRoles.SuperAdmin))
                {
                    return(Unauthorized());
                }

                var post = _mapper.Map <PostFormViewModel, Post>(postFormViewModel);

                _unitOfWork.PostRepository.Update(post);
                await _unitOfWork.Complete();

                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    //Delete old file
                    _fileHandler.DeleteMedia(
                        _env.WebRootPath,
                        post.FilesPathGuid.ToString(),
                        FileHandlerFolder.Posts
                        );

                    //Upload new file
                    await _fileHandler.UploadMedia(
                        files,
                        _env.WebRootPath,
                        post.FilesPathGuid.ToString(),
                        FileHandlerFolder.Posts);
                }

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["PostCategoryId"] = GetPostCategories();
            return(View(postFormViewModel));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, CertificateFormViewModel certificateFormViewModel)
        {
            if (id != certificateFormViewModel.Id)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var certificate = _mapper.Map <CertificateFormViewModel, Certificate>(certificateFormViewModel);

                _unitOfWork.CertificateRepository.Update(certificate);
                await _unitOfWork.Complete();

                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    //Delete old file
                    _fileHandler.DeleteMedia(
                        _webHostEnvironment.WebRootPath,
                        certificate.FilesPathGuid.ToString(),
                        FileHandlerFolder.Certificates
                        );

                    //Upload new file
                    await _fileHandler.UploadMedia(
                        files,
                        _webHostEnvironment.WebRootPath,
                        certificate.FilesPathGuid.ToString(),
                        FileHandlerFolder.Certificates);
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(certificateFormViewModel));
        }
예제 #3
0
        public async Task <IActionResult> Edit(string id, UserFormViewModel userFormViewModel)
        {
            if (id != userFormViewModel.Id)
            {
                return(BadRequest());
            }

            var user = await _unitOfWork.UserRepository.FindById(userFormViewModel.Id);

            if (user == null)
            {
                return(NotFound());
            }

            if (userFormViewModel.Role == UserRoleEnumViewModel.SuperAdmin)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var currentUser = await _unitOfWork.UserRepository.GetByClaimsPrincipal(HttpContext.User);

                if (!await _unitOfWork.UserRepository
                    .IsUserAllowedForOperation(currentUser, user.Id, ConstantUserRoles.SuperAdmin))
                {
                    return(Unauthorized());
                }

                if (!string.IsNullOrWhiteSpace(userFormViewModel.Password))
                {
                    var removePassResult = await _unitOfWork.UserRepository.RemovePassword(user);

                    if (!removePassResult.Succeeded)
                    {
                        ViewData["Error"] = ConstantMessages.IdentityError;
                        return(View(userFormViewModel));
                    }

                    user.PasswordHash = _unitOfWork.UserRepository.HashPassword(user, userFormViewModel.Password);
                }

                user.UserName  = userFormViewModel.UserName;
                user.FirstName = userFormViewModel.FirstName;
                user.LastName  = userFormViewModel.LastName;
                user.Email     = userFormViewModel.Email;
                user.Biography = userFormViewModel.Biography;
                user.IsActive  = userFormViewModel.IsActive;

                var updateRoleResult = await _unitOfWork.UserRepository.UpdateRole(user, userFormViewModel.Role.ToString());

                if (!updateRoleResult.Succeeded)
                {
                    ViewData["Error"] = ConstantMessages.IdentityError;
                    return(View(userFormViewModel));
                }

                var result = await _unitOfWork.UserRepository.Update(user);

                if (result.Succeeded)
                {
                    var files = HttpContext.Request.Form.Files;
                    if (files.Count > 0)
                    {
                        //Delete old file
                        _fileHandler.DeleteMedia(
                            _env.WebRootPath,
                            user.FilesPathGuid.ToString(),
                            FileHandlerFolder.Profiles
                            );

                        //Upload new file
                        await _fileHandler.UploadMedia(
                            files,
                            _env.WebRootPath,
                            user.FilesPathGuid.ToString(),
                            FileHandlerFolder.Profiles);
                    }

                    await _unitOfWork.UserRepository.SignOut();

                    return(RedirectToAction(nameof(Index)));
                }

                ViewData["Error"] = ConstantMessages.IdentityError;
                return(View(userFormViewModel));
            }

            return(View(userFormViewModel));
        }