SecuritySetting.jsx 17 KB

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