Skip to content
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

Prepare RepositoryMetadata role Updates for #1060 Compatibility #1061

Merged
merged 18 commits into from
Dec 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ public static ProjectRole of(JsonNode node) {
throw new IllegalArgumentException(e);
}
}

/**
* Returns {@code true} if this {@link ProjectRole} has the specified {@link ProjectRole}.
*/
public boolean has(ProjectRole other) {
requireNonNull(other, "other");
if (this == OWNER) {
return true;
}
if (this == MEMBER) {
return other != OWNER;
}
// this == GUEST
return this == other;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.centraldogma.common;

import static java.util.Objects.requireNonNull;

/**
* Roles for a repository.
*/
public enum RepositoryRole {
/**
* Able to read a file from a repository.
*/
READ,
/**
* Able to write a file to a repository.
*/
WRITE,
/**
* Able to manage a repository.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared with the current permission scheme, I understood the difference is:

  1. the ability to set ADMIN for a repository
  2. repository settings can't change project admin roles.

*/
ADMIN;

/**
* Returns {@code true} if this {@link RepositoryRole} has the specified {@link RepositoryRole}.
*/
public boolean has(RepositoryRole other) {
requireNonNull(other, "other");
if (this == ADMIN) {
return true;
}
if (this == WRITE) {
return other != ADMIN;
}
// this == READ
return this == other;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
Expand Down Expand Up @@ -185,6 +186,10 @@ public static <T> T readValue(File file, TypeReference<T> typeReference) throws
return compactMapper.readValue(file, typeReference);
}

public static <T> T readValue(JsonParser jp, TypeReference<T> typeReference) throws IOException {
return compactMapper.readValue(jp, typeReference);
}

public static JsonNode readTree(String data) throws JsonParseException {
try {
return compactMapper.readTree(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.MoreObjects;

import com.linecorp.armeria.common.util.Exceptions;
Expand All @@ -42,7 +41,6 @@
import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.ProjectRole;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.jsonpatch.JsonPatch;
import com.linecorp.centraldogma.internal.jsonpatch.JsonPatchOperation;
import com.linecorp.centraldogma.internal.jsonpatch.ReplaceOperation;
Expand All @@ -63,9 +61,6 @@
@RequiresRole(roles = ProjectRole.OWNER)
public class MetadataApiService extends AbstractService {

private static final TypeReference<Collection<Permission>> permissionsTypeRef =
new TypeReference<Collection<Permission>>() {};

private final MetadataService mds;
private final Function<String, String> loginNameNormalizer;

Expand Down Expand Up @@ -199,28 +194,6 @@ public CompletableFuture<Revision> addSpecificUserPermission(
member, memberWithPermissions.permissions());
}

/**
* PATCH /metadata/{projectName}/repos/{repoName}/perm/users/{memberId}
*
* <p>Updates {@link Permission}s for the specified {@code memberId} of the specified {@code repoName}
* in the specified {@code projectName}.
*/
@Patch("/metadata/{projectName}/repos/{repoName}/perm/users/{memberId}")
@Consumes("application/json-patch+json")
public CompletableFuture<Revision> updateSpecificUserPermission(@Param String projectName,
@Param String repoName,
@Param String memberId,
JsonPatch jsonPatch,
Author author) {
final ReplaceOperation operation = ensureSingleReplaceOperation(jsonPatch, "/permissions");
final Collection<Permission> permissions = Jackson.convertValue(operation.value(), permissionsTypeRef);
final User member = new User(loginNameNormalizer.apply(urlDecode(memberId)));
return mds.findPermissions(projectName, repoName, member)
.thenCompose(unused -> mds.updatePerUserPermission(author,
projectName, repoName, member,
permissions));
}

/**
* DELETE /metadata/{projectName}/repos/{repoName}/perm/users/{memberId}
*
Expand Down Expand Up @@ -254,26 +227,6 @@ public CompletableFuture<Revision> addSpecificTokenPermission(
tokenWithPermissions.id(), tokenWithPermissions.permissions());
}

/**
* PATCH /metadata/{projectName}/repos/{repoName}/perm/tokens/{appId}
*
* <p>Updates {@link Permission}s for the specified {@code appId} of the specified {@code repoName}
* in the specified {@code projectName}.
*/
@Patch("/metadata/{projectName}/repos/{repoName}/perm/tokens/{appId}")
@Consumes("application/json-patch+json")
public CompletableFuture<Revision> updateSpecificTokenPermission(@Param String projectName,
@Param String repoName,
@Param String appId,
JsonPatch jsonPatch,
Author author) {
final ReplaceOperation operation = ensureSingleReplaceOperation(jsonPatch, "/permissions");
final Collection<Permission> permissions = Jackson.convertValue(operation.value(), permissionsTypeRef);
return mds.findTokenByAppId(appId)
.thenCompose(token -> mds.updatePerTokenPermission(
author, projectName, repoName, appId, permissions));
}

/**
* DELETE /metadata/{projectName}/repos/{repoName}/perm/tokens/{appId}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* A default permission for a {@link Repository}.
*/
public class PerRolePermissions {
public final class PerRolePermissions {

/**
* {@link Permission}s for administrators.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.centraldogma.server.metadata;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.centraldogma.common.RepositoryRole;

/**
* Represents the roles assigned to project members and guests for a specific repository.
*/
public final class ProjectRoles {

/**
* Returns a new {@link ProjectRoles} with the specified {@link RepositoryRole}s.
*/
public static ProjectRoles of(@Nullable RepositoryRole member, @Nullable RepositoryRole guest) {
return new ProjectRoles(member, guest);
minwoox marked this conversation as resolved.
Show resolved Hide resolved
}

@Nullable
private final RepositoryRole member;

@Nullable
private final RepositoryRole guest;

/**
* Creates a new instance.
*/
@JsonCreator
public ProjectRoles(@JsonProperty("member") @Nullable RepositoryRole member,
@JsonProperty("guest") @Nullable RepositoryRole guest) {
this.member = member;
this.guest = guest;
}

/**
* Returns the role assigned to project members for this repository.
*/
@Nullable
@JsonProperty("member")
public RepositoryRole member() {
return member;
}

/**
* Returns the role assigned to project guests for this repository.
*/
@Nullable
@JsonProperty("guest")
public RepositoryRole guest() {
return guest;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).omitNullValues()
.add("member", member)
.add("guest", guest)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;

Expand All @@ -39,7 +40,8 @@
* Specifies details of a {@link Repository}.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
@JsonInclude(Include.NON_NULL) // These are used when serializing.
@JsonDeserialize(using = RepositoryMetadataDeserializer.class)
public class RepositoryMetadata implements Identifiable {

/**
Expand Down Expand Up @@ -90,16 +92,12 @@ public RepositoryMetadata(String name, UserAndTimestamp creation, PerRolePermiss
/**
* Creates a new instance.
*/
@JsonCreator
public RepositoryMetadata(@JsonProperty("name") String name,
@JsonProperty("perRolePermissions") PerRolePermissions perRolePermissions,
@JsonProperty("perUserPermissions")
Map<String, Collection<Permission>> perUserPermissions,
@JsonProperty("perTokenPermissions")
Map<String, Collection<Permission>> perTokenPermissions,
@JsonProperty("creation") UserAndTimestamp creation,
@JsonProperty("removal") @Nullable UserAndTimestamp removal,
@JsonProperty("writeQuota") @Nullable QuotaConfig writeQuota) {
RepositoryMetadata(String name, PerRolePermissions perRolePermissions,
Map<String, Collection<Permission>> perUserPermissions,
Map<String, Collection<Permission>> perTokenPermissions,
UserAndTimestamp creation,
@Nullable UserAndTimestamp removal,
@Nullable QuotaConfig writeQuota) {
this.name = requireNonNull(name, "name");
this.perRolePermissions = requireNonNull(perRolePermissions, "perRolePermissions");
this.perUserPermissions = ImmutableMap.copyOf(requireNonNull(perUserPermissions,
Expand Down Expand Up @@ -174,6 +172,30 @@ public QuotaConfig writeQuota() {
return writeQuota;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final RepositoryMetadata that = (RepositoryMetadata) o;
return name.equals(that.name) &&
perRolePermissions.equals(that.perRolePermissions) &&
perUserPermissions.equals(that.perUserPermissions) &&
perTokenPermissions.equals(that.perTokenPermissions) &&
creation.equals(that.creation) && Objects.equals(removal, that.removal) &&
Objects.equals(writeQuota, that.writeQuota);
}

@Override
public int hashCode() {
return Objects.hash(name, perRolePermissions, perUserPermissions, perTokenPermissions,
creation, removal, writeQuota);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Loading
Loading