Skip to content

Commit

Permalink
Added the user setup privilege to the APIs.
Browse files Browse the repository at this point in the history
The user/read, user/search, and user_info/read APIs will now all return whether or not a user is allowed to setup other users.

Closes #568
  • Loading branch information
John Jenkins committed May 31, 2013
1 parent 854321c commit 2f29052
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 41 deletions.
12 changes: 11 additions & 1 deletion src/org/ohmage/domain/UserInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public static enum UserPermissionColumnKey implements ColumnKey {
ENABLED ("enabled"),
NEW_ACCOUNT ("new_account"),
CAN_CREATE_CAMPAIGNS ("can_create_campaigns"),
CAN_CREATE_CLASSES ("can_create_classes");
CAN_CREATE_CLASSES ("can_create_classes"),
CAN_SETUP_USERS ("can_setup_users");

private final String key;

Expand Down Expand Up @@ -315,6 +316,7 @@ public JSONObject toJsonObject() throws JSONException {
private final boolean isNewAccount;
private final boolean campaignCreationPrivilege;
private final boolean classCreationPrivilege;
private final boolean userSetupPrivilege;

private Map<String, Set<Campaign.Role>> campaigns;
private Map<String, Clazz.Role> classes;
Expand Down Expand Up @@ -343,6 +345,9 @@ public JSONObject toJsonObject() throws JSONException {
* @param classCreationPrivilege
* Whether or not the user is allowed to create new classes.
*
* @param userSetupPrivilege
* Whether or not the user can setup other users.
*
* @param campaigns
* The map of campaign IDs to a set of the user's roles in those
* campaigns.
Expand All @@ -365,6 +370,7 @@ public UserInformation(
final boolean isNewAccount,
final boolean campaignCreationPrivilege,
final boolean classCreationPrivilege,
final boolean userSetupPrivilege,
final Map<String, Set<Campaign.Role>> campaigns,
final Map<String, Clazz.Role> classes,
final UserPersonal personalInfo)
Expand All @@ -382,6 +388,7 @@ public UserInformation(
this.isNewAccount = isNewAccount;
this.campaignCreationPrivilege = campaignCreationPrivilege;
this.classCreationPrivilege = classCreationPrivilege;
this.userSetupPrivilege = userSetupPrivilege;

if(campaigns == null) {
this.campaigns = null;
Expand Down Expand Up @@ -584,6 +591,9 @@ public JSONObject toJson(
permissionsJson.put(
UserPermissionColumnKey.CAN_CREATE_CLASSES.toString(),
classCreationPrivilege);
permissionsJson.put(
UserPermissionColumnKey.CAN_SETUP_USERS.toString(),
userSetupPrivilege);
result.put(UserColumnKey.PERMISSIONS.toString(), permissionsJson);

if(campaigns != null) {
Expand Down
68 changes: 49 additions & 19 deletions src/org/ohmage/domain/UserSummary.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class UserSummary {
"can_create_campaigns";
private static final String JSON_KEY_PERMISSIONS_CLASS_CREATION =
"can_create_classes";
private static final String JSON_KEY_PERMISSIONS_USER_SETUP =
"can_setup_users";

private static final String JSON_KEY_CAMPAIGNS = "campaigns";
private static final String JSON_KEY_CAMPAIGN_ROLES = "campaign_roles";
Expand All @@ -57,6 +59,7 @@ public class UserSummary {
private final boolean isAdmin;
private final boolean campaignCreationPrivilege;
private final boolean classCreationPrivilege;
private final boolean userSetupPrivilege;

private final Map<String, String> campaigns;
private final Set<Campaign.Role> campaignRoles;
Expand All @@ -66,41 +69,50 @@ public class UserSummary {

/**
* Creates a new user information object with a default campaign creation
* privilege, no campaigns or campaign roles, and no classes or class
* privilege, no campaigns or campaign roles, and no classes or class
* roles.
*
* @param emailAddress The user's email address if one exists, which it may
* not.
* @param emailAddress
* The user's email address if one exists, which it may not.
*
* @param isAdmin Whether or not the user is an admin.
* @param isAdmin
* Whether or not the user is an admin.
*
* @param campaignCreationPrivilege Whether or not the user is allowed to
* create new campaigns.
* @param campaignCreationPrivilege
* Whether or not the user is allowed to create new campaigns.
*
* @param campaignCreationPrivilege Whether or not the user is allowed to
* create new classes.
* @param classCreationPrivilege
* Whether or not the user is allowed to create new classes.
*
* @param campaigns The map of all campaigns to which the user is
* associated where the key is the campaign's ID and its
* value is the campaign's name.
* @param userSetupPrivilege
* Whether or not the user is allowed to setup users.
*
* @param campaignRoles A set of all of the campaign roles for the user
* across all of their campaigns.
* @param campaigns
* The map of all campaigns to which the user is associated where
* the key is the campaign's ID and its value is the campaign's
* name.
*
* @param classes The map of all classes to which the user is associated
* where the key is the class' ID and its value is the
* class' name.
* @param campaignRoles
* A set of all of the campaign roles for the user across all of
* their campaigns.
*
* @param classRoles A set of all of the class roles for the user across
* all of their classes.
* @param classes
* The map of all classes to which the user is associated where the
* key is the class' ID and its value is the class' name.
*
* @throws DomainException The campaigns, classes, or role lists is empty.
* @param classRoles
* A set of all of the class roles for the user across all of their
* classes.
*
* @throws DomainException
* The campaigns, classes, or role lists is empty.
*/
public UserSummary(
String emailAddress,
boolean isAdmin,
boolean campaignCreationPrivilege,
boolean classCreationPrivilege,
boolean userSetupPrivilege,
final Map<String, String> campaigns,
final Set<Campaign.Role> campaignRoles,
final Map<String, String> classes,
Expand Down Expand Up @@ -129,6 +141,7 @@ public UserSummary(
this.isAdmin = isAdmin;
this.campaignCreationPrivilege = campaignCreationPrivilege;
this.classCreationPrivilege = classCreationPrivilege;
this.userSetupPrivilege = userSetupPrivilege;

this.campaigns = new HashMap<String, String>(campaigns);
this.campaignRoles = new HashSet<Campaign.Role>(campaignRoles);
Expand Down Expand Up @@ -186,6 +199,13 @@ public UserSummary(final JSONObject information) throws DomainException {
throw new DomainException("The class creation permission is missing from the list of permissions.", e);
}

try {
userSetupPrivilege = permissions.getBoolean(JSON_KEY_PERMISSIONS_USER_SETUP);
}
catch(JSONException e) {
throw new DomainException("The class creation permission is missing from the list of permissions.", e);
}

try {
JSONObject campaignsJson = information.getJSONObject(JSON_KEY_CAMPAIGNS);

Expand Down Expand Up @@ -280,6 +300,15 @@ public boolean getClassCreationPrivilege() {
return classCreationPrivilege;
}

/**
* Returns whether or not the user is allowed to setup users.
*
* @return Whether or not the user is allowed to setup users.
*/
public boolean getUserSetupPrivilege() {
return userSetupPrivilege;
}

/**
* Returns an unmodifiable map of all of the campaigns associated with the
* user.
Expand Down Expand Up @@ -336,6 +365,7 @@ public JSONObject toJsonObject() throws JSONException {
permissions.put(JSON_KEY_PERMISSIONS_ADMIN, isAdmin);
permissions.put(JSON_KEY_PERMISSIONS_CAMPAIGN_CREATION, campaignCreationPrivilege);
permissions.put(JSON_KEY_PERMISSIONS_CLASS_CREATION, classCreationPrivilege);
permissions.put(JSON_KEY_PERMISSIONS_USER_SETUP, userSetupPrivilege);
result.put(JSON_KEY_PERMISSIONS, permissions);

result.put(JSON_KEY_CAMPAIGNS, campaigns);
Expand Down
5 changes: 5 additions & 0 deletions src/org/ohmage/query/impl/UserQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ public QueryResultsList<UserInformation> getUserInformation(
"u.new_account, " +
"u.campaign_creation_privilege, " +
"u.class_creation_privilege, " +
"u.user_setup_privilege, " +
"up.first_name, " +
"up.last_name, " +
"up.organization, " +
Expand Down Expand Up @@ -1698,6 +1699,9 @@ private UserInformation mapRow(
boolean canCreateClasses =
rs.getBoolean(
"class_creation_privilege");
boolean canSetupUsers =
rs.getBoolean(
"user_setup_privilege");

String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
Expand Down Expand Up @@ -1737,6 +1741,7 @@ private UserInformation mapRow(
newAccount,
canCreateCampaigns,
canCreateClasses,
canSetupUsers,
null,
null,
personalInfo);
Expand Down
65 changes: 44 additions & 21 deletions src/org/ohmage/service/UserServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -974,42 +974,64 @@ public long getUserInformation(

// Compile the set of usernames based on those that must be equal and
// those that may have wildcards.
Set<String> usernameCompilation = new HashSet<String>();
for(String username : usernames) {
usernameCompilation.add(username);
}
for(String username : usernameTokens) {
usernameCompilation.add('%' + username + '%');
Set<String> usernameCompilation = null;
if((usernames != null) || (usernameTokens != null)) {
usernameCompilation = new HashSet<String>();
if(usernames != null) {
for(String username : usernames) {
usernameCompilation.add(username);
}
}
if(usernameTokens != null) {
for(String username : usernameTokens) {
usernameCompilation.add('%' + username + '%');
}
}
}

// Compile the set of email addresses.
Set<String> emailAddressCompilation = new HashSet<String>();
for(String emailAddress : emailAddressTokens) {
emailAddressCompilation.add('%' + emailAddress + '%');
Set<String> emailAddressCompilation = null;
if(emailAddressTokens != null) {
emailAddressCompilation = new HashSet<String>();
for(String emailAddress : emailAddressTokens) {
emailAddressCompilation.add('%' + emailAddress + '%');
}
}

// Compile the set of first names.
Set<String> firstNameCompilation = new HashSet<String>();
for(String firstName : firstNameTokens) {
firstNameCompilation.add('%' + firstName + '%');
Set<String> firstNameCompilation = null;
if(firstNameTokens != null) {
firstNameCompilation = new HashSet<String>();
for(String firstName : firstNameTokens) {
firstNameCompilation.add('%' + firstName + '%');
}
}

// Compile the set of last names.
Set<String> lastNameCompilation = new HashSet<String>();
for(String lastName : lastNameTokens) {
lastNameCompilation.add('%' + lastName + '%');
Set<String> lastNameCompilation = null;
if(lastNameTokens != null) {
lastNameCompilation = new HashSet<String>();
for(String lastName : lastNameTokens) {
lastNameCompilation.add('%' + lastName + '%');
}
}

// Compile the set of organizations.
Set<String> organizationCompilation = new HashSet<String>();
for(String organization : organizationTokens) {
organizationCompilation.add('%' + organization + '%');
Set<String> organizationCompilation = null;
if(organizationTokens != null) {
organizationCompilation = new HashSet<String>();
for(String organization : organizationTokens) {
organizationCompilation.add('%' + organization + '%');
}
}

// Compile the set of personal IDs.
Set<String> personalIdCompilation = new HashSet<String>();
for(String personalId : personalIdTokens) {
personalIdCompilation.add('%' + personalId + '%');
Set<String> personalIdCompilation = null;
if(personalIdTokens != null) {
personalIdCompilation = new HashSet<String>();
for(String personalId : personalIdTokens) {
personalIdCompilation.add('%' + personalId + '%');
}
}

try {
Expand Down Expand Up @@ -1333,6 +1355,7 @@ public UserSummary getUserSummary(final String username)
userQueries.userIsAdmin(username),
userQueries.userCanCreateCampaigns(username),
userQueries.userCanCreateClasses(username),
userQueries.userCanSetupUsers(username),
campaigns,
campaignRoles,
classes,
Expand Down

0 comments on commit 2f29052

Please sign in to comment.