General.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. <div class=" flex w-full justify-between pr-2">
  101. <div class=" self-center text-xs font-medium">{$i18n.t('Enable API Key Auth')}</div>
  102. <Switch bind:state={adminConfig.ENABLE_API_KEY} />
  103. </div>
  104. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  105. <div class="my-3 flex w-full items-center justify-between pr-2">
  106. <div class=" self-center text-xs font-medium">
  107. {$i18n.t('Show Admin Details in Account Pending Overlay')}
  108. </div>
  109. <Switch bind:state={adminConfig.SHOW_ADMIN_DETAILS} />
  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 Community Sharing')}</div>
  113. <Switch bind:state={adminConfig.ENABLE_COMMUNITY_SHARING} />
  114. </div>
  115. <div class="my-3 flex w-full items-center justify-between pr-2">
  116. <div class=" self-center text-xs font-medium">{$i18n.t('Enable Message Rating')}</div>
  117. <Switch bind:state={adminConfig.ENABLE_MESSAGE_RATING} />
  118. </div>
  119. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  120. <div class=" w-full justify-between">
  121. <div class="flex w-full justify-between">
  122. <div class=" self-center text-xs font-medium">{$i18n.t('JWT Expiration')}</div>
  123. </div>
  124. <div class="flex mt-2 space-x-2">
  125. <input
  126. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  127. type="text"
  128. placeholder={`e.g.) "30m","1h", "10d". `}
  129. bind:value={adminConfig.JWT_EXPIRES_IN}
  130. />
  131. </div>
  132. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  133. {$i18n.t('Valid time units:')}
  134. <span class=" text-gray-300 font-medium"
  135. >{$i18n.t("'s', 'm', 'h', 'd', 'w' or '-1' for no expiration.")}</span
  136. >
  137. </div>
  138. </div>
  139. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  140. <div class=" w-full justify-between">
  141. <div class="flex w-full justify-between">
  142. <div class=" self-center text-xs font-medium">{$i18n.t('Webhook URL')}</div>
  143. </div>
  144. <div class="flex mt-2 space-x-2">
  145. <input
  146. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  147. type="text"
  148. placeholder={`https://example.com/webhook`}
  149. bind:value={webhookUrl}
  150. />
  151. </div>
  152. </div>
  153. <hr class=" border-gray-50 dark:border-gray-850 my-2" />
  154. <div class="pt-1 flex w-full justify-between pr-2">
  155. <div class=" self-center text-sm font-medium">
  156. {$i18n.t('Channels')} ({$i18n.t('Beta')})
  157. </div>
  158. <Switch bind:state={adminConfig.ENABLE_CHANNELS} />
  159. </div>
  160. </div>
  161. {/if}
  162. <hr class=" border-gray-50 dark:border-gray-850" />
  163. <div class=" space-y-3">
  164. <div class="mt-2 space-y-2 pr-1.5">
  165. <div class="flex justify-between items-center text-sm">
  166. <div class=" font-medium">{$i18n.t('LDAP')}</div>
  167. <div class="mt-1">
  168. <Switch
  169. bind:state={ENABLE_LDAP}
  170. on:change={async () => {
  171. updateLdapConfig(localStorage.token, ENABLE_LDAP);
  172. }}
  173. />
  174. </div>
  175. </div>
  176. {#if ENABLE_LDAP}
  177. <div class="flex flex-col gap-1">
  178. <div class="flex w-full gap-2">
  179. <div class="w-full">
  180. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  181. {$i18n.t('Label')}
  182. </div>
  183. <input
  184. class="w-full bg-transparent outline-none py-0.5"
  185. required
  186. placeholder={$i18n.t('Enter server label')}
  187. bind:value={LDAP_SERVER.label}
  188. />
  189. </div>
  190. <div class="w-full"></div>
  191. </div>
  192. <div class="flex w-full gap-2">
  193. <div class="w-full">
  194. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  195. {$i18n.t('Host')}
  196. </div>
  197. <input
  198. class="w-full bg-transparent outline-none py-0.5"
  199. required
  200. placeholder={$i18n.t('Enter server host')}
  201. bind:value={LDAP_SERVER.host}
  202. />
  203. </div>
  204. <div class="w-full">
  205. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  206. {$i18n.t('Port')}
  207. </div>
  208. <Tooltip
  209. placement="top-start"
  210. content={$i18n.t('Default to 389 or 636 if TLS is enabled')}
  211. className="w-full"
  212. >
  213. <input
  214. class="w-full bg-transparent outline-none py-0.5"
  215. type="number"
  216. placeholder={$i18n.t('Enter server port')}
  217. bind:value={LDAP_SERVER.port}
  218. />
  219. </Tooltip>
  220. </div>
  221. </div>
  222. <div class="flex w-full gap-2">
  223. <div class="w-full">
  224. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  225. {$i18n.t('Application DN')}
  226. </div>
  227. <Tooltip
  228. content={$i18n.t('The Application Account DN you bind with for search')}
  229. placement="top-start"
  230. >
  231. <input
  232. class="w-full bg-transparent outline-none py-0.5"
  233. required
  234. placeholder={$i18n.t('Enter Application DN')}
  235. bind:value={LDAP_SERVER.app_dn}
  236. />
  237. </Tooltip>
  238. </div>
  239. <div class="w-full">
  240. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  241. {$i18n.t('Application DN Password')}
  242. </div>
  243. <SensitiveInput
  244. placeholder={$i18n.t('Enter Application DN Password')}
  245. bind:value={LDAP_SERVER.app_dn_password}
  246. />
  247. </div>
  248. </div>
  249. <div class="flex w-full gap-2">
  250. <div class="w-full">
  251. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  252. {$i18n.t('Attribute for Username')}
  253. </div>
  254. <Tooltip
  255. content={$i18n.t(
  256. 'The LDAP attribute that maps to the username that users use to sign in.'
  257. )}
  258. placement="top-start"
  259. >
  260. <input
  261. class="w-full bg-transparent outline-none py-0.5"
  262. required
  263. placeholder={$i18n.t('Example: sAMAccountName or uid or userPrincipalName')}
  264. bind:value={LDAP_SERVER.attribute_for_username}
  265. />
  266. </Tooltip>
  267. </div>
  268. </div>
  269. <div class="flex w-full gap-2">
  270. <div class="w-full">
  271. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  272. {$i18n.t('Search Base')}
  273. </div>
  274. <Tooltip content={$i18n.t('The base to search for users')} placement="top-start">
  275. <input
  276. class="w-full bg-transparent outline-none py-0.5"
  277. required
  278. placeholder={$i18n.t('Example: ou=users,dc=foo,dc=example')}
  279. bind:value={LDAP_SERVER.search_base}
  280. />
  281. </Tooltip>
  282. </div>
  283. </div>
  284. <div class="flex w-full gap-2">
  285. <div class="w-full">
  286. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  287. {$i18n.t('Search Filters')}
  288. </div>
  289. <input
  290. class="w-full bg-transparent outline-none py-0.5"
  291. placeholder={$i18n.t('Example: (&(objectClass=inetOrgPerson)(uid=%s))')}
  292. bind:value={LDAP_SERVER.search_filters}
  293. />
  294. </div>
  295. </div>
  296. <div class="text-xs text-gray-400 dark:text-gray-500">
  297. <a
  298. class=" text-gray-300 font-medium underline"
  299. href="https://ldap.com/ldap-filters/"
  300. target="_blank"
  301. >
  302. {$i18n.t('Click here for filter guides.')}
  303. </a>
  304. </div>
  305. <div>
  306. <div class="flex justify-between items-center text-sm">
  307. <div class=" font-medium">{$i18n.t('TLS')}</div>
  308. <div class="mt-1">
  309. <Switch bind:state={LDAP_SERVER.use_tls} />
  310. </div>
  311. </div>
  312. {#if LDAP_SERVER.use_tls}
  313. <div class="flex w-full gap-2">
  314. <div class="w-full">
  315. <div class=" self-center text-xs font-medium min-w-fit mb-1 mt-1">
  316. {$i18n.t('Certificate Path')}
  317. </div>
  318. <input
  319. class="w-full bg-transparent outline-none py-0.5"
  320. required
  321. placeholder={$i18n.t('Enter certificate path')}
  322. bind:value={LDAP_SERVER.certificate_path}
  323. />
  324. </div>
  325. </div>
  326. <div class="flex w-full gap-2">
  327. <div class="w-full">
  328. <div class=" self-center text-xs font-medium min-w-fit mb-1">
  329. {$i18n.t('Ciphers')}
  330. </div>
  331. <Tooltip content={$i18n.t('Default to ALL')} placement="top-start">
  332. <input
  333. class="w-full bg-transparent outline-none py-0.5"
  334. placeholder={$i18n.t('Example: ALL')}
  335. bind:value={LDAP_SERVER.ciphers}
  336. />
  337. </Tooltip>
  338. </div>
  339. <div class="w-full"></div>
  340. </div>
  341. {/if}
  342. </div>
  343. </div>
  344. {/if}
  345. </div>
  346. </div>
  347. </div>
  348. <div class="flex justify-end pt-3 text-sm font-medium">
  349. <button
  350. 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"
  351. type="submit"
  352. >
  353. {$i18n.t('Save')}
  354. </button>
  355. </div>
  356. </form>