SecuritySetting.jsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* eslint-disable react/no-danger */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { Collapse } from 'reactstrap';
  5. import { withTranslation } from 'react-i18next';
  6. import { validateDeleteConfigs, prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config';
  7. import { withUnstatedContainers } from '../../UnstatedUtils';
  8. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  9. import { PageDeleteConfigValue } from '~/interfaces/page-delete-config';
  10. import AppContainer from '~/client/services/AppContainer';
  11. import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
  12. // used as the prefix of translation
  13. const DeletionTypeForT = Object.freeze({
  14. Deletion: 'deletion',
  15. CompleteDeletion: 'complete_deletion',
  16. RecursiveDeletion: 'recursive_deletion',
  17. RecursiveCompleteDeletion: 'recursive_complete_deletion',
  18. });
  19. const DeletionType = Object.freeze({
  20. Deletion: 'deletion',
  21. CompleteDeletion: 'completeDeletion',
  22. RecursiveDeletion: 'recursiveDeletion',
  23. RecursiveCompleteDeletion: 'recursiveCompleteDeletion',
  24. });
  25. const getDeletionTypeForT = (deletionType) => {
  26. switch (deletionType) {
  27. case DeletionType.Deletion:
  28. return DeletionTypeForT.Deletion;
  29. case DeletionType.RecursiveDeletion:
  30. return DeletionTypeForT.RecursiveDeletion;
  31. case DeletionType.CompleteDeletion:
  32. return DeletionTypeForT.CompleteDeletion;
  33. case DeletionType.RecursiveCompleteDeletion:
  34. return DeletionTypeForT.RecursiveCompleteDeletion;
  35. }
  36. };
  37. const getDeleteConfigValueForT = (DeleteConfigValue) => {
  38. switch (DeleteConfigValue) {
  39. case PageDeleteConfigValue.Anyone:
  40. case null:
  41. return 'security_setting.anyone';
  42. case PageDeleteConfigValue.Inherit:
  43. return 'security_setting.inherit';
  44. case PageDeleteConfigValue.AdminOnly:
  45. return 'security_setting.admin_only';
  46. case PageDeleteConfigValue.AdminAndAuthor:
  47. return 'security_setting.admin_and_author';
  48. }
  49. };
  50. /**
  51. * Return true if "deletionType" is DeletionType.RecursiveDeletion or DeletionType.RecursiveCompleteDeletion.
  52. * @param deletionType Deletion type
  53. * @returns boolean
  54. */
  55. const isRecursiveDeletion = (deletionType) => {
  56. return deletionType === DeletionType.RecursiveDeletion || deletionType === DeletionType.RecursiveCompleteDeletion;
  57. };
  58. /**
  59. * Return true if "deletionType" is DeletionType.Deletion or DeletionType.RecursiveDeletion.
  60. * @param deletionType Deletion type
  61. * @returns boolean
  62. */
  63. const isTypeDeletion = (deletionType) => {
  64. return deletionType === DeletionType.Deletion || deletionType === DeletionType.RecursiveDeletion;
  65. };
  66. class SecuritySetting extends React.Component {
  67. constructor(props) {
  68. super(props);
  69. // functions
  70. this.putSecuritySetting = this.putSecuritySetting.bind(this);
  71. this.getRecursiveDeletionConfigState = this.getRecursiveDeletionConfigState.bind(this);
  72. this.previousPageRecursiveAuthorityState = this.previousPageRecursiveAuthorityState.bind(this);
  73. this.setPagePreviousRecursiveAuthorityState = this.setPagePreviousRecursiveAuthorityState.bind(this);
  74. this.expantDeleteOptionsState = this.expantDeleteOptionsState.bind(this);
  75. this.setExpantOtherDeleteOptionsState = this.setExpantOtherDeleteOptionsState.bind(this);
  76. this.setDeletionConfigState = this.setDeletionConfigState.bind(this);
  77. // render
  78. this.renderPageDeletePermission = this.renderPageDeletePermission.bind(this);
  79. this.renderPageDeletePermissionDropdown = this.renderPageDeletePermissionDropdown.bind(this);
  80. }
  81. async putSecuritySetting() {
  82. const { t, adminGeneralSecurityContainer } = this.props;
  83. try {
  84. await adminGeneralSecurityContainer.updateGeneralSecuritySetting();
  85. toastSuccess(t('security_setting.updated_general_security_setting'));
  86. }
  87. catch (err) {
  88. toastError(err);
  89. }
  90. }
  91. getRecursiveDeletionConfigState(deletionType) {
  92. const { adminGeneralSecurityContainer } = this.props;
  93. if (isTypeDeletion(deletionType)) {
  94. return [
  95. adminGeneralSecurityContainer.state.currentPageRecursiveDeletionAuthority,
  96. adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority,
  97. ];
  98. }
  99. return [
  100. adminGeneralSecurityContainer.state.currentPageRecursiveCompleteDeletionAuthority,
  101. adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority,
  102. ];
  103. }
  104. previousPageRecursiveAuthorityState(deletionType) {
  105. const { adminGeneralSecurityContainer } = this.props;
  106. return isTypeDeletion(deletionType)
  107. ? adminGeneralSecurityContainer.state.previousPageRecursiveDeletionAuthority
  108. : adminGeneralSecurityContainer.state.previousPageRecursiveCompleteDeletionAuthority;
  109. }
  110. setPagePreviousRecursiveAuthorityState(deletionType, previousState) {
  111. const { adminGeneralSecurityContainer } = this.props;
  112. if (isTypeDeletion(deletionType)) {
  113. adminGeneralSecurityContainer.changePreviousPageRecursiveDeletionAuthority(previousState);
  114. return;
  115. }
  116. adminGeneralSecurityContainer.changePreviousPageRecursiveCompleteDeletionAuthority(previousState);
  117. }
  118. expantDeleteOptionsState(deletionType) {
  119. const { adminGeneralSecurityContainer } = this.props;
  120. return isTypeDeletion(deletionType)
  121. ? adminGeneralSecurityContainer.state.expandOtherOptionsForDeletion
  122. : adminGeneralSecurityContainer.state.expandOtherOptionsForCompleteDeletion;
  123. }
  124. setExpantOtherDeleteOptionsState(deletionType, bool) {
  125. const { adminGeneralSecurityContainer } = this.props;
  126. if (isTypeDeletion(deletionType)) {
  127. adminGeneralSecurityContainer.switchExpandOtherOptionsForDeletion(bool);
  128. return;
  129. }
  130. adminGeneralSecurityContainer.switchExpandOtherOptionsForCompleteDeletion(bool);
  131. return;
  132. }
  133. /**
  134. * Force update deletion config for recursive operation when the deletion config for general operation is updated.
  135. * @param deletionType Deletion type
  136. */
  137. setDeletionConfigState(newState, setState, deletionType) {
  138. setState(newState);
  139. if (this.previousPageRecursiveAuthorityState(deletionType) !== null) {
  140. this.setPagePreviousRecursiveAuthorityState(deletionType, null);
  141. }
  142. if (isRecursiveDeletion(deletionType)) {
  143. return;
  144. }
  145. const [recursiveState, setRecursiveState] = this.getRecursiveDeletionConfigState(deletionType);
  146. const calculableValue = prepareDeleteConfigValuesForCalc(newState, recursiveState);
  147. const shouldForceUpdate = !validateDeleteConfigs(calculableValue[0], calculableValue[1]);
  148. if (shouldForceUpdate) {
  149. setRecursiveState(newState);
  150. this.setPagePreviousRecursiveAuthorityState(deletionType, recursiveState);
  151. this.setExpantOtherDeleteOptionsState(deletionType, true);
  152. }
  153. return;
  154. }
  155. renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled) {
  156. const { t } = this.props;
  157. return (
  158. <div className="dropdown">
  159. <button
  160. className="btn btn-outline-secondary dropdown-toggle text-right"
  161. type="button"
  162. id="dropdownMenuButton"
  163. data-toggle="dropdown"
  164. aria-haspopup="true"
  165. aria-expanded="true"
  166. >
  167. <span className="float-left">
  168. {t(getDeleteConfigValueForT(currentState))}
  169. </span>
  170. </button>
  171. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  172. {
  173. isRecursiveDeletion(deletionType)
  174. ? (
  175. <button
  176. className="dropdown-item"
  177. type="button"
  178. onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.Inherit, setState, deletionType) }}
  179. >
  180. {t('security_setting.inherit')}
  181. </button>
  182. )
  183. : (
  184. <button
  185. className="dropdown-item"
  186. type="button"
  187. onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.Anyone, setState, deletionType) }}
  188. >
  189. {t('security_setting.anyone')}
  190. </button>
  191. )
  192. }
  193. <button
  194. className={`dropdown-item ${isButtonDisabled ? 'disabled' : ''}`}
  195. type="button"
  196. onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.AdminAndAuthor, setState, deletionType) }}
  197. >
  198. {t('security_setting.admin_and_author')}
  199. </button>
  200. <button
  201. className="dropdown-item"
  202. type="button"
  203. onClick={() => { this.setDeletionConfigState(PageDeleteConfigValue.AdminOnly, setState, deletionType) }}
  204. >
  205. {t('security_setting.admin_only')}
  206. </button>
  207. </div>
  208. <p className="form-text text-muted small">
  209. {t(`security_setting.${getDeletionTypeForT(deletionType)}_explain`)}
  210. </p>
  211. </div>
  212. );
  213. }
  214. renderPageDeletePermission(currentState, setState, deletionType, isButtonDisabled) {
  215. const { t } = this.props;
  216. const expantDeleteOptionsState = this.expantDeleteOptionsState(deletionType);
  217. return (
  218. <div key={`page-delete-permission-dropdown-${deletionType}`} className="row">
  219. <div className="col-md-3 text-md-right">
  220. {!isRecursiveDeletion(deletionType) && isTypeDeletion(deletionType) && (
  221. <strong>{t('security_setting.page_delete')}</strong>
  222. )}
  223. {!isRecursiveDeletion(deletionType) && !isTypeDeletion(deletionType) && (
  224. <strong>{t('security_setting.page_delete_completely')}</strong>
  225. )}
  226. </div>
  227. <div className="col-md-6">
  228. {
  229. !isRecursiveDeletion(deletionType)
  230. ? (
  231. <>{this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}</>
  232. )
  233. : (
  234. <>
  235. <button
  236. type="button"
  237. className="btn btn-link p-0 mb-4"
  238. aria-expanded="false"
  239. onClick={() => this.setExpantOtherDeleteOptionsState(deletionType, !expantDeleteOptionsState)}
  240. >
  241. <i className={`fa fa-fw fa-arrow-right ${expantDeleteOptionsState ? 'fa-rotate-90' : ''}`}></i>
  242. { t('security_setting.other_options') }
  243. </button>
  244. <Collapse isOpen={expantDeleteOptionsState}>
  245. <div className="pb-4">
  246. <p className="card well">
  247. <span className="text-warning">
  248. <i className="icon-info"></i>
  249. {/* eslint-disable-next-line react/no-danger */}
  250. <span dangerouslySetInnerHTML={{ __html: t('security_setting.page_delete_rights_caution') }} />
  251. </span>
  252. </p>
  253. { this.previousPageRecursiveAuthorityState(deletionType) !== null && (
  254. <div className="mb-3">
  255. <strong>
  256. {t('security_setting.forced_update_desc')}
  257. </strong>
  258. <code>
  259. {t(getDeleteConfigValueForT(this.previousPageRecursiveAuthorityState(deletionType)))}
  260. </code>
  261. </div>
  262. )}
  263. {this.renderPageDeletePermissionDropdown(currentState, setState, deletionType, isButtonDisabled)}
  264. </div>
  265. </Collapse>
  266. </>
  267. )
  268. }
  269. </div>
  270. </div>
  271. );
  272. }
  273. render() {
  274. const { t, adminGeneralSecurityContainer } = this.props;
  275. const {
  276. currentRestrictGuestMode, currentPageDeletionAuthority, currentPageCompleteDeletionAuthority,
  277. currentPageRecursiveDeletionAuthority, currentPageRecursiveCompleteDeletionAuthority,
  278. } = adminGeneralSecurityContainer.state;
  279. const isButtonDisabledForDeletion = !validateDeleteConfigs(
  280. adminGeneralSecurityContainer.state.currentPageDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor,
  281. );
  282. const isButtonDisabledForCompleteDeletion = !validateDeleteConfigs(
  283. adminGeneralSecurityContainer.state.currentPageCompleteDeletionAuthority, PageDeleteConfigValue.AdminAndAuthor,
  284. );
  285. return (
  286. <React.Fragment>
  287. <h2 className="alert-anchor border-bottom">
  288. {t('security_settings')}
  289. </h2>
  290. {adminGeneralSecurityContainer.retrieveError != null && (
  291. <div className="alert alert-danger">
  292. <p>{t('Error occurred')} : {adminGeneralSecurityContainer.retrieveError}</p>
  293. </div>
  294. )}
  295. <h4 className="mt-4">{ t('security_setting.page_list_and_search_results') }</h4>
  296. <div className="row justify-content-md-center">
  297. <table className="table table-bordered col-lg-9 mb-5">
  298. <thead>
  299. <tr>
  300. <th scope="col">{ t('scope_of_page_disclosure') }</th>
  301. <th scope="col">{ t('set_point') }</th>
  302. </tr>
  303. </thead>
  304. <tbody>
  305. <tr>
  306. <th scope="row">{ t('Public') }</th>
  307. <td>{ t('always_displayed') }</td>
  308. </tr>
  309. <tr>
  310. <th scope="row">{ t('Anyone with the link') }</th>
  311. <td>{ t('always_hidden') }</td>
  312. </tr>
  313. <tr>
  314. <th scope="row">{ t('Only me') }</th>
  315. <td>
  316. <div className="custom-control custom-switch custom-checkbox-success">
  317. <input
  318. type="checkbox"
  319. className="custom-control-input"
  320. id="isShowRestrictedByOwner"
  321. checked={adminGeneralSecurityContainer.state.isShowRestrictedByOwner}
  322. onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByOwner() }}
  323. />
  324. <label className="custom-control-label" htmlFor="isShowRestrictedByOwner">
  325. {t('displayed_or_hidden')}
  326. </label>
  327. </div>
  328. </td>
  329. </tr>
  330. <tr>
  331. <th scope="row">{ t('Only inside the group') }</th>
  332. <td>
  333. <div className="custom-control custom-switch custom-checkbox-success">
  334. <input
  335. type="checkbox"
  336. className="custom-control-input"
  337. id="isShowRestrictedByGroup"
  338. checked={adminGeneralSecurityContainer.state.isShowRestrictedByGroup}
  339. onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByGroup() }}
  340. />
  341. <label className="custom-control-label" htmlFor="isShowRestrictedByGroup">
  342. {t('displayed_or_hidden')}
  343. </label>
  344. </div>
  345. </td>
  346. </tr>
  347. </tbody>
  348. </table>
  349. </div>
  350. <h4>{t('security_setting.page_access_rights')}</h4>
  351. <div className="row mb-4">
  352. <div className="col-md-3 text-md-right py-2">
  353. <strong>{t('security_setting.Guest Users Access')}</strong>
  354. </div>
  355. <div className="col-md-9">
  356. <div className="dropdown">
  357. <button
  358. className={`btn btn-outline-secondary dropdown-toggle text-right col-12
  359. col-md-auto ${adminGeneralSecurityContainer.isWikiModeForced && 'disabled'}`}
  360. type="button"
  361. id="dropdownMenuButton"
  362. data-toggle="dropdown"
  363. aria-haspopup="true"
  364. aria-expanded="true"
  365. >
  366. <span className="float-left">
  367. {currentRestrictGuestMode === 'Deny' && t('security_setting.guest_mode.deny')}
  368. {currentRestrictGuestMode === 'Readonly' && t('security_setting.guest_mode.readonly')}
  369. </span>
  370. </button>
  371. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  372. <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Deny') }}>
  373. {t('security_setting.guest_mode.deny')}
  374. </button>
  375. <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Readonly') }}>
  376. {t('security_setting.guest_mode.readonly')}
  377. </button>
  378. </div>
  379. </div>
  380. {adminGeneralSecurityContainer.isWikiModeForced && (
  381. <p className="alert alert-warning mt-2 col-6">
  382. <i className="icon-exclamation icon-fw">
  383. </i><b>FIXED</b><br />
  384. <b
  385. dangerouslySetInnerHTML={{
  386. __html: t('security_setting.Fixed by env var',
  387. { key: 'FORCE_WIKI_MODE', value: adminGeneralSecurityContainer.state.wikiMode }),
  388. }}
  389. />
  390. </p>
  391. )}
  392. </div>
  393. </div>
  394. <h4>{t('security_setting.page_delete_rights')}</h4>
  395. {/* Render PageDeletePermission */}
  396. {
  397. [
  398. [currentPageDeletionAuthority, adminGeneralSecurityContainer.changePageDeletionAuthority, DeletionType.Deletion, false],
  399. // eslint-disable-next-line max-len
  400. [currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, DeletionType.RecursiveDeletion, isButtonDisabledForDeletion],
  401. ].map(arr => this.renderPageDeletePermission(arr[0], arr[1], arr[2], arr[3]))
  402. }
  403. {
  404. [
  405. [currentPageCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageCompleteDeletionAuthority, DeletionType.CompleteDeletion, false],
  406. // eslint-disable-next-line max-len
  407. [currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, DeletionType.RecursiveCompleteDeletion, isButtonDisabledForCompleteDeletion],
  408. ].map(arr => this.renderPageDeletePermission(arr[0], arr[1], arr[2], arr[3]))
  409. }
  410. <h4>{t('security_setting.session')}</h4>
  411. <div className="form-group row">
  412. <label className="text-left text-md-right col-md-3 col-form-label">{t('security_setting.max_age')}</label>
  413. <div className="col-md-6">
  414. <input
  415. className="form-control col-md-3"
  416. type="text"
  417. defaultValue={adminGeneralSecurityContainer.state.sessionMaxAge || ''}
  418. onChange={(e) => {
  419. adminGeneralSecurityContainer.setSessionMaxAge(e.target.value);
  420. }}
  421. placeholder="2592000000"
  422. />
  423. {/* eslint-disable-next-line react/no-danger */}
  424. <p className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('security_setting.max_age_desc') }} />
  425. <p className="card well">
  426. <span className="text-warning">
  427. <i className="icon-info"></i> {t('security_setting.max_age_caution')}
  428. </span>
  429. </p>
  430. </div>
  431. </div>
  432. <div className="row my-3">
  433. <div className="text-center text-md-left offset-md-3 col-md-5">
  434. <button type="button" className="btn btn-primary" disabled={adminGeneralSecurityContainer.retrieveError != null} onClick={this.putSecuritySetting}>
  435. {t('Update')}
  436. </button>
  437. </div>
  438. </div>
  439. </React.Fragment>
  440. );
  441. }
  442. }
  443. SecuritySetting.propTypes = {
  444. t: PropTypes.func.isRequired, // i18next
  445. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  446. csrf: PropTypes.string,
  447. adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
  448. };
  449. const SecuritySettingWrapper = withUnstatedContainers(SecuritySetting, [AppContainer, AdminGeneralSecurityContainer]);
  450. export default withTranslation()(SecuritySettingWrapper);