Пример #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));
        }
        public async Task <ActionResult <GroupDto> > CreateGroup([FromBody] GroupCreationDto group)
        {
            try
            {
                await _azureAdRepository.GetGroup(group.Id.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    ModelState.AddModelError("azureAdGroupNotFound",
                                             $"Group with id: {group.Id} was not found on Azure AD");
                }
            }

            var groupExists = await _groupRepository.GroupExists(group.Id);

            if (groupExists)
            {
                return(Conflict("Group already exists"));
            }

            if (group.SmartLockGroups.Count > 0)
            {
                foreach (var smartLockGroup in group.SmartLockGroups)
                {
                    var smartLockExist = await _smartLockRepository.SmartLockExists(smartLockGroup.SmartLockId);

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

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

            var groupEntity = _mapper.Map <Group>(group);

            _groupRepository.AddGroup(groupEntity);
            await _groupRepository.Save();

            var groupDto = _mapper.Map <GroupDto>(groupEntity);

            return(CreatedAtAction("GetGroup", new { groupId = groupDto.Id }, groupDto));
        }
        public async Task <ActionResult <SmartLockDto> > GetSmartLock(Guid smartLockId)
        {
            var smartLockExists = await _smartLockRepository.SmartLockExists(smartLockId);

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

            var smartLockFromRepo = await _smartLockRepository.GetSmartLock(smartLockId);

            var smartLock = _mapper.Map <SmartLockDto>(smartLockFromRepo);

            return(Ok(smartLock));
        }
Пример #4
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));
        }