// 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 { GroupStore } from 'stores/keystone/user-group'; import globalUserStore from 'stores/keystone/user'; import { FormAction } from 'containers/Action'; import { Select } from 'antd'; import { ProjectStore } from 'stores/keystone/project'; import globalRoleStore from 'stores/keystone/role'; import globalDomainStore from 'stores/keystone/domain'; import { getPasswordOtherRule, phoneNumberValidate, emailValidate, } from 'utils/validate'; import { statusTypes, getDomainFormItem, nameDomainColumns, transferFilterOption, } from 'resources/keystone/domain'; import { roleFilterOption } from 'resources/keystone/role'; export class Create extends FormAction { constructor(props) { super(props); this.state = { domain: 'default', more: false, projectRoles: {}, }; } init() { this.store = globalUserStore; this.userGroupStore = new GroupStore(); this.projectStore = new ProjectStore(); this.roleStore = globalRoleStore; this.getUserGroups(); this.getProjects(); this.getRoles(); this.getDomains(); } getDomains() { globalDomainStore.fetchDomain(); } getUserGroups() { this.userGroupStore.fetchList({ withRole: false }); } getProjects() { this.projectStore.fetchList({ withRole: false }); } getRoles() { this.roleStore.fetchList(); } static id = 'user-create'; static title = t('Create User'); static path = '/identity/user-admin/create'; static policy = [ 'identity:create_user', 'identity:update_user', 'identity:list_roles', ]; static allowed(item, containerProps) { const { match: { path }, } = containerProps; if (path.indexOf('domain-admin/detail') >= 0) { return Promise.resolve(false); } return Promise.resolve(true); } get name() { return t('Create User'); } get listUrl() { return this.getRoutePath('user'); } get defaultValue() { const data = { more: false, enabled: statusTypes[0].value, domain_id: 'default', }; return data; } get projectList() { return (this.projectStore.list.data || []).map((it) => ({ label: it.name, value: it.id, })); } get userGroupList() { return (this.userGroupStore.list.data || []).map((it) => ({ ...it, key: it.id, })); } get projects() { return (this.projectStore.list.data || []).map((it) => ({ ...it, key: it.id, })); } static allowed = () => Promise.resolve(true); get leftProjectTable() { return nameDomainColumns; } get projectRoleList() { return this.roleStore.list.data || []; } projectRolesList = (projectId) => (this.projectRoleList || []).map((it) => ({ label: it.name, value: it.id, projectId, })); defaultRoles = () => [(this.projectRoleList[0] || {}).id]; onSelectChange = (value, option, projectId) => { const { projectRoles } = this.state; if (value.length && option.length) { projectRoles[projectId] = value; } else { projectRoles[projectId] = []; } this.setState({ projectRoles }); }; onClickSelect = (e) => { if (e && e.stopPropagation) { e.stopPropagation(); } }; renderSelect = (projectId) => { return (