public PermissionDtoContainer GetAllRolePermissions(int roleId)
        {
            PermissionDtoContainer container = new PermissionDtoContainer();
            ResourceTDataAccess    rda       = new ResourceTDataAccess();
            var resources   = rda.GetAll();
            var permissions = this.GetAll();

            container.PermissionDtoList.AddRange(permissions);
            container.SecurityResourceDtoList.AddRange(resources);
            var opRes = this.Repository.Context.PermissionToRoles.Where(it => it.RoleId == roleId).Select(x => new { permission = x.Permission, operation = x.Permission.Operation, resource = x.Permission.Securityresource, AccessType = x.PermisssionAccess }).ToList();

            foreach (var item in opRes)
            {
                var permission = container.PermissionDtoList.FirstOrDefault(it => it.PermissionId == item.permission.PermissionId);
                var resource   = item.resource;

                permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                permission.SecurityResourceDto.Checked = true;


                if (item.AccessType == 0)
                {
                    permission.AccessType = Common.Enums.AccessType.None;
                }
                if (item.AccessType == -1)
                {
                    permission.AccessType = Common.Enums.AccessType.Deny;
                }
                if (item.AccessType == 1)
                {
                    permission.AccessType           = Common.Enums.AccessType.Access;
                    permission.OperationDto.Checked = true;
                    ChekckAll(container.SecurityResourceDtoList, resource.SecurityResourceId);
                }
            }
            return(container);
        }
        public PermissionDtoContainer GetAllUserPermissions(long userId)
        {
            SecurityUserTDataAccess userDa    = new SecurityUserTDataAccess();
            PermissionDtoContainer  container = new PermissionDtoContainer();
            ResourceTDataAccess     rda       = new ResourceTDataAccess();
            var resources   = rda.GetAll();
            var permissions = this.GetAll();

            container.PermissionDtoList.AddRange(permissions);
            container.SecurityResourceDtoList.AddRange(resources);

            var opRes   = this.Repository.Context.PermissionToUsers.Where(it => it.UserId == userId).Select(x => new { permission = x.Permission, operation = x.Permission.Operation, resource = x.Permission.Securityresource, AccessType = x.PermissionAccess }).ToList();
            var opRoles = (from ur in this.Repository.Context.UserToRoles
                           join pu in this.Repository.Context.PermissionToRoles on ur.RoleId equals pu.RoleId
                           where ur.UserId == userId
                           select new { permission = pu.Permission, operation = pu.Permission.Operation, resource = pu.Permission.Securityresource, AccessType = pu.PermisssionAccess, Role = ur.Role }).ToList();
            var userGroups = userDa.GetUserGroupIds(userId);
            var opGroups   = (from rg in this.Repository.Context.RoleToGroups
                              join pr in this.Repository.Context.PermissionToRoles on rg.RoleId equals pr.RoleId
                              where userGroups.Contains(rg.GroupId.Value)
                              select new { permission = pr.Permission, operation = pr.Permission.Operation, resource = pr.Permission.Securityresource, AccessType = pr.PermisssionAccess, Group = rg.Securitygroup }).ToList();

            foreach (var item in opRes)
            {
                var permission = container.PermissionDtoList.FirstOrDefault(it => it.PermissionId == item.permission.PermissionId);
                var resource   = item.resource;
                permission.IsToUser                    = true;
                permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                permission.SecurityResourceDto.Checked = true;


                if (item.AccessType == 0)
                {
                    permission.AccessType = Common.Enums.AccessType.None;
                }
                if (item.AccessType == -1)
                {
                    permission.AccessType = Common.Enums.AccessType.Deny;
                }
                if (item.AccessType == 1)
                {
                    permission.AccessType           = Common.Enums.AccessType.Access;
                    permission.OperationDto.Checked = true;
                    ChekckAll(container.SecurityResourceDtoList, resource.SecurityResourceId);
                }
            }
            foreach (var item in opRoles)
            {
                var permission = container.PermissionDtoList.FirstOrDefault(it => it.PermissionId == item.permission.PermissionId);
                if (permission.SecurityResourceDto == null)
                {
                    var resource = item.resource;

                    permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                    permission.SecurityResourceDto.Checked = true;


                    if (item.AccessType == 0)
                    {
                        permission.AccessType = Common.Enums.AccessType.None;
                    }
                    if (item.AccessType == -1)
                    {
                        permission.AccessType = Common.Enums.AccessType.Deny;
                    }
                    if (item.AccessType == 1)
                    {
                        permission.AccessType           = Common.Enums.AccessType.Access;
                        permission.OperationDto.Checked = true;
                        ChekckAll(container.SecurityResourceDtoList, resource.SecurityResourceId);
                    }
                }
                permission.RoleDtos.Add(RoleRepository.GetDto(item.Role));
            }
            foreach (var item in opGroups)
            {
                var permission = container.PermissionDtoList.FirstOrDefault(it => it.PermissionId == item.permission.PermissionId);
                if (permission.SecurityResourceDto == null)
                {
                    var resource = item.resource;

                    permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                    permission.SecurityResourceDto.Checked = true;


                    if (item.AccessType == 0)
                    {
                        permission.AccessType = Common.Enums.AccessType.None;
                    }
                    if (item.AccessType == -1)
                    {
                        permission.AccessType = Common.Enums.AccessType.Deny;
                    }
                    if (item.AccessType == 1)
                    {
                        permission.AccessType           = Common.Enums.AccessType.Access;
                        permission.OperationDto.Checked = true;
                        ChekckAll(container.SecurityResourceDtoList, resource.SecurityResourceId);
                    }
                }
                permission.GroupDtos.Add(SecurityGroupRepository.GetDto(item.Group));
            }
            return(container);
        }