From 5043976ebf631053397a6a31a45173b3a31b1084 Mon Sep 17 00:00:00 2001 From: lizhi Date: Fri, 8 Mar 2019 11:05:44 +0800 Subject: [PATCH] fix: User set role --- src/components/Layout/index.js | 4 +- src/config/roles.js | 6 +- .../admin/pages/Users/ModalActions/index.jsx | 31 ++++---- src/portals/admin/pages/Users/index.jsx | 1 + src/stores/user/detail.js | 71 ++++++++++++++++++- src/stores/user/index.js | 44 +++--------- 6 files changed, 101 insertions(+), 56 deletions(-) diff --git a/src/components/Layout/index.js b/src/components/Layout/index.js index 24bc7afd..e8d76927 100644 --- a/src/components/Layout/index.js +++ b/src/components/Layout/index.js @@ -1,5 +1,3 @@ -import Layout from './index.jsx'; - export CreateResource from './CreateResource'; export Dialog from './Dialog'; export BackBtn from './BackBtn'; @@ -14,4 +12,4 @@ export SideNav from './SideNav'; export BreadCrumb from './BreadCrumb'; export Stepper from './Stepper'; -export default Layout; +export default from './index.jsx'; diff --git a/src/config/roles.js b/src/config/roles.js index 50ccf1ec..92aaf73a 100644 --- a/src/config/roles.js +++ b/src/config/roles.js @@ -70,9 +70,9 @@ export const isvRoleDesMap = { export default roles; -export const AdminPortal = 'global_admin'; -export const ISVPortal = 'isv'; -export const UserPortal = 'user'; +export const AdminPortal = PORTAL_NAME.admin; +export const ISVPortal = PORTAL_NAME.isv; +export const UserPortal = PORTAL_NAME.user; export const getRoleName = (role = {}, portal = 'global_admin') => { let name = ''; diff --git a/src/portals/admin/pages/Users/ModalActions/index.jsx b/src/portals/admin/pages/Users/ModalActions/index.jsx index 2f9ba470..1a7766d7 100644 --- a/src/portals/admin/pages/Users/ModalActions/index.jsx +++ b/src/portals/admin/pages/Users/ModalActions/index.jsx @@ -17,11 +17,11 @@ const emailRegexp = '^[A-Za-z0-9._%-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$'; @observer export default class UserModalActions extends Component { get selectedIds() { - return this.props.userStore.selectIds; + return this.props.userDetailStore.selectIds; } get userNames() { - return this.props.userStore.userNames; + return this.props.userDetailStore.userNames; } get selectedGroupIds() { @@ -141,10 +141,10 @@ export default class UserModalActions extends Component { renderModalLeaveGroup() { const { - t, modalStore, groupStore, userStore + t, modalStore, groupStore, userDetailStore } = this.props; const { hide, isOpen } = modalStore; - const { users } = userStore; + const { users } = userDetailStore; const { leaveGroup } = groupStore; let names = _.flatMap( users.filter(user => this.selectedIds.includes(user.user_id)), @@ -175,9 +175,9 @@ export default class UserModalActions extends Component { } renderModalSetRole() { - const { userStore, modalStore, t } = this.props; + const { userDetailStore, modalStore, t } = this.props; const { isOpen, hide, item } = modalStore; - const { setRole, createRoles } = userStore; + const { setRole, createRoles } = userDetailStore; const roleId = _.get(item, 'role.role_id', ''); const userId = _.get(item, 'user_id') || this.selectedIds.join(','); const isMultip = _.get(item, 'user_id'); @@ -213,13 +213,15 @@ export default class UserModalActions extends Component { } renderModalCreateUser() { - const { userStore, modalStore, t } = this.props; + const { + userStore, userDetailStore, modalStore, t + } = this.props; const { isOpen, item } = modalStore; const { hideModifyUser, createOrModify } = userStore; const { user_id, username, description, email } = item; - const roles = userStore.createRoles; + const { createRoles } = userDetailStore; const title = !user_id ? t('Create New User') : t('Modify User'); @@ -258,8 +260,11 @@ export default class UserModalActions extends Component {
- + {createRoles.map(({ role_id, role_name }) => ( {t(role_name)} @@ -310,10 +315,10 @@ export default class UserModalActions extends Component { } renderModalDeleteUser() { - const { t, modalStore, userStore } = this.props; + const { t, modalStore, userDetailStore } = this.props; const { hide, isOpen, item } = modalStore; - const { remove } = userStore; - userStore.userId = item.type === 'one' ? [item.user_id] : this.selectedIds; + const { remove } = userDetailStore; + userDetailStore.userId = item.type === 'one' ? [item.user_id] : this.selectedIds; return ( diff --git a/src/stores/user/detail.js b/src/stores/user/detail.js index bf448f47..eb22732d 100644 --- a/src/stores/user/detail.js +++ b/src/stores/user/detail.js @@ -1,7 +1,10 @@ -import { observable } from 'mobx'; +import { observable, action } from 'mobx'; import _ from 'lodash'; import { useTableActions } from 'mixins'; +import { PORTAL_NAME } from 'config/roles'; +import { normalUserID, ISVID } from 'config/group'; + import Store from '../Store'; const defaultStatus = ['active']; @@ -26,6 +29,27 @@ export default class UserDetailStore extends Store { return this.getStore('group').selectedRoleId; } + get sortRole() { + return this.getStore('role').sortRole; + } + + get roles() { + return this.getStore('user').roles; + } + + get modal() { + return this.getStore('modal'); + } + + get userNames() { + if (_.isEmpty(this.selectIds)) { + return ''; + } + + const users = _.filter(this.users, user => this.selectIds.includes(user.user_id)); + return _.flatMap(users, 'username'); + } + fetchUserDetail = (params = {}) => { const defaultParams = { sort_key: 'status_time', @@ -49,6 +73,21 @@ export default class UserDetailStore extends Store { ); }; + get createRoles() { + const { selectedGroupIds } = this.getStore('group'); + const key = _.first(selectedGroupIds); + if (key === normalUserID) { + return this.roles.filter(r => r.portal === PORTAL_NAME.user); + } + if (key === ISVID) { + return this.roles.filter(r => r.portal === PORTAL_NAME.isv); + } + + return this.roles + .filter(r => r.portal === PORTAL_NAME.admin) + .sort(this.sortRole); + } + fetchAll = async (params = {}) => { this.selectedIds = []; this.selectedRowKeys = []; @@ -65,4 +104,34 @@ export default class UserDetailStore extends Store { this.selectStatus = ''; this.searchWord = ''; }; + + @action + setRole = async (e, data) => { + this.operateResult = await this.request.post('user:role', { + user_id: data.user_id.split(','), + role_id: [data.role_id] + }); + const { err } = this.operateResult; + if (!err) { + this.modal.hide(); + this.fetchAll(); + } + }; + + @action + remove = async () => { + const result = await this.request.delete('users', { + user_id: this.userId + }); + + if (_.get(result, 'user_id')) { + this.selectIds = []; + this.selectedRowKeys = []; + this.modal.hide(); + await this.fetchAll(); + } else { + const { err, errDetail } = result; + this.error(errDetail || err); + } + }; } diff --git a/src/stores/user/index.js b/src/stores/user/index.js index 50488418..85e4c359 100644 --- a/src/stores/user/index.js +++ b/src/stores/user/index.js @@ -3,15 +3,9 @@ import _ from 'lodash'; import { getFormData } from 'utils'; import { useTableActions } from 'mixins'; -import { PORTAL_NAME, AdminPortal } from 'config/roles'; - -import { - normalUserID, - ISVID, - rootName, - normalUserName, - ISVName -} from 'config/group'; +import { PORTAL_NAME } from 'config/roles'; + +import { rootName, normalUserName, ISVName } from 'config/group'; import Store from '../Store'; const defaultStatus = ['active']; @@ -88,19 +82,6 @@ export default class UserStore extends Store { return this.getStore('role').sortRole; } - get createRoles() { - const { selectedGroupIds } = this.getStore('group'); - const key = _.first(selectedGroupIds); - if (key === normalUserID) { - return this.roles.filter(r => r.portal === 'user'); - } - if (key === ISVID) { - return this.roles.filter(r => r.portal === 'isv'); - } - - return this.roles.filter(r => r.portal === AdminPortal).sort(this.sortRole); - } - get selectedGroupIds() { return this.getStore('group').validGroupIds; } @@ -124,6 +105,10 @@ export default class UserStore extends Store { .sort(this.sortRole); } + get fetchDetailStore() { + return this.getStore('userDetail').fetchAll; + } + @action formatUserDetail = arr => { arr = _.map( @@ -282,7 +267,7 @@ export default class UserStore extends Store { if (_.get(this.operateResult, 'user_id')) { this.modal.hide(); this.userDetail = {}; - await this.fetchAll(); + await this.fetchDetailStore(); } }; @@ -430,19 +415,6 @@ export default class UserStore extends Store { description: '' }; }; - - @action - setRole = async (e, data) => { - this.operateResult = await this.request.post('user:role', { - user_id: data.user_id.split(','), - role_id: [data.role_id] - }); - const { err } = this.operateResult; - if (!err) { - this.modal.hide(); - this.fetchAll(); - } - }; } export Role from './role';