/* Copyright 2019 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React from 'react'; import PropTypes from 'prop-types'; import {_t} from "../../../../languageHandler"; import MatrixClientPeg from "../../../../MatrixClientPeg"; import sdk from "../../../../index"; import LabelledToggleSwitch from "../../elements/LabelledToggleSwitch"; import {SettingLevel} from "../../../../settings/SettingsStore"; export default class SecurityRoomSettingsTab extends React.Component { static propTypes = { roomId: PropTypes.string.isRequired, }; componentWillMount(): void { MatrixClientPeg.get().on("RoomState.events", this._onStateEvent); } componentWillUnmount(): void { MatrixClientPeg.get().removeListener("RoomState.events", this._onStateEvent); } _onStateEvent = (e) => { const refreshWhenTypes = [ 'm.room.join_rules', 'm.room.guest_access', 'm.room.history_visibility', 'm.room.encryption', ]; if (refreshWhenTypes.includes(e.getType())) this.forceUpdate(); }; _onEncryptionChange = (e) => { MatrixClientPeg.get().sendStateEvent( this.props.roomId, "m.room.encryption", { algorithm: "m.megolm.v1.aes-sha2" }, ); }; _fixGuestAccess = (e) => { e.preventDefault(); e.stopPropagation(); const client = MatrixClientPeg.get(); client.sendStateEvent(this.props.roomId, "m.room.join_rules", {join_rule: "invite"}, ""); client.sendStateEvent(this.props.roomId, "m.room.guest_access", {guest_access: "can_join"}, ""); }; _onRoomAccessRadioToggle = (ev) => { // join_rule // INVITE | PUBLIC // ----------------------+---------------- // guest CAN_JOIN | inv_only | pub_with_guest // access ----------------------+---------------- // FORBIDDEN | inv_only | pub_no_guest // ----------------------+---------------- // we always set guests can_join here as it makes no sense to have // an invite-only room that guests can't join. If you explicitly // invite them, you clearly want them to join, whether they're a // guest or not. In practice, guest_access should probably have // been implemented as part of the join_rules enum. let joinRule = "invite"; let guestAccess = "can_join"; switch (ev.target.value) { case "invite_only": // no change - use defaults above break; case "public_no_guests": joinRule = "public"; guestAccess = "forbidden"; break; case "public_with_guests": joinRule = "public"; guestAccess = "can_join"; break; } const client = MatrixClientPeg.get(); client.sendStateEvent(this.props.roomId, "m.room.join_rules", {join_rule: joinRule}, ""); client.sendStateEvent(this.props.roomId, "m.room.guest_access", {guest_access: guestAccess}, ""); }; _onHistoryRadioToggle = (ev) => { MatrixClientPeg.get().sendStateEvent(this.props.roomId, "m.room.history_visibility", { history_visibility: ev.target.value, }, ""); }; _renderRoomAccess() { const client = MatrixClientPeg.get(); const room = client.getRoom(this.props.roomId); const joinRule = room.currentState.getStateEvents("m.room.join_rules", "").getContent()['join_rule']; const guestAccess = room.currentState.getStateEvents("m.room.guest_access", "").getContent()['guest_access']; const aliasEvents = room.currentState.getStateEvents("m.room.aliases") || []; const hasAliases = aliasEvents.includes((ev) => (ev.getContent().aliases || []).length); const canChangeAccess = room.currentState.mayClientSendStateEvent("m.room.join_rules", client) && room.currentState.mayClientSendStateEvent("m.room.guest_access", client); let guestWarning = null; if (joinRule !== 'public' && guestAccess === 'forbidden') { guestWarning = (
{_t("Guests cannot join this room even if explicitly invited.")}  {_t("Click here to fix")}
); } let aliasWarning = null; if (joinRule === 'public' && !hasAliases) { aliasWarning = (
{_t("To link to this room, please add an alias.")}
); } return (
{guestWarning} {aliasWarning}
); } _renderHistory() { const client = MatrixClientPeg.get(); const room = client.getRoom(this.props.roomId); const state = room.currentState; const history = state.getStateEvents("m.room.history_visibility", "").getContent()['history_visibility']; const canChangeHistory = state.mayClientSendStateEvent('m.room.history_visibility', client); return (
{_t('Changes to who can read history will only apply to future messages in this room. ' + 'The visibility of existing history will be unchanged.')}
); } render() { const SettingsFlag = sdk.getComponent("elements.SettingsFlag"); const client = MatrixClientPeg.get(); const room = client.getRoom(this.props.roomId); const isEncrypted = client.isRoomEncrypted(this.props.roomId); const hasEncryptionPermission = room.currentState.mayClientSendStateEvent("m.room.encryption", client); const canEnableEncryption = !isEncrypted && hasEncryptionPermission; let encryptionSettings = null; if (isEncrypted) { encryptionSettings = ; } return (
{_t("Security & Privacy")}
{_t("Encryption")}
{_t("Once enabled, encryption cannot be disabled.")}
{encryptionSettings}
{_t("Who can access this room?")}
{this._renderRoomAccess()}
{_t("Who can read history?")}
{this._renderHistory()}
); } }