This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/* Xeslint-disable */
|
||||
import apiUtil from "./apiutil";
|
||||
import { processLogin, processLogout } from "../utils/authutil";
|
||||
import { processLogin, processLogout } from "./authutil";
|
||||
|
||||
export default {
|
||||
async authenticate(login, password) {
|
||||
|
||||
71
ayanova/src/api/authutil.js
Normal file
71
ayanova/src/api/authutil.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/* xeslint-disable */
|
||||
import decode from "jwt-decode";
|
||||
import store from "../store";
|
||||
import initialize from "./initialize";
|
||||
|
||||
export function processLogin(response) {
|
||||
//is token present?
|
||||
if (!response || !response.data || !response.data.token) {
|
||||
store.commit("logItem", "auth::processLogin -> response empty");
|
||||
return Promise.reject();
|
||||
}
|
||||
const token = decode(response.data.token);
|
||||
|
||||
if (!token || !token.iss) {
|
||||
store.commit("logItem", "auth::processLogin -> response token empty");
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
if (token.iss != "ayanova.com") {
|
||||
store.commit(
|
||||
"logItem",
|
||||
"auth::processLogin -> token invalid (iss): " + token.iss
|
||||
);
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
//Put app relevant items into vuex store so app can use them
|
||||
store.commit("login", {
|
||||
apiToken: response.data.token,
|
||||
authenticated: true,
|
||||
userId: Number(token.id),
|
||||
roles: token["ayanova/roles"]
|
||||
});
|
||||
|
||||
//Initialize the application
|
||||
initialize();
|
||||
store.commit(
|
||||
"logItem",
|
||||
"auth::processLogin -> User " + token.id + " logged in"
|
||||
);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
export function processLogout() {
|
||||
if (store.state.authenticated) {
|
||||
store.commit("logItem", "auth::processLogout -> User logged out");
|
||||
}
|
||||
store.commit("logout");
|
||||
}
|
||||
|
||||
export function isLoggedIn() {
|
||||
//const token = getToken();
|
||||
return !!store.state.apiToken && !isTokenExpired(store.state.apiToken);
|
||||
}
|
||||
|
||||
function getTokenExpirationDate(encodedToken) {
|
||||
const token = decode(encodedToken);
|
||||
if (!token.exp) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const date = new Date(0);
|
||||
date.setUTCSeconds(token.exp);
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
function isTokenExpired(token) {
|
||||
const expirationDate = getTokenExpirationDate(token);
|
||||
return expirationDate < new Date();
|
||||
}
|
||||
34
ayanova/src/api/errorhandler.js
Normal file
34
ayanova/src/api/errorhandler.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/* xeslint-disable */
|
||||
import store from "../store";
|
||||
|
||||
function dealWithError(msg) {
|
||||
store.commit("logItem", msg);
|
||||
}
|
||||
export default {
|
||||
handleGeneralError(message, source, lineno, colno, error) {
|
||||
var msg = "GeneralError: \n" + message;
|
||||
if (source) {
|
||||
msg += "\nsource: " + source;
|
||||
}
|
||||
if (lineno) {
|
||||
msg += "\nlineno: " + lineno;
|
||||
}
|
||||
if (colno) {
|
||||
msg += "\ncolno: " + colno;
|
||||
}
|
||||
if (error) {
|
||||
msg += "\nerror: " + error;
|
||||
}
|
||||
dealWithError(msg);
|
||||
},
|
||||
handleVueError(err, vm, info) {
|
||||
var msg = "VueError: \n" + err;
|
||||
if (vm) {
|
||||
msg += "\nvm present ";
|
||||
}
|
||||
if (info) {
|
||||
msg += "\ninfo: " + info;
|
||||
}
|
||||
dealWithError(msg);
|
||||
}
|
||||
};
|
||||
127
ayanova/src/api/initialize.js
Normal file
127
ayanova/src/api/initialize.js
Normal file
@@ -0,0 +1,127 @@
|
||||
/* xeslint-disable */
|
||||
import store from "../store";
|
||||
import roles from "./roles";
|
||||
import lt from "./locale";
|
||||
import api from "./apiutil";
|
||||
|
||||
function addNavItem(title, icon, route) {
|
||||
store.commit("addNavItem", {
|
||||
title,
|
||||
icon,
|
||||
route
|
||||
});
|
||||
}
|
||||
|
||||
/////////////////////////////////////
|
||||
// Initialize the app
|
||||
// on change of authentication status
|
||||
export default function initialize() {
|
||||
if (store.state.authenticated) {
|
||||
//GET LOCALIZED TEXT FOR SHELL
|
||||
lt.fetch([
|
||||
"Home",
|
||||
"Service",
|
||||
"Dispatch",
|
||||
"Inventory",
|
||||
"Accounting",
|
||||
"Administration",
|
||||
"Operations",
|
||||
"HelpAboutAyaNova",
|
||||
"Logout"
|
||||
])
|
||||
.then(function() {
|
||||
//put nav items into store
|
||||
//Everyone has a home
|
||||
addNavItem(lt.get("Home"), "home", "/");
|
||||
|
||||
if (
|
||||
roles.hasRole(roles.AuthorizationRoles.TechLimited) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.TechFull) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.SubContractorLimited) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.SubContractorFull)
|
||||
) {
|
||||
addNavItem(lt.get("Service"), "toolbox", "/service");
|
||||
}
|
||||
|
||||
if (
|
||||
roles.hasRole(roles.AuthorizationRoles.DispatchLimited) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.DispatchFull)
|
||||
) {
|
||||
addNavItem(lt.get("Dispatch"), "shipping-fast", "/dispatch");
|
||||
}
|
||||
|
||||
if (
|
||||
roles.hasRole(roles.AuthorizationRoles.InventoryLimited) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.InventoryFull)
|
||||
) {
|
||||
addNavItem(lt.get("Inventory"), "dolly", "/inventory");
|
||||
}
|
||||
|
||||
if (roles.hasRole(roles.AuthorizationRoles.AccountingFull)) {
|
||||
addNavItem(
|
||||
lt.get("Accounting"),
|
||||
"file-invoice-dollar",
|
||||
"/accounting"
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
roles.hasRole(roles.AuthorizationRoles.BizAdminLimited) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.BizAdminFull)
|
||||
) {
|
||||
addNavItem(lt.get("Administration"), "user-tie", "/admin");
|
||||
}
|
||||
|
||||
if (
|
||||
roles.hasRole(roles.AuthorizationRoles.OpsAdminFull) ||
|
||||
roles.hasRole(roles.AuthorizationRoles.OpsAdminLimited)
|
||||
) {
|
||||
addNavItem(lt.get("Operations"), "cogs", "ops");
|
||||
}
|
||||
|
||||
//Everyone can see about and logout
|
||||
addNavItem(lt.get("HelpAboutAyaNova"), "info-circle", "/about");
|
||||
addNavItem(lt.get("Logout"), "sign-out-alt", "/login");
|
||||
})
|
||||
.catch(function(error) {
|
||||
store.commit("logItem", "Initialize::() ltfetch -> error" + error);
|
||||
throw error;
|
||||
});
|
||||
|
||||
//CACHE LOCALE SETTINGS
|
||||
//check the timezone offset is still valid, offer to change it if not
|
||||
//api.get("UserOptions/" + store.state.userId).then(res => {
|
||||
api
|
||||
.get("UserOptions/" + store.state.userId)
|
||||
.then(res => {
|
||||
if (res.error) {
|
||||
//In a form this would trigger a bunch of validation or error display code but for here and now:
|
||||
//convert error to human readable string for display
|
||||
alert(api.apiErrorToHumanString(res.error));
|
||||
} else {
|
||||
var localOffset = new Date().getTimezoneOffset();
|
||||
if (localOffset != 0) {
|
||||
localOffset = (localOffset / 60) * -1; //time is in minutes and reversed from what we want or expect
|
||||
}
|
||||
|
||||
if (res.data.timeZoneOffset != localOffset) {
|
||||
//todo: timezone doesn't match, offer to fix it
|
||||
alert(
|
||||
"Time zone offset for this account is set to " +
|
||||
res.data.timeZoneOffset +
|
||||
" which doesn't match the local timezone offset of " +
|
||||
localOffset +
|
||||
"."
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
store.commit(
|
||||
"logItem",
|
||||
"Initialize::() fetch useroptions -> error" + error
|
||||
);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
43
ayanova/src/api/roles.js
Normal file
43
ayanova/src/api/roles.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import store from "../store";
|
||||
export default {
|
||||
AuthorizationRoles: {
|
||||
///<summary>No role set</summary>
|
||||
NoRole: 0,
|
||||
///<summary>BizAdminLimited</summary>
|
||||
BizAdminLimited: 1,
|
||||
///<summary>BizAdminFull</summary>
|
||||
BizAdminFull: 2,
|
||||
///<summary>DispatchLimited</summary>
|
||||
DispatchLimited: 4,
|
||||
///<summary>DispatchFull</summary>
|
||||
DispatchFull: 8,
|
||||
///<summary>InventoryLimited</summary>
|
||||
InventoryLimited: 16,
|
||||
///<summary>InventoryFull</summary>
|
||||
InventoryFull: 32,
|
||||
///<summary>AccountingFull</summary>
|
||||
AccountingFull: 64, //No limited role, not sure if there is a need
|
||||
///<summary>TechLimited</summary>
|
||||
TechLimited: 128,
|
||||
///<summary>TechFull</summary>
|
||||
TechFull: 256,
|
||||
///<summary>SubContractorLimited</summary>
|
||||
SubContractorLimited: 512,
|
||||
///<summary>SubContractorFull</summary>
|
||||
SubContractorFull: 1024,
|
||||
///<summary>ClientLimited</summary>
|
||||
ClientLimited: 2048,
|
||||
///<summary>ClientFull</summary>
|
||||
ClientFull: 4096,
|
||||
///<summary>OpsAdminLimited</summary>
|
||||
OpsAdminLimited: 8192,
|
||||
///<summary>OpsAdminFull</summary>
|
||||
OpsAdminFull: 16384
|
||||
},
|
||||
hasRole(role) {
|
||||
if (!store.state.roles || store.state.roles === 0) {
|
||||
return false;
|
||||
}
|
||||
return role === (store.state.roles & role);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user