Commit a0933c55 authored by Jesús Espino's avatar Jesús Espino Committed by George Goldberg
Browse files

MM-10757: Changing default_{channel,team}_{user,admin}_role to names instead of ids (#1278)

* MM-10757: Changing default_{channel,team}_{user,admin}_role to names instead of ids

* fixed eslint errors
parent ea81c2e8
......@@ -4,9 +4,9 @@
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {loadRolesIfNeeded, editRole, getRole as loadRole} from 'mattermost-redux/actions/roles';
import {loadRolesIfNeeded, editRole} from 'mattermost-redux/actions/roles';
import {getRoles, getRolesById} from 'mattermost-redux/selectors/entities/roles';
import {getRoles} from 'mattermost-redux/selectors/entities/roles';
import {getScheme, makeGetSchemeTeams} from 'mattermost-redux/selectors/entities/schemes';
import {getScheme as loadScheme, patchScheme, createScheme, getSchemeTeams as loadSchemeTeams} from 'mattermost-redux/actions/schemes';
......@@ -24,7 +24,6 @@ function makeMapStateToProps() {
scheme: schemeId ? getScheme(state, schemeId) : null,
teams: schemeId ? getSchemeTeams(state, {schemeId}) : null,
roles: getRoles(state),
rolesById: getRolesById(state),
rolesRequest: state.requests.roles.getRolesByNames,
};
};
......@@ -37,7 +36,6 @@ function mapDispatchToProps(dispatch) {
loadScheme,
loadSchemeTeams,
editRole,
loadRole,
patchScheme,
updateTeamScheme,
createScheme,
......
......@@ -24,11 +24,9 @@ export default class PermissionTeamSchemeSettings extends React.Component {
schemeId: PropTypes.string,
scheme: PropTypes.object,
roles: PropTypes.object,
rolesById: PropTypes.object,
teams: PropTypes.array,
actions: PropTypes.shape({
loadRolesIfNeeded: PropTypes.func.isRequired,
loadRole: PropTypes.func.isRequired,
loadScheme: PropTypes.func.isRequired,
loadSchemeTeams: PropTypes.func.isRequired,
editRole: PropTypes.func.isRequired,
......@@ -64,10 +62,12 @@ export default class PermissionTeamSchemeSettings extends React.Component {
this.props.actions.loadRolesIfNeeded(['team_admin', 'team_user', 'channel_admin', 'channel_user']);
if (this.props.schemeId) {
this.props.actions.loadScheme(this.props.schemeId).then((result) => {
this.props.actions.loadRole(result.data.default_team_user_role);
this.props.actions.loadRole(result.data.default_team_admin_role);
this.props.actions.loadRole(result.data.default_channel_user_role);
this.props.actions.loadRole(result.data.default_channel_admin_role);
this.props.actions.loadRolesIfNeeded([
result.data.default_team_user_role,
result.data.default_team_admin_role,
result.data.default_channel_user_role,
result.data.default_channel_admin_role,
]);
});
this.props.actions.loadSchemeTeams(this.props.schemeId);
}
......@@ -77,10 +77,10 @@ export default class PermissionTeamSchemeSettings extends React.Component {
if (props.schemeId) {
if (props.scheme !== null &&
props.teams !== null &&
props.rolesById[props.scheme.default_team_user_role] &&
props.rolesById[props.scheme.default_team_admin_role] &&
props.rolesById[props.scheme.default_channel_user_role] &&
props.rolesById[props.scheme.default_channel_admin_role]) {
props.roles[props.scheme.default_team_user_role] &&
props.roles[props.scheme.default_team_admin_role] &&
props.roles[props.scheme.default_channel_user_role] &&
props.roles[props.scheme.default_channel_admin_role]) {
return true;
}
return false;
......@@ -135,10 +135,10 @@ export default class PermissionTeamSchemeSettings extends React.Component {
if (this.props.schemeId) {
if (this.isLoaded(this.props)) {
teamUser = this.props.rolesById[this.props.scheme.default_team_user_role];
teamAdmin = this.props.rolesById[this.props.scheme.default_team_admin_role];
channelUser = this.props.rolesById[this.props.scheme.default_channel_user_role];
channelAdmin = this.props.rolesById[this.props.scheme.default_channel_admin_role];
teamUser = this.props.roles[this.props.scheme.default_team_user_role];
teamAdmin = this.props.roles[this.props.scheme.default_team_admin_role];
channelUser = this.props.roles[this.props.scheme.default_channel_user_role];
channelAdmin = this.props.roles[this.props.scheme.default_channel_admin_role];
}
} else if (this.isLoaded(this.props)) {
teamUser = this.props.roles.team_user;
......@@ -194,8 +194,8 @@ export default class PermissionTeamSchemeSettings extends React.Component {
this.setState({saving: true});
if (this.props.schemeId) {
const derived = this.deriveRolesFromAllUsers(
this.props.rolesById[this.props.scheme.default_team_user_role],
this.props.rolesById[this.props.scheme.default_channel_user_role],
this.props.roles[this.props.scheme.default_team_user_role],
this.props.roles[this.props.scheme.default_channel_user_role],
allUsers
);
teamUser = derived.team_user;
......@@ -224,10 +224,16 @@ export default class PermissionTeamSchemeSettings extends React.Component {
}
const newScheme = result.data;
schemeId = newScheme.id;
teamUser = {...teamUser, id: newScheme.default_team_user_role};
teamAdmin = {...teamAdmin, id: newScheme.default_team_admin_role};
channelUser = {...channelUser, id: newScheme.default_channel_user_role};
channelAdmin = {...channelAdmin, id: newScheme.default_channel_admin_role};
await this.props.actions.loadRolesIfNeeded([
newScheme.default_team_user_role,
newScheme.default_team_admin_role,
newScheme.default_channel_user_role,
newScheme.default_channel_admin_role,
]);
teamUser = {...teamUser, id: this.props.roles[newScheme.default_team_user_role].id};
teamAdmin = {...teamAdmin, id: this.props.roles[newScheme.default_team_admin_role].id};
channelUser = {...channelUser, id: this.props.roles[newScheme.default_channel_user_role].id};
channelAdmin = {...channelAdmin, id: this.props.roles[newScheme.default_channel_admin_role].id};
}
const teamAdminPromise = this.props.actions.editRole(teamAdmin);
......
......@@ -31,8 +31,6 @@ describe('components/admin_console/permission_schemes_settings/permission_team_s
channel_admin: {
permissions: [],
},
},
rolesById: {
aaa: {
permissions: [],
},
......@@ -116,7 +114,15 @@ describe('components/admin_console/permission_schemes_settings/permission_team_s
test('should save each role on save clicked except system_admin role', (done) => {
const editRole = jest.fn().mockImplementation(() => Promise.resolve({data: {}}));
const createScheme = jest.fn().mockImplementation(() => Promise.resolve({data: {id: '123'}}));
const createScheme = jest.fn().mockImplementation(() => Promise.resolve({
data: {
id: '123',
default_team_user_role: 'aaa',
default_team_admin_role: 'bbb',
default_channel_user_role: 'ccc',
default_channel_admin_role: 'ddd',
},
}));
const updateTeamScheme = jest.fn().mockImplementation(() => Promise.resolve({}));
const wrapper = shallow(
<PermissionTeamSchemeSettings
......@@ -153,7 +159,15 @@ describe('components/admin_console/permission_schemes_settings/permission_team_s
test('should show error if editRole fails', (done) => {
const editRole = jest.fn().mockImplementation(() => Promise.resolve({error: {message: 'test error'}}));
const createScheme = jest.fn().mockImplementation(() => Promise.resolve({data: {id: '123'}}));
const createScheme = jest.fn().mockImplementation(() => Promise.resolve({
data: {
id: '123',
default_team_user_role: 'aaa',
default_team_admin_role: 'bbb',
default_channel_user_role: 'ccc',
default_channel_admin_role: 'ddd',
},
}));
const updateTeamScheme = jest.fn().mockImplementation(() => Promise.resolve({}));
const wrapper = shallow(
<PermissionTeamSchemeSettings
......@@ -232,7 +246,7 @@ describe('components/admin_console/permission_schemes_settings/permission_team_s
default_channel_user_role: 'ccc',
default_channel_admin_role: 'ddd',
},
rolesById: {
roles: {
aaa: {
permissions: ['invite_user'],
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment