// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import Client from 'utils/web_client.jsx'; import Constants from 'utils/constants.jsx'; import * as Utils from 'utils/utils.jsx'; import UserStore from 'stores/user_store.jsx'; import ConfirmModal from '../confirm_modal.jsx'; import TeamStore from 'stores/team_store.jsx'; import {FormattedMessage, FormattedHTMLMessage} from 'react-intl'; import React from 'react'; import {browserHistory} from 'react-router/es6'; export default class UserItem extends React.Component { constructor(props) { super(props); this.handleMakeMember = this.handleMakeMember.bind(this); this.handleRemoveFromTeam = this.handleRemoveFromTeam.bind(this); this.handleMakeActive = this.handleMakeActive.bind(this); this.handleMakeNotActive = this.handleMakeNotActive.bind(this); this.handleMakeAdmin = this.handleMakeAdmin.bind(this); this.handleMakeSystemAdmin = this.handleMakeSystemAdmin.bind(this); this.handleResetPassword = this.handleResetPassword.bind(this); this.handleResetMfa = this.handleResetMfa.bind(this); this.handleDemote = this.handleDemote.bind(this); this.handleDemoteSubmit = this.handleDemoteSubmit.bind(this); this.handleDemoteCancel = this.handleDemoteCancel.bind(this); this.state = { serverError: null, showDemoteModal: false, user: null, role: null }; } handleMakeMember(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id) { this.handleDemote(this.props.user, ''); } else { Client.updateRoles( this.props.team.id, this.props.user.id, '', () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } } handleRemoveFromTeam() { Client.removeUserFromTeam( this.props.team.id, this.props.user.id, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, true, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeNotActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, false, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeAdmin(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id) { this.handleDemote(this.props.user, 'admin'); } else { Client.updateRoles( this.props.team.id, this.props.user.id, 'admin', () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } } handleMakeSystemAdmin(e) { e.preventDefault(); Client.updateRoles( this.props.team.id, this.props.user.id, 'system_admin', () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleResetPassword(e) { e.preventDefault(); this.props.doPasswordReset(this.props.user); } handleResetMfa(e) { e.preventDefault(); Client.adminResetMfa(this.props.user.id, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleDemote(user, role) { this.setState({ serverError: this.state.serverError, showDemoteModal: true, user, role }); } handleDemoteCancel() { this.setState({ serverError: null, showDemoteModal: false, user: null, role: null }); } handleDemoteSubmit() { Client.updateRoles( this.props.team.id, this.props.user.id, this.state.role, () => { this.props.refreshProfiles(); this.setState({ serverError: null, showDemoteModal: false, user: null, role: null }); const teamUrl = TeamStore.getCurrentTeamUrl(); if (teamUrl) { browserHistory.push(teamUrl); } else { browserHistory.push('/'); } }, (err) => { this.setState({ serverError: err.message }); } ); } render() { let serverError = null; if (this.state.serverError) { serverError = (