All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.EasimartDefaultACLController Maven / Gradle / Ivy

package co.easimart;

import java.lang.ref.WeakReference;

/** package */ class EasimartDefaultACLController {

  /* package for tests */ EasimartACL defaultACL;
  /* package for tests */ boolean defaultACLUsesCurrentUser;
  /* package for tests */ WeakReference lastCurrentUser;
  /* package for tests */ EasimartACL defaultACLWithCurrentUser;

  /**
   * Sets a default ACL that will be applied to all {@link EasimartObject}s when they are created.
   *
   * @param acl
   *          The ACL to use as a template for all {@link EasimartObject}s created after set
   *          has been called. This value will be copied and used as a template for the creation of
   *          new ACLs, so changes to the instance after {@code set(EasimartACL, boolean)}
   *          has been called will not be reflected in new {@link EasimartObject}s.
   * @param withAccessForCurrentUser
   *          If {@code true}, the {@code EasimartACL} that is applied to newly-created
   *          {@link EasimartObject}s will provide read and write access to the
   *          {@link EasimartUser#getCurrentUser()} at the time of creation. If {@code false}, the
   *          provided ACL will be used without modification. If acl is {@code null}, this value is
   *          ignored.
   */
  public void set(EasimartACL acl, boolean withAccessForCurrentUser) {
    defaultACLWithCurrentUser = null;
    lastCurrentUser = null;
    if (acl != null) {
      EasimartACL newDefaultACL = acl.copy();
      newDefaultACL.setShared(true);
      defaultACL = newDefaultACL;
      defaultACLUsesCurrentUser = withAccessForCurrentUser;
    } else {
      defaultACL = null;
    }
  }

  public EasimartACL get() {
    if (defaultACLUsesCurrentUser && defaultACL != null) {
      EasimartUser currentUser = EasimartUser.getCurrentUser();
      if (currentUser != null) {
        // If the currentUser has changed, generate a new ACL from the defaultACL.
        EasimartUser last = lastCurrentUser != null ? lastCurrentUser.get() : null;
        if (last != currentUser) {
          EasimartACL newDefaultACLWithCurrentUser = defaultACL.copy();
          newDefaultACLWithCurrentUser.setShared(true);
          newDefaultACLWithCurrentUser.setReadAccess(currentUser, true);
          newDefaultACLWithCurrentUser.setWriteAccess(currentUser, true);
          defaultACLWithCurrentUser = newDefaultACLWithCurrentUser;
          lastCurrentUser = new WeakReference<>(currentUser);
        }
        return defaultACLWithCurrentUser;
      }
    }
    return defaultACL;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy