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() .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() .InnerJoin((ur, rp) => ur.RoleId == rp.RoleId) .Where((ur, rp) => ur.UserId == CurrentUser.Id) .Where((ur, rp) => rp.PermissionCode == permissionCode) .Any(); } } public static List GetUserPermissions() { if (CurrentUser == null) return new List(); using (var db = DatabaseUtil.GetDatabase()) { return db.Queryable() .InnerJoin((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() .SetColumns(u => u.LastLoginTime == DateTime.Now) .Where(u => u.Id == userId) .ExecuteCommand(); } } }