admin-auth.vue 16 KB
Newer Older
1
<template lang='pug'>
2 3 4 5
  v-container(fluid, grid-list-lg)
    v-layout(row, wrap)
      v-flex(xs12)
        .admin-header
6
          img.animated.fadeInUp(src='/_assets/svg/icon-unlock.svg', alt='Authentication', style='width: 80px;')
7
          .admin-header-title
8
            .headline.primary--text.animated.fadeInLeft {{ $t('admin:auth.title') }}
9
            .subtitle-1.grey--text.animated.fadeInLeft.wait-p4s {{ $t('admin:auth.subtitle') }}
10
          v-spacer
NGPixel's avatar
NGPixel committed
11 12 13
          v-btn.animated.fadeInDown.wait-p3s(icon, outlined, color='grey', href='https://docs.requarks.io/auth', target='_blank')
            v-icon mdi-help-circle
          v-btn.animated.fadeInDown.wait-p2s.mx-3(icon, outlined, color='grey', @click='refresh')
14
            v-icon mdi-refresh
Nick's avatar
Nick committed
15
          v-btn.animated.fadeInDown(color='success', @click='save', depressed, large)
16
            v-icon(left) mdi-check
17
            span {{$t('common:actions.apply')}}
18

19 20
      v-flex(lg3, xs12)
        v-card.animated.fadeInUp
21 22
          v-toolbar(flat, color='teal', dark, dense)
            .subtitle-1 {{$t('admin:auth.activeStrategies')}}
23
          v-list(two-line, dense).py-0
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
            draggable(
              v-model='activeStrategies'
              handle='.is-handle'
              direction='vertical'
              )
              transition-group
                v-list-item(
                  v-for='(str, idx) in activeStrategies'
                  :key='str.key'
                  @click='selectedStrategy = str.key'
                  :class='selectedStrategy === str.key ? ($vuetify.theme.dark ? `grey darken-5` : `teal lighten-5`) : ``'
                  )
                  v-list-item-avatar.is-handle(size='24')
                    v-icon(:color='selectedStrategy === str.key ? `teal` : `grey`') mdi-drag-horizontal
                  v-list-item-content
                    v-list-item-title.body-2(:class='selectedStrategy === str.key ? `teal--text` : ``') {{ str.displayName }}
                    v-list-item-subtitle: .caption(:class='selectedStrategy === str.key ? `teal--text ` : ``') {{ str.strategy.title }}
                  v-list-item-avatar(v-if='selectedStrategy === str.key', size='24')
                    v-icon.animated.fadeInLeft(color='teal', large) mdi-chevron-right
          v-card-chin
            v-menu(offset-y, bottom, min-width='250px', max-width='550px', max-height='50vh', style='flex: 1 1;', center)
              template(v-slot:activator='{ on }')
                v-btn(v-on='on', color='primary', depressed, block)
                  v-icon(left) mdi-plus
                  span {{$t('admin:auth.addStrategy')}}
              v-list(dense)
                template(v-for='(str, idx) of strategies')
                  v-list-item(
                    :key='str.key'
                    :disabled='str.isDisabled'
                    @click='addStrategy(str)'
                    )
                    v-list-item-avatar(height='24', width='48', tile)
                      v-img(:src='str.logo', width='48px', height='24px', contain, :style='str.isDisabled ? `opacity: .25;` : ``')
                    v-list-item-content
                      v-list-item-title {{str.title}}
                      v-list-item-subtitle: .caption(:style='str.isDisabled ? `opacity: .4;` : ``') {{str.description}}
                  v-divider(v-if='idx < strategies.length - 1')
62

63
      v-flex(xs12, lg9)
64
        v-card.animated.fadeInUp.wait-p2s
65
          v-toolbar(color='primary', dense, flat, dark)
66
            .subtitle-1 {{strategy.displayName}} #[em ({{strategy.strategy.title}})]
67
            v-spacer
68 69 70 71 72 73 74 75
            v-btn(small, outlined, dark, color='white', :disabled='strategy.key === `local`', @click='deleteStrategy()')
              v-icon(left) mdi-close
              span {{$t('common:actions.delete')}}
          v-card-info(color='blue')
            div
              span {{strategy.strategy.description}}
              .caption: a(:href='strategy.strategy.website') {{strategy.strategy.website}}
            v-spacer
