Skip to content

Commit

Permalink
Refactor share permission (#1154)
Browse files Browse the repository at this point in the history
* Refactored Sorting

* Refactored Pagination

* Refactored Pagination

* Pagination refactor builds and tests pass

* Codacy fixes

* Refactored SharePermission to NonTransferable

* Fixed build

* Fixed startup bug

* Fixed codacy and inspection comments

* Update elide-core/src/main/java/com/yahoo/elide/core/EntityDictionary.java

Co-Authored-By: Jon Kilroy <[email protected]>

* Inspection rework

Co-authored-by: Jon Kilroy <[email protected]>
  • Loading branch information
2 people authored and Aaron Klish committed Apr 23, 2020
1 parent 65600f6 commit 4095dfe
Show file tree
Hide file tree
Showing 58 changed files with 215 additions and 378 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.annotation;

import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Marks that the given entity cannot be added to another collection after creation of the entity.
*/
@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
@Inherited
public @interface NonTransferable {

/**
* If NonTransferable is used at the package level, it can be disabled for individual entities by setting
* this flag to false.
* @return true if enabled.
*/
boolean enabled() default true;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import com.yahoo.elide.annotation.Exclude;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.MappedInterface;
import com.yahoo.elide.annotation.NonTransferable;
import com.yahoo.elide.annotation.SecurityCheck;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.core.exceptions.HttpStatusException;
import com.yahoo.elide.core.exceptions.InternalServerErrorException;
import com.yahoo.elide.core.exceptions.InvalidAttributeException;
Expand Down Expand Up @@ -814,9 +814,10 @@ public <T> void initializeEntity(T entity) {
* @param entityClass the entity type to check for the shareable permissions
* @return true if entityClass is shareable. False otherwise.
*/
public boolean isShareable(Class<?> entityClass) {
return getAnnotation(entityClass, SharePermission.class) != null
&& getAnnotation(entityClass, SharePermission.class).sharable();
public boolean isTransferable(Class<?> entityClass) {
NonTransferable nonTransferable = getAnnotation(entityClass, NonTransferable.class);

return (nonTransferable == null || !nonTransferable.enabled());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.NonTransferable;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import com.yahoo.elide.generated.parsers.ExpressionLexer;
import com.yahoo.elide.generated.parsers.ExpressionParser;
Expand Down Expand Up @@ -43,7 +43,7 @@ public class EntityPermissions implements CheckInstantiator {
ReadPermission.class,
CreatePermission.class,
DeletePermission.class,
SharePermission.class,
NonTransferable.class,
UpdatePermission.class
);

Expand Down Expand Up @@ -79,7 +79,7 @@ public EntityPermissions(EntityDictionary dictionary,
final Map<String, ParseTree> fieldPermissions = new HashMap<>();
fieldOrMethodList.stream()
.forEach(member -> bindMemberPermissions(fieldPermissions, member, annotationClass));
if (annotationClass != SharePermission.class) {
if (annotationClass != NonTransferable.class) {
ParseTree classPermission = bindClassPermissions(cls, annotationClass);
if (classPermission != null || !fieldPermissions.isEmpty()) {
bindings.put(annotationClass, new AnnotationBinding(classPermission, fieldPermissions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.yahoo.elide.annotation.Audit;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.NonTransferable;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import com.yahoo.elide.audit.InvalidSyntaxException;
import com.yahoo.elide.audit.LogMessage;
Expand Down Expand Up @@ -139,7 +139,7 @@ public static <T> PersistentResource<T> createObject(
//hashcode and equals are only based on the ID/UUID & type.
assignId(newResource, id);

// Keep track of new resources for non shareable resources
// Keep track of new resources for non-transferable resources
requestScope.getNewPersistentResources().add(newResource);
checkPermission(CreatePermission.class, newResource);

Expand Down Expand Up @@ -477,7 +477,7 @@ protected boolean updateToManyRelation(String fieldName,

added = Sets.difference(updated, deleted);

checkSharePermission(added);
checkTransferablePermission(added);

Collection collection = (Collection) this.getValueUnchecked(fieldName);

Expand Down Expand Up @@ -539,11 +539,11 @@ protected boolean updateToOneRelation(String fieldName,
if (newValue == null) {
return false;
}
checkSharePermission(resourceIdentifiers);
checkTransferablePermission(resourceIdentifiers);
} else if (oldResource.getObject().equals(newValue)) {
return false;
} else {
checkSharePermission(resourceIdentifiers);
checkTransferablePermission(resourceIdentifiers);
if (hasInverseRelation(fieldName)) {
deleteInverseRelation(fieldName, oldResource.getObject());
oldResource.markDirty();
Expand Down Expand Up @@ -713,7 +713,7 @@ public void addRelation(String fieldName, PersistentResource newRelation) {
if (!newRelation.isNewlyCreated() && relationshipAlreadyExists(fieldName, newRelation)) {
return;
}
checkSharePermission(Collections.singleton(newRelation));
checkTransferablePermission(Collections.singleton(newRelation));
Object relation = this.getValueUnchecked(fieldName);

if (relation instanceof Collection) {
Expand All @@ -737,7 +737,7 @@ public void addRelation(String fieldName, PersistentResource newRelation) {
*
* @param resourceIdentifiers The persistent resources that are being added
*/
protected void checkSharePermission(Set<PersistentResource> resourceIdentifiers) {
protected void checkTransferablePermission(Set<PersistentResource> resourceIdentifiers) {
if (resourceIdentifiers == null) {
return;
}
Expand All @@ -747,7 +747,7 @@ protected void checkSharePermission(Set<PersistentResource> resourceIdentifiers)
for (PersistentResource persistentResource : resourceIdentifiers) {
if (!newResources.contains(persistentResource)
&& !lineage.getRecord(persistentResource.getType()).contains(persistentResource)) {
checkPermission(SharePermission.class, persistentResource);
checkPermission(NonTransferable.class, persistentResource);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.yahoo.elide.request;

import com.yahoo.elide.core.filter.expression.FilterExpression;

import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, Yahoo Inc.
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.NonTransferable;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.RequestScope;
Expand Down Expand Up @@ -98,7 +98,7 @@ public <A extends Annotation> ExpressionResult checkPermission(
}

/**
* Check permission on class. Checking on SharePermission falls to check ReadPermission.
* Check permission on class. Checking on Transferable falls to check ReadPermission.
*
* @param annotationClass annotation class
* @param resource resource
Expand All @@ -114,8 +114,8 @@ public <A extends Annotation> ExpressionResult checkPermission(Class<A> annotati
PersistentResource resource,
ChangeSpec changeSpec) {
Supplier<Expression> expressionSupplier = () -> {
if (SharePermission.class == annotationClass) {
if (requestScope.getDictionary().isShareable(resource.getResourceClass())) {
if (NonTransferable.class == annotationClass) {
if (requestScope.getDictionary().isTransferable(resource.getResourceClass())) {
return expressionBuilder.buildAnyFieldExpressions(resource, ReadPermission.class, changeSpec);
}
return PermissionExpressionBuilder.FAIL_EXPRESSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.NonTransferable;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import com.yahoo.elide.security.ChangeSpec;
import com.yahoo.elide.security.PersistentResource;
Expand Down Expand Up @@ -37,7 +37,7 @@ public class PermissionCondition {
.put(UpdatePermission.class, "UPDATE")
.put(DeletePermission.class, "DELETE")
.put(CreatePermission.class, "CREATE")
.put(SharePermission.class, "SHARE")
.put(NonTransferable.class, "NO TRANSFER")
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,12 @@ public void testGetIdAnnotationsSubClass() throws Exception {

@Test
public void testIsSharableTrue() throws Exception {
assertTrue(isShareable(Right.class));
assertTrue(isTransferable(Right.class));
}

@Test
public void testIsSharableFalse() throws Exception {
assertFalse(isShareable(Left.class));
assertFalse(isTransferable(Left.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
import example.Publisher;
import example.Right;
import example.UpdateAndCreate;
import example.packageshareable.ContainerWithPackageShare;
import example.packageshareable.ShareableWithPackageShare;
import example.packageshareable.UnshareableWithEntityUnshare;
import example.nontransferable.ContainerWithPackageShare;
import example.nontransferable.ShareableWithPackageShare;
import example.nontransferable.Untransferable;
import nocreate.NoCreateEntity;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import nocreate.NoCreateEntity;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -97,7 +97,7 @@ protected static EntityDictionary initDictionary() {
dictionary.bindEntity(ComputedBean.class);
dictionary.bindEntity(ContainerWithPackageShare.class);
dictionary.bindEntity(ShareableWithPackageShare.class);
dictionary.bindEntity(UnshareableWithEntityUnshare.class);
dictionary.bindEntity(Untransferable.class);
return dictionary;
}

Expand Down
Loading

0 comments on commit 4095dfe

Please sign in to comment.