ApplicationForm.vue 5.72 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
<template>

  <form class="ui form" @submit.prevent="submit()">
    <div v-if="errors.length > 0" class="ui negative message">
      <div class="header"><translate translate-context="Content/*/Error message.Title">We cannot save your changes</translate></div>
      <ul class="list">
        <li v-for="error in errors">{{ error }}</li>
      </ul>
    </div>
    <div class="ui field">
      <label><translate translate-context="Content/Applications/Input.Label/Noun">Name</translate></label>
      <input name="name" required type="text" v-model="fields.name" />
    </div>
    <div class="ui field">
      <label><translate translate-context="Content/Applications/Input.Label/Noun">Redirect URI</translate></label>
      <input name="redirect_uris" type="text" v-model="fields.redirect_uris" />
      <p class="help">
        <translate translate-context="Content/Applications/Help Text">
          Use "urn:ietf:wg:oauth:2.0:oob" as a redirect URI if your application is not served on the web.
        </translate>
      </p>
    </div>
    <div class="ui field">
      <label><translate translate-context="Content/Applications/Input.Label/Noun">Scopes</translate></label>
      <p>
        <translate translate-context="Content/Applications/Paragraph/">
          Checking the parent "Read" or "Write" scopes implies access to all the corresponding children scopes.
        </translate>
      </p>
      <div class="ui stackable two column grid">
        <div v-for="parent in allScopes" class="column">
          <div class="ui parent checkbox">
            <input
              v-model="scopeArray"
              :value="parent.id"
              :id="parent.id"
              type="checkbox">
            <label :for="parent.id">
              {{ parent.label }}
              <p class="help">
                {{ parent.description }}
              </p>
            </label>
          </div>

          <div v-for="child in parent.children">
            <div class="ui child checkbox">
              <input
                v-model="scopeArray"
                :value="child.id"
                :id="child.id"
                type="checkbox">
              <label :for="child.id">
                {{ child.id }}
                <p class="help">
                  {{ child.description }}
                </p>
              </label>
            </div>
          </div>
        </div>
      </div>

      </div>
    <button :class="['ui', {'loading': isLoading}, 'green', 'button']" type="submit">
      <translate v-if="updating" key="2" translate-context="Content/Applications/Button.Label/Verb">Update application</translate>
      <translate v-else key="2" translate-context="Content/Applications/Button.Label/Verb">Create application</translate>
    </button>
  </form>
</template>

<script>
73
import _ from "@/lodash"
74
75
76
77
78
79
import axios from "axios"
import TranslationsMixin from "@/components/mixins/Translations"

export default {
  mixins: [TranslationsMixin],
  props: {
80
81
    app: {type: Object, required: false},
    defaults: {type: Object, required: false}
82
83
84
  },
  data() {
    let app = this.app || {}
85
    let defaults = this.defaults || {}
86
87
88
89
    return {
      isLoading: false,
      errors: [],
      fields: {
90
91
92
        name: app.name || defaults.name || '',
        redirect_uris: app.redirect_uris || defaults.redirect_uris || 'urn:ietf:wg:oauth:2.0:oob',
        scopes: app.scopes || defaults.scopes || 'read'
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
      },
      scopes: [
        {id: "profile", icon: 'user'},
        {id: "libraries", icon: 'book'},
        {id: "favorites", icon: 'heart'},
        {id: "listenings", icon: 'music'},
        {id: "follows", icon: 'users'},
        {id: "playlists", icon: 'list'},
        {id: "radios", icon: 'rss'},
        {id: "filters", icon: 'eye slash'},
        {id: "notifications", icon: 'bell'},
        {id: "edits", icon: 'pencil alternate'},
      ]
    }
  },
  methods: {
    submit () {
      this.errors = []
      let self = this
      self.isLoading = true
      let payload = this.fields
      let event, promise, message
      if (this.updating) {
        event = 'updated'
        promise = axios.patch(`oauth/apps/${this.app.client_id}/`, payload)
      }  else {
        event = 'created'
        promise = axios.post(`oauth/apps/`, payload)
      }
      return promise.then(
        response => {
          self.isLoading = false
          self.$emit(event, response.data)
        },
        error => {
          self.isLoading = false
          self.errors = error.backendErrors
        }
      )
    },
  },
  computed: {
    updating () {
      return this.app
    },
    scopeArray: {
      get () {
        return this.fields.scopes.split(' ')
      },
      set (v) {
        this.fields.scopes = _.uniq(v).join(' ')
      }
    },
    allScopes () {
      let self = this
      let parents = [
        {
          id: 'read',
          label: this.$pgettext('Content/OAuth Scopes/Label/Verb', 'Read'),
          description: this.$pgettext('Content/OAuth Scopes/Help Text', 'Read-only access to user data'),
          value: this.scopeArray.indexOf('read') > -1
        },
        {
          id: 'write',
          label: this.$pgettext('Content/OAuth Scopes/Label/Verb', 'Write'),
          description: this.$pgettext('Content/OAuth Scopes/Help Text', 'Write-only access to user data'),
          value: this.scopeArray.indexOf('write') > -1
        },
      ]
      parents.forEach((p) => {
        p.children = self.scopes.map(s => {
          let id = `${p.id}:${s.id}`
          return {
            id,
            value: this.scopeArray.indexOf(id) > -1,
          }
        })
      })
      return parents
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.parent.checkbox {
  margin: 1em 0;
}
.child.checkbox {
  margin-left: 1em;
}
</style>