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

foam.CLASS({
  package: 'foam.nanos.auth',
  name: 'SystemAuthService',
  extends: 'foam.nanos.auth.ProxyAuthService',
  methods: [
    {
      name: 'check',
      javaCode: `
        foam.nanos.auth.User user = (foam.nanos.auth.User) x.get("user");
        return ( user != null && user.getId() == 1 ) || getDelegate().check(x, permission);
      `
    }
  ]
});

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

foam.CLASS({
  package: 'foam.nanos.auth',
  name: 'SystemAuthService',
  extends: 'foam.nanos.auth.ProxyAuthService',
  methods: [
    {
      name: 'check',
      javaCode: `
        foam.nanos.auth.User user = (foam.nanos.auth.User) x.get("user");
        return user != null && ( user.getId() == 1 || user.getGroup().equals("admin") || user.getGroup().equals("system") ) || getDelegate().check(x, permission);
      `
    }
  ]
});

Speed up authorization checks for superadmins
Speed up authorization checks for superadmins

Results in an 88.24% shorter mean duration and 93.75% shorter median
duration of calls to AuthService.check for users that meet the
conditions.

This AuthService decorator was working already, but there are other
users that could also be benefiting from the speed increase.
