/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.auth;

import foam.core.FObject;
import foam.core.X;
import foam.nanos.auth.AuthorizationException;

public class AuthorizableAuthorizer implements Authorizer {

  protected String permissionPrefix_;

  public AuthorizableAuthorizer(String permissionPrefix) {  
    permissionPrefix_ = permissionPrefix;
  }

  public String getPermissionPrefix() {
    return permissionPrefix_;
  }

  public void authorizeOnCreate(X x, FObject obj) throws AuthorizationException {
    if ( obj instanceof Authorizable ) {
      ((Authorizable) obj).authorizeOnCreate(x);
    }
  }

  public void authorizeOnRead(X x, FObject obj) throws AuthorizationException {
    if ( obj instanceof Authorizable ) {
      ((Authorizable) obj).authorizeOnRead(x);
    }
  }

  public void authorizeOnUpdate(X x, FObject oldObj, FObject obj) throws AuthorizationException {
    if ( obj instanceof Authorizable ) {
      ((Authorizable) obj).authorizeOnUpdate(x, oldObj);
    }
  }

  public void authorizeOnDelete(X x, FObject obj) throws AuthorizationException {
    if ( obj instanceof Authorizable ) {
      ((Authorizable) obj).authorizeOnDelete(x);
    }
  }

  public String createPermission(String op) {
    return permissionPrefix_ + "." + op;
  }

  public boolean checkGlobalRead(X x) {
    String permission = createPermission("read");
    AuthService authService = (AuthService) x.get("auth");
    try {
      return authService.check(x, permission);
    } catch ( AuthorizationException e ) {
      return false;
    }
  }

  public boolean checkGlobalRemove(X x) {
    String permission = createPermission("remove");
    AuthService authService = (AuthService) x.get("auth");
    try {
      return authService.check(x, permission);
    } catch ( AuthorizationException e ) {
      return false;
    }
  }
}

@@ -1360,24 +1360,138 @@
  + op;%0A  %7D%0A%0A
+  public String createPermission(String op, Object id) %7B%0A    return permissionPrefix_ + %22.%22 + op + %22.%22 + id;%0A  %7D%0A%0A
   public boo
@@ -1565,16 +1565,21 @@
 n(%22read%22
+, %22*%22
 );%0A    A
@@ -1854,16 +1854,21 @@
 %22remove%22
+, %22*%22
 );%0A    A

Update global checks on AuthorizableAuthorizer