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

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