Example #1
0
        public async Task EditUserAsync(EditUserViewModel editUserViewModel)
        {
            var user = await GetUserByIdAsync(editUserViewModel.Id);

            user.Email    = OptimizeText.OptimizeEmail(editUserViewModel.Email);
            user.Avatar   = editUserViewModel.ImageName;
            user.Name     = editUserViewModel.Name;
            user.Password = editUserViewModel.NewPassword is null
                ? user.Password
                : PasswordHelper.Hash(editUserViewModel.NewPassword);
            user.IsActive = editUserViewModel.IsActive;

            _db.Users.Update(user);
            await _db.SaveChangesAsync();

            var userRoles = await _permissionService.GetUserRolesByUserIdAsync(editUserViewModel.Id);

            await _permissionService.RemoveUserRoleAsync(userRoles);

            foreach (var roleId in editUserViewModel.Roles)
            {
                var userRole = new UserRole
                {
                    RoleId = roleId,
                    UserId = editUserViewModel.Id
                };
                await _permissionService.AddUserRoleAsync(userRole);
            }
        }
Example #2
0
        public async Task <IActionResult> Register(RegisterViewModel registerForm)
        {
            if (!ModelState.IsValid)
            {
                return(View(registerForm));
            }

            var user = new User
            {
                Email          = OptimizeText.OptimizeEmail(registerForm.Email),
                IsActive       = false,
                Name           = registerForm.Name,
                RegisterDate   = DateTime.Now,
                ActivationCode = Generator.GenerationUniqueName(),
                Password       = PasswordHelper.Hash(registerForm.Password),
                Avatar         = "default-avatar.png"
            };

            await _userService.AddUserAsync(user);

            var userRole = new UserRole
            {
                RoleId = 3,
                UserId = (await _userService.GetUserByEmailAsync(user.Email)).Id
            };

            await _permissionService.AddUserRoleAsync(userRole);

            #region Send Account Activation Email

            var emailTemplateViewModel = new EmailTemplateViewModel()
            {
                Name = user.Name,
                Url  = string.Concat(Request.Scheme, "://", Request.Host.ToUriComponent(),
                                     $"/Account/ActivateAccount/{user.ActivationCode}")
            };

            var email = new Email()
            {
                To      = user.Email,
                Subject = "فعال سازی حساب کاربری - تاپ لرن",
                Body    = await _viewRenderService.RenderToStringAsync("_AccountActivationTemplate", emailTemplateViewModel)
            };

            var emailSuccessfullySent = await _mailService.SendEmailAsync(email);

            if (!emailSuccessfullySent)
            {
                TempData["Error"] = "مشکلی پیش آمد، لطفا مجددا امتحان کنید";
                return(View(registerForm));
            }

            #endregion

            return(View("SuccessRegister", user));
        }
Example #3
0
        public async Task <IActionResult> ForgetPassword(ForgetPasswordViewModel forgetPasswordForm)
        {
            if (!ModelState.IsValid)
            {
                return(View(forgetPasswordForm));
            }


            var user = await _userService.GetUserByEmailAsync(OptimizeText.OptimizeEmail(forgetPasswordForm.Email));

            if (user == null)
            {
                TempData["Error"] = "کاربری با ایمیل وارد شده وجود ندارد";
                return(View(forgetPasswordForm));
            }

            if (user.IsActive.Equals(false))
            {
                TempData["Error"] = "حساب کاربری غیر فعال است";
                return(View(forgetPasswordForm));
            }

            var forgetPasswordViewModel = new ForgetPasswordTemplateViewModel()
            {
                Name = user.Name,
                Url  = string.Concat(Request.Scheme, "://", Request.Host.ToUriComponent(),
                                     $"/Account/ResetPassword/{user.ActivationCode}")
            };

            var email = new Email()
            {
                To      = user.Email,
                Subject = "بازیابی کلمه عبور - تاپ لرن",
                Body    = await _viewRenderService.RenderToStringAsync("_ForgetPasswordTemplate", forgetPasswordViewModel)
            };

            var emailSuccessfullySent = await _mailService.SendEmailAsync(email);

            if (!emailSuccessfullySent)
            {
                TempData["Error"] = "مشکلی پیش آمد، لطفا مجددا امتحان کنید";
                return(View(forgetPasswordForm));
            }

            return(View("SuccessForgetPasswordSent", user));
        }
Example #4
0
 public async Task <User> GetUserByEmailAsync(string email) =>
 await _db.Users.FirstOrDefaultAsync(u => u.Email.Equals(OptimizeText.OptimizeEmail(email)));
Example #5
0
 public async Task <User> LoginUserAsync(LoginViewModel loginForm)
 {
     return(await _db.Users.FirstOrDefaultAsync(a =>
                                                a.Email.Equals(OptimizeText.OptimizeEmail(loginForm.Email)) &&
                                                a.Password.Equals(PasswordHelper.Hash(loginForm.Password))));
 }
Example #6
0
        public async Task <bool> EditUserProfileAsync(EditProfileViewModel profile)
        {
            var user = await GetUserByEmailAsync(_httpContextAccessor.HttpContext.User.Identity.Name);

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

            if (profile.ImageFile != null)
            {
                if (profile.ImageFile.Length > 1000000)
                {
                    return(false);
                }

                if (profile.ImageName != "default-avatar.png")
                {
                    var oldImagePath = Path.Combine(Directory.GetCurrentDirectory(),
                                                    "wwwroot/images/avatars/", user.Avatar);

                    if (File.Exists(oldImagePath))
                    {
                        File.Delete(oldImagePath);
                    }
                    else
                    {
                        //TODO: Log error
                        Console.WriteLine($"The image path cannot be found. Path = {oldImagePath}");
                    }
                }

                user.Avatar = Generator.GenerationUniqueName() + Path.GetExtension(profile.ImageFile.FileName);

                var newImagePath = Path.Combine(
                    Directory.GetCurrentDirectory(), "wwwroot/images/avatars/", user.Avatar
                    );
                await using (var stream = new FileStream(newImagePath, FileMode.Create))
                {
                    await profile.ImageFile.CopyToAsync(stream);
                }
            }

            await DeleteCookieAsync();

            var claims = new ClaimViewModel()
            {
                Email      = OptimizeText.OptimizeEmail(profile.Email),
                Name       = profile.Name,
                RememberMe = Convert.ToBoolean(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.AuthenticationMethod).Value)
            };

            await CreateCookieAsync(claims);

            user.Name  = profile.Name;
            user.Email = OptimizeText.OptimizeEmail(profile.Email);

            await UpdateUserAsync(user);

            return(true);
        }