76
            .admin-providerlogo
77
              img(:src='strategy.strategy.logo', :alt='strategy.strategy.title')
78
          v-card-text
79 80 81 82 83 84 85 86 87 88 89 90 91 92 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
            .row
              .col-8
                v-text-field(
                  outlined
                  :label='$t(`admin:auth.displayName`)'
                  v-model='strategy.displayName'
                  prepend-icon='mdi-format-title'
                  :hint='$t(`admin:auth.displayNameHint`)'
                  persistent-hint
                  )
              .col-4
                v-switch.mt-1(
                  :label='$t(`admin:auth.strategyIsEnabled`)'
                  v-model='strategy.isEnabled'
                  color='primary'
                  prepend-icon='mdi-power'
                  :hint='$t(`admin:auth.strategyIsEnabledHint`)'
                  persistent-hint
                  inset
                  :disabled='strategy.key === `local`'
                  )
            template(v-if='strategy.config && Object.keys(strategy.config).length > 0')
              v-divider
              .overline.my-5 {{$t('admin:auth.strategyConfiguration')}}
              .pr-3
                template(v-for='cfg in strategy.config')
                  v-select.mb-3(
                    v-if='cfg.value.type === "string" && cfg.value.enum'
                    outlined
                    :items='cfg.value.enum'
                    :key='cfg.key'
                    :label='cfg.value.title'
                    v-model='cfg.value.value'
                    prepend-icon='mdi-cog-box'
                    :hint='cfg.value.hint ? cfg.value.hint : ""'
                    persistent-hint
                    :class='cfg.value.hint ? "mb-2" : ""'
                    :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
                  )
                  v-switch.mb-6(
                    v-else-if='cfg.value.type === "boolean"'
                    :key='cfg.key'
                    :label='cfg.value.title'
                    v-model='cfg.value.value'
                    color='primary'
                    prepend-icon='mdi-cog-box'
                    :hint='cfg.value.hint ? cfg.value.hint : ""'
                    persistent-hint
                    inset
                    )
                  v-textarea.mb-3(
                    v-else-if='cfg.value.type === "string" && cfg.value.multiline'
                    outlined
                    :key='cfg.key'
                    :label='cfg.value.title'
                    v-model='cfg.value.value'
                    prepend-icon='mdi-cog-box'
                    :hint='cfg.value.hint ? cfg.value.hint : ""'
                    persistent-hint
                    :class='cfg.value.hint ? "mb-2" : ""'
                    )
                  v-text-field.mb-3(
                    v-else
                    outlined
                    :key='cfg.key'
                    :label='cfg.value.title'
                    v-model='cfg.value.value'
                    prepend-icon='mdi-cog-box'
                    :hint='cfg.value.hint ? cfg.value.hint : ""'
                    persistent-hint
                    :class='cfg.value.hint ? "mb-2" : ""'
                    :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
                    )
            v-divider
153 154 155 156 157 158 159 160 161 162
            .overline.my-5 {{$t('admin:auth.registration')}}
            .pr-3
              v-switch.ml-3(
                v-model='strategy.selfRegistration'
                :label='$t(`admin:auth.selfRegistration`)'
                color='primary'
                :hint='$t(`admin:auth.selfRegistrationHint`)'
                persistent-hint
                inset
              )
163
              v-combobox.ml-3.mt-5(
164 165 166 167 168 169 170 171 172 173 174 175
                :label='$t(`admin:auth.domainsWhitelist`)'
                v-model='strategy.domainWhitelist'
                prepend-icon='mdi-email-check-outline'
                outlined
                :disabled='!strategy.selfRegistration'
                :hint='$t(`admin:auth.domainsWhitelistHint`)'
                persistent-hint
                small-chips
                deletable-chips
                clearable
                multiple
                chips
176
                )
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
              v-autocomplete.mt-3.ml-3(
                outlined
                :disabled='!strategy.selfRegistration'
                :items='groups'
                item-text='name'
                item-value='id'
                :label='$t(`admin:auth.autoEnrollGroups`)'
                v-model='strategy.autoEnrollGroups'
                prepend-icon='mdi-account-group'
                :hint='$t(`admin:auth.autoEnrollGroupsHint`)'
                small-chips
                persistent-hint
                deletable-chips
                clearable
                multiple
                chips
                )
