Exemplo n.º 1
0
        public bool HasPermission(BoardUser boardUser, BoardUserPermission permission)
        {
            if (boardUser is null)
            {
                throw new ArgumentNullException(nameof(boardUser));
            }
            if (!Enum.IsDefined(typeof(BoardUserPermission), permission))
            {
                throw new InvalidOperationException(nameof(permission));
            }

            bool CollaboratorHasPermission() => permission switch
            {
                BoardUserPermission.CAN_EDIT_BOARD => false,
                BoardUserPermission.CAN_DELETE_BOARD => false,
                _ => true
            };

            return(boardUser.Type switch
            {
                BoardUserType.None => false,
                BoardUserType.Owner => true,
                BoardUserType.Collaborator => CollaboratorHasPermission(),
                _ => throw new InvalidOperationException(nameof(boardUser.Type))
            });
        public async Task <bool> HasPermissionForActivity(int activityId, BoardUserPermission permission, CancellationToken cancellationToken)
        {
            var currentUserId = _currentUser.UserId;

            var cardId = await _dbContext.Activities
                         .Where(a => a.Id == activityId)
                         .Select(a => a.CardId)
                         .FirstOrDefaultAsync(cancellationToken);

            if (cardId == default)
            {
                throw new NotFoundApplicationException($"Could not find activity with ID: {activityId}");
            }

            var boardId = await _dbContext.Cards
                          .Where(c => c.Id == cardId)
                          .Select(c => c.BoardId)
                          .FirstOrDefaultAsync(cancellationToken);

            var boardUser = await _dbContext.BoardUsers.FirstOrDefaultAsync(bu => bu.BoardId == boardId && bu.UserId == currentUserId, cancellationToken);

            if (boardUser is null)
            {
                return(false);
            }

            return(_boardUserPermissionService.HasPermission(boardUser, permission));
        }
Exemplo n.º 3
0
        public void HasPermission_IsOwner_ReturnsTrue(BoardUserPermission permission)
        {
            var service   = new BoardUserPermissionService();
            var boardUser = new BoardUser(1, "user-id", BoardUserType.Owner);

            var result = service.HasPermission(boardUser, permission);

            Assert.True(result);
        }
Exemplo n.º 4
0
        public void HasPermission_IsNone_ReturnsFalse(BoardUserPermission permission)
        {
            var service   = new BoardUserPermissionService();
            var boardUser = new BoardUser(1, "user-id", BoardUserType.None);

            var result = service.HasPermission(boardUser, permission);

            Assert.False(result);
        }
        public async Task <bool> HasPermissionForBoard(int boardId, BoardUserPermission permission, CancellationToken cancellationToken)
        {
            var currentUserId = _currentUser.UserId;

            var boardExists = await _dbContext.Boards.Where(b => b.Id == boardId).AnyAsync(cancellationToken);

            if (!boardExists)
            {
                throw new NotFoundApplicationException($"Could not find board with ID: {boardId}");
            }

            var boardUser = await _dbContext.BoardUsers.FirstOrDefaultAsync(bu => bu.BoardId == boardId && bu.UserId == currentUserId, cancellationToken);

            if (boardUser is null)
            {
                return(false);
            }

            return(_boardUserPermissionService.HasPermission(boardUser, permission));
        }
Exemplo n.º 6
0
        public void HasPermission_AllCardAndActivityPermissions_IsCollaborator_ReturnsTrue(BoardUserPermission permission)
        {
            var service   = new BoardUserPermissionService();
            var boardUser = new BoardUser(1, "user-id", BoardUserType.Collaborator);

            var result = service.HasPermission(boardUser, permission);

            Assert.True(result);
        }