// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import * as UserUtils from 'mattermost-redux/utils/user_utils';
import {Client4} from 'mattermost-redux/client';
import {General} from 'mattermost-redux/constants';
import {trackEvent} from 'actions/diagnostics_actions.jsx';
import React from 'react';
import {Modal} from 'react-bootstrap';
import PropTypes from 'prop-types';
import {FormattedMessage, FormattedHTMLMessage} from 'react-intl';
function getStateFromProps(props) {
const roles = props.user && props.user.roles ? props.user.roles : '';
return {
error: null,
hasPostAllRole: UserUtils.hasPostAllRole(roles),
hasPostAllPublicRole: UserUtils.hasPostAllPublicRole(roles),
hasUserAccessTokenRole: UserUtils.hasUserAccessTokenRole(roles),
isSystemAdmin: UserUtils.isSystemAdmin(roles)
};
}
export default class ManageRolesModal extends React.PureComponent {
static propTypes = {
/**
* Set to render the modal
*/
show: PropTypes.bool.isRequired,
/**
* The user the roles are being managed for
*/
user: PropTypes.object,
/**
* Set if user access tokens are enabled
*/
userAccessTokensEnabled: PropTypes.bool.isRequired,
/**
* Function called when modal is dismissed
*/
onModalDismissed: PropTypes.func.isRequired,
actions: PropTypes.shape({
/**
* Function to update a user's roles
*/
updateUserRoles: PropTypes.func.isRequired
}).isRequired
};
constructor(props) {
super(props);
this.state = getStateFromProps(props);
}
componentWillReceiveProps(nextProps) {
const user = this.props.user || {};
const nextUser = nextProps.user || {};
if (user.id !== nextUser.id) {
this.setState(getStateFromProps(nextProps));
}
}
handleError = (error) => {
this.setState({
error
});
}
handleSystemAdminChange = (e) => {
if (e.target.name === 'systemadmin') {
this.setState({isSystemAdmin: true});
} else if (e.target.name === 'systemmember') {
this.setState({isSystemAdmin: false});
}
};
handleUserAccessTokenChange = (e) => {
this.setState({
hasUserAccessTokenRole: e.target.checked
});
};
handlePostAllChange = (e) => {
this.setState({
hasPostAllRole: e.target.checked
});
};
handlePostAllPublicChange = (e) => {
this.setState({
hasPostAllPublicRole: e.target.checked
});
};
trackRoleChanges = (roles, oldRoles) => {
if (UserUtils.hasUserAccessTokenRole(roles) && !UserUtils.hasUserAccessTokenRole(oldRoles)) {
trackEvent('actions', 'add_roles', {role: General.SYSTEM_USER_ACCESS_TOKEN_ROLE});
} else if (!UserUtils.hasUserAccessTokenRole(roles) && UserUtils.hasUserAccessTokenRole(oldRoles)) {
trackEvent('actions', 'remove_roles', {role: General.SYSTEM_USER_ACCESS_TOKEN_ROLE});
}
if (UserUtils.hasPostAllRole(roles) && !UserUtils.hasPostAllRole(oldRoles)) {
trackEvent('actions', 'add_roles', {role: General.SYSTEM_POST_ALL_ROLE});
} else if (!UserUtils.hasPostAllRole(roles) && UserUtils.hasPostAllRole(oldRoles)) {
trackEvent('actions', 'remove_roles', {role: General.SYSTEM_POST_ALL_ROLE});
}
if (UserUtils.hasPostAllPublicRole(roles) && !UserUtils.hasPostAllPublicRole(oldRoles)) {
trackEvent('actions', 'add_roles', {role: General.SYSTEM_POST_ALL_PUBLIC_ROLE});
} else if (!UserUtils.hasPostAllPublicRole(roles) && UserUtils.hasPostAllPublicRole(oldRoles)) {
trackEvent('actions', 'remove_roles', {role: General.SYSTEM_POST_ALL_PUBLIC_ROLE});
}
}
handleSave = async () => {
this.setState({error: null});
let roles = General.SYSTEM_USER_ROLE;
if (this.state.isSystemAdmin) {
roles += ' ' + General.SYSTEM_ADMIN_ROLE;
} else if (this.state.hasUserAccessTokenRole) {
roles += ' ' + General.SYSTEM_USER_ACCESS_TOKEN_ROLE;
if (this.state.hasPostAllRole) {
roles += ' ' + General.SYSTEM_POST_ALL_ROLE;
} else if (this.state.hasPostAllPublicRole) {
roles += ' ' + General.SYSTEM_POST_ALL_PUBLIC_ROLE;
}
}
const data = await this.props.actions.updateUserRoles(this.props.user.id, roles);
this.trackRoleChanges(roles, this.props.user.roles);
if (data) {
this.props.onModalDismissed();
} else {
this.handleError(