public List <SecurityGroupDto> GetRoleGroups(RoleDto role) { AppDomainTDataAccess appDomainDa = new AppDomainTDataAccess(); var groups = this.Repository.Context.RoleToGroups.Where(it => it.RoleId == role.RoleId).Select(it => it.Securitygroup); var dtoList = SecurityGroupRepository.GetDtos(groups); foreach (var dto in dtoList) { var appDomain = appDomainDa.GetSingle(it => it.ApplicationDomainId == dto.AppDomainId); if (appDomain != null) { dto.ApplicationDomainDto = appDomain; } } return(dtoList.ToList()); }
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); }