diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 3ea6a7ce..0d033f3b 100755 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -50,7 +50,16 @@ export const actions = { //Login error, so redirect to the sign-in page throw redirect(303, '/', errorMessage(response.Message), event); } - console.log('response ....', response); + console.log('response ....', response.Data.User.Role); + + if (!response.Data.User.Role || !response.Data.User.Role.RoleName) { + throw redirect(303, '/', errorMessage("Permission Denied!"), event); + } + + if (!['System admin','System user','Tenant admin','Tenant user'].includes(response.Data.User.Role.RoleName)) { + throw redirect(303, '/', errorMessage("Permission Denied!"), event); + } + const user = response.Data.User; user.SessionId = response.Data.SessionId; const accessToken = response.Data.AccessToken; @@ -59,7 +68,7 @@ export const actions = { const sessionId = response.Data.SessionId; const userId: string = response.Data.User.id; - const session = await SessionManager.constructSession(user, accessToken, expiryDate, refreshToken); + const session = await SessionManager.constructSession(user, accessToken, expiryDate, refreshToken); if (!session) { console.log(`Session cannot be constructed!`); throw redirect(303, `/`, errorMessage(`Use login session cannot be created!`), event); diff --git a/src/routes/api/server/users/search/+server.ts b/src/routes/api/server/users/search/+server.ts index 58ecc429..b2be1a42 100644 --- a/src/routes/api/server/users/search/+server.ts +++ b/src/routes/api/server/users/search/+server.ts @@ -1,6 +1,5 @@ import type { RequestEvent } from '@sveltejs/kit'; import { searchUsers } from '$routes/api/services/reancare/user'; - ////////////////////////////////////////////////////////////// export const GET = async (event: RequestEvent) => { @@ -10,6 +9,7 @@ export const GET = async (event: RequestEvent) => { const firstName = searchParams.get('firstName') ?? undefined; const email = searchParams.get('email') ?? undefined; const phone = searchParams.get('phone') ?? undefined; + const roleIds = searchParams.get('roleIds') ?? undefined; const sortBy = searchParams.get('sortBy') ?? 'CreatedAt'; const sortOrder = searchParams.get('sortOrder') ?? 'ascending'; const itemsPerPage_ = searchParams.get('itemsPerPage'); @@ -22,17 +22,15 @@ export const GET = async (event: RequestEvent) => { firstName, phone, email, + roleIds, orderBy: sortBy, order: sortOrder, itemsPerPage, pageIndex }; - console.log('Search parms: ', searchParams); const response = await searchUsers(sessionId, searchParams); - const items = response.Data.Users; - console.log('res==', response); - - return new Response(JSON.stringify(items)); + const users = response.Data.Users; + return new Response(JSON.stringify(users)); } catch (err) { console.error(`Error retriving users: ${err.message}`); return new Response(err.message); diff --git a/src/routes/api/services/reancare/user.ts b/src/routes/api/services/reancare/user.ts index 82966311..3fba4a7a 100644 --- a/src/routes/api/services/reancare/user.ts +++ b/src/routes/api/services/reancare/user.ts @@ -129,24 +129,28 @@ export const resetPassword = async ( export const createUser = async ( sessionId: string, - tenantId: string, + tenantId: string, firstName: string, lastName: string, - phone: string, + phone: string, email: string, role: string, - roleId: string, + roleId: string, password: string, + defaultTimeZone: string, + currentTimeZone: string ) => { const body = { - TenantId: tenantId, + TenantId: tenantId, FirstName: firstName, LastName: lastName, - Role: role, - RoleId: roleId, + Role: role, + RoleId: roleId, Phone: phone ? phone : null, - Email: email ? email : null, + Email: email ? email : null, Password: password, + DefaultTimeZone: defaultTimeZone, + CurrentTimeZone: currentTimeZone }; if (Helper.isPhone(phone)) { @@ -184,24 +188,28 @@ export const searchUsers = async (sessionId: string, searchParams?: any) => { export const updateUser = async ( sessionId: string, - userId: string, + userId: string, firstName: string, lastName: string, - phone: string, + phone: string, email: string, - roleId:string, + roleId: string, + defaultTimeZone: string, + currentTimeZone: string // role: string, // password: string, - imageResourceId: string + // imageResourceId: string ) => { const body = { FirstName: firstName, LastName: lastName, - RoleId: roleId, + RoleId: roleId, Phone: phone ? phone : null, - Email: email ? email : null, - // Password: password, - ImageResourceId: imageResourceId + Email: email ? email : null, + DefaultTimeZone: defaultTimeZone ? defaultTimeZone : null, + CurrentTimeZone: currentTimeZone ? currentTimeZone : null + // Password: password, + // ImageResourceId: imageResourceId }; if (Helper.isPhone(phone)) { body.Phone = Helper.sanitizePhone(phone); diff --git a/src/routes/users/[userId]/users/+page.server.ts b/src/routes/users/[userId]/users/+page.server.ts index 0ec9aa56..31e9b0a3 100644 --- a/src/routes/users/[userId]/users/+page.server.ts +++ b/src/routes/users/[userId]/users/+page.server.ts @@ -2,6 +2,7 @@ import type { RequestEvent, ServerLoadEvent } from '@sveltejs/kit'; import { error } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; import { searchUsers } from '$routes/api/services/reancare/user'; +import { searchPersonRoleTypes } from '$routes/api/services/reancare/person-role-types'; //////////////////////////////////////////////////////////////////////////// @@ -11,19 +12,32 @@ export const load: PageServerLoad = async (event: ServerLoadEvent) => { event.depends('app:users'); const tenantId = event.locals.sessionUser.tenantId; console.log("tenantId.....",tenantId); - const searchParams = { - tenantId:tenantId + + const personRoles = await searchPersonRoleTypes(sessionId); + if (personRoles.Status === 'failure' || personRoles.HttpCode !== 200) { + throw error(personRoles.HttpCode, personRoles.Message); } - const response = await searchUsers(sessionId, { + const selectedRoles: string[] = []; + personRoles.Data.PersonRoleTypes.map(personRole => { + if (personRole.RoleName === "System admin" || + personRole.RoleName === "System user" || + personRole.RoleName === "Tenant admin" || + personRole.RoleName === "Tenant user" + ) { + selectedRoles.push(personRole.id); + } + }); + + const response = await searchUsers(sessionId, { orderBy: 'FirstName', - order : 'ascending' + order : 'ascending', + roleIds: selectedRoles.length ? selectedRoles as string[] : null, }); if (response.Status === 'failure' || response.HttpCode !== 200) { throw error(response.HttpCode, response.Message); } const users = response.Data.Users; - console.log("users", users.Items) return { users, sessionId, diff --git a/src/routes/users/[userId]/users/+page.svelte b/src/routes/users/[userId]/users/+page.svelte index cc84ca6c..45b03b3a 100644 --- a/src/routes/users/[userId]/users/+page.svelte +++ b/src/routes/users/[userId]/users/+page.svelte @@ -6,9 +6,9 @@ import { Helper } from '$lib/utils/helper'; import Icon from '@iconify/svelte'; import { Paginator, type PaginationSettings } from '@skeletonlabs/skeleton'; - import date from 'date-and-time'; import type { PageServerData } from './$types'; import { invalidate } from '$app/navigation'; + import { LocalStorageUtils } from '$lib/utils/local.storage.utils'; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -37,6 +37,7 @@ let isSortingEmail = false; let isSortingPhone = false; let items = 10; + let selectedRoles = []; let paginationSettings = { page: 0, @@ -51,7 +52,18 @@ } } - async function searchUser(model) { + const tmp = LocalStorageUtils.getItem('personRoles'); + const personRoles = JSON.parse(tmp); + personRoles?.map((x) => { + if (x.RoleName === "System admin" || + x.RoleName === "System user" || + x.RoleName === "Tenant admin" || + x.RoleName === "Tenant user") { + selectedRoles.push(x.id); + }}); + + $: console.log("selectedRole", selectedRoles); + async function searchUser(model) { let url = `/api/server/users/search?`; if (sortOrder) url += `sortOrder=${sortOrder}`; else url += `sortOrder=ascending`; @@ -61,6 +73,7 @@ if (firstName) url += `&firstName=${firstName}`; if (email) url += `&email=${email}`; if (phone) url += `&phone=${phone}`; + if (selectedRoles.length > 0) url += `&roleIds=${selectedRoles}`; console.log('URL: ' + url); const res = await fetch(url, { method: 'GET', @@ -189,9 +202,12 @@