org.springframework.security.acls.model.AlreadyExistsException Java Examples

The following examples show how to use org.springframework.security.acls.model.AlreadyExistsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccessService.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Transactional
public MutableAclRecord init(AclEntity ae, Permission initPermission) {
    MutableAclRecord acl = null;
    ObjectIdentity objectIdentity = new ObjectIdentityImpl(ae);

    try {
        // Create acl record for secured domain object.
        acl = (MutableAclRecord) aclService.createAcl(objectIdentity);
    } catch (AlreadyExistsException e) {
        acl = aclService.readAcl(objectIdentity);
    }

    if (null != initPermission) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        PrincipalSid sid = new PrincipalSid(auth);
        acl = grant(ae, initPermission, sid);
    }

    return acl;
}
 
Example #2
Source File: JpaMutableAclService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    // Check this object identity hasn't already been persisted
    if (retrieveObjectIdentityPrimaryKey(objectIdentity) != null) {
        throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
    }

    // Need to retrieve the current principal, in order to know who "owns" this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    return (MutableAcl) acl;
}
 
Example #3
Source File: JpaMutableAclService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    // Check this object identity hasn't already been persisted
    if (retrieveObjectIdentityPrimaryKey(objectIdentity) != null) {
        throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
    }

    // Need to retrieve the current principal, in order to know who "owns" this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    return (MutableAcl) acl;
}
 
Example #4
Source File: JpaMutableAclService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    // Check this object identity hasn't already been persisted
    if (retrieveObjectIdentityPrimaryKey(objectIdentity) != null) {
        throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
    }

    // Need to retrieve the current principal, in order to know who "owns" this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    return (MutableAcl) acl;
}
 
Example #5
Source File: JpaMutableAclService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    // Check this object identity hasn't already been persisted
    if (retrieveObjectIdentityPrimaryKey(objectIdentity) != null) {
        throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
    }

    // Need to retrieve the current principal, in order to know who "owns" this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    return (MutableAcl) acl;
}
 
Example #6
Source File: JpaMutableAclService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    Assert.notNull(objectIdentity, "Object Identity required");

    // Check this object identity hasn't already been persisted
    if (retrieveObjectIdentityPrimaryKey(objectIdentity) != null) {
        throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
    }

    // Need to retrieve the current principal, in order to know who "owns" this ACL (can be changed later on)
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    PrincipalSid sid = new PrincipalSid(auth);

    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);

    // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc)
    Acl acl = readAclById(objectIdentity);
    Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

    return (MutableAcl) acl;
}
 
Example #7
Source File: AccessService.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Transactional
public MutableAclRecord init(AclEntity ae, Permission initPermission) {
    MutableAclRecord acl = null;
    ObjectIdentity objectIdentity = new ObjectIdentityImpl(ae);

    try {
        // Create acl record for secured domain object.
        acl = (MutableAclRecord) aclService.createAcl(objectIdentity);
    } catch (AlreadyExistsException e) {
        acl = aclService.readAcl(objectIdentity);
    }

    if (null != initPermission) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        PrincipalSid sid = new PrincipalSid(auth);
        acl = grant(ae, initPermission, sid);
    }

    return acl;
}
 
Example #8
Source File: PackageRepositorySecurityDecoratorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@WithMockUser(
    username = USERNAME,
    roles = {ROLE_SU})
@Test
void testAddAlreadyExists() {
  Package aPackage = mock(Package.class);
  when(aPackage.getId()).thenReturn("packageId");
  when(aPackage.getIdValue()).thenReturn("packageId");
  PackageMetadata packageMetadata =
      when(mock(PackageMetadata.class).getId()).thenReturn("Package").getMock();
  when(aPackage.getEntityType()).thenReturn(packageMetadata);
  when(mutableAclService.createAcl(new PackageIdentity(aPackage)))
      .thenThrow(new AlreadyExistsException(""));

  Exception exception =
      assertThrows(EntityAlreadyExistsException.class, () -> repo.add(aPackage));
  assertThat(exception.getMessage()).containsPattern("type:Package id:packageId");
}
 
Example #9
Source File: PluginSecurityRepositoryDecoratorTest.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testAddAlreadyExists() {
  Plugin plugin = mock(Plugin.class);
  when(plugin.getId()).thenReturn("pluginId");
  when(plugin.getIdValue()).thenReturn("pluginId");
  PluginMetadata pluginMetadata =
      when(mock(PluginMetadata.class).getId()).thenReturn("Plugin").getMock();
  when(plugin.getEntityType()).thenReturn(pluginMetadata);
  when(mutableAclService.createAcl(new PluginIdentity(plugin)))
      .thenThrow(new AlreadyExistsException(""));
  Exception exception =
      assertThrows(
          EntityAlreadyExistsException.class,
          () -> pluginSecurityRepositoryDecorator.add(plugin));
  assertThat(exception.getMessage()).containsPattern("type:Plugin id:pluginId");
}
 
