// 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 { has } from 'lodash'; import { inject, observer } from 'mobx-react'; import globalGroupStore from 'stores/keystone/user-group'; import globalUserStore from 'stores/keystone/user'; import { FormAction } from 'containers/Action'; import { Select } from 'antd'; import globalProjectStore from 'stores/keystone/project'; import globalRoleStore from 'stores/keystone/role'; import { getPasswordOtherRule, phoneNumberValidate, emailValidate, } from 'utils/validate'; import { statusTypes } from 'utils/constants'; export class CreateForm extends FormAction { constructor(props) { super(props); this.state = { domain: 'default', more: false, newProjectRoles: {}, }; } init() { this.store = globalUserStore; this.userGroupStore = globalGroupStore; this.projectStore = globalProjectStore; this.roleStore = globalRoleStore; this.getUserGroup(); this.getProject(); this.getRole(); this.getDomains(); } getDomains() { this.store.fetchDomain(); } getUserGroup() { this.userGroupStore.fetchList(); } getProject() { this.projectStore.fetchList(); } getRole() { 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 { domains } = this.store; const { domain } = this.state; const domianDefault = (domains || []).filter((it) => it.id === domain)[0]; const data = { more: false, enabled: statusTypes[0].value, domain_id: domianDefault ? domianDefault.name : '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, })); } // get roleList() { // return (this.roleStore.list.data || []).map(it => ({ // label: it.name, // value: it.id, // })); // } // get adminRole() { // return (this.roleStore.list.data || []).filter(it => // it.name === 'admin' // )[0]; // } onValuesChange = (changedFields) => { if (has(changedFields, 'more')) { this.setState({ more: changedFields.more, }); } }; static allowed = () => Promise.resolve(true); get leftProjectTable() { return [ { dataIndex: 'name', title: t('Name'), }, ]; } get projectRoleList() { const projectRole = this.roleStore.list.data || []; return projectRole; } userRolesList = (role_id) => (this.projectRoleList || []).map((it) => ({ label: it.name, value: it.id, role_id, })); defaultRoles = () => [this.projectRoleList[0].id]; userRoleChange = (value, option) => { const { newProjectRoles } = this.state; const { role_id } = option; newProjectRoles[role_id] = value; this.setState({ newProjectRoles }); }; get rightProjectTable() { return [ { dataIndex: 'name', title: t('Name'), }, { title: t('Select Projct Role'), dataIndex: 'id', render: (id) => (