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

foam.CLASS({
  package: 'foam.nanos.auth',
  name: 'PermissionedPropertyDAO',

  javaImports: [
    'foam.core.FObject',
    'foam.core.PropertyInfo',
    'java.util.Iterator',
    'java.util.HashMap',
    'java.util.ArrayList',
    'java.util.List',
    'java.util.Map'
  ],

  extends: 'foam.dao.ProxyDAO',

  documentation: `A DAO decorator that prevents users from updating / reading
      properties for which they do not have the update / read permission.

      To require update / read permission on a property, set the
      readPermissionRequired or writePermissionRequired properties
      to be true, and add the corresponding permissions,
      i.e. model.ro.prop / model.rw.prop to  the groups who are granted permissions
      on the property.`,

  methods: [
    {
      name: 'put_',
      javaCode: `
  FObject oldObj = getDelegate().find(obj.getProperty("id"));
  return super.put_(x, maybeResetProperties(x, obj, oldObj));
      `,
    },

    {
      name: 'find_',
      javaCode: `
  FObject oldObj = getDelegate().find_(x, id);

  if ( oldObj != null ) {
    return maybeRemoveProperties(x, oldObj, new HashMap());
  }

  return null;
      `,
    },

    {
      name: 'select_',
      javaCode: `
      if ( x.get("auth") != null ) {
        if ( predicate != null ) predicate.authorize(x);
          // don't decorate the sink if it's a Count or GroupBy
          if ( ! ( sink instanceof foam.mlang.sink.Count || sink instanceof foam.mlang.sink.GroupBy) ) {
            foam.dao.Sink sink2 = ( sink != null ) ? new HidePropertiesSink(x, sink, this) : sink;
            super.select_(x, sink2, skip, limit, order, predicate);
          } else {
            super.select_(x, sink, skip, limit, order, predicate);
          }
        return sink;
      }
      return super.select_(x, sink, skip, limit, order, predicate);
      `,
    },

    {
      name: 'maybeResetProperties',
      type: 'foam.core.FObject',
      args: [
        {
          name: 'x',
          type: 'Context'
        },
        {
          name: 'obj',
          type: 'foam.core.FObject'
        },
        {
          name: 'oldObj',
          type: 'foam.core.FObject'
        }
      ],
      javaCode: `
  String      of   = obj.getClass().getSimpleName().toLowerCase();
  AuthService auth = (AuthService) x.get("auth");

  if ( propertyMap_.containsKey(of) ) {<commit_msg>Just skip the decoration if its a GroupBy(Count())<commit_after>/**
 * @license
 * Copyright 2019 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  package: 'foam.nanos.auth',
  name: 'PermissionedPropertyDAO',

  javaImports: [
    'foam.core.FObject',
    'foam.core.PropertyInfo',
    'java.util.Iterator',
    'java.util.HashMap',
    'java.util.ArrayList',
    'java.util.List',
    'java.util.Map'
  ],

  extends: 'foam.dao.ProxyDAO',

  documentation: `A DAO decorator that prevents users from updating / reading
      properties for which they do not have the update / read permission.

      To require update / read permission on a property, set the
      readPermissionRequired or writePermissionRequired properties
      to be true, and add the corresponding permissions,
      i.e. model.ro.prop / model.rw.prop to  the groups who are granted permissions
      on the property.`,

  methods: [
    {
      name: 'put_',
      javaCode: `
  FObject oldObj = getDelegate().find(obj.getProperty("id"));
  return super.put_(x, maybeResetProperties(x, obj, oldObj));
      `,
    },

    {
      name: 'find_',
      javaCode: `
  FObject oldObj = getDelegate().find_(x, id);

  if ( oldObj != null ) {
    return maybeRemoveProperties(x, oldObj, new HashMap());
  }

  return null;
      `,
    },

    {
      name: 'select_',
      javaCode: `
      if ( x.get("auth") != null ) {
        if ( predicate != null ) predicate.authorize(x);
        // don't decorate the sink if it's a Count or GroupBy(Count())
        if ( ! ( sink instanceof foam.mlang.sink.Count || 
            ((sink instanceof foam.mlang.sink.GroupBy) && 
            ((foam.mlang.sink.GroupBy)sink).getArg2() instanceof foam.mlang.sink.Count && 
            ((foam.mlang.sink.Count) ((foam.mlang.sink.GroupBy)sink).getArg2()).getLabel().equals("Count") )) ) {
          foam.dao.Sink sink2 = ( sink != null ) ? new HidePropertiesSink(x, sink, this) : sink;
          super.select_(x, sink2, skip, limit, order, predicate);
        } else {
          super.select_(x, sink, skip, limit, order, predicate);
        }
        return sink;
      }
      return super.select_(x, sink, skip, limit, order, predicate);
      `,
    },

    {
      name: 'maybeResetProperties',
      type: 'foam.core.FObject',
      args: [
        {
          name: 'x',
          type: 'Context'
        },
        {
          name: 'obj',
          type: 'foam.core.FObject'
        },
        {
          name: 'oldObj',
          type: 'foam.core.FObject'
        }
      ],
      javaCode: `
  String      of   = obj.getClass().getSimpleName().toLowerCase();
  AuthService auth = (AuthService) x.get("auth");

  if ( propertyMap_.containsKey(of) ) {
{'fraction_non_alphanumeric': 0.13686200378071833, 'fraction_numerical': 0.003402646502835539, 'mean_word_length': 2.228187919463087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553520', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1652, 'n_words': 404}