推送
This commit is contained in:
70
DHSoftware/Services/AuthService.cs
Normal file
70
DHSoftware/Services/AuthService.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DHSoftware.Models;
|
||||
using DHSoftware.Utils;
|
||||
using SqlSugar;
|
||||
|
||||
namespace DHSoftware.Services
|
||||
{
|
||||
|
||||
public static class AuthService
|
||||
{
|
||||
public static User CurrentUser { get; private set; }
|
||||
|
||||
public static bool Login(string username, string password)
|
||||
{
|
||||
using (var db = DatabaseUtil.GetDatabase())
|
||||
{
|
||||
var user = db.Queryable<User>()
|
||||
.First(u => u.UserName == username);
|
||||
|
||||
if (user != null && HashHelper.MD5Encrypt(password).Equals(user.Password))
|
||||
{
|
||||
CurrentUser = user;
|
||||
UpdateLastLoginTime(db, user.Id);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HasPermission(string permissionCode)
|
||||
{
|
||||
if (CurrentUser == null) return false;
|
||||
|
||||
using (var db = DatabaseUtil.GetDatabase())
|
||||
{
|
||||
return db.Queryable<UserRole>()
|
||||
.InnerJoin<RolePermission>((ur, rp) => ur.RoleId == rp.RoleId)
|
||||
.Where((ur, rp) => ur.UserId == CurrentUser.Id)
|
||||
.Where((ur, rp) => rp.PermissionCode == permissionCode)
|
||||
.Any();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<string> GetUserPermissions()
|
||||
{
|
||||
if (CurrentUser == null) return new List<string>();
|
||||
|
||||
using (var db = DatabaseUtil.GetDatabase())
|
||||
{
|
||||
return db.Queryable<UserRole>()
|
||||
.InnerJoin<RolePermission>((ur, rp) => ur.RoleId == rp.RoleId)
|
||||
.Where((ur, rp) => ur.UserId == CurrentUser.Id)
|
||||
.Select((ur, rp) => rp.PermissionCode)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateLastLoginTime(SqlSugarClient db, int userId)
|
||||
{
|
||||
db.Updateable<User>()
|
||||
.SetColumns(u => u.LastLoginTime == DateTime.Now)
|
||||
.Where(u => u.Id == userId)
|
||||
.ExecuteCommand();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user