| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <template>
- <base-dialog @cancel="cancelDialog">
- <div slot="header">{{params.title}}</div>
- <div slot="body">
- <a-alert :message="$t('iam.project.alter_role_tips')" class="mb-2" />
- <dialog-selected-tips :name="$t('common_578')" :count="params.data.length" :action="params.title" />
- <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
- <a-form
- :form="form.fc"
- v-bind="formItemLayout">
- <a-form-item :label="$t('dictionary.role')">
- <a-select
- :filterOption="filterOption"
- :loading="roleLoading"
- v-decorator="decorators.roles"
- mode="multiple"
- :placeholder="$t('rules.role')"
- @deselect="handleDeselect"
- @select="handleSelect">
- <a-select-option v-for="item in roleList" :key="item.id">
- {{item.name}}
- </a-select-option>
- </a-select>
- </a-form-item>
- </a-form>
- </div>
- <div slot="footer">
- <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
- <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
- </div>
- </base-dialog>
- </template>
- <script>
- import * as R from 'ramda'
- import DialogMixin from '@/mixins/dialog'
- import WindowsMixin from '@/mixins/windows'
- export default {
- name: 'ProjectEditRolesDialog',
- mixins: [DialogMixin, WindowsMixin],
- data () {
- return {
- loading: false,
- domainId: this.params.data[0].domain_id,
- roleList: [],
- roleLoading: false,
- initialValue: this.params.data[0].roles.map(item => { return item.id }),
- deselectList: [],
- selectList: [],
- form: {
- fc: this.$form.createForm(this),
- },
- decorators: {
- roles: [
- 'roles',
- {
- rules: [
- { required: true, message: this.$t('rules.role') },
- ],
- },
- ],
- },
- formItemLayout: {
- wrapperCol: {
- span: 21,
- },
- labelCol: {
- span: 3,
- },
- },
- type: this.params.data[0].groupId && this.params.data[0].groupName ? 'group' : 'user',
- }
- },
- created () {
- this.manager = new this.$Manager('projects', 'v1')
- this.fetchRoles()
- },
- methods: {
- async fetchRoles () {
- const manager = new this.$Manager('roles', 'v1')
- const params = {
- domain_id: this.domainId,
- scope: this.$store.getters.scope,
- }
- this.roleLoading = true
- try {
- const { data = {} } = await manager.list({ params })
- this.roleList = data.data || []
- this.form.fc.getFieldDecorator('roles', { initialValue: this.initialValue })
- } catch (err) {
- throw err
- } finally {
- this.roleLoading = false
- }
- },
- filterOption (input, option) {
- return (
- option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
- )
- },
- handleDeselect (value, option) {
- if (R.includes(value, this.initialValue) && !R.includes(value, this.deselectList)) this.deselectList.push(value)
- if (!R.includes(value, this.initialValue) && R.includes(value, this.selectList)) {
- for (let i = 0; i < this.selectList.length; i++) {
- if (this.selectList[i] === value) {
- this.selectList.splice(i, 1)
- }
- }
- }
- },
- handleSelect (val) {
- if (!R.includes(val, this.initialValue) && !R.includes(val, this.selectList)) this.selectList.push(val)
- if (R.includes(val, this.initialValue) && R.includes(val, this.deselectList)) {
- for (let i = 0; i < this.deselectList.length; i++) {
- if (this.deselectList[i] === val) {
- this.deselectList.splice(i, 1)
- }
- }
- }
- },
- doJoint (roles) {
- const data = {
- [this.type + 's']: [this.params.data[0].groupId ? this.params.data[0].groupId : this.params.data[0].id],
- roles: roles,
- }
- return this.manager.performAction({
- id: this.params.uid,
- action: 'join',
- data,
- })
- },
- doLeave (roles) {
- const data = {
- group_roles: [],
- user_roles: [],
- }
- if (this.type === 'group') {
- data.group_roles = roles
- } else {
- data.user_roles = roles
- }
- return this.manager.performAction({
- id: this.params.uid,
- action: 'leave',
- data,
- })
- },
- async handleConfirm () {
- this.loading = true
- try {
- if (this.selectList.length > 0) {
- const { roles } = await this.form.fc.validateFields()
- await this.doJoint(roles)
- }
- if (this.deselectList.length > 0) {
- const roles = this.deselectList.map(item => ({
- [this.type]: this.params.data[0].groupId ? this.params.data[0].groupId : this.params.data[0].id,
- role: item,
- }))
- await this.doLeave(roles)
- }
- this.cancelDialog()
- this.params.success()
- } catch (error) {
- this.loading = false
- throw error
- }
- },
- },
- }
- </script>
|