Skip to content

Commit

Permalink
Update to check org owner via correct end point
Browse files Browse the repository at this point in the history
  • Loading branch information
prajwal-pai77 committed Mar 27, 2024
1 parent 21b3716 commit ef5f28b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-couchbase",
"displayName": "Couchbase",
"description": "",
"version": "1.2.2",
"version": "1.2.3",
"engines": {
"vscode": "^1.63.1"
},
Expand Down
34 changes: 28 additions & 6 deletions src/commands/iq/iqLoginhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface IFormData {
rememberMe: boolean;
}

type User = {
id: string;
roles: string[];
};

export interface ISavedLoginDataGetter {
doesLoginDetailsExists: boolean;
username: string;
Expand Down Expand Up @@ -114,6 +119,16 @@ export const handleIqSupplementalTerms = async (
);
};

export const checkIfUserIsOrgOwner = async (
userId: string,
user: User
): Promise<boolean> => {
if (user.id === userId) {
return user.roles.includes("organizationOwner");
}
return false;
};

export const verifyOrganization = async (orgId: string): Promise<any> => {
const jwtToken = Memory.state.get<string>("vscode-couchbase.iq.jwtToken");
if (jwtToken === undefined) {
Expand All @@ -128,6 +143,13 @@ export const verifyOrganization = async (orgId: string): Promise<any> => {
orgId
);
const userId = Memory.state.get<string>("vscode-couchbase.iq.userId");
if (userId === undefined) {
return {
shouldAcceptIqTerms: false,
isOrgVerified: false,
errorMessage: "",
};
}
if (!orgDetails.iq || orgDetails.iq.enabled === false) {
return {
shouldAcceptIqTerms: false,
Expand All @@ -139,13 +161,13 @@ export const verifyOrganization = async (orgId: string): Promise<any> => {
!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,
Expand Down
17 changes: 17 additions & 0 deletions src/commands/iq/iqRestApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ef5f28b

Please sign in to comment.