Пример #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);
            }
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync(List <int> roleIds)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (!roleIds.Any())
            {
                TempData["Error"] = "لطفا حداقل یک نقش را انتخاب کنید";
                return(Page());
            }

            if (await _userService.GetUserByEmailAsync(UserViewModel.Email) != null)
            {
                TempData["Error"] = "کاربر با ایمیل وارد شده موجود است";
                return(Page());
            }

            var result = await _pictureService.SaveImageAsync(UserViewModel.AvatarFile, "avatars");

            if (result.LimitReached)
            {
                TempData["Error"] = "حجم عکس بیش از 500 کیلوبایت می باشد";
                return(Page());
            }

            if (string.IsNullOrEmpty(result.ImageName))
            {
                TempData["Error"] = "مشکلی در ارسال عکس به وجود آمد";
                return(Page());
            }

            var user = new User
            {
                Avatar         = result.ImageName,
                Email          = UserViewModel.Email,
                Password       = PasswordHelper.Hash(UserViewModel.Password),
                RegisterDate   = DateTime.Now,
                Name           = UserViewModel.Name,
                IsActive       = true,
                ActivationCode = Generator.GenerationUniqueName()
            };

            await _userService.AddUserAsync(user);

            foreach (var roleId in roleIds)
            {
                var userRole = new UserRole
                {
                    RoleId = roleId,
                    UserId = (await _userService.GetUserByEmailAsync(user.Email)).Id
                };
                await _permissionService.AddUserRoleAsync(userRole);
            }

            TempData["Success"] = "کاربر جدید با موفقیت افزوده شد";
            return(Redirect("/Admin/Users"));
        }
Пример #3
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));
        }