旧版报表、仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

511 lines
22 KiB

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Web.Http;
using Warehouse.Code;
using Warehouse.DAL.Permission;
using Warehouse.Models;
using Warehouse.Models.whMS;
using Warehouse.Models.Permission;
using Z.EntityFramework.Plus;
namespace Warehouse.WebApi
{
[RoutePrefix("api/Permission")]
public partial class PermissionController : ApiController
{
PermissionDal Dal = new PermissionDal();
PermissionMSDal MSDal = new PermissionMSDal();
[Route("test")]
[HttpPost]
public IHttpActionResult test()
{
using (var context = new whModel())
{
var res = context.tusers.Where(a => a.UserId == 11).Delete();
return Json("");
}
}
[Route("GetInfo")]
[HttpPost]
public IHttpActionResult GetInfo(dynamic para)
{
if (Common.DBType == (int)DB.sqlserver)
{
// int UserId = Convert.ToInt32(para.userid);
if (para.userid == null && Convert.ToString(para.username) == "") //|| Convert.ToString( para.username)=="" )
{
var res = MSDal.GetAllInfo();
//return Json(res);
return Json(res.Select(a => new { a.Password, a.workshop, a.Username, a.UserId, a.Createtime, a.DisplayName }));
}
else
{
int UserId = Convert.ToInt32(para.userid);
var a = MSDal.GetUserInfo(UserId);
return Json(new { info = new { a.Password, a.workshop, a.Username, a.UserId, a.Createtime, a.DisplayName } });
}
}
else
{
// int UserId = Convert.ToInt32(para.userid);
if (para.userid == null && Convert.ToString(para.username) == "") //|| Convert.ToString( para.username)=="" )
{
var res = Dal.GetAllInfo();
//return Json(res);
return Json(res.Select(a => new { a.Password, a.workshop, a.Username, a.UserId, a.Createtime, a.DisplayName }));
}
else
{
int UserId = Convert.ToInt32(para.userid);
var a = Dal.GetUserInfo(UserId);
return Json(new { info = new { a.Password, a.workshop, a.Username, a.UserId, a.Createtime, a.DisplayName } });
}
}
}
[Route("AddUser")]
[HttpPost]
public IHttpActionResult AddUser(dynamic para)
{
try
{
string UserName = Convert.ToString(para.username);
string DisplayName = Convert.ToString(para.displayname);
string Workshop = Convert.ToString(para.workshop);
string Password = Convert.ToString(para.password);
int[] RoleIDs = para.roleids.ToObject<int[]>();//para.roleids.ToArray();
int UserID = -1;
if (Common.DBType == (int)DB.sqlserver)
{
var res = MSDal.AddUser(new Models.whMS.tuser() { Password = Password, Username = UserName, DisplayName = DisplayName, workshop = Workshop, IsActive = 1,Createtime=System.DateTime.Now }, ref UserID);
if (res == false)
{
return Json(new { result = "fail", msg = "添加用户出错" });
}
//给用户添加角色
List<Models.whMS.tuserrole> ListUserRoles = new List<Models.whMS.tuserrole>();
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
ListUserRoles.Add(new Models.whMS.tuserrole() { UserId = UserID, RoleId = RoleIDs[i] });
}
if (ListUserRoles.Count > 0 && (MSDal.AddUserRole(ListUserRoles) == false))
{
return Json(new { result = "fail", msg = "添加用户的角色出错" });
}
return Json(new { result = "success", msg = "添加成功" });
}
else
{
var res = MSDal.AddUser(new Models.whMS.tuser() { Password = Password, Username = UserName, DisplayName = DisplayName, workshop = Workshop, IsActive = 1 }, ref UserID);
if (res == false)
{
return Json(new { result = "fail", msg = "添加用户出错" });
}
//给用户添加角色
List<Models.Permission.tuserrole> ListUserRoles = new List<Models.Permission.tuserrole>();
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
ListUserRoles.Add(new Models.Permission.tuserrole() { UserId = UserID, RoleId = RoleIDs[i] });
}
if (ListUserRoles.Count > 0 && (Dal.AddUserRole(ListUserRoles) == false))
{
return Json(new { result = "fail", msg = "添加用户的角色出错" });
}
return Json(new { result = "success", msg = "添加成功" });
}
}
catch (Exception ex)
{
if (ex.GetType().Name == "DbEntityValidationException")
{
var a = (DbEntityValidationException)ex;
var errorMessages = a.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
var fullErrorMessage = string.Join("; ", errorMessages);
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
return Json(new { result = "fail", msg = exceptionMessage });
}
return Json(new { result = "fail", msg = ex.Message });
}
}
[Route("EditUser")]
[HttpPost]
public IHttpActionResult EditUser(dynamic para)
{
Int32 UserId = Convert.ToInt32(para.userid);
string UserName = Convert.ToString(para.username);
string DisplayName = Convert.ToString(para.displayname);
string Workshop = Convert.ToString(para.workshop);
string Password = Convert.ToString(para.password);
int[] RoleIDs = para.roleids.ToObject<int[]>();
if (Common.DBType == (int)DB.sqlserver)
{
var res = MSDal.UpdateUser(new Models.whMS.tuser { UserId = UserId, Username = UserName, DisplayName = DisplayName, workshop = Workshop, Password = Password });
if (res == false)
return Json(new { result = "fail", msg = "更新用户信息出错" });
//删除用户角色
MSDal.DelUserRoles(UserId);
//给用户添加角色
List<Models.whMS.tuserrole> ListUserRoles = new List<Models.whMS.tuserrole>();
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
ListUserRoles.Add(new Models.whMS.tuserrole() { UserId = UserId, RoleId = RoleIDs[i] });
}
if (MSDal.AddUserRole(ListUserRoles) == false)
{
return Json(new { result = "fail", msg = "更新用户的角色出错" });
}
return Json(new { result = "success", msg = "更新成功" });
}
else
{
var res = Dal.UpdateUser(new Models.Permission.tuser { UserId = UserId, Username = UserName, DisplayName = DisplayName, workshop = Workshop, Password = Password });
if (res == false)
return Json(new { result = "fail", msg = "更新用户信息出错" });
//删除用户角色
Dal.DelUserRoles(UserId);
//给用户添加角色
List<Models.Permission.tuserrole> ListUserRoles = new List<Models.Permission.tuserrole>();
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
ListUserRoles.Add(new Models.Permission.tuserrole() { UserId = UserId, RoleId = RoleIDs[i] });
}
if (Dal.AddUserRole(ListUserRoles) == false)
{
return Json(new { result = "fail", msg = "更新用户的角色出错" });
}
return Json(new { result = "success", msg = "更新成功" });
}
}
[Route("DelUser")]
[HttpPost]
public IHttpActionResult DelUser(dynamic para)
{
try
{
int[] UserIDs = para.userids.ToObject<int[]>();//para.userids.ToArray();
if (Common.DBType == (int)DB.sqlserver)
{
for (int i = 0; i <= UserIDs.Length - 1; i++)
{
Int32 UserID = UserIDs[i];
//if (Dal.DelUserRoles(UserID) == false) return Json(new { result = "fail", msg = "删除用户角色出错" });
//if (Dal.DelUser(UserID) == false) return Json(new { result = "fail", msg = "删除用户信息出错" });
MSDal.DelUserRoles(UserID);
MSDal.DelUser(UserID);
}
}
else
{
for (int i = 0; i <= UserIDs.Length - 1; i++)
{
Int32 UserID = UserIDs[i];
//if (Dal.DelUserRoles(UserID) == false) return Json(new { result = "fail", msg = "删除用户角色出错" });
//if (Dal.DelUser(UserID) == false) return Json(new { result = "fail", msg = "删除用户信息出错" });
Dal.DelUserRoles(UserID);
Dal.DelUser(UserID);
}
}
return Json(new { result = "success", msg = "删除成功" });
}
catch (Exception ex)
{
return Json(new { result = "fail", msg = "删除出错:" + ex.Message });
}
}
[Route("QueryRole")]
[HttpPost]
public IHttpActionResult QueryRole(dynamic para)
{
Int32 UserID = Convert.ToInt32(para.userid);
if (Common.DBType == (int)DB.sqlserver)
{
if (UserID == -1)
{
//查询所有角色
var Roles = MSDal.QueryRole();
return Json(new { result = "success", msg = "", info = Roles.Select(a => new { a.RoleId, a.RoleName }) });
}
else
{
var UserRoles = MSDal.QueryUserRole(UserID);
return Json(new { result = "success", msg = "", info = UserRoles.Select(a => new { a.UserId, a.RoleId }) });
}
}
else
{
if (UserID == -1)
{
//查询所有角色
var Roles = Dal.QueryRole();
return Json(new { result = "success", msg = "", info = Roles.Select(a => new { a.RoleId, a.RoleName }) });
}
else
{
var UserRoles = Dal.QueryUserRole(UserID);
return Json(new { result = "success", msg = "", info = UserRoles.Select(a => new { a.UserId, a.RoleId }) });
}
}
}
#region 角色管理
[Route("GetRoleInfo")]
[HttpPost]
public IHttpActionResult GetRoleInfo(dynamic para)
{
// int UserId = Convert.ToInt32(para.userid);
if (Common.DBType == (int)DB.sqlserver)
{
if (para.roleid == null && Convert.ToString(para.rolename) == "") //|| Convert.ToString( para.username)=="" )
{
var res = MSDal.GetAllRoleInfo();
//return Json(res);
return Json(res.Select(a => new { a.RoleId, a.RoleName }));
}
else
{
int RoleId = Convert.ToInt32(para.roleid);
var a = MSDal.GetRoleInfo(RoleId);
return Json(new { info = new { a.RoleId, a.RoleName } });
}
}
else
{
if (para.roleid == null && Convert.ToString(para.rolename) == "") //|| Convert.ToString( para.username)=="" )
{
var res = Dal.GetAllRoleInfo();
//return Json(res);
return Json(res.Select(a => new { a.RoleId, a.RoleName }));
}
else
{
int RoleId = Convert.ToInt32(para.roleid);
var a = Dal.GetRoleInfo(RoleId);
return Json(new { info = new { a.RoleId, a.RoleName } });
}
}
}
[Route("AddRole")]
[HttpPost]
public IHttpActionResult AddRole(dynamic para)
{
try
{
string RoleName = Convert.ToString(para.rolename);
int[] PermissionIDs = para.permissionids.ToObject<int[]>();//para.roleids.ToArray();
int RoleID = -1;
if (Common.DBType == (int)DB.sqlserver)
{
var res = MSDal.AddRole(new Models.whMS.trole() { RoleName = RoleName }, ref RoleID);
if (res == false)
{
return Json(new { result = "fail", msg = "添加角色出错" });
}
//给用角色添加权限
List<Models.whMS.trolepermission> ListRolePermission = new List<Models.whMS.trolepermission>();
for (int i = 0; i <= PermissionIDs.Length - 1; i++)
{
ListRolePermission.Add(new Models.whMS.trolepermission() { RoleId = RoleID, PermissionId = PermissionIDs[i] });
}
if (ListRolePermission.Count > 0 && (MSDal.AddRolePermission(ListRolePermission) == false))
{
return Json(new { result = "fail", msg = "添加角色的权限出错" });
}
return Json(new { result = "success", msg = "添加成功" });
}
else
{
var res = Dal.AddRole(new Models.Permission.trole() { RoleName = RoleName }, ref RoleID);
if (res == false)
{
return Json(new { result = "fail", msg = "添加角色出错" });
}
//给用角色添加权限
List<Models.Permission.trolepermission> ListRolePermission = new List<Models.Permission.trolepermission>();
for (int i = 0; i <= PermissionIDs.Length - 1; i++)
{
ListRolePermission.Add(new Models.Permission.trolepermission() { RoleId = RoleID, PermissionId = PermissionIDs[i] });
}
if (ListRolePermission.Count > 0 && (Dal.AddRolePermission(ListRolePermission) == false))
{
return Json(new { result = "fail", msg = "添加角色的权限出错" });
}
return Json(new { result = "success", msg = "添加成功" });
}
}
catch (Exception ex)
{
if (ex.GetType().Name == "DbEntityValidationException")
{
var a = (DbEntityValidationException)ex;
var errorMessages = a.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
var fullErrorMessage = string.Join("; ", errorMessages);
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
return Json(new { result = "fail", msg = exceptionMessage });
}
return Json(new { result = "fail", msg = ex.Message });
}
}
[Route("EditRole")]
[HttpPost]
public IHttpActionResult EditRole(dynamic para)
{
Int32 RoleId = Convert.ToInt32(para.roleid);
string RoleName = Convert.ToString(para.rolename);
int[] PermissionIDs = para.permissionids.ToObject<int[]>();
if (Common.DBType == (int)DB.sqlserver)
{
var res = MSDal.UpdateRole(new Models.whMS.trole { RoleId = RoleId, RoleName = RoleName });
if (res == false)
return Json(new { result = "fail", msg = "更新角色信息出错" });
//删除角色权限
MSDal.DelRolePermissions(RoleId);
//给角色添加权限
List<Models.whMS.trolepermission> ListRolePerssions = new List<Models.whMS.trolepermission>();
for (int i = 0; i <= PermissionIDs.Length - 1; i++)
{
ListRolePerssions.Add(new Models.whMS.trolepermission() { RoleId = RoleId, PermissionId = PermissionIDs[i] });
}
if (MSDal.AddRolePermission(ListRolePerssions) == false)
{
return Json(new { result = "fail", msg = "更新角色的权限出错" });
}
return Json(new { result = "success", msg = "更新成功" });
}
else
{
var res = Dal.UpdateRole(new Models.Permission.trole { RoleId = RoleId, RoleName = RoleName });
if (res == false)
return Json(new { result = "fail", msg = "更新角色信息出错" });
//删除角色权限
Dal.DelRolePermissions(RoleId);
//给角色添加权限
List<Models.Permission.trolepermission> ListRolePerssions = new List<Models.Permission.trolepermission>();
for (int i = 0; i <= PermissionIDs.Length - 1; i++)
{
ListRolePerssions.Add(new Models.Permission.trolepermission() { RoleId = RoleId, PermissionId = PermissionIDs[i] });
}
if (Dal.AddRolePermission(ListRolePerssions) == false)
{
return Json(new { result = "fail", msg = "更新角色的权限出错" });
}
return Json(new { result = "success", msg = "更新成功" });
}
}
[Route("DelRole")]
[HttpPost]
public IHttpActionResult DelRole(dynamic para)
{
try
{
int[] RoleIDs = para.roleids.ToObject<int[]>();//para.userids.ToArray();
if (Common.DBType == (int)DB.sqlserver)
{
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
Int32 RoleID = RoleIDs[i];
//if (Dal.DelUserRoles(UserID) == false) return Json(new { result = "fail", msg = "删除用户角色出错" });
//if (Dal.DelUser(UserID) == false) return Json(new { result = "fail", msg = "删除用户信息出错" });
MSDal.DelRolePermissions(RoleID);
MSDal.DelRole(RoleID);
}
}
else
{
for (int i = 0; i <= RoleIDs.Length - 1; i++)
{
Int32 RoleID = RoleIDs[i];
//if (Dal.DelUserRoles(UserID) == false) return Json(new { result = "fail", msg = "删除用户角色出错" });
//if (Dal.DelUser(UserID) == false) return Json(new { result = "fail", msg = "删除用户信息出错" });
Dal.DelRolePermissions(RoleID);
Dal.DelRole(RoleID);
}
}
return Json(new { result = "success", msg = "删除成功" });
}
catch (Exception ex)
{
return Json(new { result = "fail", msg = "删除出错:" + ex.Message });
}
}
[Route("QueryPermission")]
[HttpPost]
public IHttpActionResult QueryPermission(dynamic para)
{
Int32 RoleID = Convert.ToInt32(para.roleid);
if (Common.DBType == (int)DB.sqlserver)
{
if (RoleID == -1)
{
//查询所有权限
var Permissions = MSDal.QueryPermission();
return Json(new { result = "success", msg = "", info = Permissions.Select(a => new { a.PermissionId, a.PermissionName }) });
}
else
{
var RolePermission = MSDal.QueryRolePermission(RoleID);
return Json(new { result = "success", msg = "", info = RolePermission.Select(a => new { a.PermissionId, a.RoleId }) });
}
}
else
{
if (RoleID == -1)
{
//查询所有权限
var Permissions = Dal.QueryPermission();
return Json(new { result = "success", msg = "", info = Permissions.Select(a => new { a.PermissionId, a.PermissionName }) });
}
else
{
var RolePermission = Dal.QueryRolePermission(RoleID);
return Json(new { result = "success", msg = "", info = RolePermission.Select(a => new { a.PermissionId, a.RoleId }) });
}
}
}
#endregion
}
}