コード例 #1
0
        protected override CoreResponse Create(CoreRequest core_request)
        {
            CoreResponse core_response = new CoreResponse(core_request);
            UserDTO      loginsession  = Newtonsoft.Json.JsonConvert.DeserializeObject <UserDTO>(core_request.HttpContext.Session.GetString("User"));

            foreach (var item in core_request.DtRequest.Data)
            {
                string key = item.Key;
                List <Dictionary <string, object> > list_pair = new List <Dictionary <string, object> >();
                var     pair = item.Value as Dictionary <string, object>;
                RoleDTO orgin;

                orgin = new RoleDTO();
                base.ConvertDictionaryToObject(orgin, pair, core_response.DtResponse.fieldErrors);
                if (core_response.DtResponse.fieldErrors != null && core_response.DtResponse.fieldErrors.Count > 0)
                {
                    return(core_response);
                }
                //登录名要唯一
                var existUser = this.GetAll().Where(p => p.Name == orgin.Name).FirstOrDefault();
                if (existUser != null)
                {
                    Core.Infrastructure.DataTables.DtResponse.FieldError fe = new Core.Infrastructure.DataTables.DtResponse.FieldError();
                    fe.name   = "Name";
                    fe.status = "创建的角色名已经存在";
                    core_response.DtResponse.fieldErrors.Add(fe);
                    return(core_response);
                }

                DBResult dbresult;
                orgin.CreateTime = DateTime.Now;
                orgin.Creator    = loginsession.LoginName;

                dbresult = this.Add(orgin);
                if (dbresult.Code != 0)
                {
                    core_response.DtResponse.error += dbresult.ErrMsg;
                }
                else
                {
                    Role role = dbresult.Result as Role;
                    if (orgin.RolePermission != null)
                    {
                        foreach (var v in orgin.RolePermission)
                        {
                            v.RoleId     = role.Id;
                            v.CreateTime = DateTime.Now;
                            v.Creator    = loginsession.LoginName;
                        }
                        dbresult = _IRolePermissionService.AddRange(orgin.RolePermission.ToArray());
                        if (dbresult.Code != 0)
                        {
                            core_response.DtResponse.error += dbresult.ErrMsg;
                        }
                    }
                }
            }
            return(core_response);
        }
コード例 #2
0
 /// <summary>
 /// 提交权限配置
 /// </summary>
 /// <param name="roleDTO"></param>
 /// <returns></returns>
 public IActionResult CommitPermissions(RoleDTO roleDTO)
 {
     if (roleDTO == null)
     {
         return(NotFound());
     }
     else
     {
         if (roleDTO.Id != 0)
         {
             roleDTO.ModifyTime = DateTime.Now;
             roleDTO.Modifier   = "administrator";
             var roleRes = _roleService.Update(roleDTO);
             List <RolePermissionDTO> rolePermissionDTOs = new List <RolePermissionDTO>();
             foreach (var item in roleDTO.Permissions)
             {
                 RolePermissionDTO rolePermissionDTO = new RolePermissionDTO();
                 rolePermissionDTO.RoleId = roleDTO.Id;
                 int.TryParse(item, out var pid);
                 rolePermissionDTO.PermissionId = pid;
                 rolePermissionDTO.Remarks      = roleDTO.Remarks;
                 rolePermissionDTO.CreateTime   = DateTime.Now;
                 rolePermissionDTO.Creator      = "administrator";
                 rolePermissionDTOs.Add(rolePermissionDTO);
             }
             var olds = _rolePermissionService.GetAll().Where(t => t.RoleId == roleDTO.Id).Select(t => t.Id).ToArray();
             _rolePermissionService.DeleteRange(olds);
             var permissionRes = _rolePermissionService.AddRange(rolePermissionDTOs.ToArray());
             if (permissionRes.Code == 0)
             {
                 return(Json("success"));
             }
             else
             {
                 return(Json("failed"));
             }
         }
         else//新增
         {
             roleDTO.CreateTime = DateTime.Now;
             roleDTO.Creator    = "administrator";
             var  roleRes = _roleService.Add(roleDTO);
             Role role    = (Role)roleRes.Result;
             List <RolePermissionDTO> rolePermissionDTOs = new List <RolePermissionDTO>();
             foreach (var item in roleDTO.Permissions)
             {
                 RolePermissionDTO rolePermissionDTO = new RolePermissionDTO();
                 rolePermissionDTO.RoleId = role.Id;
                 int.TryParse(item, out var pid);
                 rolePermissionDTO.PermissionId = pid;
                 rolePermissionDTO.Remarks      = roleDTO.Remarks;
                 rolePermissionDTO.CreateTime   = DateTime.Now;
                 rolePermissionDTO.Creator      = "administrator";
                 rolePermissionDTOs.Add(rolePermissionDTO);
             }
             var permissionRes = _rolePermissionService.AddRange(rolePermissionDTOs.ToArray());
             if (permissionRes.Code == 0)
             {
                 return(Json("success"));
             }
             else
             {
                 return(Json("failed"));
             }
         }
     }
 }