/** * Returns the scopes allowed for credentials stored within the specified object or {@code null} if the * object is not relevant for scopes and the object's container should be considered instead. * * @param object the object. * @return the set of scopes that are relevant for the object or {@code null} if the object is not a credentials * container. */ @CheckForNull public static Set<CredentialsScope> lookupScopes(ModelObject object) { object = CredentialsDescriptor.unwrapContext(object); Set<CredentialsScope> result = null; for (CredentialsProvider provider : all()) { if (provider.isEnabled(object)) { try { Set<CredentialsScope> scopes = provider.getScopes(object); if (scopes != null) { // if multiple providers for the same object, then combine scopes if (result == null) { result = new LinkedHashSet<CredentialsScope>(); } result.addAll(scopes); } } catch (NoClassDefFoundError e) { // ignore optional dependency } } } return result; }
/** * Returns the scopes allowed for credentials stored within the specified object or {@code null} if the * object is not relevant for scopes and the object's container should be considered instead. * * @param object the object. * @return the set of scopes that are relevant for the object or {@code null} if the object is not a credentials * container. */ @CheckForNull public static Set<CredentialsScope> lookupScopes(ModelObject object) { object = CredentialsDescriptor.unwrapContext(object); Set<CredentialsScope> result = null; for (CredentialsProvider provider : all()) { if (provider.isEnabled(object)) { try { Set<CredentialsScope> scopes = provider.getScopes(object); if (scopes != null) { // if multiple providers for the same object, then combine scopes if (result == null) { result = new LinkedHashSet<CredentialsScope>(); } result.addAll(scopes); } } catch (NoClassDefFoundError e) { // ignore optional dependency } } } return result; }
/** * Checks if asking for a credentials scope is relevant. For example, when a scope will be stored in * {@link UserCredentialsProvider}, there is no need to specify the scope, * as it can only be {@link CredentialsScope#USER}, but where the credential will be stored in * {@link SystemCredentialsProvider}, there are multiple scopes relevant for that container, so the scope * field is relevant. * * @return {@code true} if the nearest request object that acts as a container for credentials needs a scope * to be specified. */ @SuppressWarnings("unused") // used by stapler @Restricted(NoExternalUse.class) @RestrictedSince("2.1.5") public boolean isScopeRelevant() { Ancestor ancestor = Stapler.getCurrentRequest().findAncestor(Object.class); while (ancestor != null) { if (ancestor.getObject() instanceof ModelObject) { ModelObject context = unwrapContext((ModelObject) ancestor.getObject()); Set<CredentialsScope> scopes = CredentialsProvider.lookupScopes(context); if (scopes != null) { return scopes.size() > 1; } } ancestor = ancestor.getPrev(); } return false; }
/** * Checks if asking for a credentials scope is relevant. For example, when a scope will be stored in * {@link UserCredentialsProvider}, there is no need to specify the scope, * as it can only be {@link CredentialsScope#USER}, but where the credential will be stored in * {@link SystemCredentialsProvider}, there are multiple scopes relevant for that container, so the scope * field is relevant. * * @return {@code true} if the nearest request object that acts as a container for credentials needs a scope * to be specified. */ @SuppressWarnings("unused") // used by stapler @Restricted(NoExternalUse.class) @RestrictedSince("2.1.5") public boolean isScopeRelevant() { Ancestor ancestor = Stapler.getCurrentRequest().findAncestor(Object.class); while (ancestor != null) { if (ancestor.getObject() instanceof ModelObject) { ModelObject context = unwrapContext((ModelObject) ancestor.getObject()); Set<CredentialsScope> scopes = CredentialsProvider.lookupScopes(context); if (scopes != null) { return scopes.size() > 1; } } ancestor = ancestor.getPrev(); } return false; }