SecuritySetting.jsx 18 KB

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