/**
 * @license
 * Copyright 2019 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.AuthService;
import foam.nanos.auth.AuthorizationException;
import foam.mlang.predicate.Predicate;

public class StandardAuthorizer implements Authorizer {

  // Standard authorizer to be used for authorization on object not implementing the authorizable interface
  // Performs authorization by checking permission generated from permissionPrefix passed in

  protected String permissionPrefix_ = "";

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

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

  public String createPermission(String op, Object id) {
    try {
      return permissionPrefix_ + "." + op + "." + id;
    } catch (NullPointerException t) {
      System.err.println(this.getClass().getSimpleName()+","+t.getMessage()+","+"id"+","+id.getClass().getSimpleName()+","+id.toString());
      return permissionPrefix_ + "." + op + "." + id.toString();
      //      throw t;
    }
  }

  public void authorizeOnCreate(X x, FObject obj) throws AuthorizationException {

    String permission = createPermission("create");
    AuthService authService = (AuthService) x.get("auth");
    if ( ! authService.check(x, permission) ) {
      throw new AuthorizationException();
    }
  }

  public void authorizeOnRead(X x, FObject obj) throws AuthorizationException {

    String permission = createPermission("read", obj.getProperty("id"));
    AuthService authService = (AuthService) x.get("auth");

    if ( ! authService.check(x, permission) ) {
      throw new AuthorizationException();
    }
  }

  public void authorizeOnUpdate(X x, FObject oldObj, FObject obj) throws AuthorizationException {
    String permission = createPermission("update", obj.getProperty("id"));
    AuthService authService = (AuthService) x.get("auth");

    if ( ! authService.check(x, permission) ) {
      throw new AuthorizationException();
    }
  }

  public void authorizeOnDelete(X x, FObject obj) throws AuthorizationException {

    String permission  = createPermission("remove", obj.getProperty("id"));
    AuthService authService = (AuthService) x.get("auth");

    if ( ! authService.check(x, permission) ) {
      throw new AuthorizationException();
    }
  }

  public boolean checkGlobalRead(X x, Predicate predicate) {
    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;
    }

  }
}

@@ -611,15 +611,10 @@
 fix_
- = %22%22
 ;%0A
+
 %0A  p
@@ -786,27 +786,40 @@
 nPrefix_
- + %22.%22 + 
+.contact(%22.%22).concat(
 op
+)
 ;%0A  %7D%0A%0A 
@@ -874,28 +874,16 @@
 t id) %7B%0A
-    try %7B%0A  
     retu
@@ -906,303 +906,66 @@
 fix_
- + %22.%22 + op + %22.%22 + id;%0A    %7D catch (NullPointerException t) %7B%0A      System.err.println(this.getClass().getSimpleName()+%22,%22+t.getMessage()+%22,%22+%22id%22+%22,%22+id.getClass().getSimpleName()+%22,%22+id.toString());%0A      return permissionPrefix_ + %22.%22 + op + %22.%22 + id.toString();%0A      //      throw t;%0A    %7D
+.concat(%22.%22).concat(op).concat(%22.%22).concat(id.toString());
 %0A  %7D
@@ -1674,16 +1674,17 @@
 ption %7B%0A
+%0A
     Stri

Revert to development version. Issues has gone away!