<commit_before>import checkAuthorization from 'xo-acl-resolver'

import {
  ModelAlreadyExists
} from '../collection'
import {
  Acls
} from '../models/acl'
import {
  createRawObject,
  forEach,
  includes,
  mapToArray
} from '../utils'

// ===================================================================

export default class {
  constructor (xo) {
    this._xo = xo

    const aclsDb = this._acls = new Acls({
      connection: xo._redis,
      prefix: 'xo:acl',
      indexes: ['subject', 'object']
    })

    xo.on('start', () => {
      xo.addConfigManager('acls',
        () => this.getAllAcls(),
        acls => Promise.all(mapToArray(acls, acl =>
          this.addAcl(acl.subjectId, acl.objectId, acl.action)
        ))
      )
    })

    xo.on('clean', async () => {
      const acls = await aclsDb.get()
      const toRemove = []
      forEach(acls, ({ subject, object, action, id }) => {
        if (!subject || !object || !action) {
          toRemove.push(id)
        }
      })
      await aclsDb.remove(toRemove)
    })
  }

  async _getAclsForUser (userId) {
    const user = await this._xo.getUser(userId)
    const { groups } = user

    const subjects = groups
      ? groups.concat(userId)
      : [ userId ]

    const acls = []
    const pushAcls = (push => entries => {
      push.apply(acls, entries)
    })(acls.push)
<commit_msg>fix(xo.importConfig): correctly import ACLs<commit_after>import checkAuthorization from 'xo-acl-resolver'

import {
  ModelAlreadyExists
} from '../collection'
import {
  Acls
} from '../models/acl'
import {
  createRawObject,
  forEach,
  includes,
  mapToArray
} from '../utils'

// ===================================================================

export default class {
  constructor (xo) {
    this._xo = xo

    const aclsDb = this._acls = new Acls({
      connection: xo._redis,
      prefix: 'xo:acl',
      indexes: ['subject', 'object']
    })

    xo.on('start', () => {
      xo.addConfigManager('acls',
        () => aclsDb.get(),
        acls => aclsDb.update(acls)
      )
    })

    xo.on('clean', async () => {
      const acls = await aclsDb.get()
      const toRemove = []
      forEach(acls, ({ subject, object, action, id }) => {
        if (!subject || !object || !action) {
          toRemove.push(id)
        }
      })
      await aclsDb.remove(toRemove)
    })
  }

  async _getAclsForUser (userId) {
    const user = await this._xo.getUser(userId)
    const { groups } = user

    const subjects = groups
      ? groups.concat(userId)
      : [ userId ]

    const acls = []
    const pushAcls = (push => entries => {
      push.apply(acls, entries)
    })(acls.push)

{'fraction_non_alphanumeric': 0.19414194517461508, 'fraction_numerical': 0.0, 'mean_word_length': 2.4374193548387098, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1511481', 'n_tokens_mistral': 886, 'n_tokens_neox': 868, 'n_words': 181}