diff --git a/frontend/src/API.js b/frontend/src/API.js index 138ffdc7..e323888c 100644 --- a/frontend/src/API.js +++ b/frontend/src/API.js @@ -200,8 +200,8 @@ class Api { return localStorage.removeItem(tokenKey) } - saveToken(username, token) { - const user = {username: username, token: token} + saveToken(username, token, admin) { + const user = {username: username, token: token, admin: admin} localStorage.setItem(tokenKey, JSON.stringify(user)); return user } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 94586455..69a26c2b 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -22,20 +22,20 @@ app: null } }, - async created() { - this.app = await this.$store.dispatch('loadRequired') + async created() { + this.app = await this.$store.dispatch('loadRequired') - this.app = {...this.$store.state} + this.app = {...this.$store.state} - if (this.$store.getters.core.logged_in) { - await this.$store.dispatch('loadAdmin') - } - this.loaded = true - if (!this.$store.getters.core.setup) { - this.$router.push('/setup') - } - window.console.log('finished loadRequired') - }, + if (this.$store.getters.core.logged_in) { + await this.$store.dispatch('loadAdmin') + } + this.loaded = true + if (!this.$store.getters.core.setup) { + this.$router.push('/setup') + } + window.console.log('finished loadRequired') + }, async mounted() { if (this.$route.path !== '/setup') { const tk = localStorage.getItem("statping_user") diff --git a/frontend/src/components/Dashboard/DashboardMessages.vue b/frontend/src/components/Dashboard/DashboardMessages.vue index 0b5ac770..b70c90ad 100644 --- a/frontend/src/components/Dashboard/DashboardMessages.vue +++ b/frontend/src/components/Dashboard/DashboardMessages.vue @@ -21,7 +21,7 @@ {{niceDate(message.start_on)}} -
+
Edit
@@ -33,7 +33,7 @@
- + diff --git a/frontend/src/components/Dashboard/DashboardServices.vue b/frontend/src/components/Dashboard/DashboardServices.vue index 8617cdf9..fa1e6e3f 100644 --- a/frontend/src/components/Dashboard/DashboardServices.vue +++ b/frontend/src/components/Dashboard/DashboardServices.vue @@ -2,7 +2,7 @@ diff --git a/frontend/src/components/Dashboard/ServicesList.vue b/frontend/src/components/Dashboard/ServicesList.vue index a8a1fb0d..8b5539c5 100644 --- a/frontend/src/components/Dashboard/ServicesList.vue +++ b/frontend/src/components/Dashboard/ServicesList.vue @@ -12,11 +12,11 @@ - + {{service.name}} - + @@ -25,17 +25,19 @@ -
{{serviceGroup(service)}}
+
+ {{serviceGroup(service)}} +
diff --git a/frontend/src/components/Dashboard/TopNav.vue b/frontend/src/components/Dashboard/TopNav.vue index 4df6a352..89c6dcef 100644 --- a/frontend/src/components/Dashboard/TopNav.vue +++ b/frontend/src/components/Dashboard/TopNav.vue @@ -14,17 +14,16 @@ - - - - @@ -47,17 +46,18 @@ name: 'TopNav', data () { return { - navopen: false, + navopen: false } }, - methods: { - async logout () { - await Api.logout() - this.$store.commit('setHasAllData', false) - this.$store.commit('setToken', null) - await this.$router.push('/') + methods: { + async logout () { + await Api.logout() + this.$store.commit('setHasAllData', false) + this.$store.commit('setToken', null) + this.$store.commit('setAdmin', false) + await this.$router.push('/') + } } - } } diff --git a/frontend/src/forms/Incident.vue b/frontend/src/forms/Incident.vue index 170dde8c..eee400db 100644 --- a/frontend/src/forms/Incident.vue +++ b/frontend/src/forms/Incident.vue @@ -3,7 +3,7 @@
Incident: {{incident.title}} -
@@ -15,7 +15,7 @@
-
+
Create Incident for {{service.name}}
diff --git a/frontend/src/forms/Login.vue b/frontend/src/forms/Login.vue index fc03faab..8b007bd3 100644 --- a/frontend/src/forms/Login.vue +++ b/frontend/src/forms/Login.vue @@ -60,8 +60,10 @@ if (auth.error) { this.error = true } else if (auth.token) { - this.auth = Api.saveToken(this.username, auth.token) + this.auth = Api.saveToken(this.username, auth.token, auth.admin) this.$store.dispatch('loadAdmin') + this.$store.commit('setAdmin', auth.admin) + window.console.log(auth) this.$router.push('/dashboard') } this.loading = false diff --git a/frontend/src/mixin.js b/frontend/src/mixin.js index 07c87fa5..386ad777 100644 --- a/frontend/src/mixin.js +++ b/frontend/src/mixin.js @@ -61,6 +61,9 @@ export default Vue.mixin({ isInt(n) { return n % 1 === 0; }, + isAdmin() { + return this.$store.state.admin + }, loggedIn() { const core = this.$store.getters.core return core.logged_in === true diff --git a/frontend/src/pages/Dashboard.vue b/frontend/src/pages/Dashboard.vue index 7044bf8a..2f95a017 100644 --- a/frontend/src/pages/Dashboard.vue +++ b/frontend/src/pages/Dashboard.vue @@ -1,12 +1,12 @@ diff --git a/frontend/src/store.js b/frontend/src/store.js index f89c1e1c..d2f51e28 100644 --- a/frontend/src/store.js +++ b/frontend/src/store.js @@ -25,7 +25,8 @@ export default new Vuex.Store({ groups: [], messages: [], users: [], - notifiers: [] + notifiers: [], + admin: false }, getters: { hasAllData: state => state.hasAllData, @@ -39,6 +40,8 @@ export default new Vuex.Store({ users: state => state.users, notifiers: state => state.notifiers, + isAdmin: state => state.admin, + servicesInOrder: state => state.services.sort((a, b) => a.order_id - b.order_id), groupsInOrder: state => state.groups.sort((a, b) => a.order_id - b.order_id), groupsClean: state => state.groups.filter(g => g.name !== '').sort((a, b) => a.order_id - b.order_id), @@ -106,7 +109,10 @@ export default new Vuex.Store({ }, setNotifiers (state, notifiers) { state.notifiers = notifiers - } + }, + setAdmin (state, admin) { + state.admin = admin + }, }, actions: { async getAllServices(context) { diff --git a/handlers/api.go b/handlers/api.go index fae5ad22..e823fa02 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -43,13 +43,12 @@ type apiResponse struct { } func apiIndexHandler(r *http.Request) interface{} { - coreClone := core.App - var loggedIn bool + coreClone := *core.App _, err := getJwtToken(r) if err == nil { - loggedIn = true + coreClone.LoggedIn = true + coreClone.IsAdmin = IsAdmin(r) } - coreClone.LoggedIn = loggedIn return coreClone } diff --git a/handlers/dashboard.go b/handlers/dashboard.go index a0cfa5a7..64d7dd84 100644 --- a/handlers/dashboard.go +++ b/handlers/dashboard.go @@ -212,12 +212,14 @@ func apiLoginHandler(w http.ResponseWriter, r *http.Request) { user, auth := users.AuthUser(username, password) if auth { utils.Log.Infoln(fmt.Sprintf("User %v logged in from IP %v", user.Username, r.RemoteAddr)) - _, token := setJwtToken(user, w) + claim, token := setJwtToken(user, w) resp := struct { - Token string `json:"token"` + Token string `json:"token"` + IsAdmin bool `json:"admin"` }{ token, + claim.Admin, } returnJson(resp, w, r) } else { diff --git a/handlers/routes.go b/handlers/routes.go index 418d6061..46fd0ca8 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -87,9 +87,9 @@ func Router() *mux.Router { // API Routes r.Handle("/api", scoped(apiIndexHandler)) + r.Handle("/api/setup", http.HandlerFunc(processSetupHandler)).Methods("POST") //r.Handle("/oauth/callback", http.HandlerFunc(OAuthRedirect)) api.Handle("/api/login", http.HandlerFunc(apiLoginHandler)).Methods("POST") - r.Handle("/api/setup", http.HandlerFunc(processSetupHandler)).Methods("POST") api.Handle("/api/logout", http.HandlerFunc(logoutHandler)) api.Handle("/api/renew", authenticated(apiRenewHandler, false)) api.Handle("/api/cache", authenticated(apiCacheHandler, false)).Methods("GET") diff --git a/types/core/struct.go b/types/core/struct.go index a7351a9e..20dfe8dd 100644 --- a/types/core/struct.go +++ b/types/core/struct.go @@ -31,6 +31,7 @@ type Core struct { UseCdn null.NullBool `gorm:"column:use_cdn;default:false" json:"using_cdn,omitempty"` Timezone float32 `gorm:"column:timezone;default:-8.0" json:"timezone,omitempty"` LoggedIn bool `gorm:"-" json:"logged_in"` + IsAdmin bool `gorm:"-" json:"admin"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` Started time.Time `gorm:"-" json:"started_on"`