admin-edit-user.component.js 1.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
'use strict'

export default {
  name: 'admin-edit-user',
  props: ['usrdata'],
  data() {
    return {
      id: '',
      email: '',
      password: '********',
      name: '',
      rights: [],
      roleoverride: 'none'
    }
  },
  methods: {
    addRightsRow() {
      this.rights.push({
        role: 'write',
        path: '/',
        exact: false,
        deny: false
      })
    },
    removeRightsRow(idx) {
      this._.pullAt(this.rights, idx)
      this.$forceUpdate()
    },
    saveUser() {
      let self = this
      let formattedRights = this._.cloneDeep(this.rights)
      switch (this.roleoverride) {
        case 'admin':
          formattedRights.push({
            role: 'admin',
            path: '/',
            exact: false,
            deny: false
          })
          break
      }
      this.$http.post(window.location.href, {
        password: this.password,
        name: this.name,
        rights: JSON.stringify(formattedRights)
      }).then(resp => {
        self.$store.dispatch('alert', {
          style: 'green',
          icon: 'check',
          msg: 'Changes have been applied successfully.'
        })
      }).catch(err => {
        self.$store.dispatch('alert', {
          style: 'red',
          icon: 'square-cross',
          msg: 'Error: ' + err.body.msg
        })
      })
    }
  },
  mounted() {
    let usr = JSON.parse(this.usrdata)
    this.id = usr._id
    this.email = usr.email
    this.name = usr.name

    if (this._.find(usr.rights, { role: 'admin' })) {
      this.rights = this._.reject(usr.rights, ['role', 'admin'])
      this.roleoverride = 'admin'
    } else {
      this.rights = usr.rights
    }
  }
}