Exemplo n.º 1
0
        public JsonResult AddRoleFunction(OperationRoleModel model)
        {
            CommonResult r = new CommonResult();

            try
            {
                if (!CheckRights("TJJS100"))
                {
                    return(Json(new { IsSuccess = false, Msg = "您没有添加角色的权限" }));
                }
                var       id       = Guid.NewGuid().ToString();
                AdminRole roleInfo = new AdminRole();
                roleInfo.Name          = PreconditionAssert.IsNotEmptyString(model.RoleName, "角色名不能为空");
                roleInfo.Id            = id;
                roleInfo.Type          = model.RoleType;
                roleInfo.UpdateDate    = DateTime.Now;
                roleInfo.CreationDate  = DateTime.Now;
                roleInfo.CreatorUserId = CurUser.UserId;
                roleInfo.UpdatorUserId = CurUser.UserId;

                if (roleInfo.Type == IdentityManager.RoleType.SuperAdmin)
                {
                    roleInfo.Permissions = PermissionManager.GetAllPermissions()?.Select(p =>
                    {
                        return(new AdminRolePermission()
                        {
                            RoleId = id,
                            PermissionId = p.Id,
                            CreationDate = DateTime.Now
                        });
                    }).ToList();
                }
                else
                {
                    roleInfo.Permissions = model.FunctionIds.Select(p =>
                    {
                        return(new AdminRolePermission()
                        {
                            RoleId = id,
                            PermissionId = p,
                            CreationDate = DateTime.Now
                        });
                    }).ToList();
                }
                RoleManager.AddRole(roleInfo);
                return(Json(r));
            }
            catch (Exception ex)
            {
                logger.Error("AddRoleFunciton", ex);
                return(Json(new { IsSuccess = false, Msg = ex.Message }));
            }
        }
Exemplo n.º 2
0
        public JsonResult UpdateRoleFunciton(OperationRoleModel model)
        {
            CommonResult r = new CommonResult();

            try
            {
                if (!CheckRights("UPDATEROLE"))
                {
                    r.code    = (int)ResultCodeEnum.NoAuth;
                    r.message = "您没有修改角色的权限";
                    return(Json(r));
                }
                var roleInfo = new AdminRole();
                roleInfo.Name = PreconditionAssert.IsNotEmptyString(model.RoleName, "角色名不能为空");
                roleInfo.Id   = PreconditionAssert.IsNotEmptyString(model.RoleId, "角色编号不能为空");
                var roleById = RoleManager.FindById(model.RoleId);
                if (roleById == null)
                {
                    r.code    = (int)ResultCodeEnum.NotFound;
                    r.message = "指定的角色不存在";
                    return(Json(r));
                }
                var roleCount = RoleManager.GetRoleCountByRoleName(model.RoleName);
                if (roleCount >= 2)
                {
                    r.code    = (int)ResultCodeEnum.NotFound;
                    r.message = "指定的角色已经存在";
                    return(Json(r));
                }
                if (IdentityManager.RoleType.SuperAdmin == model.RoleType)
                {
                    var res = RoleManager.UpdateRole(roleInfo);// GlobalCache.ExternalClient.UpdateSystemRole(roleInfo, CurrentUser.UserToken);
                    if (res > 0)
                    {
                        r.code    = (int)ResultCodeEnum.OK;
                        r.message = "更新成功";
                    }
                    else
                    {
                        r.code    = (int)ResultCodeEnum.SystemError;
                        r.message = "更新失败";
                    }
                    return(Json(r));
                }
                var funIds = (List <string>)(PreconditionAssert.IsNotNull(model.FunctionIds, "功能编号集异常"));

                var pers = funIds.Select(p =>
                {
                    return(new AdminRolePermission()
                    {
                        RoleId = model.RoleId,
                        PermissionId = p,
                        CreationDate = DateTime.Now
                    });
                }).ToList();

                AdminRolePermissionManager m = new AdminRolePermissionManager();
                m.DeleteAndAddRolePermissions(model.RoleId, pers);
                AdminOperLogManager operLog = new AdminOperLogManager();
                operLog.AddOperationLog(new AdminOperationLog()
                {
                    CreationDate  = DateTime.Now,
                    Description   = "操作员【" + User.Identity.Name + "】修改角色,角色名称:" + model.RoleName + ",角色编号:" + model.RoleId,
                    OperationName = "角色管理",
                    OperUserId    = CurUser.UserId,
                    UserId        = ""
                });

                RoleManager.UpdateRole(roleInfo);
                return(Json(r));
            }
            catch (Exception ex)
            {
                logger.Error("UpdateRoleFunciton", ex);
                return(Json(new { IsSuccess = false, Msg = ex.Message }));
            }
        }