General.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <script lang="ts">
  2. import { getBackendConfig, getWebhookUrl, updateWebhookUrl } from '$lib/apis';
  3. import {
  4. getAdminConfig,
  5. getLdapConfig,
  6. getLdapServer,
  7. updateAdminConfig,
  8. updateLdapConfig,
  9. updateLdapServer
  10. } from '$lib/apis/auths';
  11. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  12. import Switch from '$lib/components/common/Switch.svelte';
  13. import Tooltip from '$lib/components/common/Tooltip.svelte';
  14. import { config } from '$lib/stores';
  15. import { onMount, getContext } from 'svelte';
  16. import { toast } from 'svelte-sonner';
  17. const i18n = getContext('i18n');
  18. export let saveHandler: Function;
  19. let adminConfig = null;
  20. let webhookUrl = '';
  21. // LDAP
  22. let ENABLE_LDAP = false;
  23. let LDAP_SERVER = {
  24. label: '',
  25. host: '',
  26. port: '',
  27. attribute_for_username: 'uid',
  28. app_dn: '',
  29. app_dn_password: '',
  30. search_base: '',
  31. search_filters: '',
  32. use_tls: false,
  33. certificate_path: '',
  34. ciphers: ''
  35. };
  36. const updateLdapServerHandler = async () => {
  37. if (!ENABLE_LDAP) return;
  38. const res = await updateLdapServer(localStorage.token, LDAP_SERVER).catch((error) => {
  39. toast.error(error);
  40. return null;
  41. });
  42. if (res) {
  43. toast.success($i18n.t('LDAP server updated'));
  44. }
  45. };
  46. const updateHandler = async () => {
  47. webhookUrl = await updateWebhookUrl(localStorage.token, webhookUrl);
  48. const res = await updateAdminConfig(localStorage.token, adminConfig);
  49. await updateLdapServerHandler();
  50. if (res) {
  51. saveHandler();
  52. } else {
  53. toast.error(i18n.t('Failed to update settings'));
  54. }
  55. };
  56. onMount(async () => {
  57. await Promise.all([
  58. (async () => {
  59. adminConfig = await getAdminConfig(localStorage.token);
  60. })(),
  61. (async () => {
  62. webhookUrl = await getWebhookUrl(localStorage.token);
  63. })(),
  64. (async () => {
  65. LDAP_SERVER = await getLdapServer(localStorage.token);
  66. })()
  67. ]);
  68. const ldapConfig = await getLdapConfig(localStorage.token);
  69. ENABLE_LDAP = ldapConfig.ENABLE_LDAP;
  70. });
  71. </script>
  72. <form
  73. class="flex flex-col h-full justify-between space-y-3 text-sm"
  74. on:submit|preventDefault={async () => {
  75. updateHandler();
  76. }}
  77. >
  78. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  79. {#if adminConfig !== null}
  80. <div>
  81. <div class=" mb-3 text-sm font-medium">{$i18n.t('General Settings')}</div>
  82. <div class=" flex w-full justify-between pr-2">
  83. <div class=" self-center text-xs font-medium">{$i18n.t('Enable New Sign Ups')}</div>
  84. <Switch bind:state={adminConfig.ENABLE_SIGNUP} />
  85. </div>
  86. <div class=" my-3 flex w-full justify-between">
  87. <div class=" self-center text-xs font-medium">{$i18n.t('Default User Role')}</div>
  88. <div class="flex items-center relative">
  89. <select
  90. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 text-xs bg-transparent outline-none text-right"
  91. bind:value={adminConfig.DEFAULT_USER_ROLE}
  92. placeholder="Select a role"
  93. >
  94. <option value="pending">{$i18n.t('pending')}</option>
  95. <option value="user">{$i18n.t('user')}</option>
  96. <option value="admin">{$i18n.t('admin')}</option>
  97. </select>
  98. </div>
  99. </div>
  100. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  101. <div class="my-3 flex w-full items-center justify-between pr-2">
  102. <div class=" self-center text-xs font-medium">
  103. {$i18n.t('Show Admin Details in Account Pending Overlay')}
  104. </div>
  105. <Switch bind:state={adminConfig.SHOW_ADMIN_DETAILS} />
  106. </div>
  107. <div class="my-3 flex w-full items-center justify-between pr-2">
  108. <div class=" self-center text-xs font-medium">{$i18n.t('Enable Community Sharing')}</div>
  109. <Switch bind:state={adminConfig.ENABLE_COMMUNITY_SHARING} />
  110. </div>
  111. <div class="my-3 flex w-full items-center justify-between pr-2">
  112. <div class=" self-center text-xs font-medium">{$i18n.t('Enable Message Rating')}</div>
  113. <Switch bind:state={adminConfig.ENABLE_MESSAGE_RATING} />
  114. </div>
  115. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  116. <div class=" w-full justify-between">
  117. <div class="flex w-full justify-between">
  118. <div class=" self-center text-xs font-medium">{$i18n.t('JWT Expiration')}</div>
  119. </div>
  120. <div class="flex mt-2 space-x-2">
  121. <input
  122. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  123. type="text"
  124. placeholder={`e.g.) "30m","1h", "10d". `}
  125. bind:value={adminConfig.JWT_EXPIRES_IN}
  126. />
  127. </div>
  128. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  129. {$i18n.t('Valid time units:')}
  130. <span class=" text-gray-300 font-medium"
  131. >{$i18n.t("'s', 'm', 'h', 'd', 'w' or '-1' for no expiration.")}</span
  132. >
  133. </div>
  134. </div>
  135. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  136. <div class=" w-full justify-between">
  137. <div class="flex w-full justify-between">
  138. <div class=" self-center text-xs font-medium">{$i18n.t('Webhook URL')}</div>
  139. </div>
  140. <div class="flex mt-2 space-x-2">
  141. <input
  142. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  143. type="text"
  144. placeholder={`https://example.com/webhook`}
  145. bind:value={webhookUrl}
  146. />
  147. </div>
  148. </div>
  149. </div>
  150. {/if}
  151. <hr class=" border-gray-50 dark:border-gray-850" />
  152. <div class=" space-y-3">
  153. <div class="mt-2 space-y-2 pr-1.5">
  154. <div class="flex justify-between items-center text-sm">
  155. <div class=" font-medium">{$i18n.t('LDAP')}</div>
  156. <div class="mt-1">
  157. <Switch
  158. bind:state={ENABLE_LDAP}
  159. on:change={async () => {
  160. updateLdapConfig(localStorage.token, ENABLE_LDAP);
  161. }}
  162. />
  163. </div>
  164. </div>
  165. {#if ENABLE_LDAP}
  166. <div class="flex flex-col gap-1">
  167. <div class="flex w-full gap-2">
  168. <div class="w-full">
  169. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  170. {$i18n.t('Label')}
  171. </div>
  172. <input
  173. class="w-full bg-transparent outline-none py-0.5"
  174. required
  175. placeholder={$i18n.t('Enter server label')}
  176. bind:value={LDAP_SERVER.label}
  177. />
  178. </div>
  179. <div class="w-full"></div>
  180. </div>
  181. <div class="flex w-full gap-2">
  182. <div class="w-full">
  183. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  184. {$i18n.t('Host')}
  185. </div>
  186. <input
  187. class="w-full bg-transparent outline-none py-0.5"
  188. required
  189. placeholder={$i18n.t('Enter server host')}
  190. bind:value={LDAP_SERVER.host}
  191. />
  192. </div>
  193. <div class="w-full">
  194. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  195. {$i18n.t('Port')}
  196. </div>
  197. <Tooltip
  198. placement="top-start"
  199. content={$i18n.t('Default to 389 or 636 if TLS is enabled')}
  200. className="w-full"
  201. >
  202. <input
  203. class="w-full bg-transparent outline-none py-0.5"
  204. type="number"
  205. placeholder={$i18n.t('Enter server port')}
  206. bind:value={LDAP_SERVER.port}
  207. />
  208. </Tooltip>
  209. </div>
  210. </div>
  211. <div class="flex w-full gap-2">
  212. <div class="w-full">
  213. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  214. {$i18n.t('Application DN')}
  215. </div>
  216. <Tooltip
  217. content={$i18n.t('The Application Account DN you bind with for search')}
  218. placement="top-start"
  219. >
  220. <input
  221. class="w-full bg-transparent outline-none py-0.5"
  222. required
  223. placeholder={$i18n.t('Enter Application DN')}
  224. bind:value={LDAP_SERVER.app_dn}
  225. />
  226. </Tooltip>
  227. </div>
  228. <div class="w-full">
  229. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  230. {$i18n.t('Application DN Password')}
  231. </div>
  232. <SensitiveInput
  233. placeholder={$i18n.t('Enter Application DN Password')}
  234. bind:value={LDAP_SERVER.app_dn_password}
  235. />
  236. </div>
  237. </div>
  238. <div class="flex w-full gap-2">
  239. <div class="w-full">
  240. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  241. {$i18n.t('Attribute for Username')}
  242. </div>
  243. <Tooltip
  244. content={$i18n.t(
  245. 'The LDAP attribute that maps to the username that users use to sign in.'
  246. )}
  247. placement="top-start"
  248. >
  249. <input
  250. class="w-full bg-transparent outline-none py-0.5"
  251. required
  252. placeholder={$i18n.t('Example: sAMAccountName or uid or userPrincipalName')}
  253. bind:value={LDAP_SERVER.attribute_for_username}
  254. />
  255. </Tooltip>
  256. </div>
  257. </div>
  258. <div class="flex w-full gap-2">
  259. <div class="w-full">
  260. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  261. {$i18n.t('Search Base')}
  262. </div>
  263. <Tooltip content={$i18n.t('The base to search for users')} placement="top-start">
  264. <input
  265. class="w-full bg-transparent outline-none py-0.5"
  266. required
  267. placeholder={$i18n.t('Example: ou=users,dc=foo,dc=example')}
  268. bind:value={LDAP_SERVER.search_base}
  269. />
  270. </Tooltip>
  271. </div>
  272. </div>
  273. <div class="flex w-full gap-2">
  274. <div class="w-full">
  275. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  276. {$i18n.t('Search Filters')}
  277. </div>
  278. <input
  279. class="w-full bg-transparent outline-none py-0.5"
  280. placeholder={$i18n.t('Example: (&(objectClass=inetOrgPerson)(uid=%s))')}
  281. bind:value={LDAP_SERVER.search_filters}
  282. />
  283. </div>
  284. </div>
  285. <div class="text-xs text-gray-400 dark:text-gray-500">
  286. <a
  287. class=" text-gray-300 font-medium underline"
  288. href="https://ldap.com/ldap-filters/"
  289. target="_blank"
  290. >
  291. {$i18n.t('Click here for filter guides.')}
  292. </a>
  293. </div>
  294. <div>
  295. <div class="flex justify-between items-center text-sm">
  296. <div class=" font-medium">{$i18n.t('TLS')}</div>
  297. <div class="mt-1">
  298. <Switch bind:state={LDAP_SERVER.use_tls} />
  299. </div>
  300. </div>
  301. {#if LDAP_SERVER.use_tls}
  302. <div class="flex w-full gap-2">
  303. <div class="w-full">
  304. <div class=" self-center text-xs font-medium min-w-fit mb-1 mt-1">
  305. {$i18n.t('Certificate Path')}
  306. </div>
  307. <input
  308. class="w-full bg-transparent outline-none py-0.5"
  309. required
  310. placeholder={$i18n.t('Enter certificate path')}
  311. bind:value={LDAP_SERVER.certificate_path}
  312. />
  313. </div>
  314. </div>
  315. <div class="flex w-full gap-2">
  316. <div class="w-full">
  317. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  318. {$i18n.t('Ciphers')}
  319. </div>
  320. <Tooltip content={$i18n.t('Default to ALL')} placement="top-start">
  321. <input
  322. class="w-full bg-transparent outline-none py-0.5"
  323. placeholder={$i18n.t('Example: ALL')}
  324. bind:value={LDAP_SERVER.ciphers}
  325. />
  326. </Tooltip>
  327. </div>
  328. <div class="w-full"></div>
  329. </div>
  330. {/if}
  331. </div>
  332. </div>
  333. {/if}
  334. </div>
  335. </div>
  336. </div>
  337. <div class="flex justify-end pt-3 text-sm font-medium">
  338. <button
  339. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  340. type="submit"
  341. >
  342. {$i18n.t('Save')}
  343. </button>
  344. </div>
  345. </form>