194

195
        v-card.mt-4.wiki-form.animated.fadeInUp.wait-p4s(v-if='selectedStrategy !== `local`')
196
          v-toolbar(color='primary', dense, flat, dark)
197
            .subtitle-1 {{$t('admin:auth.configReference')}}
198
          v-card-text
199
            .body-2 {{$t('admin:auth.configReferenceSubtitle')}}
200
            v-alert.mt-3.radius-7(v-if='host.length < 8', color='red', outlined, :value='true', icon='mdi-alert')
Nick's avatar
Nick committed
201 202 203
              i18next(path='admin:auth.siteUrlNotSetup', tag='span')
                strong(place='siteUrl') {{$t('admin:general.siteUrl')}}
                strong(place='general') {{$t('admin:general.title')}}
204
            .pa-3.mt-3.radius-7.grey(v-else, :class='$vuetify.theme.dark ? `darken-3-d5` : `lighten-3`')
205 206
              .body-2: strong {{$t('admin:auth.allowedWebOrigins')}}
              .body-2 {{host}}
207
              v-divider.my-3
208 209
              .body-2: strong {{$t('admin:auth.callbackUrl')}}
              .body-2 {{host}}/login/{{strategy.key}}/callback
210
              v-divider.my-3
211 212
              .body-2: strong {{$t('admin:auth.loginUrl')}}
              .body-2 {{host}}/login
213
              v-divider.my-3
214 215
              .body-2: strong {{$t('admin:auth.logoutUrl')}}
              .body-2 {{host}}
216
              v-divider.my-3
217 218
              .body-2: strong {{$t('admin:auth.tokenEndpointAuthMethod')}}
              .body-2 HTTP-POST
219 220 221
</template>

<script>
222
import _ from 'lodash'
223 224
import gql from 'graphql-tag'
import { v4 as uuid } from 'uuid'
NGPixel's avatar
NGPixel committed
225

226
import groupsQuery from 'gql/admin/auth/auth-query-groups.gql'
227
import hostQuery from 'gql/admin/auth/auth-query-host.gql'
228

229 230
import draggable from 'vuedraggable'

231
export default {
232 233 234
  components: {
    draggable
  },
235 236 237
  filters: {
    startCase(val) { return _.startCase(val) }
  },
238 239
  data() {
    return {
240
      groups: [],
241
      strategies: [],
242
      activeStrategies: [],
243 244
      selectedStrategy: '',
      host: '',
245 246 247
      strategy: {
        strategy: {}
      }
248
    }
249
  },
250 251
  watch: {
    selectedStrategy(newValue, oldValue) {
252
      this.strategy = _.find(this.activeStrategies, ['key', newValue]) || {}
253
    },
254
    activeStrategies(newValue, oldValue) {
255 256 257
      this.selectedStrategy = 'local'
    }
  },
NGPixel's avatar
NGPixel committed
258 259 260
  methods: {
    async refresh() {
      await this.$apollo.queries.strategies.refetch()
261
      await this.$apollo.queries.activeStrategies.refetch()
NGPixel's avatar
NGPixel committed
262
      this.$store.commit('showNotification', {
Nick's avatar
Nick committed
263
        message: this.$t('admin:auth.refreshSuccess'),
NGPixel's avatar
NGPixel committed
264 265 266 267
        style: 'success',
        icon: 'cached'
      })
    },
268 269 270 271 272 273 274 275 276 277 278 279
    addStrategy (str) {
      const newStr = {
        key: uuid(),
        strategy: str,
        config: str.props.map(c => ({
          key: c.key,
          value: {
            ...c,
            value: c.default
          }
        })),
        order: this.activeStrategies.length,
280
        isEnabled: true,
281 282 283 284 285 286 287 288 289 290 291 292 293
        displayName: str.title,
        selfRegistration: false,
        domainWhitelist: [],
        autoEnrollGroups: []
      }
      this.activeStrategies = [...this.activeStrategies, newStr]
      this.$nextTick(() => {
        this.selectedStrategy = newStr.key
      })
    },
    deleteStrategy () {
      this.activeStrategies = _.reject(this.activeStrategies, ['key', this.strategy.key])
    },
294
    async save() {
NGPixel's avatar
NGPixel committed
295
      this.$store.commit(`loadingStart`, 'admin-auth-savestrategies')
296
      try {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        const resp = await this.$apollo.mutate({
          mutation: gql`
            mutation($strategies: [AuthenticationStrategyInput]!) {
              authentication {
                updateStrategies(strategies: $strategies) {
                  responseResult {
                    succeeded
                    errorCode
                    slug
                    message
                  }
                }
              }
            }
          `,
312
          variables: {
313
            strategies: this.activeStrategies.map((str, idx) => ({
314 315 316
              key: str.key,
              strategyKey: str.strategy.key,
              displayName: str.displayName,
317
              order: idx,
318
              isEnabled: str.isEnabled,
319 320 321 322 323
              config: str.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })})),
              selfRegistration: str.selfRegistration,
              domainWhitelist: str.domainWhitelist,
              autoEnrollGroups: str.autoEnrollGroups
            }))
324 325
          }
        })
