public async Task <ViewModels.Vm> GetAsync(Guid id, CancellationToken ct) { var vmEntity = await _context.Vms .Include(v => v.VmTeams) .Where(v => v.Id == id) .SingleOrDefaultAsync(ct); if (vmEntity == null) { return(null); } var teamIds = vmEntity.VmTeams.Select(x => x.TeamId); if (!(await _playerService.CanAccessTeamsAsync(teamIds, ct))) { throw new ForbiddenException(); } if (vmEntity.UserId.HasValue && vmEntity.UserId != _user.GetId() && !(await _playerService.CanManageTeamsAsync(teamIds, false, ct))) { throw new ForbiddenException("This machine belongs to another user"); } var model = _mapper.Map <ViewModels.Vm>(vmEntity); model.CanAccessNicConfiguration = await _playerService.CanManageTeamsAsync(teamIds, false, ct); var teamId = await _playerService.GetPrimaryTeamByExerciseIdAsync(model.ExerciseId, ct); model.TeamId = teamId; return(model); }
protected async Task <VsphereVirtualMachine> GetVsphereVirtualMachine(Features.Vms.Vm vm, CancellationToken cancellationToken) { var domainMachine = await _vsphereService.GetMachineById(vm.Id); if (domainMachine == null) { throw new EntityNotFoundException <VsphereVirtualMachine>(); } var vsphereVirtualMachine = _mapper.Map <VsphereVirtualMachine>(domainMachine); var canManage = await _playerService.CanManageTeamsAsync(vm.TeamIds, false, cancellationToken); vsphereVirtualMachine.Ticket = await _vsphereService.GetConsoleUrl(domainMachine);; vsphereVirtualMachine.NetworkCards = await _vsphereService.GetNicOptions( id : vm.Id, canManage : canManage, allowedNetworks : vm.AllowedNetworks, machine : domainMachine); // copy vm properties vsphereVirtualMachine = _mapper.Map(vm, vsphereVirtualMachine); vsphereVirtualMachine.CanAccessNicConfiguration = canManage; vsphereVirtualMachine.IsOwner = vsphereVirtualMachine.UserId == _userId; return(vsphereVirtualMachine); }
public async Task <VsphereVirtualMachine> Handle(Command request, CancellationToken cancellationToken) { var vm = await base.GetVm(request.Id, Permissions.ReadOnly, cancellationToken); if (!(await _playerService.CanManageTeamsAsync(vm.TeamIds, false, cancellationToken))) { throw new ForbiddenException("You do not have permission to change networks on this vm."); } await _vsphereService.ReconfigureVm(request.Id, Feature.net, request.Adapter, request.Network); return(await base.GetVsphereVirtualMachine(vm, cancellationToken)); }
public async Task <bool> CanAccessVm(Domain.Models.Vm vm, CancellationToken ct) { if (vm == null) { throw new EntityNotFoundException <Vm>(); } var teamIds = vm.VmTeams.Select(x => x.TeamId); if (!await _playerService.CanAccessTeamsAsync(teamIds, ct)) { throw new ForbiddenException(); } if (vm.UserId.HasValue && vm.UserId != _user.GetId() && !await _playerService.CanManageTeamsAsync(teamIds, false, ct)) { throw new ForbiddenException("This machine belongs to another user"); } return(true); }