Browse Source

added API link for drupal admins in menu

bach 2 years ago
parent
commit
6776b8f1d8
2 changed files with 65 additions and 7 deletions
  1. 8 2
      src/components/block/UserTools.vue
  2. 57 5
      src/stores/user.js

+ 8 - 2
src/components/block/UserTools.vue

@@ -9,7 +9,7 @@ export default {
     return { userStore }
   },
   computed: {
-    ...mapState(UserStore,['isloggedin', 'mail', 'name'])
+    ...mapState(UserStore,['isloggedin', 'isAdmin', 'mail', 'name'])
   },
   methods: {
     // ...mapActions({
@@ -30,10 +30,16 @@ export default {
 
 <template>
   <div id="user-tools">
-    <a class="mdi mdi-account" href="/user">
+    <a class="mdi mdi-account" href="/api/user">
       <span>{{ name }}</span>
       <!-- <span v-else>{{ mail }}</span> -->
     </a><br/>
+    <a
+      v-if="isAdmin"
+      class="api" 
+      href="/api/admin/content/concernements">
+      <span>API</span>
+    </a><br/>
     <a href="/user/logout"
       @click.prevent="onLogout()"
       class="mdi mdi-logout"

+ 57 - 5
src/stores/user.js

@@ -3,6 +3,10 @@ import REST from '@api/rest-axios'
 import JSONAPI from '@api/json-axios'
 import qs from 'querystring-es3'
 
+import { print } from 'graphql/language/printer'
+import gql from 'graphql-tag'
+import GQL from '@api/graphql-axios'
+
 export const UserStore = defineStore({
   id: 'user',
   state: () => ({
@@ -13,7 +17,8 @@ export const UserStore = defineStore({
     uuid: 0,
     mail: '',
     name: null,
-    roles: null,
+    roles: [],
+    isAdmin: false,
     logginMessage: null
   }),
   getters: {
@@ -31,6 +36,17 @@ export const UserStore = defineStore({
             this.name = data.attributes.name
             this.isloggedin = true //data.attributes.status
             console.log('user store checkuser isloggedin', this.isloggedin);
+
+            this.userGetRoles()
+              .then(({ data : { data  : { user } } }) => {
+                console.log('graphql user loaded', user)
+                this.roles = user.roles
+                this.checkIsAdmin()
+              })
+              .catch(error => {
+                console.warn('Issue with graphql user loading', error)
+                Promise.reject(error)
+              })
           })
         }
       })
@@ -97,14 +113,23 @@ export const UserStore = defineStore({
               this.name = data.name[0].value
               // with session_limit, only here we are certain that the user is logged
               this.isloggedin = true
-              if (data.roles) {
-                this.roles = data.roles
-              }
+              
               // console.log('customer_profiles', data.customer_profiles.length)
               // if (data.customer_profiles.length) {
               //   dispatch('getUserProfiles', data.customer_profiles[data.customer_profiles.length - 1].target_id)
               // }
-              resolve()
+
+              this.userGetRoles()
+                .then(({ data : { data  : { user } } }) => {
+                  console.log('graphql user loaded', user)
+                  this.roles = user.roles
+                  this.checkIsAdmin()
+                  resolve()
+                })
+                .catch(error => {
+                  console.warn('Issue with graphql user loading', error)
+                  Promise.reject(error)
+                })
             })
             .catch(error => {
               console.warn('Issue with getUser', error)
@@ -113,6 +138,32 @@ export const UserStore = defineStore({
         })
       })
     },
+    userGetRoles () {
+      // GRAPHQL USER QUERY (needed to get roles)
+      // https://stackoverflow.com/a/55401805
+      const body = {
+        query: `
+          query User($id: Int!){
+            user(id: $id) {
+              id
+              roles
+              username
+            }
+          }`,
+        variables: { id: parseInt(this.uid) }
+      } 
+
+      return GQL.post('', body)
+    },
+    checkIsAdmin () {
+      const adminroles = ['admin', 'group_admin']
+      for (let r = 0; r < this.roles.length; r++) {
+        if (adminroles.indexOf(this.roles[r]) !== -1) {
+          this.isAdmin = true;
+          break;
+        }
+      }
+    },
     userLogout () {
       const credentials = qs.stringify({
         token: this.csrf_token
@@ -122,6 +173,7 @@ export const UserStore = defineStore({
           .then(resp => {
             console.log('userStore userLogout resp', resp)
             this.isloggedin = false
+            this.roles = []
             // window.location.reload(true) ???
             resolve()
           })