Skip to content

Commit

Permalink
fix: User set role (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
liiil825 authored Mar 8, 2019
1 parent 79a93fc commit e9336e2
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 56 deletions.
4 changes: 1 addition & 3 deletions src/components/Layout/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Layout from './index.jsx';

export CreateResource from './CreateResource';
export Dialog from './Dialog';
export BackBtn from './BackBtn';
Expand All @@ -14,4 +12,4 @@ export SideNav from './SideNav';
export BreadCrumb from './BreadCrumb';
export Stepper from './Stepper';

export default Layout;
export default from './index.jsx';
6 changes: 3 additions & 3 deletions src/config/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
31 changes: 18 additions & 13 deletions src/portals/admin/pages/Users/ModalActions/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -258,8 +260,11 @@ export default class UserModalActions extends Component {
<Fragment>
<div className={styles.formItem}>
<label>{t('Role')}</label>
<Select defaultValue={_.get(roles, '[0].role_id')} name="role_id">
{roles.map(({ role_id, role_name }) => (
<Select
defaultValue={_.get(createRoles, '[0].role_id')}
name="role_id"
>
{createRoles.map(({ role_id, role_name }) => (
<Select.Option key={role_id} value={role_id}>
{t(role_name)}
</Select.Option>
Expand Down Expand Up @@ -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 (
<Dialog
Expand Down
1 change: 1 addition & 0 deletions src/portals/admin/pages/Users/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export default class Users extends Component {
t={t}
modalStore={modalStore}
userStore={userStore}
userDetailStore={userDetailStore}
groupStore={groupStore}
/>
</Layout>
Expand Down
71 changes: 70 additions & 1 deletion src/stores/user/detail.js
Original file line number Diff line number Diff line change
@@ -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'];
Expand All @@ -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',
Expand All @@ -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 = [];
Expand All @@ -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);
}
};
}
44 changes: 8 additions & 36 deletions src/stores/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down Expand Up @@ -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();
}
};

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit e9336e2

Please sign in to comment.