<commit_before>import { alias, or } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import NewOrEdit from 'ui/mixins/new-or-edit';
import C from 'ui/utils/constants';
import layout from './template';
import { get, computed, set } from '@ember/object';

const ruleVerbs = C.RULE_VERBS.map(verb => `rolesPage.new.form.allow.${verb}`);

const BASIC_CONTEXT = [
  {
    label: 'All',
    value: '',
  },
  {
    label: 'Project',
    value: 'project',
  },
  {
    label: 'Cluster',
    value: 'cluster',
  },
];

export default Component.extend(NewOrEdit, {
  layout,
  intl: service(),
  router: service(),
  model: null,

  primaryResource: alias('model.role'),
  ruleArray: null,
  roleArray: null,
  readOnly: null,
  contexts: BASIC_CONTEXT,
  ruleVerbs,

  init: function () {
    this._super(...arguments);

    const ruleArray = [];
    const model = get(this, 'primaryResource');


    (get(model, 'rules') || []).filter(r => (r.resources || r.nonResourceURLs)).forEach((rule) => {
      ruleArray.push(rule);
    });

    const roleArray = (get(model, 'roleTemplateIds') || []).map((id) => {
      return { roleId: id };
    });


    this.setProperties({
      ruleArray,
      roleArray,
    });
  },

  actions: {
    cancel() {
      this.goBack();
    },

    addRule() {
      get(this, 'ruleArray').pushObject({
        apiGroups: ['*'],
        type: 'policyRule',
        resource: null,
        resources: [],
        verbs: [],
      });
    },

    removeRule(obj) {
      get(this, 'ruleArray').removeObject(obj);
    },

    addOtherRole() {
      get(this, 'roleArray').pushObject({
        roleId: null,
      });
    },

    removeOtherRole(obj) {
      get(this, 'roleArray').removeObject(obj);
    },

  },

  goBack() {
    get(this, 'router').transitionTo('global-admin.security.roles.index');
  },

  builtIn: computed('model.role.builtin', function() {
    return get(this, 'model.role.builtin') === true;
  }),

  readOnlyOrBuiltIn: or('readOnly','builtIn'),

  otherRoles: computed('model.role.id', 'model.roles.<EMAIL>', function () {
    return get(this, 'model.roles').filter(role => get(this, 'model.role.id') !== role.id);
  }),

  validate() {
    this._super();
    var errors = get(this, 'errors') || [];

    if ((get(this, 'primaryResource.name') || '').trim().length === 0) {
      errors.push(get(this, 'intl').findTranslationByKey('rolesPage.new.errors.nameReq'));
    }

    set(this, 'errors', errors);
    return get(this, 'errors.length') === 0;
  },

  willSave() {
    const role = get(this, 'model.role');
    const actualRules = [];

    // Create the actual rules
    const rules = get(this, 'ruleArray');
    let obj;
    for (let i = rules.length - 1; i >= 0; i--) {
      obj = rules[i];
      if (obj.resources.length && obj.verbs.length) {
        actualRules.push({
          type: obj.type,
          apiGroups: obj.apiGroups.slice(),
          verbs: obj.verbs.slice(),
          resources: obj.resources.slice(),
        });
      } else {
        rules.removeObject(obj);
      }
    }<commit_msg>Fix update cluster owner role issue<commit_after>import { alias, or } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import NewOrEdit from 'ui/mixins/new-or-edit';
import C from 'ui/utils/constants';
import layout from './template';
import { get, computed, set } from '@ember/object';

const ruleVerbs = C.RULE_VERBS.map(verb => `rolesPage.new.form.allow.${verb}`);

const BASIC_CONTEXT = [
  {
    label: 'All',
    value: '',
  },
  {
    label: 'Project',
    value: 'project',
  },
  {
    label: 'Cluster',
    value: 'cluster',
  },
];

export default Component.extend(NewOrEdit, {
  layout,
  intl: service(),
  router: service(),
  model: null,

  primaryResource: alias('model.role'),
  ruleArray: null,
  roleArray: null,
  readOnly: null,
  contexts: BASIC_CONTEXT,
  ruleVerbs,

  init: function () {
    this._super(...arguments);

    const ruleArray = [];
    const model = get(this, 'primaryResource');


    (get(model, 'rules') || []).filter(r => (r.resources || r.nonResourceURLs)).forEach((rule) => {
      ruleArray.push(rule);
    });

    const roleArray = (get(model, 'roleTemplateIds') || []).map((id) => {
      return { roleId: id };
    });


    this.setProperties({
      ruleArray,
      roleArray,
    });
  },

  actions: {
    cancel() {
      this.goBack();
    },

    addRule() {
      get(this, 'ruleArray').pushObject({
        apiGroups: ['*'],
        type: 'policyRule',
        resource: null,
        resources: [],
        verbs: [],
      });
    },

    removeRule(obj) {
      get(this, 'ruleArray').removeObject(obj);
    },

    addOtherRole() {
      get(this, 'roleArray').pushObject({
        roleId: null,
      });
    },

    removeOtherRole(obj) {
      get(this, 'roleArray').removeObject(obj);
    },

  },

  goBack() {
    get(this, 'router').transitionTo('global-admin.security.roles.index');
  },

  builtIn: computed('model.role.builtin', function() {
    return get(this, 'model.role.builtin') === true;
  }),

  readOnlyOrBuiltIn: or('readOnly','builtIn'),

  otherRoles: computed('model.role.id', '<EMAIL>', function () {
    return get(this, 'model.roles').filter(role => get(this, 'model.role.id') !== role.id);
  }),

  validate() {
    this._super();
    var errors = get(this, 'errors') || [];

    if ((get(this, 'primaryResource.name') || '').trim().length === 0) {
      errors.push(get(this, 'intl').findTranslationByKey('rolesPage.new.errors.nameReq'));
    }

    set(this, 'errors', errors);
    return get(this, 'errors.length') === 0;
  },

  willSave() {
    const role = get(this, 'model.role');
    const actualRules = [];

    // Create the actual rules
    const rules = get(this, 'ruleArray');
    let obj;
    for (let i = rules.length - 1; i >= 0; i--) {
      obj = rules[i];
      if (obj.resources && obj.resources.length && obj.verbs.length) {
        actualRules.push({
          type: obj.type,
          apiGroups: obj.apiGroups.slice(),
          verbs: obj.verbs.slice(),
          resources: obj.resources.slice(),
        });
      } else {
        rules.removeObject(obj);
      }
    }
{'fraction_non_alphanumeric': 0.18514992860542598, 'fraction_numerical': 0.0012692368713311123, 'mean_word_length': 2.7368109069353883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '3225765', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2059, 'n_words': 414}