Exemple #1
0
        public ActionResult Edit(Guid?id, SysRole collection, IEnumerable <Guid> sysControllerSysActionsId)
        {
            var area = (string)Request.RequestContext.RouteData.DataTokens["area"];

            if (!ModelState.IsValid)
            {
                Edit(id);
                return(View(collection));
            }

            if (id.HasValue)
            {
                //清除原有数据
                _sysRoleSysControllerSysActionService.Delete(a => a.SysRoleId.Equals(id.Value) && a.SysControllerSysAction.SysController.SysArea.AreaName.Equals(area));
            }

            _sysRoleService.Save(id, collection);

            if (sysControllerSysActionsId != null)
            {
                foreach (var sysControllerSysActionId in sysControllerSysActionsId)
                {
                    _sysRoleSysControllerSysActionService.Save(null, new SysRoleSysControllerSysAction
                    {
                        SysRoleId = collection.Id,
                        SysControllerSysActionId = sysControllerSysActionId
                    });
                }
            }

            _unitOfWork.Commit();

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> EditAsync(string id, IdentityRole collection, IEnumerable <string> sysControllerSysActionsId)
        {
            if (!ModelState.IsValid)
            {
                await Edit(id);

                return(View(collection));
            }

            if (!string.IsNullOrEmpty(id))
            {
                var roleExis = await roleManager.RoleExistsAsync(collection.Name);

                if (!roleExis)
                {
                    //更新角色
                    await roleManager.UpdateAsync(collection);
                }

                //清除原有数据
                await _sysRoleSysControllerSysActionService.Delete(a => a.RoleId.Equals(id));
            }
            else
            {
                // 新建角色
                var identityResult = await roleManager.CreateAsync(collection);

                if (!identityResult.Succeeded)
                {
                    foreach (var error in identityResult.Errors)
                    {
                        throw new Exception(error.Description);
                    }
                }
            }

            if (sysControllerSysActionsId != null)
            {
                foreach (var sysControllerSysActionId in sysControllerSysActionsId)
                {
                    await _sysRoleSysControllerSysActionService.SaveAsync(null, new SysRoleSysControllerSysAction
                    {
                        RoleId = collection.Id,
                        SysControllerSysActionId = sysControllerSysActionId
                    });
                }
            }

            await _unitOfWork.CommitAsync();

            return(new EditSuccessResult(id));
        }
Exemple #3
0
        public async Task <ActionResult> Edit(string id, SysRole collection, IEnumerable <string> sysControllerSysActionsId)
        {
            if (string.IsNullOrEmpty(collection.RoleName))
            {
                ModelState.AddModelError("RoleName", "请填写角色名称");
            }

            if (!ModelState.IsValid)
            {
                Edit(id);
                return(View(collection));
            }

            collection.Name = Guid.NewGuid().ToString();

            if (!string.IsNullOrEmpty(id))
            {
                //清除原有数据
                _sysRoleSysControllerSysActionService.Delete(a => a.RoleId.Equals(id));
            }

            _iSysRoleService.Save(id, collection);

            if (sysControllerSysActionsId != null)
            {
                foreach (var sysControllerSysActionId in sysControllerSysActionsId)
                {
                    _sysRoleSysControllerSysActionService.Save(null, new SysRoleSysControllerSysAction
                    {
                        RoleId = collection.Id,
                        SysControllerSysActionId = sysControllerSysActionId
                    });
                }
            }

            await _unitOfWork.CommitAsync();

            return(new EditSuccessResult(id));
        }