package com.atlassian.bitbucket.internal.mirroring.mirror.user;

import com.atlassian.bitbucket.internal.mirroring.user.ApplicationUserWithPermissions;
import com.atlassian.bitbucket.permission.EffectivePermission;
import com.atlassian.bitbucket.permission.EffectivePermissionsProvider;
import com.atlassian.bitbucket.user.ApplicationUser;

import javax.annotation.Nonnull;

/**
 * Supplies the effective permissions for the supplied user if they are of type {@link ApplicationUserWithPermissions}.
 * This is used to apply effective permissions retrieved during delegated authentication.
 */
public class DelegatedAuthenticationPermissionsSupplier implements EffectivePermissionsProvider {

    @Nonnull
    @Override
    public Iterable<EffectivePermission> getPermissions(@Nonnull ApplicationUser user) {
        return user.accept(new NoneByDefaultPermissionsVisitorAdapter() {
            @Override
            public Iterable<EffectivePermission> visit(@Nonnull ApplicationUserWithPermissions user) {
                return user.getEffectivePermissions();
            }
        });
    }
}
