|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +<template> |
| 19 | + <span class="header-notice-opener" v-if="showSwitcher"> |
| 20 | + <a-select |
| 21 | + class="domain-select" |
| 22 | + :loading="loading" |
| 23 | + :defaultValue="currentAccount" |
| 24 | + :value="currentAccount" |
| 25 | + @change="changeAccount" |
| 26 | + @focus="fetchData" > |
| 27 | + |
| 28 | + <a-tooltip placement="bottom" slot="suffixIcon"> |
| 29 | + <template slot="title"> |
| 30 | + <span>{{ $t('label.domain') }}</span> |
| 31 | + </template> |
| 32 | + <span style="font-size: 20px; color: #999; margin-top: -5px"> |
| 33 | + <a-icon v-if="!loading" type="block" /> |
| 34 | + <a-icon v-else type="loading" /> |
| 35 | + </span> |
| 36 | + </a-tooltip> |
| 37 | + |
| 38 | + <a-select-option v-for="(account, index) in samlAccounts" :key="index"> |
| 39 | + {{ `${account.accountName} (${account.domainName})` }} |
| 40 | + </a-select-option> |
| 41 | + </a-select> |
| 42 | + </span> |
| 43 | +</template> |
| 44 | + |
| 45 | +<script> |
| 46 | +import store from '@/store' |
| 47 | +import { api } from '@/api' |
| 48 | +import _ from 'lodash' |
| 49 | +
|
| 50 | +export default { |
| 51 | + name: 'SamlDomainSwitcher', |
| 52 | + data () { |
| 53 | + return { |
| 54 | + showSwitcher: false, |
| 55 | + samlAccounts: [], |
| 56 | + currentAccount: '', |
| 57 | + loading: false |
| 58 | + } |
| 59 | + }, |
| 60 | + mounted () { |
| 61 | + this.fetchData() |
| 62 | + }, |
| 63 | + methods: { |
| 64 | + fetchData () { |
| 65 | + var page = 1 |
| 66 | + const samlAccounts = [] |
| 67 | + const getNextPage = () => { |
| 68 | + this.loading = true |
| 69 | + api('listAndSwitchSamlAccount', { listAll: true, details: 'min', page: page, pageSize: 500 }).then(json => { |
| 70 | + if (json && json.listandswitchsamlaccountresponse && json.listandswitchsamlaccountresponse.samluseraccount) { |
| 71 | + samlAccounts.push(...json.listandswitchsamlaccountresponse.samluseraccount) |
| 72 | + } |
| 73 | + if (samlAccounts.length < json.listandswitchsamlaccountresponse.count) { |
| 74 | + page++ |
| 75 | + getNextPage() |
| 76 | + } |
| 77 | + }).catch(error => { |
| 78 | + console.log(error) |
| 79 | + }).finally(() => { |
| 80 | + if (samlAccounts.length < 2) { |
| 81 | + this.showSwitcher = false |
| 82 | + return |
| 83 | + } |
| 84 | + this.samlAccounts = _.orderBy(samlAccounts, ['domainPath'], ['asc']) |
| 85 | + const currentAccount = this.samlAccounts.filter(x => { |
| 86 | + return x.userId === store.getters.userInfo.id |
| 87 | + })[0] |
| 88 | + this.currentAccount = `${currentAccount.accountName} (${currentAccount.domainName})` |
| 89 | + this.loading = false |
| 90 | + this.showSwitcher = true |
| 91 | + }) |
| 92 | + } |
| 93 | + getNextPage() |
| 94 | + }, |
| 95 | + changeAccount (index) { |
| 96 | + const account = this.samlAccounts[index] |
| 97 | + api('listAndSwitchSamlAccount', {}, 'POST', { |
| 98 | + userid: account.userId, |
| 99 | + domainid: account.domainId |
| 100 | + }).then(response => { |
| 101 | + store.dispatch('GetInfo').then(() => { |
| 102 | + this.$message.success(`Switched to "${account.accountName} (${account.domainPath})"`) |
| 103 | + this.$router.go() |
| 104 | + }) |
| 105 | + }) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +</script> |
| 110 | + |
| 111 | +<style lang="less" scoped> |
| 112 | +.domain { |
| 113 | + &-select { |
| 114 | + width: 20vw; |
| 115 | + } |
| 116 | +
|
| 117 | + &-icon { |
| 118 | + font-size: 20px; |
| 119 | + line-height: 1; |
| 120 | + padding-top: 5px; |
| 121 | + padding-right: 5px; |
| 122 | + } |
| 123 | +} |
| 124 | +</style> |
0 commit comments