<commit_before>import { inject as service } from '@ember/service';
import Component from '@ember/component';
import Errors from 'ui/utils/errors';
import layout from './template';
import { get, set, observer, computed, setProperties } from '@ember/object';
import { on } from '@ember/object/evented';

export default Component.extend({
  layout,
  tagName:     'section',
  classNames:  ['well'],
  settings:    service(),
  access:      service(),
  github:      service(),
  intl:        service(),

  model:       null,
  individuals: 'siteAccess.users',
  collection:  'siteAccess.groups',
  principals:  null,

  saved:       true,
  errors:      null,

  actions: {
    addAuthorized(principal) {
      if ( !principal ) {
        return;
      }

      this.send('clearError');

      set(this, 'saved', false);

      if (this.checkDuplicate(principal)) {
        this.send('gotError', get(this, 'intl').t('siteAccess.dupe'))
      } else {
        get(this, 'model.allowedPrincipalIds').pushObject(principal.id);
      }
    },

    removeAuthorized(id) {
      set(this, 'saved', false);

      get(this, 'model.allowedPrincipalIds').removeObject(id);
    },

    save(btnCb) {
      this.send('clearError');

      if ( get(this, 'model.accessMode') === 'required' && !get(this, 'model.allowedPrincipalIds.length') ) {
        this.send('gotError', get(this, 'intl').t('siteAccess.minimum'));

        btnCb();

        return;
      }

      set(this, 'saved', false);

      const model = get(this, 'model');

      model.save().then(() => {
        set(this, 'saved', true);
      }).catch((err) => {
        this.send('gotError', err);
      }).finally(() => {
        btnCb();
      });
    },

    gotError(err) {
      set(this, 'errors', [Errors.stringify(err)]);
    },

    clearError() {
      set(this, 'errors', null);

      setProperties(this, {
        errors:    null,
      });
    },
  },

  showList: computed('model.accessMode', function() {
    return get(this, 'model.accessMode') !== 'unrestricted';
  }),

  accessModeChanged: on('init', observer('model.accessMode', function() {
    set(this, 'saved',false);

    const allowedPrincipals = get(this, 'model.allowedPrincipalIds') || []; // ['princ_id1://yada']

    if ( get(this, 'model.accessMode') !== 'unrestricted' ) {

      const me    = get(this, 'access.principal')||[];
      const found = allowedPrincipals.filter(ident => get(me, 'id') === ident).length > 0;

      if ( !found ) {
        allowedPrincipals.pushObject(get(me, 'id'));
      }
    }

    set(this, 'model.allowedPrincipalIds', allowedPrincipals);
  })),
<commit_msg>Fix duplicate `me` users from being added to site access<commit_after>import { inject as service } from '@ember/service';
import Component from '@ember/component';
import Errors from 'ui/utils/errors';
import layout from './template';
import { get, set, observer, computed, setProperties } from '@ember/object';
import { on } from '@ember/object/evented';

export default Component.extend({
  layout,
  tagName:     'section',
  classNames:  ['well'],
  settings:    service(),
  access:      service(),
  github:      service(),
  intl:        service(),

  model:       null,
  individuals: 'siteAccess.users',
  collection:  'siteAccess.groups',
  principals:  null,

  saved:       true,
  errors:      null,

  actions: {
    addAuthorized(principal) {
      if ( !principal ) {
        return;
      }

      this.send('clearError');

      set(this, 'saved', false);

      if (this.checkDuplicate(principal)) {
        this.send('gotError', get(this, 'intl').t('siteAccess.dupe'))
      } else {
        get(this, 'model.allowedPrincipalIds').pushObject(principal.id);
      }
    },

    removeAuthorized(id) {
      set(this, 'saved', false);

      get(this, 'model.allowedPrincipalIds').removeObject(id);
    },

    save(btnCb) {
      this.send('clearError');

      if ( get(this, 'model.accessMode') === 'required' && !get(this, 'model.allowedPrincipalIds.length') ) {
        this.send('gotError', get(this, 'intl').t('siteAccess.minimum'));

        btnCb();

        return;
      }

      set(this, 'saved', false);

      const model = get(this, 'model');

      model.save().then(() => {
        set(this, 'saved', true);
      }).catch((err) => {
        this.send('gotError', err);
      }).finally(() => {
        btnCb();
      });
    },

    gotError(err) {
      set(this, 'errors', [Errors.stringify(err)]);
    },

    clearError() {
      set(this, 'errors', null);

      setProperties(this, {
        errors:    null,
      });
    },
  },

  showList: computed('model.accessMode', function() {
    return get(this, 'model.accessMode') !== 'unrestricted';
  }),

  accessModeChanged: on('init', observer('model.accessMode', function() {
    set(this, 'saved',false);

    const allowedPrincipals = get(this, 'model.allowedPrincipalIds') || []; // ['princ_id1://yada']

    if ( get(this, 'model.accessMode') !== 'unrestricted' ) {

      let found    = false;

      const myPIds = get(this, 'access.me.principalIds');

      myPIds.forEach( id => {
        if (allowedPrincipals.indexOf(id) >= 0) {
          found = true;
        }
      });

      if ( !found ) {
        allowedPrincipals.pushObject(get(this, 'access.me.id'));
      }
    }

    set(this, 'model.allowedPrincipalIds', allowedPrincipals);
  })),

{'fraction_non_alphanumeric': 0.1759895930124512, 'fraction_numerical': 0.0007433562534844825, 'mean_word_length': 2.701513067400275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '6813626', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1674, 'n_words': 334}