Search in sources :

Example 1 with AlreadyExistsException

use of org.springframework.security.acls.model.AlreadyExistsException in project spring-security by spring-projects.

the class JdbcMutableAclService method createAcl.

// ~ Methods
// ========================================================================================================
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;
}
Also used : AlreadyExistsException(org.springframework.security.acls.model.AlreadyExistsException) Authentication(org.springframework.security.core.Authentication) MutableAcl(org.springframework.security.acls.model.MutableAcl) Acl(org.springframework.security.acls.model.Acl) MutableAcl(org.springframework.security.acls.model.MutableAcl) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid)

Example 2 with AlreadyExistsException

use of org.springframework.security.acls.model.AlreadyExistsException in project spring-security by spring-projects.

the class JdbcMutableAclServiceTests method createAclForADuplicateDomainObject.

@Test
@Transactional
public void createAclForADuplicateDomainObject() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(auth);
    ObjectIdentity duplicateOid = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(100));
    jdbcMutableAclService.createAcl(duplicateOid);
    // Try to add the same object second time
    try {
        jdbcMutableAclService.createAcl(duplicateOid);
        fail("It should have thrown AlreadyExistsException");
    } catch (AlreadyExistsException expected) {
    }
}
Also used : ObjectIdentity(org.springframework.security.acls.model.ObjectIdentity) AlreadyExistsException(org.springframework.security.acls.model.AlreadyExistsException) ObjectIdentityImpl(org.springframework.security.acls.domain.ObjectIdentityImpl) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AlreadyExistsException (org.springframework.security.acls.model.AlreadyExistsException)2 Test (org.junit.Test)1 ObjectIdentityImpl (org.springframework.security.acls.domain.ObjectIdentityImpl)1 PrincipalSid (org.springframework.security.acls.domain.PrincipalSid)1 Acl (org.springframework.security.acls.model.Acl)1 MutableAcl (org.springframework.security.acls.model.MutableAcl)1 ObjectIdentity (org.springframework.security.acls.model.ObjectIdentity)1 Authentication (org.springframework.security.core.Authentication)1 Transactional (org.springframework.transaction.annotation.Transactional)1