Add app/menu support for "become / stop being root" feature

This commit is contained in:
Lance Edgar 2019-11-26 16:43:35 -06:00
parent 3240da9121
commit 817d8bef13
3 changed files with 73 additions and 2 deletions

View file

@ -35,6 +35,7 @@ export default {
// let all of app know who the user is(n't)
this.$store.commit('SET_USER', response.data.user)
this.$store.commit('SET_USER_IS_ROOT', response.data.user.is_root)
// also keep track of user's permissions
this.$store.commit('SET_PERMISSIONS', response.data.permissions)

View file

@ -6,7 +6,7 @@
<b-dropdown v-if="user"
aria-role="list">
<button class="button is-primary"
:class="{'is-danger': user.is_root}"
:class="{'is-danger': user_is_root}"
slot="trigger">
<span>{{ user.short_name }}</span>
<!-- <b-icon icon="menu-down"></b-icon> -->
@ -18,6 +18,20 @@
<slot></slot>
<b-dropdown-item v-if="user.is_admin && !user_is_root"
aria-role="listitem"
has-link
class="root-user">
<a href="#" @click.prevent="becomeRoot()">Become root</a>
</b-dropdown-item>
<b-dropdown-item v-if="user_is_root"
aria-role="listitem"
has-link
class="root-user">
<a href="#" @click.prevent="stopRoot()">Stop being root</a>
</b-dropdown-item>
<b-dropdown-item aria-role="listitem" has-link>
<router-link to="/logout">Logout</router-link>
</b-dropdown-item>
@ -57,6 +71,14 @@ export default {
type: String,
default: '/api/feedback',
},
becomeRootUrl: {
type: String,
default: '/api/become-root',
},
stopRootUrl: {
type: String,
default: '/api/stop-root',
},
},
components: {
ByjoveFeedback,
@ -71,7 +93,51 @@ export default {
},
user: function() {
return this.$store.state.user
}
},
user_is_root: function() {
return this.$store.state.user_is_root
},
},
methods: {
becomeRoot() {
if (this.user_is_root || !this.user.is_admin) {
return
}
this.$http.post(this.becomeRootUrl).then(response => {
this.$store.commit('SET_USER_IS_ROOT', true)
this.$buefy.toast.open({
message: "You have been elevated to 'root' and now have full system access",
type: 'is-success',
position: 'is-bottom',
})
}, response => {
this.$buefy.toast.open({
message: "Something went wrong!",
type: 'is-danger',
position: 'is-bottom',
})
})
},
stopRoot() {
if (!this.user_is_root) {
return
}
this.$http.post(this.stopRootUrl).then(response => {
this.$store.commit('SET_USER_IS_ROOT', false)
this.$buefy.toast.open({
message: "Your normal system access has been restored",
type: 'is-success',
position: 'is-bottom',
})
}, response => {
this.$buefy.toast.open({
message: "Something went wrong!",
type: 'is-danger',
position: 'is-bottom',
})
})
},
},
}
</script>

View file

@ -2,12 +2,16 @@
export let ByjoveStoreConfig = {
state: {
user: null,
user_is_root: null,
permissions: [],
},
mutations: {
SET_USER(state, user) {
state.user = user
},
SET_USER_IS_ROOT(state, is_root) {
state.user_is_root = is_root
},
SET_PERMISSIONS(state, permissions) {
state.permissions = permissions
},