public void addAuthsToBucket(String policyId, String bucketId, QuotaRLBucket bucket) throws IllegalArgumentException {
Policy policy = getPolicyById(policyId);
// Find the correct bucket in this policy
QuotaRLBucket pAuthIds = getBucketWithIdForPolicy(policy, bucketId);
if(pAuthIds == null) {
throw new IllegalArgumentException("A Bucket with that ID [" + bucketId + "] doesn't exist for this Policy [" + policyId + "]");
}
checkIfAuthAlreadyInPolicy(policyId, bucket.getAuthIds(), bucketId);
for(String authId : bucket.getAuthIds()) {
String authToken = cachingTableAuthIdToAuthToken.get(authId);
if (authToken == null)
throw new IllegalArgumentException("Auth ID [" + authId + "] not found");
Auth auth = cachingTableAuth.get(authToken);
if (auth == null)
throw new IllegalArgumentException("Auth token not found");
// Add auth to the bucket
if (pAuthIds.getAuthIds().indexOf(auth.getId()) < 0)
{
pAuthIds.getAuthIds().add(auth.getId());
cachingTablePolicy.set(policy.getId(), policy);
}
// Add policy to auth
boolean found = false;
for (AuthIds authCtx : auth.getPolicyContexts()) {
if (authCtx.getPolicyId().equals(policy.getId())) {
//update Auth with good Policy context and bucket
authCtx.setPolicyContextId(getPolicyContextId(auth, policy));
authCtx.setPolicyBucketId(bucket.getBucketId());
cachingTableAuth.set(authToken, auth);//updating auth
found = true;
break;
}
}
if (!found) {
auth.getPolicyContexts().add(new AuthIds(policyId, bucketId, getPolicyContextId(auth, policy), pAuthIds.getBucketId(), auth.getStatus().isActive()));
cachingTableAuth.set(authToken, auth);
}
}
}