You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecurityDialog.tsx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. import React, { PureComponent } from 'react';
  2. import {
  3. Text,
  4. TextStyle,
  5. View,
  6. ViewStyle
  7. } from 'react-native';
  8. import { connect } from 'react-redux';
  9. import { IReduxState, IStore } from '../../../../app/types';
  10. import { IJitsiConference } from '../../../../base/conference/reducer';
  11. import { getSecurityUiConfig } from '../../../../base/config/functions.any';
  12. import { MEETING_PASSWORD_ENABLED } from '../../../../base/flags/constants';
  13. import { getFeatureFlag } from '../../../../base/flags/functions';
  14. import { translate } from '../../../../base/i18n/functions';
  15. import JitsiScreen from '../../../../base/modal/components/JitsiScreen';
  16. import { isLocalParticipantModerator } from '../../../../base/participants/functions';
  17. import Button from '../../../../base/ui/components/native/Button';
  18. import Input from '../../../../base/ui/components/native/Input';
  19. import Switch from '../../../../base/ui/components/native/Switch';
  20. import { BUTTON_TYPES } from '../../../../base/ui/constants.native';
  21. import { copyText } from '../../../../base/util/copyText.native';
  22. import { isInBreakoutRoom } from '../../../../breakout-rooms/functions';
  23. import { toggleLobbyMode } from '../../../../lobby/actions.any';
  24. import { isEnablingLobbyAllowed } from '../../../../lobby/functions';
  25. import {
  26. endRoomLockRequest,
  27. unlockRoom
  28. } from '../../../../room-lock/actions';
  29. import { LOCKED_LOCALLY, LOCKED_REMOTELY } from '../../../../room-lock/constants';
  30. import styles from './styles';
  31. /**
  32. * The style of the {@link TextInput} rendered by {@code SecurityDialog}. As it
  33. * requests the entry of a password, {@code TextInput} automatically correcting
  34. * the entry of the password is a pain to deal with as a user.
  35. */
  36. const _TEXT_INPUT_PROPS = {
  37. autoCapitalize: 'none',
  38. autoCorrect: false
  39. };
  40. /**
  41. * The type of the React {@code Component} props of {@link SecurityDialog}.
  42. */
  43. interface IProps {
  44. /**
  45. * The JitsiConference which requires a password.
  46. */
  47. _conference?: IJitsiConference;
  48. /**
  49. * Whether enabling lobby is allowed or not.
  50. */
  51. _isEnablingLobbyAllowed: boolean;
  52. /**
  53. * Whether the local user is the moderator.
  54. */
  55. _isModerator: boolean;
  56. /**
  57. * Whether lobby mode is enabled or not.
  58. */
  59. _lobbyEnabled: boolean;
  60. /**
  61. * Whether the lobby mode switch is available or not.
  62. */
  63. _lobbyModeSwitchVisible: boolean;
  64. /**
  65. * The value for how the conference is locked (or undefined if not locked)
  66. * as defined by room-lock constants.
  67. */
  68. _locked?: string;
  69. /**
  70. * Checks if the conference room is locked or not.
  71. */
  72. _lockedConference: boolean;
  73. /**
  74. * The current known password for the JitsiConference.
  75. */
  76. _password?: string;
  77. /**
  78. * Number of digits used in the room-lock password.
  79. */
  80. _passwordNumberOfDigits?: number;
  81. /**
  82. * Whether setting a room password is available or not.
  83. */
  84. _roomPasswordControls: boolean;
  85. /**
  86. * Redux store dispatch function.
  87. */
  88. dispatch: IStore['dispatch'];
  89. /**
  90. * Invoked to obtain translated strings.
  91. */
  92. t: Function;
  93. }
  94. /**
  95. * The type of the React {@code Component} state of {@link SecurityDialog}.
  96. */
  97. interface IState {
  98. /**
  99. * State of lobby mode.
  100. */
  101. lobbyEnabled: boolean;
  102. /**
  103. * Password added by the participant for room lock.
  104. */
  105. passwordInputValue: string;
  106. /**
  107. * Shows an input or a message.
  108. */
  109. showElement: boolean;
  110. }
  111. /**
  112. * Component that renders the security options dialog.
  113. *
  114. * @returns {React$Element<any>}
  115. */
  116. class SecurityDialog extends PureComponent<IProps, IState> {
  117. /**
  118. * Instantiates a new {@code SecurityDialog}.
  119. *
  120. * @inheritdoc
  121. */
  122. constructor(props: IProps) {
  123. super(props);
  124. this.state = {
  125. lobbyEnabled: props._lobbyEnabled,
  126. passwordInputValue: '',
  127. showElement: props._locked === LOCKED_LOCALLY || false
  128. };
  129. this._onChangeText = this._onChangeText.bind(this);
  130. this._onCancel = this._onCancel.bind(this);
  131. this._onCopy = this._onCopy.bind(this);
  132. this._onSubmit = this._onSubmit.bind(this);
  133. this._onToggleLobbyMode = this._onToggleLobbyMode.bind(this);
  134. this._onAddPassword = this._onAddPassword.bind(this);
  135. }
  136. /**
  137. * Implements {@code SecurityDialog.render}.
  138. *
  139. * @inheritdoc
  140. */
  141. render() {
  142. return (
  143. <JitsiScreen style = { styles.securityDialogContainer }>
  144. { this._renderLobbyMode() }
  145. { this._renderSetRoomPassword() }
  146. </JitsiScreen>
  147. );
  148. }
  149. /**
  150. * Renders lobby mode.
  151. *
  152. * @returns {ReactElement}
  153. * @private
  154. */
  155. _renderLobbyMode() {
  156. const {
  157. _isEnablingLobbyAllowed,
  158. _lobbyModeSwitchVisible,
  159. t
  160. } = this.props;
  161. if (!_lobbyModeSwitchVisible || !_isEnablingLobbyAllowed) {
  162. return null;
  163. }
  164. return (
  165. <View style = { styles.lobbyModeContainer }>
  166. <View style = { styles.lobbyModeContent } >
  167. <Text style = { styles.lobbyModeText }>
  168. { t('lobby.enableDialogText') }
  169. </Text>
  170. <View style = { styles.lobbyModeSection as ViewStyle }>
  171. <Text style = { styles.lobbyModeLabel as TextStyle } >
  172. { t('lobby.toggleLabel') }
  173. </Text>
  174. <Switch
  175. checked = { this.state.lobbyEnabled }
  176. onChange = { this._onToggleLobbyMode } />
  177. </View>
  178. </View>
  179. </View>
  180. );
  181. }
  182. /**
  183. * Renders setting the password.
  184. *
  185. * @returns {ReactElement}
  186. * @private
  187. */
  188. _renderSetRoomPassword() {
  189. const {
  190. _isModerator,
  191. _locked,
  192. _lockedConference,
  193. _password,
  194. _roomPasswordControls,
  195. t
  196. } = this.props;
  197. const { showElement } = this.state;
  198. let setPasswordControls;
  199. if (!_roomPasswordControls) {
  200. return null;
  201. }
  202. if (_locked && showElement) {
  203. setPasswordControls = (
  204. <>
  205. <Button
  206. accessibilityLabel = 'dialog.Remove'
  207. labelKey = 'dialog.Remove'
  208. labelStyle = { styles.passwordSetupButtonLabel }
  209. onClick = { this._onCancel }
  210. type = { BUTTON_TYPES.TERTIARY } />
  211. {
  212. _password
  213. && <Button
  214. accessibilityLabel = 'dialog.copy'
  215. labelKey = 'dialog.copy'
  216. labelStyle = { styles.passwordSetupButtonLabel }
  217. onClick = { this._onCopy }
  218. type = { BUTTON_TYPES.TERTIARY } />
  219. }
  220. </>
  221. );
  222. } else if (!_lockedConference && showElement) {
  223. setPasswordControls = (
  224. <>
  225. <Button
  226. accessibilityLabel = 'dialog.Cancel'
  227. labelKey = 'dialog.Cancel'
  228. labelStyle = { styles.passwordSetupButtonLabel }
  229. onClick = { this._onCancel }
  230. type = { BUTTON_TYPES.TERTIARY } />
  231. <Button
  232. accessibilityLabel = 'dialog.add'
  233. labelKey = 'dialog.add'
  234. labelStyle = { styles.passwordSetupButtonLabel }
  235. onClick = { this._onSubmit }
  236. type = { BUTTON_TYPES.TERTIARY } />
  237. </>
  238. );
  239. } else if (!_lockedConference && !showElement) {
  240. setPasswordControls = (
  241. <Button
  242. accessibilityLabel = 'info.addPassword'
  243. disabled = { !_isModerator }
  244. labelKey = 'info.addPassword'
  245. labelStyle = { styles.passwordSetupButtonLabel }
  246. onClick = { this._onAddPassword }
  247. type = { BUTTON_TYPES.TERTIARY } />
  248. );
  249. }
  250. if (_locked === LOCKED_REMOTELY) {
  251. if (_isModerator) {
  252. setPasswordControls = (
  253. <View style = { styles.passwordSetRemotelyContainer as ViewStyle }>
  254. <Text style = { styles.passwordSetRemotelyText }>
  255. { t('passwordSetRemotely') }
  256. </Text>
  257. <Button
  258. accessibilityLabel = 'dialog.Remove'
  259. labelKey = 'dialog.Remove'
  260. labelStyle = { styles.passwordSetupButtonLabel }
  261. onClick = { this._onCancel }
  262. type = { BUTTON_TYPES.TERTIARY } />
  263. </View>
  264. );
  265. } else {
  266. setPasswordControls = (
  267. <View style = { styles.passwordSetRemotelyContainer as ViewStyle }>
  268. <Text style = { styles.passwordSetRemotelyTextDisabled }>
  269. { t('passwordSetRemotely') }
  270. </Text>
  271. <Button
  272. accessibilityLabel = 'info.addPassword'
  273. disabled = { !_isModerator }
  274. labelKey = 'info.addPassword'
  275. labelStyle = { styles.passwordSetupButtonLabel }
  276. onClick = { this._onAddPassword }
  277. type = { BUTTON_TYPES.TERTIARY } />
  278. </View>
  279. );
  280. }
  281. }
  282. return (
  283. <View
  284. style = { styles.passwordContainer } >
  285. <Text style = { styles.passwordContainerText }>
  286. { t(_isModerator ? 'security.about' : 'security.aboutReadOnly') }
  287. </Text>
  288. <View
  289. style = {
  290. _locked !== LOCKED_REMOTELY
  291. && styles.passwordContainerControls as ViewStyle
  292. }>
  293. <View>
  294. { this._setRoomPasswordMessage() }
  295. </View>
  296. { _isModerator && setPasswordControls }
  297. </View>
  298. </View>
  299. );
  300. }
  301. /**
  302. * Renders room lock text input/message.
  303. *
  304. * @returns {ReactElement}
  305. * @private
  306. */
  307. _setRoomPasswordMessage() {
  308. let textInputProps: any = _TEXT_INPUT_PROPS;
  309. const {
  310. _isModerator,
  311. _locked,
  312. _password,
  313. _passwordNumberOfDigits,
  314. t
  315. } = this.props;
  316. const { passwordInputValue, showElement } = this.state;
  317. if (_passwordNumberOfDigits) {
  318. textInputProps = {
  319. ...textInputProps,
  320. keyboardType: 'numeric',
  321. maxLength: _passwordNumberOfDigits
  322. };
  323. }
  324. if (!_isModerator) {
  325. return null;
  326. }
  327. if (showElement) {
  328. if (typeof _locked === 'undefined') {
  329. return (
  330. <Input
  331. accessibilityLabel = { t('info.addPassword') }
  332. autoFocus = { true }
  333. clearable = { true }
  334. customStyles = {{ container: styles.customContainer }}
  335. onChange = { this._onChangeText }
  336. placeholder = { t('dialog.password') }
  337. value = { passwordInputValue }
  338. { ...textInputProps } />
  339. );
  340. } else if (_locked) {
  341. if (_locked === LOCKED_LOCALLY && typeof _password !== 'undefined') {
  342. return (
  343. <View style = { styles.savedPasswordContainer as ViewStyle }>
  344. <Text style = { styles.savedPasswordLabel as TextStyle }>
  345. { t('info.password') }
  346. </Text>
  347. <Text style = { styles.savedPassword }>
  348. { _password }
  349. </Text>
  350. </View>
  351. );
  352. }
  353. }
  354. }
  355. }
  356. /**
  357. * Handles the enable-disable lobby mode switch.
  358. *
  359. * @private
  360. * @returns {void}
  361. */
  362. _onToggleLobbyMode() {
  363. const { dispatch } = this.props;
  364. const { lobbyEnabled } = this.state;
  365. this.setState({
  366. lobbyEnabled: !lobbyEnabled
  367. });
  368. dispatch(toggleLobbyMode(!lobbyEnabled));
  369. }
  370. /**
  371. * Callback to be invoked when add password button is pressed.
  372. *
  373. * @returns {void}
  374. */
  375. _onAddPassword() {
  376. const { showElement } = this.state;
  377. this.setState({
  378. showElement: !showElement
  379. });
  380. }
  381. /**
  382. * Verifies input in case only digits are required.
  383. *
  384. * @param {string} passwordInputValue - The value of the password
  385. * text input.
  386. * @private
  387. * @returns {boolean} False when the value is not valid and True otherwise.
  388. */
  389. _validateInputValue(passwordInputValue: string) {
  390. const { _passwordNumberOfDigits } = this.props;
  391. // we want only digits,
  392. // but both number-pad and numeric add ',' and '.' as symbols
  393. if (_passwordNumberOfDigits
  394. && passwordInputValue.length > 0
  395. && !/^\d+$/.test(passwordInputValue)) {
  396. return false;
  397. }
  398. return true;
  399. }
  400. /**
  401. * Callback to be invoked when the text in the field changes.
  402. *
  403. * @param {string} passwordInputValue - The value of password input.
  404. * @returns {void}
  405. */
  406. _onChangeText(passwordInputValue: string) {
  407. if (!this._validateInputValue(passwordInputValue)) {
  408. return;
  409. }
  410. this.setState({
  411. passwordInputValue
  412. });
  413. }
  414. /**
  415. * Cancels value typed in text input.
  416. *
  417. * @returns {void}
  418. */
  419. _onCancel() {
  420. this.setState({
  421. passwordInputValue: '',
  422. showElement: false
  423. });
  424. this.props.dispatch(unlockRoom());
  425. }
  426. /**
  427. * Copies room password.
  428. *
  429. * @returns {void}
  430. */
  431. _onCopy() {
  432. const { passwordInputValue } = this.state;
  433. copyText(passwordInputValue);
  434. }
  435. /**
  436. * Submits value typed in text input.
  437. *
  438. * @returns {void}
  439. */
  440. _onSubmit() {
  441. const {
  442. _conference,
  443. dispatch
  444. } = this.props;
  445. const { passwordInputValue } = this.state;
  446. _conference && dispatch(endRoomLockRequest(_conference, passwordInputValue));
  447. }
  448. }
  449. /**
  450. * Maps part of the Redux state to the props of this component.
  451. *
  452. * @param {Object} state - The Redux state.
  453. * @returns {IProps}
  454. */
  455. function _mapStateToProps(state: IReduxState) {
  456. const { conference, locked, password } = state['features/base/conference'];
  457. const { disableLobbyPassword, hideLobbyButton } = getSecurityUiConfig(state);
  458. const { lobbyEnabled } = state['features/lobby'];
  459. const { roomPasswordNumberOfDigits } = state['features/base/config'];
  460. const lobbySupported = conference?.isLobbySupported();
  461. const visible = getFeatureFlag(state, MEETING_PASSWORD_ENABLED, true);
  462. return {
  463. _conference: conference,
  464. _isEnablingLobbyAllowed: isEnablingLobbyAllowed(state),
  465. _isModerator: isLocalParticipantModerator(state),
  466. _lobbyEnabled: lobbyEnabled,
  467. _lobbyModeSwitchVisible:
  468. lobbySupported && isLocalParticipantModerator(state) && !hideLobbyButton && !isInBreakoutRoom(state),
  469. _locked: locked,
  470. _lockedConference: Boolean(conference && locked),
  471. _password: password,
  472. _passwordNumberOfDigits: roomPasswordNumberOfDigits,
  473. _roomPasswordControls: visible && !disableLobbyPassword
  474. };
  475. }
  476. export default translate(connect(_mapStateToProps)(SecurityDialog));