/// <summary> /// Gets the list permission by user. /// </summary> /// <param name="username">The username.</param> /// <returns>Get List permission By User</returns> public List <PagePermission> GetListPermissionByUser(string username) { List <PagePermission> lstPagePermission = new List <PagePermission>(); List <PagePermission> lstData = new List <PagePermission>(); using (var data = new Entities()) { var query = from u in data.C_UserPermission join p in data.C_UserPage on u.page_id equals p.id where u.user == username select new { u.user, u.page_id, u.del, u.add, u.edit, u.view, p.Title, p.ParentId, p.OrderDisplay }; if (query.Any()) { foreach (var obj in query) { PagePermission page = new PagePermission(); page.Page_ID = (int)obj.page_id; page.User_Name = obj.user; page.Title = obj.Title; page.Add = (bool)obj.add; page.Edit = (bool)obj.edit; page.Delete = (bool)obj.del; page.View = (bool)obj.view; page.Parent_ID = (int)obj.ParentId; page.OrderDisplay = (int)obj.OrderDisplay; lstPagePermission.Add(page); } List <C_UserPermission> lstUserPermission = data.C_UserPermission.Where(u => u.user == username).ToList(); var dataID = from c in data.C_UserPermission where c.user == username select new { c.page_id }; List <int> lstPageID = new List <int>(); if (dataID.Any()) { foreach (var obj in dataID) { lstPageID.Add((int)obj.page_id); } } List <C_UserPage> lstUserPage = data.C_UserPage.Where(c => !lstPageID.Contains(c.id) && c.ParentId > 0).ToList(); if (lstUserPage.Any()) { foreach (var obj in lstUserPage) { PagePermission page = new PagePermission(); page.Page_ID = obj.id; page.User_Name = username; page.Title = obj.Title; page.Add = false; page.Edit = false; page.Delete = false; page.View = false; page.Parent_ID = (int)obj.ParentId; page.OrderDisplay = (int)obj.OrderDisplay; lstPagePermission.Add(page); } } } else { List <C_UserPage> lstUserPage = data.C_UserPage.Where(p => p.ParentId > 0).ToList(); if (lstUserPage.Any()) { foreach (var obj in lstUserPage) { PagePermission page = new PagePermission(); page.Page_ID = obj.id; page.User_Name = username; page.Title = obj.Title; page.Add = false; page.Edit = false; page.Delete = false; page.View = false; page.Parent_ID = (int)obj.ParentId; page.OrderDisplay = (int)obj.OrderDisplay; lstPagePermission.Add(page); } } } List <C_UserPage> lstUserPageParent = data.C_UserPage.Where(p => p.ParentId == 0).OrderBy(p => p.OrderDisplay).ToList(); foreach (var parent in lstUserPageParent) { PagePermission page = new PagePermission(); page.Page_ID = parent.id; page.User_Name = username; page.Title = parent.Title; page.Add = false; page.Edit = false; page.Delete = false; page.View = false; page.Parent_ID = (int)parent.ParentId; page.OrderDisplay = (int)parent.OrderDisplay; lstData.Add(page); lstData.AddRange(lstPagePermission.Where(p => p.Parent_ID == parent.id).OrderBy(p => p.OrderDisplay)); } } return(lstData); }
/// <summary> /// Checks the permission. /// </summary> /// <param name="user_name">Name of the user.</param> /// <param name="act">The act.</param> /// <param name="ctrl">The control.</param> /// <param name="type_action">The type action.</param> /// <param name="type">The type.</param> /// <returns>Checks the permission</returns> public static bool CheckPermission(string user_name, string act, string ctrl, string type_action, string type) { try { using (var data = new Entities()) { C_UserPage objUserPage = new C_UserPage(); if (string.IsNullOrEmpty(type)) { objUserPage = data.C_UserPage.Where(c => c.act == act && c.ctrl == ctrl).FirstOrDefault(); } else { objUserPage = data.C_UserPage.Where(c => c.act == act && c.ctrl == ctrl && c.Tye == type).FirstOrDefault(); } if (objUserPage != null) { C_UserPermission objUserPermission = data.C_UserPermission.Where(p => p.page_id == objUserPage.id && p.user == user_name).FirstOrDefault(); if (objUserPermission != null) { bool isOK = false; switch (type_action) { case CommonGlobal.Add: isOK = (bool)objUserPermission.add; break; case CommonGlobal.Edit: isOK = (bool)objUserPermission.edit; break; case CommonGlobal.Delete: isOK = (bool)objUserPermission.del; break; case CommonGlobal.View: isOK = (bool)objUserPermission.view; break; default: isOK = false; break; } return(isOK); } else { return(false); } } else { return(false); } } } catch (Exception) { return(false); } }