203 lines
6.6 KiB
C#
203 lines
6.6 KiB
C#
|
|
|
|||
|
|
using Cowain.Bake.Common.Core;
|
|||
|
|
using Cowain.Bake.Common.Enums;
|
|||
|
|
using Cowain.Bake.Model;
|
|||
|
|
using Cowain.Bake.Model.Entity;
|
|||
|
|
using Prism.Ioc;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Unity;
|
|||
|
|
|
|||
|
|
namespace Cowain.Bake.BLL
|
|||
|
|
{
|
|||
|
|
public class UserService : ServiceBase
|
|||
|
|
{
|
|||
|
|
public UserService(IUnityContainer unityContainer) : base(unityContainer)
|
|||
|
|
{
|
|||
|
|
_unityContainer = unityContainer;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 登录
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="name">用户名</param>
|
|||
|
|
/// <param name="pwd">密码</param>
|
|||
|
|
/// <returns>登录成功</returns>
|
|||
|
|
public bool Login(string name, string pwd, out string msg)
|
|||
|
|
{
|
|||
|
|
msg = "";
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
TUserManage ulist = Context.Set<TUserManage>().Where(item => item.UserId == name && item.Password == pwd).FirstOrDefault();
|
|||
|
|
if (ulist != null)
|
|||
|
|
{
|
|||
|
|
if (!ulist.Valid ?? false)
|
|||
|
|
{
|
|||
|
|
msg = $"用户:{ulist.UserName},已注销!";
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var ge = _unityContainer.Resolve<MemoryDataProvider>(); //重新开辟了内存。
|
|||
|
|
ge.CurrentUser.Copy(ulist); //重新开辟了内存。
|
|||
|
|
//获取菜单,后期需要修改,根据权限加载,现在是加载所有菜单
|
|||
|
|
var ml = Context.Set<TMenuInfo>().Where(item => item.State == true).OrderBy(x => x.Id).ToList();
|
|||
|
|
if (ml != null && ml.Count > 0)
|
|||
|
|
{
|
|||
|
|
ge.CurrentUser.Menus.Clear();
|
|||
|
|
ml.ForEach(p => ge.CurrentUser.Menus.Add(p));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_unityContainer.Resolve<LogService>().AddLog(name, E_LogType.Operate.ToString());
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//public int ModifyPassword(int id, string newPassword)
|
|||
|
|
//{
|
|||
|
|
// using (var Context = new BakingEntities())
|
|||
|
|
// {
|
|||
|
|
// var ulist = Context.Set<TUserManage>().Where(item => item.Id == id).ToList();
|
|||
|
|
// if (ulist.Count == 1)
|
|||
|
|
// {
|
|||
|
|
// ulist[0].Password = newPassword;
|
|||
|
|
|
|||
|
|
// return Context.SaveChanges();
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// return 0;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
public List<TUserManage> GetAllUsers()
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TUserManage>().ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TUserManage> QueryUser(string userEnter)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
List<TUserManage> user = Context.Set<TUserManage>().Where(x => x.UserId == userEnter).ToList();
|
|||
|
|
return user;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string GetCurrentUserAuthority()
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var memory = _unityContainer.Resolve<MemoryDataProvider>();
|
|||
|
|
TUserManage user = Context.Set<TUserManage>().Where(x => x.UserId == memory.CurrentUser.UserId).FirstOrDefault();
|
|||
|
|
return Context.Set<TRoleInfo>().Where(a => a.RoleId == user.RoleId).ToList()[0].AccessNode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public bool IsHasAuthority(string target)
|
|||
|
|
{
|
|||
|
|
if (GetCurrentUserAuthority().Contains(target))
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TRoleInfo> GetAllRole()
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TRoleInfo>().ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public int GetCurrentRoleId()
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var memory = _unityContainer.Resolve<MemoryDataProvider>();
|
|||
|
|
TUserManage user = Context.Set<TUserManage>().Where(x => x.UserId == memory.CurrentUser.UserId).FirstOrDefault();
|
|||
|
|
return user.RoleId;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool CheckPermission(ERole permission)
|
|||
|
|
{
|
|||
|
|
// 获取当前登录用户的权限级别
|
|||
|
|
int userPermissionLevel = GetCurrentRoleId();
|
|||
|
|
|
|||
|
|
// 根据权限级别判断是否具有指定权限
|
|||
|
|
if (userPermissionLevel == (int)ERole.Admin) // 管理员
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if (userPermissionLevel == (int)ERole.Mantainer && (permission == ERole.Operater || permission == ERole.Mantainer))
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else if (userPermissionLevel == (int)ERole.Operater && permission == ERole.Operater)
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
public List<TMenuInfo> GetAllMenuInfo()
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TMenuInfo>().ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string GetAuthority(string role)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
return Context.Set<TRoleInfo>().Where(x => x.RoleName == role).FirstOrDefault().AccessNode;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool ValidPassword(string name, string pwd) //有效密码
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
if (null == Context.Set<TUserManage>().Where(item => item.UserId == name && item.Password == pwd).FirstOrDefault())
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public int ModifyPassword(string newPassword)
|
|||
|
|
{
|
|||
|
|
using (var Context = new BakingEntities())
|
|||
|
|
{
|
|||
|
|
var memory = _unityContainer.Resolve<MemoryDataProvider>();
|
|||
|
|
var userInfo = Context.Set<TUserManage>().Where(item => item.UserId == memory.CurrentUser.UserId).FirstOrDefault();
|
|||
|
|
if (null == userInfo)
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
userInfo.Password = newPassword;
|
|||
|
|
return Context.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|