Skip to content

Commit

Permalink
Improved: syntax, removed unused code, and fixed permissionId duplica…
Browse files Browse the repository at this point in the history
…tion in api payload (hotwax#244)
  • Loading branch information
amansinghbais committed Jan 18, 2024
1 parent 250b44c commit 44588ed
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 81 deletions.
3 changes: 1 addition & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ export default defineComponent({
computed: {
...mapGetters({
userToken: 'user/getUserToken',
instanceUrl: 'user/getInstanceUrl',
permissions: 'user/getUserPermissions'
instanceUrl: 'user/getInstanceUrl'
})
},
setup(){
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,6 @@
"Username": "Username",
"View all date time formats supported by the HotWax Import app.": "View all date time formats supported by the HotWax Import app.",
"View": "View",
"Would you like to update your time zone to . Your profile is currently set to . This setting can always be changed from the settings menu.": "Would you like to update your time zone to {localTimeZone}. Your profile is currently set to {profileTimeZone}. This setting can always be changed from the settings menu."
"Would you like to update your time zone to . Your profile is currently set to . This setting can always be changed from the settings menu.": "Would you like to update your time zone to {localTimeZone}. Your profile is currently set to {profileTimeZone}. This setting can always be changed from the settings menu.",
"You do not have permission to access this page": "You do not have permission to access this page"
}
12 changes: 0 additions & 12 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ 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",
Expand Down Expand Up @@ -175,6 +164,5 @@ export const UserService = {
getProfile,
getUserPermissions,
setUserTimeZone,
checkPermission,
updateFieldMapping
}
102 changes: 36 additions & 66 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,44 @@ const actions: ActionTree<UserState, RootState> = {
const { token, oms } = payload;
dispatch("setUserInstanceUrl", oms);

const permissionId = process.env.VUE_APP_PERMISSION_ID;

// Prepare permissions list
const serverPermissionsFromRules = getServerPermissionsFromRules();
if (permissionId) serverPermissionsFromRules.push(permissionId);

const serverPermissions = await UserService.getUserPermissions({
permissionIds: serverPermissionsFromRules
}, token);

const appPermissions = prepareAppPermissions(serverPermissions);

// Checking if the user has permission to access the app
// If there is no configuration, the permission check is not enabled
if (permissionId) {
// As the token is not yet set in the state passing token headers explicitly
// TODO Abstract this out, how token is handled should be part of the method not the callee
const hasPermission = appPermissions.some((appPermission: any) => appPermission.action === permissionId);
// If there are any errors or permission check fails do not allow user to login
if (!hasPermission) {
const permissionError = 'You do not have permission to access the app.';
showToast(translate(permissionError));
logger.error("error", permissionError);
return Promise.reject(new Error(permissionError));
if(token) {
const permissionId = process.env.VUE_APP_PERMISSION_ID;

// Prepare permissions list
const serverPermissionsFromRules = getServerPermissionsFromRules();
if (permissionId) serverPermissionsFromRules.push(permissionId);

const serverPermissions = await UserService.getUserPermissions({
permissionIds: [...new Set(serverPermissionsFromRules)]
}, token);

const appPermissions = prepareAppPermissions(serverPermissions);

// Checking if the user has permission to access the app
// If there is no configuration, the permission check is not enabled
if (permissionId) {
// As the token is not yet set in the state passing token headers explicitly
// TODO Abstract this out, how token is handled should be part of the method not the callee
const hasPermission = appPermissions.some((appPermission: any) => appPermission.action === permissionId);
// If there are any errors or permission check fails do not allow user to login
if (!hasPermission) {
const permissionError = 'You do not have permission to access the app.';
showToast(translate(permissionError));
logger.error("error", permissionError);
return Promise.reject(new Error(permissionError));
}
}

updateToken(token)
setPermissions(appPermissions);

// TODO user single mutation
commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
commit(types.USER_TOKEN_CHANGED, { newToken: token })

await dispatch('getProfile')
dispatch('setPreferredDateTimeFormat', process.env.VUE_APP_DATE_FORMAT ? process.env.VUE_APP_DATE_FORMAT : 'MM/dd/yyyy');
}

updateToken(token)
setPermissions(appPermissions);

// TODO user single mutation
commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
commit(types.USER_TOKEN_CHANGED, { newToken: token })

await dispatch('getProfile')
dispatch('setPreferredDateTimeFormat', process.env.VUE_APP_DATE_FORMAT ? process.env.VUE_APP_DATE_FORMAT : 'MM/dd/yyyy');

// if (token) {
// const permissionId = process.env.VUE_APP_PERMISSION_ID;
// if (permissionId) {
// const checkPermissionResponse = await UserService.checkPermission({
// data: {
// permissionId
// },
// headers: {
// Authorization: 'Bearer ' + token,
// 'Content-Type': 'application/json'
// }
// });

// if (checkPermissionResponse.status === 200 && !hasError(checkPermissionResponse) && checkPermissionResponse.data && checkPermissionResponse.data.hasPermission) {
// commit(types.USER_TOKEN_CHANGED, { newToken: token })
// updateToken(token)
// await dispatch('getProfile')
// dispatch('setPreferredDateTimeFormat', process.env.VUE_APP_DATE_FORMAT ? process.env.VUE_APP_DATE_FORMAT : 'MM/dd/yyyy');
// } else {
// const permissionError = 'You do not have permission to access the app.';
// showToast(translate(permissionError));
// logger.error("error", permissionError);
// return Promise.reject(new Error(permissionError));
// }
// } else {
// commit(types.USER_TOKEN_CHANGED, { newToken: token })
// updateToken(token)
// await dispatch('getProfile')
// dispatch('setPreferredDateTimeFormat', process.env.VUE_APP_DATE_FORMAT ? process.env.VUE_APP_DATE_FORMAT : 'MM/dd/yyyy');
// }
// }
} catch (err: any) {
showToast(translate('Something went wrong'));
logger.error("error", err);
Expand Down

0 comments on commit 44588ed

Please sign in to comment.