Browse Source

display admin link if admin role

Bachir Soussi Chiadmi 5 years ago
parent
commit
ebfd405153

File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/main.js


+ 7 - 1
web/themes/custom/materiotheme/vuejs/components/User/UserTools.vue

@@ -1,6 +1,11 @@
 <template lang="html">
   <div id="user-tools">
     <h4>{{ mail }}</h4>
+    <a href="/admin/content/materials"
+      v-if="isAdmin"
+    >
+      Admin
+    </a>
     <a href="/user/logout"
       @click.prevent="onLogout()"
     >
@@ -20,7 +25,8 @@ export default {
   // },
   computed: {
     ...mapState({
-      mail: state => state.User.mail
+      mail: state => state.User.mail,
+      isAdmin: state => state.User.isAdmin
     })
   },
   methods: {

+ 35 - 11
web/themes/custom/materiotheme/vuejs/store/modules/user.js

@@ -12,7 +12,8 @@ export default {
     mail:'',
     token: null,
     logout_token: null,
-    isloggedin: false
+    isloggedin: false,
+    isAdmin: false
   },
 
   // getters
@@ -36,11 +37,22 @@ export default {
       state.mail = data.mail[0].value
       state.uuid = data.uuid[0].value
     },
+    setRoles (state, roles) {
+      state.roles = []
+      for (var i = 0; i < roles.length; i++) {
+        state.roles.push(roles[i].target_id)
+      }
+      if(state.roles.indexOf('admin') != -1){
+        console.log('is admin');
+        state.isAdmin = true
+      }
+    },
     setLoggedOut (state) {
       state.uid= null
       state.mail = ''
       state.token = null
       state.isloggedin = false
+      state.asAdmin = false
       state.logout_token = null
     }
   },
@@ -59,29 +71,41 @@ export default {
             Promise.reject(error)
         })
     },
-    getUser ({ commit, state }) {
+    getUser ({ dispatch, commit, state }) {
       let params = {
         token: state.token
       }
       REST.get(`/user/${state.uid}?_format=json`, params)
         .then(({ data }) => {
           console.log('user REST getUser data', data)
-          console.log('roles', data['roles']);
+          console.log('roles', data['roles'])
           commit('setUser', data)
-          JSONAPI.get(`/user/user/${state.uuid}?include=roles`)
-            .then(({ data }) => {
-              console.log('user JSONAPI getUser data', data)
-            })
-            .catch(( error ) => {
-                console.warn('Issue with getUser', error)
-                Promise.reject(error)
-            })
+          if(data.roles){
+            commit('setRoles', data.roles)
+            // dispatch('checkLoginRole')
+          }
+          // dispatch('checkLoginRole')
+          // JSONAPI.get(`/user/user/${state.uuid}?include=roles`)
+          //   .then(({ data }) => {
+          //     console.log('user JSONAPI getUser data', data)
+          //   })
+          //   .catch(( error ) => {
+          //       console.warn('Issue with getUser', error)
+          //       Promise.reject(error)
+          //   })
         })
         .catch(( error ) => {
             console.warn('Issue with getUser', error)
             Promise.reject(error)
         })
     },
+    // checkLoginRole ({ commit, state }){
+    //   console.log('checkLoginRole', state.roles, state.roles.indexOf('admin'))
+    //   if(state.roles.indexOf('admin') != -1){
+    //     console.log('is admin')
+    //     window.
+    //   }
+    // },
     userLogout ({ commit, state }) {
       let credentials = qs.stringify({
         token: state.token

Some files were not shown because too many files changed in this diff