Exemplo n.º 1
0
        public async Task <Response> Handle(ChangeUserRolesCommand request, CancellationToken cancellationToken)
        {
            var user = await _appUserManager.FindByIdAsync(request.UserId.ToString());

            await _appUserManager.RemoveFromRolesAsync(user,
                                                       await _appUserManager.GetRolesAsync(user));

            await _appUserManager.AddToRolesAsync(user, request.Roles);

            return(Response.Success());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(UserCreateDto user)
        {
            if (ModelState.IsValid)
            {
                AppUser createUser = user.GetUser();
                if (user.ImageFile != null)
                {
                    createUser.Image = await _fileWorker.EncodeFormFile(user.ImageFile);
                }
                var result = await _userManager.CreateAsync(createUser, user.Password);

                if (!result.Succeeded)
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError(item.Code, item.Description);
                    }
                }
                else
                {
                    Popup.PopupModel = new Popup("افزودن کاربر", $"کاربر {user.Username} با موفقیت اضافه شد.", IconType.Success);
                    if (!string.IsNullOrEmpty(user.UserRoles))
                    {
                        result = await _userManager.AddToRolesAsync(createUser, user.UserRoles.Split(','));

                        if (!result.Succeeded)
                        {
                            Popup.PopupModel = new Popup("خطا در افزودن نقش", $"در هنگام افزودن نقش به کاربر {user.Username} خطایی رخ داد.", IconType.Error);
                            if (result.Errors.Count() > 0)
                            {
                                Popup.PopupModel.Message += result.Errors.First();
                            }
                        }
                    }
                    return(RedirectToAction("Index"));
                }
            }
            await SetRoles();

            return(View(user));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create(UserCreateDTO userCreate)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser
                {
                    Address        = userCreate.Address,
                    Email          = userCreate.Email,
                    PostalCode     = userCreate.PostalCode,
                    UserName       = userCreate.Username,
                    FirstName      = userCreate.FirstName,
                    PhoneNumber    = userCreate.PhoneNumber,
                    EmailConfirmed = true,
                    LastName       = userCreate.LastName,
                };
                if (userCreate.ImageFile != null)
                {
                    user.ImageName = await _fileWorker.AddFileToPathAsync(userCreate.ImageFile, "img");
                }
                var result = await _userManager.CreateAsync(user, userCreate.Password);

                if (result.Succeeded)
                {
                    string[] roles = userCreate.UserRoles.Split(',');
                    if (roles.Any())
                    {
                        await _userManager.AddToRolesAsync(user, roles);
                    }
                    return(LocalRedirect("/admin/usersmanager"));
                }
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(item.Code, item.Description);
                }
            }
            await SetRoles();

            return(View(userCreate));
        }