-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10906 from IQSS/6467-optimize-permission-lookups-…
…for-a-user Optimize permission lookups for a user
- Loading branch information
Showing
10 changed files
with
341 additions
and
38 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
doc/release-notes/6467-optimize-permission-lookups-for-a-user.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The following API have been added: | ||
|
||
/api/users/{identifier}/allowedCollections/{permission} | ||
|
||
This API lists the dataverses/collections that the user has access to via the permission passed. | ||
By passing "any" as the permission the list will return all dataverse/collections that the user can access regardless of which permission is used. | ||
This API can be executed only by the User requesting their own list of accessible collections or by an Administrator. | ||
Valid Permissions are: AddDataverse, AddDataset, ViewUnpublishedDataverse, ViewUnpublishedDataset, DownloadFile, EditDataverse, EditDataset, ManageDataversePermissions, | ||
ManageDatasetPermissions, ManageFilePermissions, PublishDataverse, PublishDataset, DeleteDataverse, DeleteDatasetDraft, and "any" as a wildcard option. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...java/edu/harvard/iq/dataverse/engine/command/impl/GetUserPermittedCollectionsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package edu.harvard.iq.dataverse.engine.command.impl; | ||
|
||
import edu.harvard.iq.dataverse.Dataverse; | ||
import edu.harvard.iq.dataverse.DvObject; | ||
import edu.harvard.iq.dataverse.authorization.Permission; | ||
import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress; | ||
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | ||
import edu.harvard.iq.dataverse.engine.command.AbstractCommand; | ||
import edu.harvard.iq.dataverse.engine.command.CommandContext; | ||
import edu.harvard.iq.dataverse.engine.command.DataverseRequest; | ||
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions; | ||
import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonArrayBuilder; | ||
import jakarta.json.JsonObjectBuilder; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import static edu.harvard.iq.dataverse.util.json.JsonPrinter.json; | ||
|
||
@RequiredPermissions({}) | ||
public class GetUserPermittedCollectionsCommand extends AbstractCommand<JsonObjectBuilder> { | ||
private static final Logger logger = Logger.getLogger(GetUserPermittedCollectionsCommand.class.getCanonicalName()); | ||
|
||
private DataverseRequest request; | ||
private AuthenticatedUser user; | ||
private String permission; | ||
public GetUserPermittedCollectionsCommand(DataverseRequest request, AuthenticatedUser user, String permission) { | ||
super(request, (DvObject) null); | ||
this.request = request; | ||
this.user = user; | ||
this.permission = permission; | ||
} | ||
|
||
@Override | ||
public JsonObjectBuilder execute(CommandContext ctxt) throws CommandException { | ||
if (user == null) { | ||
throw new CommandException("User not found.", this); | ||
} | ||
int permissionBit; | ||
try { | ||
permissionBit = permission.equalsIgnoreCase("any") ? | ||
Integer.MAX_VALUE : (1 << Permission.valueOf(permission).ordinal()); | ||
} catch (IllegalArgumentException e) { | ||
throw new CommandException("Permission not valid.", this); | ||
} | ||
List<Dataverse> collections = ctxt.permissions().findPermittedCollections(request, user, permissionBit); | ||
if (collections != null) { | ||
JsonObjectBuilder job = Json.createObjectBuilder(); | ||
JsonArrayBuilder jab = Json.createArrayBuilder(); | ||
for (Dataverse dv : collections) { | ||
jab.add(json(dv)); | ||
} | ||
job.add("count", collections.size()); | ||
job.add("items", jab); | ||
return job; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.