SecuritySetting.jsx 18 KB

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