Esempio n. 1
0
        public async Task <ActionResult <UserDto> > CreateUser([FromBody] UserCreationDto userToAdd)
        {
            var client = await MicrosoftGraphClient.GetGraphServiceClient();

            try
            {
                await _azureAdRepository.GetUser(client, userToAdd.Id.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    ModelState.AddModelError("azureAdUserNotFound",
                                             $"User with id: {userToAdd.Id} was not found on Azure AD");
                }
            }

            var userExists = await _userRepository.UserExists(userToAdd.Id);

            if (userExists)
            {
                return(Conflict("User already exists"));
            }
            if (userToAdd.SmartLockUsers.Count > 0)
            {
                foreach (var smartLockUser in userToAdd.SmartLockUsers)
                {
                    var smartLockExist = await _smartLockRepository.SmartLockExists(smartLockUser.SmartLockId);

                    if (!smartLockExist)
                    {
                        ModelState.AddModelError("smartLockNotExist",
                                                 $"Smart lock with id: {smartLockUser.SmartLockId} doesn't exist");
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var userEntity = _mapper.Map <User>(userToAdd);

            _userRepository.AddUser(userEntity);
            await _userRepository.Save();

            var userDto = _mapper.Map <UserDto>(userEntity);

            return(CreatedAtAction("GetUser", new { userId = userDto.Id }, userDto));
        }
Esempio n. 2
0
        public async Task <ActionResult <UserAccessDto> > AccessSmartLock(SmartLockUserAccessDto smartLockUser)
        {
            var userExists = await _userRepository.UserExists(smartLockUser.UserId);

            if (!userExists)
            {
                return(NotFound());
            }

            var smartLockExists = await _smartLockRepository.SmartLockExists(smartLockUser.SmartLockId);

            if (!smartLockExists)
            {
                return(NotFound());
            }

            var client = await MicrosoftGraphClient.GetGraphServiceClient();

            try
            {
                await _azureAdRepository.GetUser(client, smartLockUser.UserId.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    ModelState.AddModelError("azureAdUserNotFound",
                                             $"User with id: {smartLockUser.UserId} was not found on Azure AD");
                }
            }

            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var userAccessStatus = await _accessService.GetUserAccessStatus(smartLockUser);

            return(Ok(userAccessStatus));
        }
        public async Task <ActionResult <AzureAdUserDto> > GetUser(string userId)
        {
            try
            {
                if (string.IsNullOrEmpty(userId) || string.IsNullOrWhiteSpace(userId))
                {
                    return(BadRequest());
                }

                // Initialize the GraphServiceClient.
                var client = await MicrosoftGraphClient.GetGraphServiceClient();

                // Load user profile.
                var user = await _azureAdRepository.GetUser(client, userId);

                var userDto = _mapper.Map <AzureAdUserDto>(user);

                var userExist = await _userRepository.UserExists(userDto.Id);

                if (userExist)
                {
                    userDto.AddedToDb = true;
                }

                return(Ok(userDto));
            }
            catch (ServiceException ex)
            {
                if (ex.StatusCode == HttpStatusCode.BadRequest)
                {
                    return(BadRequest());
                }
                else
                {
                    return(NotFound());
                }
            }
        }
        public async Task <ActionResult <IEnumerable <UserDto> > > AddSmartLockUser(Guid smartLockId,
                                                                                    SmartLockUserCreationDto smartLockUser)
        {
            var client = await MicrosoftGraphClient.GetGraphServiceClient();

            try
            {
                await _azureAdRepository.GetUser(client, smartLockUser.UserId.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    _logger.LogWarning("User was not found on Azure AD");
                    ModelState.AddModelError("azureAdUserNotFound",
                                             $"User with id: {smartLockUser.UserId} was not found on Azure AD");
                }
            }
            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var userExists = await _smartLockRepository.SmartLockUserExists(smartLockId, smartLockUser.UserId);

            if (userExists)
            {
                _logger.LogWarning("User already exists for this smart lock");
                return(Conflict("User already exists for this smart lock"));
            }

            _smartLockRepository.AddSmartLockUser(smartLockId, smartLockUser.UserId);
            await _smartLockRepository.Save();

            return(CreatedAtAction("GetSmartLockUser", new { smartLockId = smartLockId, userId = smartLockUser.UserId },
                                   smartLockUser));
        }