package com.atlassian.stash.internal.user;

import com.atlassian.plugin.spring.AvailableToPlugins;
import com.atlassian.bitbucket.AuthorisationException;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionValidationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;

/**
 * Implementation of {@link PermissionValidationService} which uses Spring Security
 * annotations and exception rewrite advice to throw {@link AuthorisationException}
 */
@Service("permissionValidationService")
@AvailableToPlugins(PermissionValidationService.class)
public class PermissionValidationServiceImpl implements PermissionValidationService {

    @Override
    @PreAuthorize("isAuthenticated()")
    public void validateAuthenticated() throws AuthorisationException {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("hasGlobalPermission(#permission)")
    public void validateForGlobal(@Nonnull Permission permission) throws AuthorisationException {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("hasProjectPermission(#project, #permission)")
    public void validateForProject(@Nonnull Project project, @Nonnull Permission permission) {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("hasProjectPermission(#projectId, #permission)")
    public void validateForProject(int projectId, @Nonnull Permission permission) {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("hasRepositoryPermission(#repository, #permission)")
    public void validateForRepository(@Nonnull Repository repository, @Nonnull Permission permission) {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("hasRepositoryPermission(#repositoryId, #permission)")
    public void validateForRepository(int repositoryId, @Nonnull Permission permission) {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("isRepositoryAccessible(#repository)")
    public void validateRepositoryAccessible(@Nonnull Repository repository) {
        // Noop. Annotation driven
    }

    @Override
    @PreAuthorize("isRepositoryAccessible(#repositoryId)")
    public void validateRepositoryAccessible(int repositoryId) {
        // Noop. Annotation driven
    }
}
