public PermissionDtoContainer GetCurrentRolePermissions(int roleId)
        {
            PermissionDtoContainer container = new PermissionDtoContainer();
            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 = PermissionRepository.GetDto(item.permission);
                var operatoin  = item.operation;
                var resource   = item.resource;
                permission.OperationDto = OperationRepository.GetDto(operatoin);

                permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                permission.SecurityResourceDto.Checked = true;
                container.PermissionDtoList.Add(permission);
                //  container.SecurityResourceDtoList.Add(permission.SecurityResourceDto);
                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;
                    AddResources(permission.SecurityResourceDto, container.SecurityResourceDtoList);
                }
            }
            return(container);
        }
        public List <OperationDto> UserOperations(Common.DTO.UserDto user, Common.DTO.SecurityResourceDto resource)
        {
            List <OperationDto> lst = new List <OperationDto>();
            var operations          = this.Repository.Context.PermissionToUsers.Where(it => it.UserId == user.UserId && it.Permission.ResourceId == resource.SecurityResourceId).Select(it => it.Permission.Operation).ToList();

            foreach (var operation in operations)
            {
                var dto = OperationRepository.GetDto(operation);
                lst.Add(dto);
            }

            return(lst);
        }
        public PermissionDtoContainer GetGroupPermissionContainer(int groupId)
        {
            PermissionDtoContainer container = new PermissionDtoContainer();
            var opRes = (from g in this.Repository.Context.SecurityGroups
                         join rg in this.Repository.Context.RoleToGroups on g.SecurityGroupId equals rg.GroupId
                         join rp in this.Repository.Context.PermissionToRoles on rg.RoleId equals rp.RoleId
                         where g.SecurityGroupId == groupId && rp.PermisssionAccess > 0
                         select new { permission = rp.Permission, operation = rp.Permission.Operation, resource = rp.Permission.Securityresource, AccessType = rp.PermisssionAccess }).ToList();

            foreach (var item in opRes)
            {
                var permission = PermissionRepository.GetDto(item.permission);
                var operatoin  = item.operation;
                var resource   = item.resource;
                permission.OperationDto = OperationRepository.GetDto(operatoin);

                permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                permission.SecurityResourceDto.Checked = true;
                container.PermissionDtoList.Add(permission);
                //  container.SecurityResourceDtoList.Add(permission.SecurityResourceDto);
                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;
                    AddResources(permission.SecurityResourceDto, container.SecurityResourceDtoList);
                }
            }

            return(container);
        }
        public PermissionDtoContainer GetCurrentUserPermissions(long userId)
        {
            PermissionDtoContainer  container = new PermissionDtoContainer();
            SecurityUserTDataAccess userDa    = new SecurityUserTDataAccess();

            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();

            //TODO Make faster
            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 = PermissionRepository.GetDto(item.permission);
                var operatoin  = item.operation;
                var resource   = item.resource;
                permission.OperationDto                = OperationRepository.GetDto(operatoin);
                permission.IsToUser                    = true;
                permission.SecurityResourceDto         = SecurityResourceRepository.GetDto(resource);
                permission.SecurityResourceDto.Checked = true;
                container.PermissionDtoList.Add(permission);

                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;
                    AddResources(permission.SecurityResourceDto, container.SecurityResourceDtoList);
                }
            }
            foreach (var item in opRoles)
            {
                var permission = PermissionRepository.GetDto(item.permission);
                var operatoin  = item.operation;
                var resource   = item.resource;
                permission.SecurityResourceDto = SecurityResourceRepository.GetDto(resource);
                permission.OperationDto        = OperationRepository.GetDto(operatoin);
                if (permission != null)
                {
                    if (permission.SecurityResourceDto == null)
                    {
                        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));
                }
                else
                {
                    var permissionDto = PermissionRepository.GetDto(item.permission);
                    permissionDto.RoleDtos.Add(RoleRepository.GetDto(item.Role));
                    container.PermissionDtoList.Add(permissionDto);
                }
            }
            foreach (var item in opGroups)
            {
                var permission = container.PermissionDtoList.FirstOrDefault(it => it.PermissionId == item.permission.PermissionId);
                if (permission != null)
                {
                    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));
                }
                else
                {
                    var permissionDto = PermissionRepository.GetDto(item.permission);
                    permissionDto.GroupDtos.Add(SecurityGroupRepository.GetDto(item.Group));
                    container.PermissionDtoList.Add(permissionDto);
                }
            }
            return(container);
        }