Example #10
Source File: TransactionalJdbcMutableAclService.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Same as {@link JdbcMutableAclService#createAcl(ObjectIdentity)} except for duplicate key
 * checking which is handled by by the database for performance reasons.
 */
@Transactional
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) {
  Assert.notNull(objectIdentity, "Object Identity required");

  // Need to retrieve the current principal, in order to know who "owns" this ACL
  // (can be changed later on)
  Sid sid = SidUtils.createSecurityContextSid();

  try {
    // Create the acl_object_identity row
    createObjectIdentity(objectIdentity, sid);
  } catch (DuplicateKeyException e) {
    throw new AlreadyExistsException("Object identity '" + objectIdentity + "' already exists");
  }
  // Retrieve the ACL via superclass (ensures cache registration, proper retrieval
  // etc)
  Acl acl = readAclById(objectIdentity);
  Assert.isInstanceOf(MutableAcl.class, acl, "MutableAcl should be been returned");

  return (MutableAcl) acl;
}
 
Example #11
Source File: PermissionServiceImpl.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
@Transactional
public void addType(String typeId) {
  entityHelper.checkEntityTypeExists(typeId);
  entityHelper.checkIsNotSystem(typeId);
  EntityType entityType = dataService.getEntityType(entityHelper.getEntityTypeIdFromType(typeId));
  if (mutableAclClassService.getAclClassTypes().contains(typeId)) {
    throw new AclClassAlreadyExistsException(typeId);
  }
  mutableAclClassService.createAclClass(typeId, EntityIdentityUtils.toIdType(entityType));
  // Create ACL's for existing rows
  dataService
      .findAll(entityType.getId())
      .forEach(
          entity -> {
            try {
              mutableAclService.createAcl(new EntityIdentity(entity));
            } catch (AlreadyExistsException e) {
              throw new AclAlreadyExistsException(typeId, entityType.getId());
            }
          });
}
 
Example #12
Source File: PackageRepositorySecurityDecorator.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void createAcl(Package pack) {
  PackageIdentity packageIdentity = new PackageIdentity(pack);
  MutableAcl acl;
  try {
    acl = mutableAclService.createAcl(packageIdentity);
  } catch (AlreadyExistsException e) {
    throw new EntityAlreadyExistsException(pack, e);
  }
  if (pack.getParent() != null) {
    ObjectIdentity parentIdentity = new PackageIdentity(pack.getParent());
    Acl parentAcl = mutableAclService.readAclById(parentIdentity);
    acl.setParent(parentAcl);
    mutableAclService.updateAcl(acl);
  }
}
 
Example #13
Source File: RowLevelSecurityRepositoryDecoratorTest.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@WithMockUser(username = USERNAME)
@Test
void testAddAlreadyExists() {
  Entity entity = getEntityMock();
  when(mutableAclService.createAcl(new EntityIdentity(entity)))
      .thenThrow(new AlreadyExistsException(""));

  Exception exception =
      assertThrows(
          EntityAlreadyExistsException.class,
          () -> rowLevelSecurityRepositoryDecorator.add(entity));
  assertThat(exception.getMessage()).containsPattern("type:entityTypeId id:entityId");
}
 
Example #14
Source File: AclService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    try (AutoLock l = lock.lockForWrite()) {
        AclRecord aclRecord = getAclRecordByCache(objID(objectIdentity));
        if (aclRecord != null) {
            throw new AlreadyExistsException("ACL of " + objectIdentity + " exists!");
        }
        AclRecord record = newPrjACL(objectIdentity);
        crud.save(record);
        logger.debug("ACL of " + objectIdentity + " created successfully.");
    } catch (IOException e) {
        throw new InternalErrorException(e);
    }
    return (MutableAcl) readAclById(objectIdentity);
}
 
Example #15
Source File: RowLevelSecurityRepositoryDecorator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void createAcl(Entity entity) {
  MutableAcl acl;
  try {
    acl = mutableAclService.createAcl(new EntityIdentity(entity));
  } catch (AlreadyExistsException e) {
    throw new EntityAlreadyExistsException(entity, e);
  }
  Sid sid = SidUtils.createSecurityContextSid();
  acl.insertAce(acl.getEntries().size(), PermissionSet.WRITE, sid, true);
  mutableAclService.updateAcl(acl);
}
 
Example #16
Source File: EntityTypeRepositorySecurityDecorator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void createAcl(EntityType entityType) {
  MutableAcl acl;
  try {
    acl = mutableAclService.createAcl(new EntityTypeIdentity(entityType.getId()));
  } catch (AlreadyExistsException e) {
    throw new EntityAlreadyExistsException(entityType, e);
  }
  Package pack = entityType.getPackage();
  if (pack != null) {
    ObjectIdentity objectIdentity = new PackageIdentity(pack);
    acl.setParent(mutableAclService.readAclById(objectIdentity));
    mutableAclService.updateAcl(acl);
  }
}
 
Example #17
Source File: PluginSecurityRepositoryDecorator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createAcl(Plugin plugin) {
  try {
    mutableAclService.createAcl(new PluginIdentity(plugin.getId()));
  } catch (AlreadyExistsException e) {
    throw new EntityAlreadyExistsException(plugin, e);
  }
}
 
Example #18
Source File: AclService.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public MutableAcl createAcl(ObjectIdentity objectIdentity) throws AlreadyExistsException {
    try (AutoLock l = lock.lockForWrite()) {
        AclRecord aclRecord = getAclRecordByCache(objID(objectIdentity));
        if (aclRecord != null) {
            throw new AlreadyExistsException("ACL of " + objectIdentity + " exists!");
        }
        AclRecord record = newPrjACL(objectIdentity);
        crud.save(record);
        logger.debug("ACL of " + objectIdentity + " created successfully.");
    } catch (IOException e) {
        throw new InternalErrorException(e);
    }
    return (MutableAcl) readAclById(objectIdentity);
}