Beispiel #1
0
        public IActionResult Add([FromBody] MenuDto viewModel)
        {
            var sameCodePermission = AdminDbContext.Menus
                                     .FirstOrDefault(_ => _.ParentId == viewModel.ParentId && _.Code == viewModel.Code);

            if (sameCodePermission != null)
            {
                return(EasyResult.Error("编码已存在"));
            }

            var sort = AdminDbContext.Menus.Count(_ => _.ParentId == viewModel.ParentId) + 1;

            var entity = new Menu
            {
                Code     = viewModel.Code,
                ParentId = viewModel.ParentId,
                Name     = viewModel.Name,
                Path     = viewModel.Path,
                Type     = viewModel.Type,
                Remarks  = viewModel.Remarks,
                Id       = Guid.NewGuid().ToString(),
                Sort     = sort,
                Icon     = viewModel.Icon
            }.OpsBeforeAdd <Menu>(HttpContext);

            AdminDbContext.Menus.Add(entity);
            AdminDbContext.SaveChanges();

            return(EasyResult.Ok());
        }
Beispiel #2
0
        public async Task <IActionResult> AddUserAsync([FromBody] UserDto viewModel)
        {
            var user = await _userManager.FindByNameAsync(viewModel.UserName);

            if (user != null)
            {
                return(EasyResult.Error("用户已存在"));
            }

            user = new IdentityUser
            {
                UserName    = viewModel.UserName,
                Email       = viewModel.Email,
                PhoneNumber = viewModel.Mobile
            };

            var addResult = await _userManager.CreateAsync(user);

            if (!addResult.Succeeded)
            {
                return(EasyResult.Error("新增用户失败"));
            }

            _userManager.PasswordHasher.HashPassword(user, viewModel.Password);

            return(EasyResult.Ok());
        }
Beispiel #3
0
        public async Task <IActionResult> UpdateUserStateAsync([FromRoute] string id, [FromBody] UserDto viewModel)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                var claims = await _userManager.GetClaimsAsync(user);

                var activeClaim    = claims.FirstOrDefault(_ => _.Type == "isactive");
                var newActiveClaim = new Claim("isactive", viewModel.IsActive.ToString());
                if (activeClaim != null)
                {
                    var identityResult = await _userManager.ReplaceClaimAsync(user, activeClaim, newActiveClaim);

                    if (!identityResult.Succeeded)
                    {
                        return(EasyResult.Error("修改用户状态失败"));
                    }
                }
                else
                {
                    var identityResult = await _userManager.AddClaimAsync(user, newActiveClaim);

                    if (!identityResult.Succeeded)
                    {
                        return(EasyResult.Error("修改用户状态失败"));
                    }
                }
            }

            return(EasyResult.Ok());
        }
Beispiel #4
0
        public async Task <IActionResult> DeleteUserAsync([FromRoute] string id)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                var result = await _userManager.DeleteAsync(user);

                if (!result.Succeeded)
                {
                    return(EasyResult.Error("删除用户失败"));
                }
            }
            return(EasyResult.Ok());
        }
Beispiel #5
0
        public async Task <IActionResult> UpdateUserAsync([FromRoute] string id, [FromBody] UserDto viewModel)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                user.PhoneNumber = viewModel.Mobile;
                user.Email       = viewModel.Email;
                var result = await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    return(EasyResult.Error("更新用户失败"));
                }
            }

            return(EasyResult.Ok());
        }
Beispiel #6
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel vm)
        {
            var user = await _userManager.FindByNameAsync(vm.Username);

            if (user != null)
            {
                var signInResult = await _signInManager.PasswordSignInAsync(user, vm.Password, false, false);

                if (signInResult.Succeeded)
                {
                    return(EasyResult.Ok());
                }

                return(EasyResult.Error("用户名或密码错误"));
            }

            return(EasyResult.Error("用户名或密码错误"));
        }
Beispiel #7
0
        public IActionResult Delete([FromRoute] string id)
        {
            var entity = AdminDbContext.Menus.FirstOrDefault(_ => _.Id == id);

            if (entity != null)
            {
                var hasChildren = AdminDbContext.Menus.Count(_ => _.ParentId == id) > 0;

                if (hasChildren)
                {
                    return(EasyResult.Error("存在子级,不允许删除"));
                }

                AdminDbContext.Menus.Remove(entity);
                AdminDbContext.SaveChanges();
            }

            return(EasyResult.Ok());
        }