SecuritySetting.jsx 16 KB

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