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.js 16KB

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