Exemple #1
0
        /// <inheritdoc />
        public async Task <Guid> Create(CreateDeviceRequest request, string token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            var devices = await _deviceRepository.GetAsync();

            if (devices.Any(x => x.Udid == request.Udid))
            {
                throw new ApplicationException($"Device with udid: {request.Udid} already exist");
            }

            var scopeArray       = new [] { Const.Permissions.Device.AdminCreate };
            var correctCompanyId = await _validationHelper.GetCompanyIdByPermission(token, scopeArray, request.CompanyId);

            if (!correctCompanyId.HasValue)
            {
                throw new ArgumentException("Invalid companyId", nameof(request.CompanyId));
            }
            request.CompanyId = correctCompanyId.Value;

            await _validationHelper.ValidateCompanyAndBranch(request.CompanyId, request.BranchId, token);

            var device = request.Adapt <Device>();

            device.CreatedOn = DateTime.UtcNow;
            return(await _deviceRepository.AddAsync(device));
        }
Exemple #2
0
        /// <inheritdoc />
        public async Task <LazyLoadedResult <DeviceHistoryModel> > GetHistoryByDeviceId(string token, Guid deviceId, LazyLoadParameters filters = null)
        {
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            var device = await _deviceRepository.GetAsync(deviceId);

            if (device == null)
            {
                throw new KeyNotFoundException($"Device with id: {deviceId} not found");
            }

            IQueryable <DeviceHistory> deviceHistory = await _deviceHistoryRepository.GetAsync(x => x.DeviceId == device.Id);

            var scopeArray = new[] { Const.Permissions.History.AdminRead };
            var companyId  = await _validationHelper.GetCompanyIdByPermission(token, scopeArray);

            if (companyId.HasValue && companyId != device.CompanyId)
            {
                throw new AccessException();
            }
            else if (companyId == device.CompanyId)
            {
                deviceHistory = deviceHistory.Where(x => x.CompanyId == device.CompanyId);
            }

            return(deviceHistory.OrderByDescending(x => x.CreatedOn).GetPart(filters).Adapt <LazyLoadedResult <DeviceHistoryModel> >());
        }
Exemple #3
0
        public async Task ShouldReturnSuccessWhenGetCompanyIdByPermissionAsSuperviser()
        {
            // Arrange
            int    companyId     = 5;
            int    userCompanyId = 6;
            string token         = _baseTest.Fixture.Create <string>();

            string[] superviserScopes = _baseTest.Fixture.Create <string[]>();
            var      userModel        = _baseTest.Fixture.Build <UserModel>().With(x => x.CompanyId, userCompanyId).Create();

            _httpServiceMock.Setup(x => x.GetCurrentUser(token)).ReturnsAsync(userModel);
            _identityManagerMock.Setup(x => x.HasAccess(token, superviserScopes)).ReturnsAsync(false);

            // Act
            var correctCompanyId = await _validationHelper.GetCompanyIdByPermission(token, superviserScopes, companyId);

            //Assert
            correctCompanyId.Should().Be(userCompanyId);
        }