Cleanup global state logic a bit, for current user and permissions

this still isn't ideal perhaps, but leaves some clues for improvement
This commit is contained in:
Lance Edgar 2019-11-12 11:49:07 -06:00
parent eab0ebcc00
commit 84b2a2bf70
3 changed files with 33 additions and 26 deletions

View file

@ -20,7 +20,11 @@ export default {
props: {
appsettings: Object,
},
created: function() {
beforeCreate: function() {
// TODO: even with beforeCreate() this logic is still happening "too
// slowly" for us to reliably check perms on first page load,
// i.e. after browser refresh. maybe can avoid async for the GET?
// when main app is first created, fetch user session data from
// backend; this will tell us who the user is (or if they're not yet
@ -30,10 +34,10 @@ export default {
this.$http.get('/api/session').then(response => {
// let all of app know who the user is(n't)
this.$store.commit('setUser', response.data.user)
this.$store.commit('SET_USER', response.data.user)
// also keep track of user's permissions
this.$store.commit('setPermissions', response.data.permissions)
this.$store.commit('SET_PERMISSIONS', response.data.permissions)
// if user is anonymous, and requested logout page, send to login instead
if (!response.data.user && this.$route.name == 'logout') {
@ -47,10 +51,6 @@ export default {
},
mounted: function () {
// // cache the app version
// // this.$store.commit('setAppVersion', appsettings.version);
// this.$store.commit('setAppVersion', this.appsettings.version);
// add "testing" watermark unless running in production mode
// if (!appsettings.production) {
if (!this.appsettings.production) {

View file

@ -174,16 +174,24 @@ export default {
mounted() {
// redirect if user doesn't have permission to be here
if ((this.mode == 'creating' && !this.hasModelPerm('create'))
|| (this.mode == 'editing' && !this.hasModelPerm('edit'))) {
this.$buefy.toast.open({
message: "You do not have permission to access that page.",
type: 'is-danger',
position: 'is-bottom',
})
this.$router.push(this.getModelPathPrefix() + '/')
}
// TODO: this seems like a "good" idea, but in practice, when reloading
// the page/app via browser (Ctrl+Shift+R), the app must re-fetch the
// session details before it knows which user/permissions are in
// effect, and that takes "too long" which means these checks fail!
// // redirect if user doesn't have permission to be here
// if ((this.mode == 'viewing' && !this.hasModelPerm('view'))
// || (this.mode == 'creating' && !this.hasModelPerm('create'))
// || (this.mode == 'editing' && !this.hasModelPerm('edit'))
// || (this.mode == 'deleting' && !this.hasModelPerm('delete'))) {
// this.$buefy.toast.open({
// message: "You do not have permission to access that page.",
// type: 'is-danger',
// position: 'is-bottom',
// })
// this.$router.push(this.getModelPathPrefix() + '/')
// return
// }
// fetch initial page data unless 'creating'
if (this.mode != 'creating') {

View file

@ -1,20 +1,19 @@
export let ByjoveStoreConfig = {
state: {
appVersion: null,
user: null,
permissions: [],
},
mutations: {
setAppVersion(state, payload) {
state.appVersion = payload
SET_USER(state, user) {
state.user = user
},
setUser(state, payload) {
state.user = payload
},
setPermissions(state, payload) {
state.permissions = payload
SET_PERMISSIONS(state, permissions) {
state.permissions = permissions
},
},
// actions: {},
actions: {
// TODO: should we define the logic here, for fetching current session
// from backend API? (thus far that happens in App.mount() instead)
},
}