diff --git a/package-lock.json b/package-lock.json index bb73bac3..943bce85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-couchbase", - "version": "1.2.2", + "version": "1.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vscode-couchbase", - "version": "1.2.2", + "version": "1.2.3", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@chatscope/chat-ui-kit-styles": "^1.4.0", diff --git a/package.json b/package.json index 4d2ab58f..ff931584 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-couchbase", "displayName": "Couchbase", "description": "", - "version": "1.2.2", + "version": "1.2.3", "engines": { "vscode": "^1.63.1" }, diff --git a/src/commands/iq/iqLoginhandler.ts b/src/commands/iq/iqLoginhandler.ts index bbb1ef8d..f46f46ef 100644 --- a/src/commands/iq/iqLoginhandler.ts +++ b/src/commands/iq/iqLoginhandler.ts @@ -9,6 +9,11 @@ interface IFormData { rememberMe: boolean; } +type User = { + id: string; + roles: string[]; +}; + export interface ISavedLoginDataGetter { doesLoginDetailsExists: boolean; username: string; @@ -114,6 +119,16 @@ export const handleIqSupplementalTerms = async ( ); }; +export const checkIfUserIsOrgOwner = async ( + userId: string, + user: User +): Promise => { + if (user.id === userId) { + return user.roles.includes("organizationOwner"); + } + return false; +}; + export const verifyOrganization = async (orgId: string): Promise => { const jwtToken = Memory.state.get("vscode-couchbase.iq.jwtToken"); if (jwtToken === undefined) { @@ -128,6 +143,13 @@ export const verifyOrganization = async (orgId: string): Promise => { orgId ); const userId = Memory.state.get("vscode-couchbase.iq.userId"); + if (userId === undefined) { + return { + shouldAcceptIqTerms: false, + isOrgVerified: false, + errorMessage: "", + }; + } if (!orgDetails.iq || orgDetails.iq.enabled === false) { return { shouldAcceptIqTerms: false, @@ -139,13 +161,13 @@ export const verifyOrganization = async (orgId: string): Promise => { !orgDetails.iq.other || orgDetails.iq.other.isTermsAcceptedForOrg === false ) { + const userList = await iqRestApiService.fetchUserData( + jwtToken, + orgId, + userId + ); // Allow to Accept Terms ONLY if user is org owner - if ( - (orgDetails.createdByUserID && - orgDetails.createdByUserID === userId) || - (orgDetails.modifiedByUserID && - orgDetails.modifiedByUserID === userId) - ) { + if (await checkIfUserIsOrgOwner(userId, userList)) { return { shouldAcceptIqTerms: true, isOrgVerified: false, diff --git a/src/commands/iq/iqRestApiService.ts b/src/commands/iq/iqRestApiService.ts index 7c84c1a2..873daee8 100644 --- a/src/commands/iq/iqRestApiService.ts +++ b/src/commands/iq/iqRestApiService.ts @@ -95,6 +95,23 @@ export class iqRestApiService { } }; + public static fetchUserData = async (jwt: string, orgId: string, userId:string) => { + try { + const content = await axios.get( + this.FETCH_ORGANIZATIONS_URL + "/" + orgId + "/users" + "/" + userId, + { + headers: { + Authorization: `Bearer ${jwt}`, + }, + } + ); + return content.data.data; + } catch (error) { + logger.error("failed to Fetch Users Data in the organization " + error); + throw new Error("Failed to fetch users data in the organization "); + } + }; + public static sendIqMessage = async ( jwt: string, orgId: string,