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.
401 lines
11 KiB
401 lines
11 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity.Validation;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using Warehouse.Models;
|
|
|
|
using Warehouse.Models.whMS;
|
|
using Z.EntityFramework.Plus;
|
|
|
|
namespace Warehouse.DAL.Permission
|
|
{
|
|
public partial class PermissionMSDal
|
|
{
|
|
//查询用户权限
|
|
public IEnumerable<dynamic> GetUserPermission(int userid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = from a in context.tpermissions
|
|
join b in context.trolepermissions on a.PermissionId equals b.PermissionId
|
|
join c in context.tuserroles on b.RoleId equals c.RoleId
|
|
join d in context.tusers on c.UserId equals d.UserId
|
|
where (d.UserId == userid)
|
|
select a;
|
|
//var res = context.tusers.Where(a => a.UserId == 2).Select(a=>new {a.tuserroles,a.UserId }).ToList();
|
|
return res.ToList();
|
|
}
|
|
}
|
|
|
|
//查询用户信息
|
|
public tuser GetUserInfo (int userid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.tusers.Find(userid);
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//query user info by user name
|
|
public tuser GetUserInfoByName(string name)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.tusers.Where(a => a.Username == name);
|
|
if (res != null) return res.FirstOrDefault();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//查询所有用户信息
|
|
public List<tuser> GetAllInfo()
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res= context.tusers.Include("tuserroles").ToList();
|
|
//var res = context.tusers.ToList();
|
|
|
|
//foreach (var item in res)
|
|
//{
|
|
// var a = item.tuserroles;
|
|
//}
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//添加用户
|
|
public bool AddUser(tuser user,ref int userid)
|
|
{
|
|
try
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
context.tusers.Add(user);
|
|
int res = context.SaveChanges();
|
|
if (res > 0)
|
|
{
|
|
userid = user.UserId;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException dbEx)
|
|
{
|
|
|
|
var a = dbEx.EntityValidationErrors;
|
|
|
|
//return false;
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
//删除用户
|
|
public bool DelUser(int userid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
//先删除用户角色
|
|
|
|
//DelUserRoles(userid);
|
|
|
|
var res = context.tusers.Find(userid);
|
|
if (res == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
context.tusers.Remove(res);
|
|
context.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
//更新用户信息
|
|
public bool UpdateUser(tuser user)
|
|
{
|
|
try
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.tusers.Find(user.UserId);
|
|
if (res == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
context.Entry(res).CurrentValues.SetValues(user);
|
|
context.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException dbEx)
|
|
{
|
|
var a = dbEx.EntityValidationErrors;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//添加用户角色
|
|
public bool AddUserRole(List<tuserrole> userrolelist)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
context.tuserroles.AddRange(userrolelist);
|
|
int res = context.SaveChanges();
|
|
if (res > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
//删除用户角色
|
|
public bool DelUserRoles(int userid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
//var res = context.tuserroles.Where(a => a.UserId == userid).Delete();
|
|
var res = context.tuserroles.Where(a => a.UserId == userid);
|
|
context.tuserroles.RemoveRange(res);
|
|
int delres= context.SaveChanges();
|
|
if (delres > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//查询所有角色
|
|
public List<trole> QueryRole()
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.troles.ToList();
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//查询用户的角色
|
|
public List<tuserrole> QueryUserRole(int userid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.tuserroles.Where(a => a.UserId == userid).ToList();
|
|
return res;
|
|
}
|
|
}
|
|
}
|
|
#region 角色CRUD
|
|
/// <summary>
|
|
/// 角色dal
|
|
/// </summary>
|
|
public partial class PermissionMSDal
|
|
{
|
|
//查询角色信息
|
|
public trole GetRoleInfo(int roleid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
|
|
var res = context.troles.Find(roleid);
|
|
return res;
|
|
}
|
|
}
|
|
//查询所有角色信息
|
|
public List<trole> GetAllRoleInfo()
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.troles.Include("trolepermissions").ToList();
|
|
//var res = context.tusers.ToList();
|
|
|
|
//foreach (var item in res)
|
|
//{
|
|
// var a = item.tuserroles;
|
|
//}
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//添加角色
|
|
public bool AddRole(trole role, ref int roleid)
|
|
{
|
|
try
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
context.troles.Add(role);
|
|
int res = context.SaveChanges();
|
|
if (res > 0)
|
|
{
|
|
roleid = role.RoleId;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException dbEx)
|
|
{
|
|
|
|
var a = dbEx.EntityValidationErrors;
|
|
|
|
//return false;
|
|
throw dbEx;
|
|
}
|
|
}
|
|
|
|
//删除角色
|
|
public bool DelRole(int roleid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.troles.Find(roleid);
|
|
if (res == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
context.troles.Remove(res);
|
|
context.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
//更新角色信息
|
|
public bool UpdateRole(trole role)
|
|
{
|
|
try
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.troles.Find(role.RoleId);
|
|
if (res == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
context.Entry(res).CurrentValues.SetValues(role);
|
|
context.SaveChanges();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException dbEx)
|
|
{
|
|
var a = dbEx.EntityValidationErrors;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//添加角色权限
|
|
public bool AddRolePermission(List<trolepermission> rolepermissionlist)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
context.trolepermissions.AddRange(rolepermissionlist);
|
|
int res = context.SaveChanges();
|
|
if (res > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
//删除角色权限
|
|
public bool DelRolePermissions(int roleid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
//var res = context.tuserroles.Where(a => a.UserId == userid).Delete();
|
|
var res = context.trolepermissions.Where(a => a.RoleId == roleid);
|
|
context.trolepermissions.RemoveRange(res);
|
|
int delres = context.SaveChanges();
|
|
if (delres > 0)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//查询所有权限
|
|
public List<tpermission> QueryPermission()
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.tpermissions.ToList();
|
|
return res;
|
|
}
|
|
}
|
|
|
|
//查询角色的权限
|
|
public List<trolepermission> QueryRolePermission(int roleid)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
var res = context.trolepermissions.Where(a => a.RoleId == roleid).ToList();
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//通过userid获取用户权限
|
|
public List<dynamic> Permissions(string UserId)
|
|
{
|
|
using (var context = new whMSModel())
|
|
{
|
|
string strWhere = string.Empty;
|
|
strWhere += "and c.UserId = '" + UserId + "' ";
|
|
string sql = @"SELECT a.PermissionName
|
|
FROM [wh].[dbo].[tpermission] a
|
|
left join [wh].[dbo].[trolepermissions] b
|
|
on a.PermissionId =b.PermissionId
|
|
left join [wh].[dbo].[tuserroles] c
|
|
on b.RoleId =c.RoleId
|
|
where 1=1" + strWhere;
|
|
|
|
var res = context.DynamicListFromSql(sql, null);
|
|
return res.ToList();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
#endregion
|
|
}
|