Skip to content

Commit d2cf67d

Browse files
authored
Merge pull request #132 from iceljc/features/add-user-role-limit
add user role limit
2 parents c32f961 + 6ba810b commit d2cf67d

File tree

13 files changed

+163
-55
lines changed

13 files changed

+163
-55
lines changed

src/lib/common/Breadcrumb.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="page-title-right">
1414
<ol class="breadcrumb m-0">
1515
<BreadcrumbItem>
16-
<Link href="#">{title}</Link>
16+
<Link disabled>{title}</Link>
1717
</BreadcrumbItem>
1818
<BreadcrumbItem active>
1919
{pagetitle}

src/lib/common/NotificationDropdown.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
<h6 class="m-0">{$_('Notifications')}</h6>
3838
</div>
3939
<div class="col-auto">
40-
<Link href="#!" class="small">{$_('View All')}</Link>
40+
<Link class="small" disabled>{$_('View All')}</Link>
4141
</div>
4242
</div>
4343
</div>
4444
<div style="max-height: 230px;" id="notification">
45-
<Link href="javascript: void(0);" class="text-reset notification-item">
45+
<Link href="javascript: void(0);" class="text-reset notification-item" disabled>
4646
<div class="d-flex">
4747
<div class="avatar-xs me-3">
4848
<span class="avatar-title bg-primary rounded-circle font-size-16">

src/lib/common/ProfileDropdown.svelte

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
22
import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from '@sveltestrap/sveltestrap';
3+
import { resetLocalStorage } from '$lib/helpers/store';
34
import { goto } from '$app/navigation';
45
import { browser } from '$app/environment';
56
import { _ } from 'svelte-i18n';
@@ -10,7 +11,7 @@
1011
export let user;
1112
function logout() {
1213
if (browser){
13-
localStorage.removeItem('user');
14+
resetLocalStorage(true);
1415
}
1516
goto('login');
1617
};
@@ -29,14 +30,14 @@
2930
</DropdownToggle>
3031
<DropdownMenu end>
3132
<!-- item-->
32-
<DropdownItem href="page/user/me"
33-
><i class="bx bx-user font-size-16 align-middle me-1" />
33+
<DropdownItem href="page/user/me">
34+
<i class="bx bx-user font-size-16 align-middle me-1" />
3435
<span>{$_('Profile')}</span>
3536
</DropdownItem>
36-
<DropdownItem href="#"
37-
><span class="badge bg-success float-end">11</span><i
38-
class="bx bx-wrench font-size-16 align-middle me-1"
39-
/> <span key="t-settings">{$_('Settings')}</span>
37+
<DropdownItem href="#" disabled>
38+
<span class="badge bg-success float-end">11</span>
39+
<i class="bx bx-wrench font-size-16 align-middle me-1"/>
40+
<span key="t-settings">{$_('Settings')}</span>
4041
</DropdownItem>
4142
<DropdownItem divider />
4243
<DropdownItem href="#">

