// Copyright 2021 99cloud // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from 'react'; import { inject, observer } from 'mobx-react'; import { Select } from 'antd'; import globalProjectStore from 'stores/keystone/project'; import { UserStore } from 'stores/keystone/user'; import { RoleStore } from 'stores/keystone/role'; import { ModalAction } from 'containers/Action'; import globalDomainStore from 'stores/keystone/domain'; export class UserManager extends ModalAction { static id = 'management-user'; static title = t('Manage User'); get name() { return t('Manager user'); } async init() { const projectRole = JSON.stringify(this.item.userMapProjectRoles); this.state.domainDefault = this.item.domain_id; this.state.userRoles = JSON.parse(projectRole); this.store = new RoleStore(); this.domainStore = globalDomainStore; this.userStore = new UserStore(); await this.getRoleList(); this.getDomains(); this.getUser(); } getRoleList() { return this.store.fetchList(); } getDomains() { this.domainStore.fetchDomain(); } getUser() { this.userStore.fetchList(); } static get modalSize() { return 'large'; } getModalSize() { return 'large'; } get multipleMode() { return 'multiple'; } // get defaultUserRoles() { // const { users } = this.item; // const defaultUsers = Object.keys(users); // const projectRole = (this.store.list.data || []).filter(it => // ((it.name.indexOf('project_') !== -1) && (it.name.indexOf('_project_') === -1)) || // it.name === 'admin' // ); // const projectRoleId = projectRole.map(it => it.id); // const defaultUserRoles = {}; // defaultUsers.forEach((user_id) => { // const roles = users[user_id].filter(role_id => projectRoleId.indexOf(role_id) !== -1); // if (roles[0]) { // defaultUserRoles[user_id] = roles; // } // }); // return defaultUserRoles; // } get domainList() { const { rootStore: { baseDomains }, } = this.props; const { domains } = this.domainStore; const domainList = (domains || []).filter( (it) => baseDomains.indexOf(it.name) === -1 || it.id === this.item.domain_id ); return domainList.map((it) => ({ label: it.name, value: it.id, key: it.id, })); } get checkedList() { const { domains } = this.domainStore; return (domains || []).map((it) => ({ label: it.name, value: it.id, key: it.id, })); } get item() { const { item } = this.props; item.roles = {}; return item; } get userList() { return (this.userStore.list.data || []).map((it) => ({ ...it, key: it.id, })); } get projectRoleList() { const projectRole = this.store.list.data || []; return projectRole; } get adminRoleId() { const adminRole = (this.store.list.data || []).filter( (it) => it.name === 'admin' ); return adminRole[0].id; } adminRoleList = (user_id) => { const adminRole = (this.store.list.data || []).filter( (it) => it.name === 'admin' ); return adminRole.map((it) => ({ label: it.name, value: it.id, key: it.id, user_id, })); }; userRolesList = (user_id) => this.projectRoleList.map((it) => ({ label: it.name, value: it.id, key: it.id, user_id, })); defaultRoles = (userId) => { const { roles, users } = this.item; const { userRoles: projectUsers } = this.state; const filterUsers = this.multipleMode ? users : projectUsers; if (!filterUsers[userId]) { roles[userId] = [this.projectRoleList[0].id]; } else { const usersProjectRole = filterUsers[userId].filter((it) => { const projectRole = this.projectRoleList.find((role) => role.id === it); return !!projectRole; }); return this.multipleMode ? usersProjectRole : usersProjectRole.slice(0, 1); } return roles[userId]; }; static policy = 'identity:update_project'; static allowed = () => Promise.resolve(true); get leftUserTable() { return [ { dataIndex: 'name', title: t('Name'), }, ]; } get rightUserTable() { return [ { dataIndex: 'name', title: t('Name'), }, { title: t('Select Project Role'), dataIndex: 'id', render: (id) => this.renderSelect(id), }, ]; } renderSelect = (id) => { let disable = false; if (this.item.users && this.item.users[id]) { // eslint-disable-next-line prefer-destructuring disable = this.item.users[id].filter((it) => it === this.adminRoleId)[0]; } return (