SecuritySetting.jsx 20 KB

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