src/lib/helpers/http.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ axios.interceptors.response.use(
5555
/** @param {import('axios').InternalAxiosRequestConfig<any>} config */
5656
function skipLoader(config) {
5757
const postRegexes = [
58-
new RegExp('http(s*)://(.*?)/conversation/(.*?)/(.*?)', 'g')
58+
new RegExp('http(s*)://(.*?)/conversation/(.*?)/(.*?)', 'g'),
59+
new RegExp('http(s*)://(.*?)/agent', 'g')
5960
];
6061

6162
const getRegexes = [

src/lib/helpers/store.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,17 @@ const createConversationUserAttachmentStore = () => {
152152
}
153153
};
154154

155-
export const conversationUserAttachmentStore = createConversationUserAttachmentStore();
155+
export const conversationUserAttachmentStore = createConversationUserAttachmentStore();
156+
157+
158+
export function resetLocalStorage(resetUser = false) {
159+
conversationUserStateStore.reset();
160+
conversationSearchOptionStore.reset();
161+
conversationUserMessageStore.reset();
162+
conversationUserAttachmentStore.reset();
163+
localStorage.removeItem('conversation');
164+
165+
if (resetUser) {
166+
localStorage.removeItem('user');
167+
}
168+
}

src/lib/helpers/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
* @property {Object[]} responses
130130
* @property {RoutingRule[]} routing_rules
131131
* @property {AgentWelcomeInfo} welcome_info - Welcome information.
132+
* @property {boolean} editable
132133
*/
133134

134135
/**

src/lib/services/agent-service.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export async function saveAgent(agent) {
4848
await axios.put(url, agent);
4949
}
5050

51+
/**
52+
* Delete agent detail
53+
* @param {string} agentId
54+
*/
55+
export async function deleteAgent(agentId) {
56+
let url = endpoints.agentDetailUrl.replace("{id}", agentId);
57+
await axios.delete(url);
58+
}
59+
5160
/**
5261
* Refresh agent data
5362
*/

src/routes/(authentication)/login/+page.svelte

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131
PUBLIC_AUTH_ENABLE_FIND_PWD,
3232
} from '$env/static/public';
3333
import { onMount } from 'svelte';
34-
import {
35-
conversationSearchOptionStore,
36-
conversationUserAttachmentStore,
37-
conversationUserMessageStore,
38-
conversationUserStateStore
39-
} from '$lib/helpers/store';
34+
import { resetLocalStorage } from '$lib/helpers/store';
4035
4136
let username = PUBLIC_ADMIN_USERNAME;
4237
let password = PUBLIC_ADMIN_PASSWORD;
@@ -71,7 +66,7 @@
7166
status = 'success';
7267
const redirectUrl = $page.url.searchParams.get('redirect');
7368
isSubmitting = false;
74-
resetStorage();
69+
resetLocalStorage();
7570
if (redirectUrl) {
7671
window.location.href = decodeURIComponent(redirectUrl);
7772
} else {
@@ -105,13 +100,6 @@
105100
icon.className = 'mdi mdi-eye-outline';
106101
}
107102
}
108-
109-
function resetStorage() {
110-
conversationUserStateStore.reset();
111-
conversationSearchOptionStore.reset();
112-
conversationUserMessageStore.reset();
113-
conversationUserAttachmentStore.reset();
114-
}
115103
</script>
116104

