Repository navigation
server: add provider-admin share entities and CRUD #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,285
−27
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
server/opensharing-server-core/src/main/java/io/opensharing/Transactions.java
This file contains hidden or 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,17 @@ | ||
| package io.opensharing; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Runs store work in one database transaction. Each host supplies its own, backed by its own | ||
| * persistence setup; OpenSharing's entities must be registered with it. | ||
| */ | ||
| public interface Transactions { | ||
|
|
||
| /** | ||
| * Runs {@code work} in a new transaction and commits it, or rolls it back when {@code work} | ||
| * throws. Entities returned from {@code work} are detached once it returns. | ||
| */ | ||
| <T> T inTransaction(boolean readOnly, Function<EntityManager, T> work); | ||
| } |
16 changes: 16 additions & 0 deletions
16
server/opensharing-server-core/src/main/java/io/opensharing/auth/Ownership.java
This file contains hidden or 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,16 @@ | ||
| package io.opensharing.auth; | ||
|
|
||
| import io.opensharing.http.ApiException; | ||
|
|
||
| /** Ownership checks for objects OpenSharing stores. */ | ||
| public final class Ownership { | ||
|
|
||
| private Ownership() {} | ||
|
|
||
| /** Passes only when {@code user} has the id {@code ownerId}; a missing id never matches. */ | ||
| public static void check(UserContext user, String ownerId, String what) { | ||
| if (user.userId() == null || ownerId == null || !ownerId.equals(user.userId())) { | ||
| throw ApiException.permissionDenied("user '" + user.userName() + "' does not own " + what); | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
server/opensharing-server-core/src/main/java/io/opensharing/http/ListResponse.java
This file contains hidden or 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,20 @@ | ||
| package io.opensharing.http; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Paginated list envelope shared by list endpoints. {@code nextPageToken} is omitted on the last | ||
| * page: | ||
| * | ||
| * <pre>{@code | ||
| * {"items": [<item>, ...], "nextPageToken": "<page token>"} | ||
| * }</pre> | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record ListResponse<T>(List<T> items, String nextPageToken) { | ||
|
|
||
| public static <T> ListResponse<T> of(List<T> items) { | ||
| return new ListResponse<>(items, null); | ||
| } | ||
| } |
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
server/opensharing-server-core/src/main/java/io/opensharing/share/CreateShareRequest.java
This file contains hidden or 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,7 @@ | ||
| package io.opensharing.share; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** POST body to create a share. The authenticated caller becomes the owner. */ | ||
| public record CreateShareRequest( | ||
| String name, String displayName, String comment, Map<String, String> properties) {} |
96 changes: 96 additions & 0 deletions
96
server/opensharing-server-core/src/main/java/io/opensharing/share/ShareEntity.java
This file contains hidden or 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,96 @@ | ||
| package io.opensharing.share; | ||
|
|
||
| import io.opensharing.BaseEntity; | ||
| import io.opensharing.ObjectNames; | ||
| import jakarta.persistence.CollectionTable; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.ElementCollection; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.MapKeyColumn; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.UniqueConstraint; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** Persisted share row. {@code name} is always stored lowercase. */ | ||
| @Entity | ||
| @Table( | ||
| name = "os_shares", | ||
| uniqueConstraints = @UniqueConstraint(name = "uk_shares_name", columnNames = "name")) | ||
| public class ShareEntity extends BaseEntity { | ||
|
|
||
| @Column(nullable = false, length = 255) | ||
| private String name; | ||
|
|
||
| @Column(name = "display_name", length = 255) | ||
| private String displayName; | ||
|
|
||
| @Column(length = 8192) | ||
| private String comment; | ||
|
|
||
| /** | ||
| * The catalog's user id for whoever created this share. It is not kept in sync with the catalog, | ||
| * so it goes stale if the catalog removes the user. | ||
| */ | ||
| @Column(name = "owner_id", nullable = false, length = 255) | ||
| private String ownerId; | ||
|
|
||
| /** | ||
| * User-defined key-value properties, one row per key in {@code os_share_properties}. Loaded | ||
| * eagerly because the share is read after the store's transaction has closed. | ||
| */ | ||
| @ElementCollection(fetch = FetchType.EAGER) | ||
| @CollectionTable( | ||
| name = "os_share_properties", | ||
| joinColumns = @JoinColumn(name = "share_id"), | ||
| uniqueConstraints = | ||
| @UniqueConstraint( | ||
| name = "uk_share_properties", | ||
| columnNames = {"share_id", "property_key"})) | ||
| @MapKeyColumn(name = "property_key", length = 255) | ||
| @Column(name = "property_value", length = 1000) | ||
| private Map<String, String> properties = new LinkedHashMap<>(); | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = ObjectNames.normalize(name); | ||
| } | ||
|
|
||
| public String getDisplayName() { | ||
| return displayName; | ||
| } | ||
|
|
||
| public void setDisplayName(String displayName) { | ||
| this.displayName = displayName; | ||
| } | ||
|
|
||
| public String getComment() { | ||
| return comment; | ||
| } | ||
|
|
||
| public void setComment(String comment) { | ||
| this.comment = comment; | ||
| } | ||
|
|
||
| public Map<String, String> getProperties() { | ||
| return Collections.unmodifiableMap(properties); | ||
| } | ||
|
|
||
| public void setProperties(Map<String, String> properties) { | ||
| this.properties = properties == null ? new LinkedHashMap<>() : new LinkedHashMap<>(properties); | ||
| } | ||
|
|
||
| public String getOwnerId() { | ||
| return ownerId; | ||
| } | ||
|
|
||
| public void setOwnerId(String ownerId) { | ||
| this.ownerId = ownerId; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
server/opensharing-server-core/src/main/java/io/opensharing/share/ShareResponse.java
This file contains hidden or 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,35 @@ | ||
| package io.opensharing.share; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A share as the protocol reports it. Fields without a value are omitted: | ||
| * | ||
| * <pre>{@code | ||
| * { | ||
| * "id": "<share id>", | ||
| * "name": "<share name>", | ||
| * "displayName": "<display name>", | ||
| * "comment": "<comment>", | ||
| * "properties": {"<key>": "<value>"} | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| @JsonInclude(JsonInclude.Include.NON_NULL) | ||
| public record ShareResponse( | ||
| String id, | ||
| String name, | ||
| String displayName, | ||
| String comment, | ||
| Map<String, String> properties) { | ||
|
|
||
| public static ShareResponse from(ShareEntity share) { | ||
| return new ShareResponse( | ||
| share.getId(), | ||
| share.getName(), | ||
| share.getDisplayName(), | ||
| share.getComment(), | ||
| Map.copyOf(share.getProperties())); | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
server/opensharing-server-core/src/main/java/io/opensharing/share/ShareService.java
This file contains hidden or 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,79 @@ | ||
| package io.opensharing.share; | ||
|
|
||
| import io.opensharing.ObjectNames; | ||
| import io.opensharing.auth.AuthContext; | ||
| import io.opensharing.auth.Privilege; | ||
| import io.opensharing.auth.UserContext; | ||
| import io.opensharing.catalog.CatalogConnector; | ||
| import io.opensharing.http.ApiException; | ||
| import io.opensharing.http.ListResponse; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Provider share CRUD for a caller the host has authenticated. Creating a share needs CREATE_SHARE | ||
| * from the catalog; any caller may read shares, only the owner may change them. | ||
| */ | ||
| public class ShareService { | ||
|
|
||
| private final CatalogConnector catalog; | ||
| private final ShareStore shares; | ||
|
|
||
| public ShareService(CatalogConnector catalog, ShareStore shares) { | ||
| this.catalog = catalog; | ||
| this.shares = shares; | ||
| } | ||
|
|
||
| /** Creates a share owned by the caller, who needs CREATE_SHARE from the catalog. */ | ||
| public ShareResponse create(UserContext user, CreateShareRequest request) { | ||
| // The host has already authenticated the caller. A host that also checks CREATE_SHARE at its | ||
| // endpoint can make this a no-op. | ||
| catalog.authorize(AuthContext.of(user), Privilege.CREATE_SHARE); | ||
| return ShareResponse.from( | ||
| shares.create( | ||
| user, | ||
| ObjectNames.validateShareName(request.name()), | ||
| request.displayName(), | ||
| request.comment(), | ||
| validateProperties(request.properties()))); | ||
| } | ||
|
|
||
| /** Lists every share by name, unpaged. */ | ||
| public ListResponse<ShareResponse> list() { | ||
| // TODO: page with maxResults and pageToken. | ||
| // TODO: check USE_SHARE through catalog.authorize. | ||
| return ListResponse.of(shares.list().stream().map(ShareResponse::from).toList()); | ||
| } | ||
|
|
||
| /** Gets a share by name in any case. */ | ||
| public ShareResponse get(String share) { | ||
| return ShareResponse.from(shares.getOrThrow(share)); | ||
| } | ||
|
|
||
| /** Updates the fields set in {@code request}. Owner only. */ | ||
| public ShareResponse update(UserContext user, String share, UpdateShareRequest request) { | ||
| return ShareResponse.from( | ||
| shares.update( | ||
| user, | ||
| share, | ||
| request.displayName(), | ||
| request.comment(), | ||
| validateProperties(request.properties()))); | ||
| } | ||
|
|
||
| /** Deletes the share. Owner only. */ | ||
| public void delete(UserContext user, String share) { | ||
| shares.delete(share, user); | ||
| } | ||
|
|
||
| private static Map<String, String> validateProperties(Map<String, String> properties) { | ||
| if (properties != null) { | ||
| for (Map.Entry<String, String> property : properties.entrySet()) { | ||
| if (property.getValue() == null) { | ||
| throw ApiException.invalidParameter( | ||
| "property '" + property.getKey() + "' must not be null"); | ||
| } | ||
| } | ||
| } | ||
| return properties; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.