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

Implemented: Code to check if user has permission to access the app(#2hr41aq) #221

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Changes from all 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 .env.example
Original file line number Diff line number Diff line change
@@ -13,3 +13,4 @@ VUE_APP_INITIAL_JOB_TYPES={"JOB_IMP_PROD_NEW_BLK":"products","JOB_IMP_ORD_BLK":"
VUE_APP_BASE_URL=
VUE_APP_BATCH_JOB_ENUMS={"JOB_BKR_ORD_UNF":{"id":"JOB_BKR_ORD_UNF","facilityId":"_NA_","unfillable": true},"JOB_BKR_ORD":{"id": "JOB_BKR_ORD","facilityId":"_NA_","unfillable": false},"JOB_BKR_PREORD_UNF":{"id":"JOB_BKR_PREORD_UNF","facilityId":"PRE_ORDER_PARKING","unfillable":true},"JOB_BKR_PREORD":{"id":"JOB_BKR_PREORD","facilityId":"PRE_ORDER_PARKING","unfillable":false},"JOB_BKR_BACKORD_UNF":{"id":"JOB_BKR_BACKORD_UNF","facilityId":"BACKORDER_PARKING","unfillable":true},"JOB_BKR_BACKORD":{"id":"JOB_BKR_BACKORD","facilityId":"BACKORDER_PARKING","unfillable":false}}
VUE_APP_WEBHOOK_ENUMS={"NEW_PRODUCTS":"products/create","DELETE_PRODUCTS":"products/update","NEW_ORDERS":"orders/create","CANCELLED_ORDERS":"orders/cancelled","PAYMENT_STATUS":"orders/paid","RETURNS":"","BULK_OPERATIONS_FINISH":"bulk_operations/finish"}
VUE_APP_PERMISSION_ID=
7 changes: 2 additions & 5 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -106,11 +106,8 @@ const api = async (customConfig: any) => {
}
}

let baseURL = process.env.VUE_APP_BASE_URL;
if (!baseURL) {
baseURL = store.getters['user/getInstanceUrl'];
baseURL = baseURL && baseURL.startsWith('http') ? baseURL : `https://${baseURL}.hotwax.io/api/`;
}
let baseURL = store.getters['user/getInstanceUrl'];
baseURL = baseURL && baseURL.startsWith('http') ? baseURL : `https://${baseURL}.hotwax.io/api/`;
if (baseURL) config.baseURL = baseURL;

if(customConfig.cache) config.adapter = axiosCache.adapter;
17 changes: 15 additions & 2 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import api from '@/api'
import api, {client} from '@/api'
import store from '@/store';

const login = async (username: string, password: string): Promise <any> => {
return api({
@@ -11,6 +12,17 @@ const login = async (username: string, password: string): Promise <any> => {
});
}

const checkPermission = async (payload: any): Promise <any> => {
let baseURL = store.getters['user/getInstanceUrl'];
baseURL = baseURL && baseURL.startsWith('http') ? baseURL : `https://${baseURL}.hotwax.io/api/`;
return client({
url: "checkPermission",
method: "post",
baseURL: baseURL,
...payload
});
}

const getProfile = async (): Promise <any> => {
return api({
url: "user-profile",
@@ -109,5 +121,6 @@ export const UserService = {
setUserTimeZone,
updatePinnedJobPref,
setUserPreference,
getUserPreference
getUserPreference,
checkPermission
}
28 changes: 28 additions & 0 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
@@ -17,9 +17,37 @@ const actions: ActionTree<UserState, RootState> = {
const resp = await UserService.login(username, password)
if (resp.status === 200 && resp.data) {
if (resp.data.token) {
const permissionId = process.env.VUE_APP_PERMISSION_ID;
if (permissionId) {
const checkPermissionResponse = await UserService.checkPermission({
data: {
permissionId
},
headers: {
Authorization: 'Bearer ' + resp.data.token,
'Content-Type': 'application/json'
}
});

if (checkPermissionResponse.status === 200 && !hasError(checkPermissionResponse) && checkPermissionResponse.data && checkPermissionResponse.data.hasPermission) {
commit(types.USER_TOKEN_CHANGED, { newToken: resp.data.token })
dispatch('getProfile')
if (resp.data._EVENT_MESSAGE_ && resp.data._EVENT_MESSAGE_.startsWith("Alert:")) {
// TODO Internationalise text
showToast(translate(resp.data._EVENT_MESSAGE_));
}
return resp.data;
} else {
const permissionError = 'You do not have permission to access the app.';
showToast(translate(permissionError));
console.error("error", permissionError);
return Promise.reject(new Error(permissionError));
}
} else {
commit(types.USER_TOKEN_CHANGED, { newToken: resp.data.token })
await dispatch('getProfile')
return resp.data;
}
} else if (hasError(resp)) {
showToast(translate('Sorry, your username or password is incorrect. Please try again.'));
console.error("error", resp.data._ERROR_MESSAGE_);
3 changes: 2 additions & 1 deletion src/store/modules/user/getters.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@ const getters: GetterTree <UserState, RootState> = {
return state.current
},
getInstanceUrl (state) {
return state.instanceUrl;
const baseUrl = process.env.VUE_APP_BASE_URL;
return baseUrl ? baseUrl : state.instanceUrl;
},
getCurrentShopifyConfigId (state) {
return state.currentShopifyConfigId;