public static void DeleteRole(string roleID) { using (var db = SysContext.GetCurrent()) { var oldItem = db.Roles.FirstOrDefault(r => r.RoleID.Equals(roleID, StringComparison.OrdinalIgnoreCase)); if (oldItem == null) { throw new Exception("角色不存在"); } var count = db.UserRoles.Count(r => r.RoleID.Equals(roleID, StringComparison.OrdinalIgnoreCase)); if (count > 0) { throw new Exception("有用户关联此角色,删除取消."); } foreach (var item in db.RolePages.Where(r => r.RoleID.Equals(roleID)).ToList()) { db.RolePages.Remove(item); db.SaveChanges(); } oldItem.ResetUpdated(); db.Roles.Remove(oldItem); db.SaveChanges(); } }
public static RoleModel CreateRole(string name, string description = "") { using (var db = SysContext.GetCurrent()) { var oldItem = db.Roles.FirstOrDefault(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (oldItem != null) { throw new Exception("角色已存在"); } var newID = db.GetNextRoleID(); Role role = new Role { RoleID = newID, Name = name, Description = description }; role.ResetCreated(); db.Roles.Add(role); var user = SysService.GetCurrentUser(); if (user.UserID != "U00001") { UserRole ur = new UserRole { UserID = user.UserID, RoleID = role.RoleID }; ur.ResetCreated(); db.UserRoles.Add(ur); } db.SaveChanges(); return(new RoleModel { RoleID = newID, Name = name, Description = description }); } }
public static void SaveUserRole(string userID, string[] roles) { using (var db = SysContext.GetCurrent()) { var oldItems = db.UserRoles.Where(r => r.UserID.Equals(userID)); var adds = roles.Where(w => oldItems.FirstOrDefault(t => t.RoleID.Equals(w)) == null).ToList(); var removes = oldItems.Where(w => roles.FirstOrDefault(t => t.Equals(w.RoleID)) == null).ToList(); foreach (var t in removes) { db.UserRoles.Remove(t); } foreach (var t in adds) { var newItem = new UserRole { UserID = userID, RoleID = t }; newItem.ResetCreated(); db.UserRoles.Add(newItem); } db.SaveChanges(); } }
public static string[] GetUserRoles(string userID) { using (var db = SysContext.GetCurrent()) { return(db.UserRoles.Where(r => r.UserID.Equals(userID)).Select(r => r.RoleID).ToArray()); } }
public static IEnumerable <MailServer> GetIndex() { using (var db = SysContext.GetCurrent()) { return(db.MailServers.ToList()); } }
public static MailServer GetItem(string idOrName) { using (var db = SysContext.GetCurrent()) { return(db.MailServers.FirstOrDefault(m => m.ServerID == idOrName || m.ServerName == idOrName)); } }
//如果同一个页面的某个功能,A角色有权限,B角色没有权限,但某用户A、B角色都有,那该用户应该有权 public static List <RolePageModel> GetUserPages(string userID) { using (var db = SysContext.GetCurrent()) { var items = (from ur in db.UserRoles join rp in db.RolePages on ur.RoleID equals rp.RoleID where ur.UserID.Equals(userID) select new RolePageModel { RoleID = rp.RoleID, PageID = rp.PageID, ActionValue = rp.ActionValue }).ToList(); db.RolePages.Where(rp => rp.RoleID.Equals(userID)). Select((rp) => new RolePageModel { RoleID = rp.RoleID, PageID = rp.PageID, ActionValue = rp.ActionValue }).ToList().AddToCollection(items); var groups = items.GroupBy(g => g.PageID).Select(g => new RolePageModel { PageID = g.Key, ActionValue = g.Count() > 1 ? g.Select(s => s.ActionValue).Aggregate((v1, v2) => v1 | v2) : g.First().ActionValue, DataFilter = g.Select(r => r.DataFilter).AggregateSplitStrings(",") }).ToList(); return(groups); } }
internal static User GetUser(string userName) { using (var db = SysContext.GetCurrent()) { return(db.Users.FirstOrDefault(u => (u.UserID.Equals(userName) || u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase) && u.IsValid.Equals(true)))); } }
public static List <UserModel> GetUsers(string nameOrEmail, bool isValid, string email, string fullname) { using (var db = SysContext.GetCurrent()) { Func <User, bool> nameWhere = (u) => true; var query = db.Users.Where(u => u.IsValid == isValid && u.UserName != "admin").ToList(); var user = SysService.GetCurrentUser(); if (user != null && user.UserID != "U00001") { var companyfilter = ((from u in db.Users join sr in db.UserRoles on u.UserID equals sr.UserID join rp in db.RolePages on sr.RoleID equals rp.RoleID where (u.UserID.Equals(user.UserID) && rp.PageID.Equals("P01001")) select rp.DataFilter.ToString()).Distinct()).ToList(); var deptfilter = ((from u in db.Users join sr in db.UserRoles on u.UserID equals sr.UserID join rp in db.RolePages on sr.RoleID equals rp.RoleID where (u.UserID.Equals(user.UserID) && rp.PageID.Equals("P01002")) select rp.DataFilter.ToString()).Distinct()).ToList(); List <string> eu = new List <string>(); eu = (from e in db.hr_Employees where (deptfilter.Contains(e.DeptID) || deptfilter.Contains(e.CompanyID)) select e.UserID).Distinct().ToList(); if (eu.Count != 0) { query = query.Where(u => eu.Contains(u.UserID)).ToList(); } } if (!string.IsNullOrEmpty(nameOrEmail) || !string.IsNullOrEmpty(email) || !string.IsNullOrEmpty(fullname)) { query = string.IsNullOrEmpty(nameOrEmail) ? query : query.Where(u => u.UserName.Contains(nameOrEmail)).ToList(); query = string.IsNullOrEmpty(email) ? query : query.Where(u => u.Email.Contains(email)).ToList(); query = string.IsNullOrEmpty(fullname) ? query : query.Where(u => u.FullName.Contains(fullname)).ToList(); } // UserDatagrid ud = new UserDatagrid(); // var demo = ""; return(query .Select((u) => new UserModel { UserID = u.UserID, UserName = u.UserName, FullName = u.FullName, Email = u.Email, Roles = (from r in db.Roles join s in db.UserRoles on r.RoleID equals s.RoleID where s.UserID.Equals(u.UserID) select r.Name.ToString()).ToArray(), Company = (from em in db.hr_Employees join com in db.hr_Companies on em.CompanyID equals com.CompanyID where em.UserID.Equals(u.UserID) select com.Name).FirstOrDefault(), Dept = (from em in db.hr_Employees join dept in db.hr_Depts on em.DeptID equals dept.DeptID where em.UserID.Equals(u.UserID) select dept.Name.ToString()).FirstOrDefault(), Job = (from em in db.hr_Employees join job in db.hr_Jobs on em.JobID equals job.JobID where em.UserID.Equals(u.UserID) select job.Name.ToString()).FirstOrDefault(), IsValid = u.IsValid }).ToList()); } }
public static List <RoleModel> GetRoles() { using (var db = SysContext.GetCurrent()) { return(db.Roles.Select(r => new RoleModel { RoleID = r.RoleID, Name = r.Name, Description = r.Description }).ToList()); } }
public static RoleModel GetRole(string roleID) { using (var db = SysContext.GetCurrent()) { return(db.Roles.Where(r => r.RoleID.Equals(roleID, StringComparison.OrdinalIgnoreCase)) .Select(r => new RoleModel { RoleID = r.RoleID, Name = r.Name, Description = r.Description }).FirstOrDefault()); } }
public static void UpdateRole(string roleID, string name, string description) { using (var db = SysContext.GetCurrent()) { var oldItem = db.Roles.FirstOrDefault(r => r.RoleID.Equals(roleID, StringComparison.OrdinalIgnoreCase)); if (oldItem == null) { throw new Exception("角色不存在"); } oldItem.Name = name; oldItem.Description = description; oldItem.ResetUpdated(); db.SaveChanges(); } }
public static void ResetPassword(string userID) { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var oldUser = db.Users.FirstOrDefault(u => u.UserID.Equals(userID)); if (oldUser != null) { oldUser.Password = Sys.SystemConsts.DefaultPassword.GetMd5_32().ToString(); oldUser.ResetUpdated(); db.SaveChanges(); } } } }
public static void DeleteUser(string userID) { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var oldUser = db.Users.FirstOrDefault(u => u.UserID.Equals(userID)); if (oldUser != null) { oldUser.IsValid = false; oldUser.ResetUpdated(); db.SaveChanges(); } } } }
public static void DeleteUsers(string userKeys) { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var userIDs = userKeys.Split(','); var oldUsers = db.Users.Where(u => userIDs.Contains(u.UserID)); foreach (var oldUser in oldUsers) { oldUser.IsValid = false; oldUser.ResetUpdated(); } db.SaveChanges(); } } }
public static bool EmailReset(string uid)//邮件链接重置密码 { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var oldUser = db.Users.FirstOrDefault(u => u.UserID.Equals(uid)); if (oldUser != null) { oldUser.Password = Sys.SystemConsts.DefaultPassword.GetMd5_32(); oldUser.ResetUpdated(); db.SaveChanges(); } } } return(true); }
public static void AddLog(string actionName, string url, string description = "", string userName = "") { using (var db = SysContext.GetCurrent()) { var user = SysService.GetCurrentUser(); var newLog = new UserLog { ActionName = actionName, Url = url, Description = description, ClientIP = HttpContext.Current.Request.GetClientIPAddress(), UserName = user == null ? userName : user.FullName }; newLog.ResetCreated(); db.UserLogs.Add(newLog); db.SaveChanges(); } }
public object GetSerialCode(string parameter) { if (!parameter.IsNullOrEmpty()) { string[] paras = parameter.Trim().Split(','); if (paras.Length >= 3) { var category = paras[0]; var prefix = GetPrefix(paras[1]); var length = Convert.ToInt32(paras[2]); using (var db = SysContext.GetCurrent()) { return(db.GetNextSerialCode(category, prefix, length)); } } } return(""); }
public static IEnumerable <RolePageModel> GetRolePages(string roleID) { using (var db = SysContext.GetCurrent()) { return(db.RolePages.Where(w => w.RoleID.Equals(roleID, StringComparison.OrdinalIgnoreCase)).Select(w => new RolePageModel { RoleID = roleID, PageID = w.PageID, DataFilter = w.DataFilter, ActionValue = w.ActionValue }).ToList());; //return count.Select(w => new RolePageModel //{ // RoleID=roleID, // PageID=w.PageID, // DataFilter=w.DataFilter, // ActionValue=w.ActionValue //}).ToList(); //RolePage rolepage = list; } }
public static bool ChangePassword(string oldpassword, string password) { using (var db = SysContext.GetCurrent()) { string id = SysService.GetCurrentUser().UserID; var oldUser = db.Users.FirstOrDefault(u => u.UserID.Equals(id)); var op = oldpassword.GetMd5_32(); var np = password.GetMd5_32(); if (oldUser != null && oldUser.Password == op) { oldUser.Password = np; oldUser.ResetUpdated(); db.SaveChanges(); return(true); } else { return(false); } } }
public static List <RoleModel> GetRoles(string searchName, string description) { using (var db = SysContext.GetCurrent()) { var user = SysService.GetCurrentUser(); if (user.UserID == "U00001") { return(GetRoles()); } else { return((from r in db.Roles join ur in db.UserRoles on r.RoleID equals ur.RoleID where ur.UserID.Equals(user.UserID) select r) .Select(r => new RoleModel { RoleID = r.RoleID, Name = r.Name, Description = r.Description }).ToList()); } } }
public static UserModel GetUserModel(string userIDorName) { using (var db = SysContext.GetCurrent()) { var query = db.Users.Where(u => u.UserID.Equals(userIDorName) || u.UserName.Equals(userIDorName, StringComparison.OrdinalIgnoreCase)); // var user = db.Users.FirstOrDefault(u => (u.UserID.Equals(userIDorName) // || u.UserName.Equals(userIDorName, StringComparison.OrdinalIgnoreCase))); var user = query.Select((u) => new UserModel { UserID = u.UserID, UserName = u.UserName, FullName = u.FullName, Email = u.Email, IsValid = u.IsValid }).ToList().FirstOrDefault(); if (userIDorName.IndexOf("U") != -1) { user.Roles = (from r in db.Roles join s in db.UserRoles on r.RoleID equals s.RoleID where s.UserID.Equals(userIDorName) select r.Name).ToArray(); } return(user); } }
public static UserModel CreateUser(string userName, string email, string fullName, string password) { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var oldUser = db.Users.FirstOrDefault(u => u.IsValid && (u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase) || u.Email.Equals(email, StringComparison.OrdinalIgnoreCase))); if (oldUser != null) { throw new Exception("重复的用户名或邮件"); } var userID = db.GetNextUserID(); User newUser = new User { UserID = userID, Email = email, UserName = userName, FullName = fullName, IsValid = true, Password = password.GetMd5_32() }; newUser.ResetCreated(); db.Users.Add(newUser); db.SaveChanges(); return(new UserModel { UserID = newUser.UserID, UserName = newUser.UserName, Email = newUser.Email, IsValid = newUser.IsValid, FullName = newUser.FullName }); } } }
public static UserModel UpdateUser(UserModel user) { lock (_UserLock) { using (var db = SysContext.GetCurrent()) { var oldUser = db.Users.FirstOrDefault(u => u.UserID.Equals(user.UserID)); if (oldUser == null) { throw new Exception("用户不存在"); } oldUser.UserName = user.UserName; oldUser.FullName = user.FullName; oldUser.Email = user.Email; oldUser.ResetUpdated(); db.SaveChanges(); return(user); } } }
public static void UpdateRolePages(string roleID, IEnumerable <RolePage> items) { using (var db = SysContext.GetCurrent()) { var oldItems = db.RolePages.Where(w => w.RoleID.Equals(roleID)).ToList(); var adds = items.Where(w => oldItems.FirstOrDefault(t => t.PageID.Equals(w.PageID)) == null).ToList(); var removes = oldItems.Where(w => items.FirstOrDefault(t => t.PageID.Equals(w.PageID)) == null).ToList(); var updates = oldItems.Where(w => items.FirstOrDefault(t => t.PageID.Equals(w.PageID)) != null).ToList(); foreach (var t in removes) { db.RolePages.Remove(t); } foreach (var t in updates) { var newItem = items.FirstOrDefault(w => t.PageID.Equals(w.PageID)); var dataFilter = newItem.DataFilter; if (newItem.DataFilter == null) { dataFilter = ""; } if (newItem == null) { continue; } t.DataFilter = dataFilter; t.ActionValue = newItem.ActionValue; t.ResetUpdated(); } foreach (var t in adds) { var dataFilter = t.DataFilter; if (t.DataFilter == null) { dataFilter = ""; } var newItem = new RolePage { RoleID = roleID, PageID = t.PageID, ActionValue = t.ActionValue, DataFilter = dataFilter }; newItem.ResetCreated(); db.RolePages.Add(newItem); } //如果缓存了本角色相关的用户,则重置缓存 var users = db.UserRoles.Where(r => r.RoleID.Equals(roleID)).Select(r => r.UserID).ToArray(); if (users.Length > 0) { lock (_UserPages) { users.ForEach(u => { _UserPages.Remove(u); }); } } db.SaveChanges(); } }