Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto-applied subs #855

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"formik": "2.2.9",
"history": "4.10.1",
"iso-639-1": "2.1.4",
"jwt-decode": "3.1.2",
"lodash.camelcase": "4.3.0",
"lodash.capitalize": "4.2.1",
"lodash.clonedeep": "4.5.0",
Expand Down
23 changes: 21 additions & 2 deletions src/components/enterprise-user-subsidy/data/hooks/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
useState, useEffect, useReducer, useCallback, useMemo,
} from 'react';
import { logError } from '@edx/frontend-platform/logging';
import { getConfig } from '@edx/frontend-platform/config';
import { camelCaseObject } from '@edx/frontend-platform/utils';
import { useQuery } from '@tanstack/react-query';
import jwtDecode from 'jwt-decode';
import Cookies from 'universal-cookie';

import { sendEnterpriseTrackEvent } from '@edx/frontend-enterprise-utils';
import { fetchCouponCodeAssignments } from '../../coupons';
Expand Down Expand Up @@ -104,6 +107,20 @@ export function useSubscriptionLicense({
enterpriseIdentityProvider: enterpriseConfig.identityProvider,
}), [enterpriseConfig]);

const decodeJwtCookie = () => {
const cookies = new Cookies();
const cookieValue = cookies.get(getConfig().ACCESS_TOKEN_COOKIE_NAME);
if (!cookieValue) { return null; }
try {
return jwtDecode(cookieValue);
} catch (e) {
const error = Object.create(e);
error.message = '[useSubscriptionLicense] Error decoding JWT token';
error.customAttributes = { cookieValue };
throw error;
}
};

useEffect(() => {
async function retrieveUserLicense() {
let result = await fetchExistingUserLicense(enterpriseId);
Expand All @@ -118,9 +135,11 @@ export function useSubscriptionLicense({
];
const hasCustomerAgreementData = customerAgreementMetadata.every(item => !!item);

// Only request an auto-applied license if ther user is a learner of the enterprise.
const decodedJwt = decodeJwtCookie();

// Only request an auto-applied license if the user is a learner of the enterprise.
// This is mainly to prevent edx operators from accidently getting a license.
const isEnterpriseLearner = !!user.roles.find(userRole => {
const isEnterpriseLearner = !!decodedJwt.roles.find(userRole => {
const [role, enterprise] = userRole.split(':');
return role === 'enterprise_learner' && enterprise === enterpriseId;
});
Expand Down
Loading