AccessControl.svelte 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <script lang="ts">
  2. import { getContext, onMount } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import { getGroups } from '$lib/apis/groups';
  5. import Tooltip from '$lib/components/common/Tooltip.svelte';
  6. import Plus from '$lib/components/icons/Plus.svelte';
  7. import UserCircleSolid from '$lib/components/icons/UserCircleSolid.svelte';
  8. import XMark from '$lib/components/icons/XMark.svelte';
  9. import Badge from '$lib/components/common/Badge.svelte';
  10. export let onChange: Function = () => {};
  11. export let accessRoles = ['read'];
  12. export let accessControl = null;
  13. let selectedGroupId = '';
  14. let groups = [];
  15. onMount(async () => {
  16. groups = await getGroups(localStorage.token);
  17. if (accessControl === null) {
  18. accessControl = null;
  19. } else {
  20. accessControl = {
  21. read: {
  22. group_ids: accessControl?.read?.group_ids ?? [],
  23. user_ids: accessControl?.read?.user_ids ?? []
  24. },
  25. write: {
  26. group_ids: accessControl?.write?.group_ids ?? [],
  27. user_ids: accessControl?.write?.user_ids ?? []
  28. }
  29. };
  30. }
  31. });
  32. $: onChange(accessControl);
  33. $: if (selectedGroupId) {
  34. onSelectGroup();
  35. }
  36. const onSelectGroup = () => {
  37. if (selectedGroupId !== '') {
  38. accessControl.read.group_ids = [...accessControl.read.group_ids, selectedGroupId];
  39. selectedGroupId = '';
  40. }
  41. };
  42. </script>
  43. <div class=" rounded-lg flex flex-col gap-2">
  44. <div class="">
  45. <div class=" text-sm font-semibold mb-1">{$i18n.t('Visibility')}</div>
  46. <div class="flex gap-2.5 items-center mb-1">
  47. <div>
  48. <div class=" p-2 bg-black/5 dark:bg-white/5 rounded-full">
  49. {#if accessControl !== null}
  50. <svg
  51. xmlns="http://www.w3.org/2000/svg"
  52. fill="none"
  53. viewBox="0 0 24 24"
  54. stroke-width="1.5"
  55. stroke="currentColor"
  56. class="w-5 h-5"
  57. >
  58. <path
  59. stroke-linecap="round"
  60. stroke-linejoin="round"
  61. d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z"
  62. />
  63. </svg>
  64. {:else}
  65. <svg
  66. xmlns="http://www.w3.org/2000/svg"
  67. fill="none"
  68. viewBox="0 0 24 24"
  69. stroke-width="1.5"
  70. stroke="currentColor"
  71. class="w-5 h-5"
  72. >
  73. <path
  74. stroke-linecap="round"
  75. stroke-linejoin="round"
  76. d="M6.115 5.19l.319 1.913A6 6 0 008.11 10.36L9.75 12l-.387.775c-.217.433-.132.956.21 1.298l1.348 1.348c.21.21.329.497.329.795v1.089c0 .426.24.815.622 1.006l.153.076c.433.217.956.132 1.298-.21l.723-.723a8.7 8.7 0 002.288-4.042 1.087 1.087 0 00-.358-1.099l-1.33-1.108c-.251-.21-.582-.299-.905-.245l-1.17.195a1.125 1.125 0 01-.98-.314l-.295-.295a1.125 1.125 0 010-1.591l.13-.132a1.125 1.125 0 011.3-.21l.603.302a.809.809 0 001.086-1.086L14.25 7.5l1.256-.837a4.5 4.5 0 001.528-1.732l.146-.292M6.115 5.19A9 9 0 1017.18 4.64M6.115 5.19A8.965 8.965 0 0112 3c1.929 0 3.716.607 5.18 1.64"
  77. />
  78. </svg>
  79. {/if}
  80. </div>
  81. </div>
  82. <div>
  83. <select
  84. id="models"
  85. class="outline-hidden bg-transparent text-sm font-medium rounded-lg block w-fit pr-10 max-w-full placeholder-gray-400"
  86. value={accessControl !== null ? 'private' : 'public'}
  87. on:change={(e) => {
  88. if (e.target.value === 'public') {
  89. accessControl = null;
  90. } else {
  91. accessControl = {
  92. read: {
  93. group_ids: []
  94. },
  95. write: {
  96. group_ids: []
  97. }
  98. };
  99. }
  100. }}
  101. >
  102. <option class=" text-gray-700" value="private" selected>{$i18n.t('Private')}</option>
  103. <option class=" text-gray-700" value="public" selected>{$i18n.t('Public')}</option>
  104. </select>
  105. <div class=" text-xs text-gray-400 font-medium">
  106. {#if accessControl !== null}
  107. {$i18n.t('Only select users and groups with permission can access')}
  108. {:else}
  109. {$i18n.t('Accessible to all users')}
  110. {/if}
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. {#if accessControl !== null}
  116. {@const accessGroups = groups.filter((group) =>
  117. accessControl.read.group_ids.includes(group.id)
  118. )}
  119. <div>
  120. <div class="">
  121. <div class="flex justify-between mb-1.5">
  122. <div class="text-sm font-semibold">
  123. {$i18n.t('Groups')}
  124. </div>
  125. </div>
  126. <div class="mb-1">
  127. <div class="flex w-full">
  128. <div class="flex flex-1 items-center">
  129. <div class="w-full px-0.5">
  130. <select
  131. class="outline-hidden bg-transparent text-sm rounded-lg block w-full pr-10 max-w-full
  132. {selectedGroupId ? '' : 'text-gray-500'}
  133. dark:placeholder-gray-500"
  134. bind:value={selectedGroupId}
  135. >
  136. <option class=" text-gray-700" value="" disabled selected
  137. >{$i18n.t('Select a group')}</option
  138. >
  139. {#each groups.filter((group) => !accessControl.read.group_ids.includes(group.id)) as group}
  140. <option class=" text-gray-700" value={group.id}>{group.name}</option>
  141. {/each}
  142. </select>
  143. </div>
  144. <!-- <div>
  145. <Tooltip content={$i18n.t('Add Group')}>
  146. <button
  147. class=" p-1 rounded-xl bg-transparent dark:hover:bg-white/5 hover:bg-black/5 transition font-medium text-sm flex items-center space-x-1"
  148. type="button"
  149. on:click={() => {}}
  150. >
  151. <Plus className="size-3.5" />
  152. </button>
  153. </Tooltip>
  154. </div> -->
  155. </div>
  156. </div>
  157. </div>
  158. <hr class=" border-gray-100 dark:border-gray-700/10 mt-1.5 mb-2.5 w-full" />
  159. <div class="flex flex-col gap-2 mb-1 px-0.5">
  160. {#if accessGroups.length > 0}
  161. {#each accessGroups as group}
  162. <div class="flex items-center gap-3 justify-between text-xs w-full transition">
  163. <div class="flex items-center gap-1.5 w-full font-medium">
  164. <div>
  165. <UserCircleSolid className="size-4" />
  166. </div>
  167. <div>
  168. {group.name}
  169. </div>
  170. </div>
  171. <div class="w-full flex justify-end items-center gap-0.5">
  172. <button
  173. class=""
  174. type="button"
  175. on:click={() => {
  176. if (accessRoles.includes('write')) {
  177. if (accessControl.write.group_ids.includes(group.id)) {
  178. accessControl.write.group_ids = accessControl.write.group_ids.filter(
  179. (group_id) => group_id !== group.id
  180. );
  181. } else {
  182. accessControl.write.group_ids = [
  183. ...accessControl.write.group_ids,
  184. group.id
  185. ];
  186. }
  187. }
  188. }}
  189. >
  190. {#if accessControl.write.group_ids.includes(group.id)}
  191. <Badge type={'success'} content={$i18n.t('Write')} />
  192. {:else}
  193. <Badge type={'info'} content={$i18n.t('Read')} />
  194. {/if}
  195. </button>
  196. <button
  197. class=" rounded-full p-1 hover:bg-gray-100 dark:hover:bg-gray-850 transition"
  198. type="button"
  199. on:click={() => {
  200. accessControl.read.group_ids = accessControl.read.group_ids.filter(
  201. (id) => id !== group.id
  202. );
  203. }}
  204. >
  205. <XMark />
  206. </button>
  207. </div>
  208. </div>
  209. {/each}
  210. {:else}
  211. <div class="flex items-center justify-center">
  212. <div class="text-gray-500 text-xs text-center py-2 px-10">
  213. {$i18n.t('No groups with access, add a group to grant access')}
  214. </div>
  215. </div>
  216. {/if}
  217. </div>
  218. </div>
  219. </div>
  220. {/if}
  221. </div>