SecuritySetting.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 { withUnstatedContainers } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  7. import { PageDeleteConfigValue } from '~/interfaces/page-delete-config';
  8. import AppContainer from '~/client/services/AppContainer';
  9. import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
  10. // used as the prefix of translation
  11. const DeletionType = Object.freeze({
  12. Deletion: 'deletion',
  13. CompleteDeletion: 'complete_deletion',
  14. RecursiveDeletion: 'recursive_deletion',
  15. RecursiveCompleteDeletion: 'recursive_complete_deletion',
  16. });
  17. class SecuritySetting extends React.Component {
  18. constructor(props) {
  19. super(props);
  20. this.putSecuritySetting = this.putSecuritySetting.bind(this);
  21. this.renderPageDeletePermissionDropdown = this.renderPageDeletePermissionDropdown.bind(this);
  22. }
  23. async putSecuritySetting() {
  24. const { t, adminGeneralSecurityContainer } = this.props;
  25. try {
  26. await adminGeneralSecurityContainer.updateGeneralSecuritySetting();
  27. toastSuccess(t('security_setting.updated_general_security_setting'));
  28. }
  29. catch (err) {
  30. toastError(err);
  31. }
  32. }
  33. renderPageDeletePermissionDropdown(currentState, setState, deletionType, t) {
  34. const isRecursiveDeletion = deletionType === DeletionType.RecursiveDeletion || deletionType === DeletionType.RecursiveCompleteDeletion;
  35. return (
  36. <div className="row mb-4">
  37. <div className="col-md-3 text-md-right mb-2">
  38. <strong>{t(`security_setting.${deletionType}`)}</strong>
  39. </div>
  40. <div className="col-md-6">
  41. <div className="dropdown">
  42. <button
  43. className="btn btn-outline-secondary dropdown-toggle text-right col-12 col-md-auto"
  44. type="button"
  45. id="dropdownMenuButton"
  46. data-toggle="dropdown"
  47. aria-haspopup="true"
  48. aria-expanded="true"
  49. >
  50. <span className="float-left">
  51. {currentState === PageDeleteConfigValue.Inherit && t('security_setting.inherit')}
  52. {(currentState === PageDeleteConfigValue.Anyone || currentState == null)
  53. && t('security_setting.anyone')}
  54. {currentState === PageDeleteConfigValue.AdminOnly && t('security_setting.admin_only')}
  55. {currentState === PageDeleteConfigValue.AdminAndAuthor && t('security_setting.admin_and_author')}
  56. </span>
  57. </button>
  58. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  59. {
  60. isRecursiveDeletion
  61. ? (
  62. <button
  63. className="dropdown-item"
  64. type="button"
  65. onClick={() => { setState(PageDeleteConfigValue.Inherit) }}
  66. >
  67. {t('security_setting.inherit')}
  68. </button>
  69. )
  70. : (
  71. <button
  72. className="dropdown-item"
  73. type="button"
  74. onClick={() => { setState(PageDeleteConfigValue.Anyone) }}
  75. >
  76. {t('security_setting.anyone')}
  77. </button>
  78. )
  79. }
  80. <button
  81. className="dropdown-item"
  82. type="button"
  83. onClick={() => { setState(PageDeleteConfigValue.AdminOnly) }}
  84. >
  85. {t('security_setting.admin_only')}
  86. </button>
  87. <button
  88. className="dropdown-item"
  89. type="button"
  90. onClick={() => { setState(PageDeleteConfigValue.AdminAndAuthor) }}
  91. >
  92. {t('security_setting.admin_and_author')}
  93. </button>
  94. </div>
  95. <p className="form-text text-muted small">
  96. {t(`security_setting.${deletionType}_explain`)}
  97. </p>
  98. </div>
  99. </div>
  100. </div>
  101. );
  102. }
  103. render() {
  104. const { t, adminGeneralSecurityContainer } = this.props;
  105. const {
  106. currentRestrictGuestMode, currentPageDeletionAuthority, currentPageCompleteDeletionAuthority,
  107. currentPageRecursiveDeletionAuthority, currentPageRecursiveCompleteDeletionAuthority,
  108. } = adminGeneralSecurityContainer.state;
  109. return (
  110. <React.Fragment>
  111. <h2 className="alert-anchor border-bottom">
  112. {t('security_settings')}
  113. </h2>
  114. {adminGeneralSecurityContainer.retrieveError != null && (
  115. <div className="alert alert-danger">
  116. <p>{t('Error occurred')} : {adminGeneralSecurityContainer.retrieveError}</p>
  117. </div>
  118. )}
  119. <h4 className="mt-4">{ t('security_setting.page_list_and_search_results') }</h4>
  120. <table className="table table-bordered col-lg-9 mb-5">
  121. <thead>
  122. <tr>
  123. <th scope="col">{ t('scope_of_page_disclosure') }</th>
  124. <th scope="col">{ t('set_point') }</th>
  125. </tr>
  126. </thead>
  127. <tbody>
  128. <tr>
  129. <th scope="row">{ t('Public') }</th>
  130. <td>{ t('always_displayed') }</td>
  131. </tr>
  132. <tr>
  133. <th scope="row">{ t('Anyone with the link') }</th>
  134. <td>{ t('always_hidden') }</td>
  135. </tr>
  136. <tr>
  137. <th scope="row">{ t('Only me') }</th>
  138. <td>
  139. <div className="custom-control custom-switch custom-checkbox-success">
  140. <input
  141. type="checkbox"
  142. className="custom-control-input"
  143. id="isShowRestrictedByOwner"
  144. checked={adminGeneralSecurityContainer.state.isShowRestrictedByOwner}
  145. onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByOwner() }}
  146. />
  147. <label className="custom-control-label" htmlFor="isShowRestrictedByOwner">
  148. {t('displayed_or_hidden')}
  149. </label>
  150. </div>
  151. </td>
  152. </tr>
  153. <tr>
  154. <th scope="row">{ t('Only inside the group') }</th>
  155. <td>
  156. <div className="custom-control custom-switch custom-checkbox-success">
  157. <input
  158. type="checkbox"
  159. className="custom-control-input"
  160. id="isShowRestrictedByGroup"
  161. checked={adminGeneralSecurityContainer.state.isShowRestrictedByGroup}
  162. onChange={() => { adminGeneralSecurityContainer.switchIsShowRestrictedByGroup() }}
  163. />
  164. <label className="custom-control-label" htmlFor="isShowRestrictedByGroup">
  165. {t('displayed_or_hidden')}
  166. </label>
  167. </div>
  168. </td>
  169. </tr>
  170. </tbody>
  171. </table>
  172. <h4>{t('security_setting.page_access_and_delete_rights')}</h4>
  173. <div className="row mb-4">
  174. <div className="col-md-3 text-md-right py-2">
  175. <strong>{t('security_setting.Guest Users Access')}</strong>
  176. </div>
  177. <div className="col-md-9">
  178. <div className="dropdown">
  179. <button
  180. className={`btn btn-outline-secondary dropdown-toggle text-right col-12
  181. col-md-auto ${adminGeneralSecurityContainer.isWikiModeForced && 'disabled'}`}
  182. type="button"
  183. id="dropdownMenuButton"
  184. data-toggle="dropdown"
  185. aria-haspopup="true"
  186. aria-expanded="true"
  187. >
  188. <span className="float-left">
  189. {currentRestrictGuestMode === 'Deny' && t('security_setting.guest_mode.deny')}
  190. {currentRestrictGuestMode === 'Readonly' && t('security_setting.guest_mode.readonly')}
  191. </span>
  192. </button>
  193. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  194. <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Deny') }}>
  195. {t('security_setting.guest_mode.deny')}
  196. </button>
  197. <button className="dropdown-item" type="button" onClick={() => { adminGeneralSecurityContainer.changeRestrictGuestMode('Readonly') }}>
  198. {t('security_setting.guest_mode.readonly')}
  199. </button>
  200. </div>
  201. </div>
  202. {adminGeneralSecurityContainer.isWikiModeForced && (
  203. <p className="alert alert-warning mt-2 text-left offset-3 col-6">
  204. <i className="icon-exclamation icon-fw">
  205. </i><b>FIXED</b><br />
  206. <b
  207. dangerouslySetInnerHTML={{
  208. __html: t('security_setting.Fixed by env var',
  209. { forcewikimode: 'FORCE_WIKI_MODE', wikimode: adminGeneralSecurityContainer.state.wikiMode }),
  210. }}
  211. />
  212. </p>
  213. )}
  214. </div>
  215. </div>
  216. {/* Render PageDeletePermissionDropdown */}
  217. {
  218. [
  219. [currentPageDeletionAuthority, adminGeneralSecurityContainer.changePageDeletionAuthority, DeletionType.Deletion],
  220. [currentPageCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageCompleteDeletionAuthority, DeletionType.CompleteDeletion],
  221. [currentPageRecursiveDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveDeletionAuthority, DeletionType.RecursiveDeletion],
  222. // eslint-disable-next-line max-len
  223. [currentPageRecursiveCompleteDeletionAuthority, adminGeneralSecurityContainer.changePageRecursiveCompleteDeletionAuthority, DeletionType.RecursiveCompleteDeletion],
  224. ].map(arr => this.renderPageDeletePermissionDropdown(arr[0], arr[1], arr[2], t))
  225. }
  226. <h4>{t('security_setting.session')}</h4>
  227. <div className="form-group row">
  228. <label className="text-left text-md-right col-md-3 col-form-label">{t('security_setting.max_age')}</label>
  229. <div className="col-md-6">
  230. <input
  231. className="form-control col-md-3"
  232. type="text"
  233. defaultValue={adminGeneralSecurityContainer.state.sessionMaxAge || ''}
  234. onChange={(e) => {
  235. adminGeneralSecurityContainer.setSessionMaxAge(e.target.value);
  236. }}
  237. placeholder="2592000000"
  238. />
  239. {/* eslint-disable-next-line react/no-danger */}
  240. <p className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('security_setting.max_age_desc') }} />
  241. <p className="card well">
  242. <span className="text-warning">
  243. <i className="icon-info"></i> {t('security_setting.max_age_caution')}
  244. </span>
  245. </p>
  246. </div>
  247. </div>
  248. <div className="row my-3">
  249. <div className="text-center text-md-left offset-md-3 col-md-5">
  250. <button type="button" className="btn btn-primary" disabled={adminGeneralSecurityContainer.retrieveError != null} onClick={this.putSecuritySetting}>
  251. {t('Update')}
  252. </button>
  253. </div>
  254. </div>
  255. </React.Fragment>
  256. );
  257. }
  258. }
  259. SecuritySetting.propTypes = {
  260. t: PropTypes.func.isRequired, // i18next
  261. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  262. csrf: PropTypes.string,
  263. adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
  264. };
  265. const SecuritySettingWrapper = withUnstatedContainers(SecuritySetting, [AppContainer, AdminGeneralSecurityContainer]);
  266. export default withTranslation()(SecuritySettingWrapper);