326 327 328 329 330 331 332 333 334
        if (_.get(resp, 'data.authentication.updateStrategies.responseResult.succeeded', false)) {
          this.$store.commit('showNotification', {
            message: this.$t('admin:auth.saveSuccess'),
            style: 'success',
            icon: 'check'
          })
        } else {
          throw new Error(_.get(resp, 'data.authentication.updateStrategies.responseResult.message', this.$t('common:error.unexpected')))
        }
335 336 337
      } catch (err) {
        this.$store.commit('pushGraphError', err)
      }
NGPixel's avatar
NGPixel committed
338
      this.$store.commit(`loadingStop`, 'admin-auth-savestrategies')
339
    }
340
  },
NGPixel's avatar
NGPixel committed
341 342
  apollo: {
    strategies: {
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
      query: gql`
        query {
          authentication {
            strategies {
              key
              title
              description
              isAvailable
              useForm
              logo
              website
              props {
                key
                value
              }
            }
          }
        }
      `,
      fetchPolicy: 'network-only',
      update: (data) => _.get(data, 'authentication.strategies', []).map(str => ({
        ...str,
        isDisabled: !str.isAvailable || str.key === `local`,
        props: _.sortBy(str.props.map(cfg => ({
          key: cfg.key,
          ...JSON.parse(cfg.value)
        })), [t => t.order])
      })),
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-strategies-refresh')
      }
    },
    activeStrategies: {
      query: gql`
        query {
          authentication {
            activeStrategies {
              key
              strategy {
                key
                title
                description
                useForm
                logo
                website
              }
              config {
                key
                value
              }
              order
394
              isEnabled
395 396 397 398 399 400 401 402
              displayName
              selfRegistration
              domainWhitelist
              autoEnrollGroups
            }
          }
        }
      `,
NGPixel's avatar
NGPixel committed
403
      fetchPolicy: 'network-only',
404
      update: (data) => _.sortBy(_.get(data, 'authentication.activeStrategies', []).map(str => ({
405 406 407 408 409
        ...str,
        config: _.sortBy(str.config.map(cfg => ({
          ...cfg,
          value: JSON.parse(cfg.value)
        })), [t => t.value.order])
410
      })), ['order']),
NGPixel's avatar
NGPixel committed
411
      watchLoading (isLoading) {
412
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-activestrategies-refresh')
NGPixel's avatar
NGPixel committed
413
      }
414 415 416 417 418 419 420 421
    },
    groups: {
      query: groupsQuery,
      fetchPolicy: 'network-only',
      update: (data) => data.groups.list,
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-groups-refresh')
      }
422 423 424 425 426 427 428 429
    },
    host: {
      query: hostQuery,
      fetchPolicy: 'network-only',
      update: (data) => _.cloneDeep(data.site.config.host),
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-auth-host-refresh')
      }
430
    }
431 432 433
  }
}
</script>