Example #1
0
 public PermissionService()
 {
     _CU_Role_PageService = new CU_Role_PageService();
     _CU_PageService      = new CU_PageService();
     _CU_ActionService    = new CU_ActionService();
     _CU_LogService       = new CU_LogService();
 }
        public Dictionary <string, Dictionary <QVEnterprise.ActionType, bool> > GetPermission(IEnumerable <string> Controllers, int userID)
        {
            // CU_Employee_RoleService ser = new CU_Employee_RoleService();
            CU_Employee_RoleProgramService ser = new CU_Employee_RoleProgramService();

            Dictionary <int, string> pages = new CU_PageService().GetPageIdsByURLs(Controllers);

            //Get all permissions for all pages once and query them when needed instead of querying the database everytime
            Dictionary <string, Dictionary <QVEnterprise.ActionType, bool> > pagesPermissions = new Dictionary <string, Dictionary <QVEnterprise.ActionType, bool> >();


            CU_PageService page = new CU_PageService();

            CU_Role_PageService rolepageservice = new CU_Role_PageService();
            var rolepages = rolepageservice.CU_RolepageList;
            int progID    = Extention.GeKeyValue <int>("ProgramID");

            var actionList = new CU_ActionService().CU_ActionList.ToList();

            var employeeRolesProgram = ser.GetEmployeeRoleProgram(userID, progID);

            foreach (int pageId in pages.Keys)
            {
                List <CU_Role_Page> rolePageLst = new List <CU_Role_Page>();
                var Permission = new Dictionary <QVEnterprise.ActionType, bool>();
                foreach (var role in employeeRolesProgram)
                {
                    var rolesPage = rolepages.Where(r => r.IdRoleProgram == role.IdRoleProgram && pageId == r.IdPage).FirstOrDefault();
                    if (rolesPage != null)
                    {
                        rolePageLst.Add(rolesPage);
                    }
                }
                //for each action get max permission allawed in logined user roles
                actionList.ForEach(delegate(CU_Action i)
                {
                    bool hasPermission = rolePageLst.Where(p => p.Permission.Length > i.Order && p.Permission.Substring(i.Order, 1) == "1").Count() > 0;
                    Permission.Add((QVEnterprise.ActionType)System.Enum.Parse(typeof(QVEnterprise.ActionType), i.EnName), hasPermission);
                });

                //we should not check for this.. but because there are 2 records in the page table having same url :(
                if (!pagesPermissions.ContainsKey(pages[pageId]))
                {
                    pagesPermissions.Add(pages[pageId], Permission);
                }
            }

            return(pagesPermissions);
        }