{ /// <summary> /// 判断用户访问某个页面的权限 /// </summary> /// <param name="memberID">会员ID</param> /// <returns></returns> public static bool ValidateMemberPagePermission(tb_Mem_Member member, tb_Pages page) { if (member == null || page == null) { return(false); } //先判断用户是否有权访问该页面 tb_Permissions permission = DataContext.tb_Permissions.Where(t => t.MemberID == member.MemberID && t.PageID == page.PageID).SingleOrDefault(); if (permission != null) { return(true); } //若用户没有权限,判断他所属的角色是否有权访问该页面 else { foreach (tb_MemberInRoles role in member.tb_MemberInRoles) { permission = DataContext.tb_Permissions.Where(t => t.RoleID == role.RoleID && t.PageID == page.PageID).SingleOrDefault(); if (permission != null) { return(true); } } } return(false); }
/// <summary> /// 根据PageID删除某个页面 /// </summary> /// <param name="pageID"></param> public static void DeletePageByID(long pageID) { try { tb_Pages page = GetPageByID(pageID); DataContext.tb_Pages.DeleteObject(page); DataContext.SaveChanges(); } catch (Exception ex) { LogHelper.g_Logger.ErrorFormat("删除页面信息失败!错误信息:{0}", ex.Message); throw ex; } }
/// <summary> /// 根据PageID修改某个页面 /// </summary> /// <param name="pageID"></param> /// <param name="pageTitle"></param> /// <param name="memo"></param> public static void UpdatePageByID(long pageID, string pageTitle, string memo) { try { tb_Pages page = GetPageByID(pageID); page.PageTitle = pageTitle; page.Memo = memo; DataContext.SaveChanges(); } catch (Exception ex) { LogHelper.g_Logger.ErrorFormat("修改页面信息失败!错误信息:{0}", ex.Message); throw ex; } }
/// <summary> /// 添加新的页面 /// </summary> /// <param name="pageTitle">页面名称</param> /// <param name="memo">备注</param> public static void AddNewPage(string pageTitle, string memo) { try { tb_Pages newPage = new tb_Pages(); newPage.PageTitle = pageTitle; newPage.Memo = memo; DataContext.tb_Pages.AddObject(newPage); DataContext.SaveChanges(); } catch (Exception ex) { LogHelper.g_Logger.ErrorFormat("添加页面信息失败!错误信息:{0}", ex.Message); throw ex; } }
{ /// <summary> /// 在每个页面加载时判断用户访问该页面的权限 /// </summary> /// <param name="e"></param> protected override void OnPreLoad(EventArgs e) { base.OnPreLoad(e); if (!string.IsNullOrEmpty(User.Identity.Name)) { tb_Mem_Member currentUser = BizMember.GetMemberByID(long.Parse(User.Identity.Name)); tb_Pages currentPage = BizPage.GetPageByTitle(this.Title); if (!BALPermission.ValidateMemberPagePermission(currentUser, currentPage)) { Response.Redirect("~/Common/Aspx/ErrorPage.aspx"); } } else { Response.Redirect("~/Login.aspx"); } }
/// <summary> /// 授予一个或多个用户访问该页面的权限 /// </summary> /// <param name="pageID"></param> /// <returns></returns> public static void AddMemberPages(long pageID, List <long> memberIDList) { try { tb_Pages page = GetPageByID(pageID); foreach (long id in memberIDList) { tb_Permissions p = new tb_Permissions(); p.MemberID = id; page.tb_Permissions.Add(p); } DataContext.SaveChanges(); } catch (Exception ex) { LogHelper.g_Logger.ErrorFormat("授予用户权限失败!错误信息:{0}", ex.Message); throw ex; } }
/// <summary> /// 删除一个或多个用户访问该页面的权限 /// </summary> /// <param name="pageID"></param> /// <param name="roleIDList"></param> public static void RemovePageMembers(long pageID, List <long> memberIDList) { try { tb_Pages page = GetPageByID(pageID); foreach (long id in memberIDList) { tb_Permissions p = page.tb_Permissions.Where(t => t.MemberID == id).First(); page.tb_Permissions.Remove(p); DataContext.tb_Permissions.DeleteObject(p); } DataContext.SaveChanges(); } catch (Exception ex) { LogHelper.g_Logger.ErrorFormat("删除用户权限失败!错误信息:{0}", ex.Message); throw ex; } }