117105
<Headtitle title="Login" />

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
let loadTextEditor = false;
128128
let loadFileEditor = false;
129129
let autoScrollLog = false;
130+
let disableAction = false;
130131
131132
$: {
132133
const editor = lastBotMsg?.rich_content?.editor || '';
@@ -135,6 +136,10 @@
135136
loadEditor = !isSendingMsg && !isThinking && (loadTextEditor || loadFileEditor);
136137
}
137138
139+
$: {
140+
disableAction = currentUser?.role !== UserRole.Admin && currentUser?.id !== conversationUser?.id;
141+
}
142+
138143
setContext('chat-window-context', {
139144
autoScrollToBottom: autoScrollToBottom
140145
});
@@ -914,12 +919,26 @@
914919
</DropdownToggle>
915920
<DropdownMenu>
916921
{#if !isLoadStateLog}
917-
<DropdownItem on:click={() => toggleStateLog()}>View States</DropdownItem>
922+
<DropdownItem
923+
on:click={() => toggleStateLog()}
924+
>
925+
View States
926+
</DropdownItem>
918927
{/if}
919928
{#if !isOpenUserAddStateModal}
920-
<DropdownItem on:click={() => toggleUserAddStateModal()}>Add States</DropdownItem>
929+
<DropdownItem
930+
disabled={disableAction}
931+
on:click={() => toggleUserAddStateModal()}
932+
>
933+
Add States
934+
</DropdownItem>
921935
{/if}
922-
<DropdownItem on:click={() => clearUserAddStates()}>Clear States</DropdownItem>
936+
<DropdownItem
937+
disabled={disableAction}
938+
on:click={() => clearUserAddStates()}
939+
>
940+
Clear States
941+
</DropdownItem>
923942
</DropdownMenu>
924943
</Dropdown>
925944
</li>
@@ -932,6 +951,7 @@
932951
<li class="list-inline-item d-md-inline-block">
933952
<button
934953
class="btn btn-primary btn-rounded btn-sm chat-send waves-effect waves-light"
954+
disabled={disableAction}
935955
on:click={() => endChat()}
936956
>
937957
<span class="d-none d-sm-inline-block me-2" >End Conversation</span> <i class="mdi mdi-window-close"></i>
@@ -986,7 +1006,7 @@
9861006
</div>
9871007
{#if !isLite}
9881008
<Dropdown>
989-
<DropdownToggle class="dropdown-toggle" tag="span" disabled={isSendingMsg || isThinking}>
1009+
<DropdownToggle class="dropdown-toggle" tag="span" disabled={isSendingMsg || isThinking || disableAction}>
9901010
<i class="bx bx-dots-vertical-rounded" />
9911011
</DropdownToggle>
9921012
<DropdownMenu class="dropdown-menu-end">
@@ -1055,7 +1075,7 @@
10551075
<button
10561076
type="submit"
10571077
class="btn btn-primary btn-rounded waves-effect waves-light"
1058-
disabled={isSendingMsg || isThinking}
1078+
disabled={isSendingMsg || isThinking || disableAction}
10591079
on:click={startListen}
10601080
>
10611081
<i class="mdi mdi-{microphoneIcon} md-36" />
@@ -1066,13 +1086,13 @@
10661086
<ChatTextArea
10671087
className={`chat-input ${loadFileEditor ? 'chat-uploader' : ''}`}
10681088
bind:text={text}
1069-
disabled={isSendingMsg || isThinking}
1089+
disabled={isSendingMsg || isThinking || disableAction}
10701090
editor={lastBotMsg?.rich_content?.editor || ''}
10711091
onKeyDown={e => onSendMessage(e)}
10721092
/>
10731093
{#if loadFileEditor}
10741094
<div class="chat-input-links">
1075-
<ChatImageUploader />
1095+
<ChatImageUploader disabled={disableAction} />
10761096
</div>
10771097
{/if}
10781098
</div>
@@ -1081,7 +1101,7 @@
10811101
<button
10821102
type="submit"
10831103
class="btn btn-primary btn-rounded chat-send waves-effect waves-light"
1084-
disabled={!!!_.trim(text) || isSendingMsg || isThinking}
1104+
disabled={!!!_.trim(text) || isSendingMsg || isThinking || disableAction}
10851105
on:click={() => sentTextMessage()}
10861106
><span class="d-none d-md-inline-block me-2">Send</span>
10871107
<i class="mdi mdi-send" />

src/routes/page/agent/+page.svelte

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import { createAgent, getAgents } from '$lib/services/agent-service.js';
77
import { onMount } from 'svelte';
88
import PlainPagination from '$lib/common/PlainPagination.svelte';
9+
import { UserRole } from '$lib/helpers/enums';
910
import { _ } from 'svelte-i18n'
1011
import { goto } from '$app/navigation';
11-
12+
import { myInfo } from '$lib/services/auth-service';
13+
import Swal from 'sweetalert2/dist/sweetalert2.js';
14+
import "sweetalert2/src/sweetalert2.scss";
15+
1216
const firstPage = 1;
1317
const pageSize = 12;
1418
@@ -26,16 +30,37 @@
2630
/** @type {import('$types').Pagination} */
2731
let pager = filter.pager;
2832
33+
/** @type {import('$types').UserModel} */
34+
let user;
35+
2936
onMount(async () => {
3037
await getPagedAgents();
38+
user = await myInfo();
3139
});
3240
3341
async function getPagedAgents() {
3442
agents = await getAgents(filter);
3543
refresh();
3644
}
3745
38-
async function createAndEditAgent() {
46+
function createNewAgent() {
47+
// @ts-ignore
48+
Swal.fire({
49+
title: 'Are you sure?',
50+
text: "Are you sure you want to create a new agent?",
51+
icon: 'warning',
52+
showCancelButton: true,
53+
cancelButtonText: 'No',
54+
confirmButtonText: 'Yes'
55+
// @ts-ignore
56+
}).then(async (result) => {
57+
if (result.value) {
58+
await handleCreateNewAgent();
59+
}
60+
});
61+
}
62+
63+
async function handleCreateNewAgent() {
3964
const newAgent = {
4065
name: 'New Agent',
4166
description: 'New Agent Description',
@@ -87,9 +112,12 @@
87112
88113
<HeadTitle title="{$_('List')}" />
89114
<Breadcrumb title="{$_('Agent')}" pagetitle="{$_('List')}" />
90-
<Button class="mb-4" color="primary" on:click={() => createAndEditAgent()}>
115+
116+
{#if !!user && user.role == UserRole.Admin}
117+
<Button class="mb-4" color="primary" on:click={() => createNewAgent()}>
91118
<i class="bx bx-copy" /> {$_('New Agent')}
92119
</Button>
120+
{/if}
93121
94122
<Row>
95123
<CardAgent agents={agents.items} />

0 commit comments

Comments
 (0)