system scope ordering consistency

pull/306/merge
Justin Richer 2013-02-05 11:11:41 -05:00
parent 173281f039
commit c2b9fd4db1
4 changed files with 44 additions and 12 deletions

View File

@ -20,7 +20,7 @@ import javax.persistence.Table;
@Entity
@Table(name = "system_scope")
@NamedQueries({
@NamedQuery(name = "SystemScope.findAll", query = "select s from SystemScope s"),
@NamedQuery(name = "SystemScope.findAll", query = "select s from SystemScope s ORDER BY s.id"),
@NamedQuery(name = "SystemScope.getByValue", query = "select s from SystemScope s WHERE s.value = :value")
})
public class SystemScope {
@ -29,9 +29,15 @@ public class SystemScope {
private String value; // scope value
private String description; // human-readable description
private String icon; // class of the icon to display on the auth page
private boolean allowDynReg; // can a dynamically registered client ask for this scope?
private boolean defaultScope; // is this a default scope for newly-registered clients?
private boolean allowDynReg = false; // can a dynamically registered client ask for this scope?
private boolean defaultScope = false; // is this a default scope for newly-registered clients?
/**
* @param input
*/
public SystemScope(String value) {
this.value = value;
}
/**
* @return the id
*/

View File

@ -15,8 +15,16 @@ public interface SystemScopeService {
public Set<SystemScope> getAll();
/**
* Get all scopes that are defaulted to new clients on this system
* @return
*/
public Set<SystemScope> getDefaults();
/**
* Get all scopes that are allowed for dynamic registration on this system
* @return
*/
public Set<SystemScope> getDynReg();
public SystemScope getById(Long id);
@ -27,8 +35,18 @@ public interface SystemScopeService {
public SystemScope save(SystemScope scope);
/**
* Translate the set of scope strings into a set of SystemScope objects.
* @param scope
* @return
*/
public Set<SystemScope> fromStrings(Set<String> scope);
/**
* Pluck the scope values from the set of SystemScope objects and return a list of strings
* @param scope
* @return
*/
public Set<String> toStrings(Set<SystemScope> scope);
}

View File

@ -3,12 +3,11 @@
*/
package org.mitre.oauth2.repository.impl;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import org.mitre.oauth2.model.SystemScope;
@ -37,7 +36,7 @@ public class JpaSystemScopeRepository implements SystemScopeRepository {
public Set<SystemScope> getAll() {
TypedQuery<SystemScope> query = em.createNamedQuery("SystemScope.findAll", SystemScope.class);
return new HashSet<SystemScope>(query.getResultList());
return new LinkedHashSet<SystemScope>(query.getResultList());
}
/* (non-Javadoc)

View File

@ -3,7 +3,7 @@
*/
package org.mitre.oauth2.service.impl;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.Nullable;
@ -31,7 +31,6 @@ public class DefaultSystemScopeService implements SystemScopeService {
private SystemScopeRepository repository;
private Predicate<SystemScope> isDefault = new Predicate<SystemScope>() {
@Override
public boolean apply(@Nullable SystemScope input) {
return (input != null && input.isDefaultScope());
@ -40,7 +39,6 @@ public class DefaultSystemScopeService implements SystemScopeService {
private Predicate<SystemScope> isDynReg = new Predicate<SystemScope>() {
@Override
public boolean apply(@Nullable SystemScope input) {
return (input != null && input.isAllowDynReg());
@ -50,7 +48,18 @@ public class DefaultSystemScopeService implements SystemScopeService {
private Function<String, SystemScope> stringToSystemScope = new Function<String, SystemScope>() {
@Override
public SystemScope apply(@Nullable String input) {
return getByValue(input);
if (input == null) {
return null;
} else {
SystemScope s = getByValue(input);
if (s != null) {
// get the real scope if it's available
return s;
} else {
// make a fake one otherwise
return new SystemScope(input);
}
}
}
};
@ -130,7 +139,7 @@ public class DefaultSystemScopeService implements SystemScopeService {
if (scope == null) {
return null;
} else {
return new HashSet<SystemScope>(Collections2.filter(Collections2.transform(scope, stringToSystemScope), Predicates.notNull()));
return new LinkedHashSet<SystemScope>(Collections2.filter(Collections2.transform(scope, stringToSystemScope), Predicates.notNull()));
}
}
@ -142,7 +151,7 @@ public class DefaultSystemScopeService implements SystemScopeService {
if (scope == null) {
return null;
} else {
return new HashSet<String>(Collections2.filter(Collections2.transform(scope, systemScopeToString), Predicates.notNull()));
return new LinkedHashSet<String>(Collections2.filter(Collections2.transform(scope, systemScopeToString), Predicates.notNull()));
}
}