// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import ConfirmModal from '../confirm_modal.jsx'; import UserStore from 'stores/user_store.jsx'; import TeamStore from 'stores/team_store.jsx'; import Client from 'client/web_client.jsx'; import Constants from 'utils/constants.jsx'; import * as Utils from 'utils/utils.jsx'; import * as AsyncClient from 'utils/async_client.jsx'; import {updateUserRoles} from 'actions/user_actions.jsx'; import {FormattedMessage} from 'react-intl'; import React from 'react'; export default class AdminTeamMembersDropdown 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.handleMakeTeamAdmin = this.handleMakeTeamAdmin.bind(this); this.handleMakeSystemAdmin = this.handleMakeSystemAdmin.bind(this); this.handleResetPassword = this.handleResetPassword.bind(this); this.handleResetMfa = this.handleResetMfa.bind(this); this.handleDemoteSystemAdmin = this.handleDemoteSystemAdmin.bind(this); this.handleDemoteSubmit = this.handleDemoteSubmit.bind(this); this.handleDemoteCancel = this.handleDemoteCancel.bind(this); this.doMakeMember = this.doMakeMember.bind(this); this.doMakeTeamAdmin = this.doMakeTeamAdmin.bind(this); this.state = { serverError: null, showDemoteModal: false, user: null, role: null }; } doMakeMember() { updateUserRoles( this.props.user.id, 'system_user', null, (err) => { this.setState({serverError: err.message}); } ); Client.updateTeamMemberRoles( this.props.teamMember.team_id, this.props.user.id, 'team_user', () => { AsyncClient.getTeamMember(this.props.teamMember.team_id, this.props.user.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeMember(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id && me.roles.includes('system_admin')) { this.handleDemoteSystemAdmin(this.props.user, 'member'); } else { this.doMakeMember(); } } handleRemoveFromTeam() { Client.removeUserFromTeam( this.props.teamMember.team_id, this.props.user.id, () => { AsyncClient.getTeamStats(this.props.teamMember.team_id); UserStore.removeProfileFromTeam(this.props.teamMember.team_id, this.props.user.id); UserStore.emitInTeamChange(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, true, () => { AsyncClient.getUser(this.props.user.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeNotActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, false, () => { AsyncClient.getUser(this.props.user.id); }, (err) => { this.setState({serverError: err.message}); } ); } doMakeTeamAdmin() { Client.updateTeamMemberRoles( this.props.teamMember.team_id, this.props.user.id, 'team_user team_admin', () => { AsyncClient.getTeamMember(this.props.teamMember.team_id, this.props.user.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeTeamAdmin(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id && me.roles.includes('system_admin')) { this.handleDemoteSystemAdmin(this.props.user, 'teamadmin'); } else { this.doMakeTeamAdmin(); } } handleMakeSystemAdmin(e) { e.preventDefault(); updateUserRoles( this.props.user.id, 'system_user system_admin', null, (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, () => { AsyncClient.getUser(this.props.user.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleDemoteSystemAdmin(user, role) { this.setState({ serverError: this.state.serverError, showDemoteModal: true, user, role }); } handleDemoteCancel() { this.setState({ serverError: null, showDemoteModal: false, user: null, role: null }); } handleDemoteSubmit() { if (this.state.role === 'member') { this.doMakeMember(); } else { this.doMakeTeamAdmin(); } const teamUrl = TeamStore.getCurrentTeamUrl(); if (teamUrl) { // the channel is added to the URL cause endless loading not being fully fixed window.location.href = teamUrl + '/channels/town-square'; } else { window.location.href = '/'; } } render() { let serverError = null; if (this.state.serverError) { serverError = (
); } const teamMember = this.props.teamMember; const user = this.props.user; if (!user || !teamMember) { return
; } let currentRoles = ( ); if (teamMember.roles.length > 0 && Utils.isAdmin(teamMember.roles)) { currentRoles = ( ); } if (user.roles.length > 0 && Utils.isSystemAdmin(user.roles)) { currentRoles = ( ); } const me = UserStore.getCurrentUser(); let showMakeMember = Utils.isAdmin(teamMember.roles) || Utils.isSystemAdmin(user.roles); let showMakeAdmin = !Utils.isAdmin(teamMember.roles) && !Utils.isSystemAdmin(user.roles); let showMakeSystemAdmin = !Utils.isSystemAdmin(user.roles); let showMakeActive = false; let showMakeNotActive = !Utils.isSystemAdmin(user.roles); const mfaEnabled = global.window.mm_license.IsLicensed === 'true' && global.window.mm_license.MFA === 'true' && global.window.mm_config.EnableMultifactorAuthentication === 'true'; const showMfaReset = mfaEnabled && user.mfa_active; if (user.delete_at > 0) { currentRoles = ( ); showMakeMember = false; showMakeAdmin = false; showMakeSystemAdmin = false; showMakeActive = true; showMakeNotActive = false; } let disableActivationToggle = false; if (user.auth_service === Constants.LDAP_SERVICE) { disableActivationToggle = true; } let makeSystemAdmin = null; if (showMakeSystemAdmin) { makeSystemAdmin = (
  • ); } let makeAdmin = null; if (showMakeAdmin) { makeAdmin = (
  • ); } let makeMember = null; if (showMakeMember) { makeMember = (
  • ); } let removeFromTeam = null; if (this.props.user.id !== me.id) { removeFromTeam = (
  • ); } let menuClass = ''; if (disableActivationToggle) { menuClass = 'disabled'; } let makeActive = null; if (showMakeActive) { makeActive = (
  • ); } let makeNotActive = null; if (showMakeNotActive) { makeNotActive = (
  • ); } let mfaReset = null; if (showMfaReset) { mfaReset = (
  • ); } let passwordReset; if (user.auth_service) { passwordReset = (
  • ); } else { passwordReset = (
  • ); } if (global.window.mm_config.EnableSignInWithEmail !== 'true') { passwordReset = null; } let makeDemoteModal = null; if (this.props.user.id === me.id) { const title = ( ); const message = (


    {serverError}
    ); const confirmButton = ( ); makeDemoteModal = ( ); } let displayedName = Utils.getDisplayName(user); if (displayedName !== user.username) { displayedName += ' (@' + user.username + ')'; } return (
    {currentRoles}
      {removeFromTeam} {makeAdmin} {makeMember} {makeActive} {makeNotActive} {makeSystemAdmin} {mfaReset} {passwordReset}
    {makeDemoteModal} {serverError}
    ); } } AdminTeamMembersDropdown.propTypes = { user: React.PropTypes.object.isRequired, teamMember: React.PropTypes.object.isRequired, doPasswordReset: React.PropTypes.func.isRequired };