made location.reload if user loggedin is admin (need to display admin menu)

This commit is contained in:
Bachir Soussi Chiadmi 2019-05-20 18:49:31 +02:00
parent 9613eabcbc
commit a8c8d04408
4 changed files with 26 additions and 14 deletions

View File

@ -405,7 +405,7 @@ eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n
/***/ (function(module, exports, __webpack_require__) { /***/ (function(module, exports, __webpack_require__) {
"use strict"; "use strict";
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = void 0;\n\nvar _vuex = __webpack_require__(/*! vuex */ \"./node_modules/vuex/dist/vuex.esm.js\");\n\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\nvar _default = {\n data: function data() {\n return {\n mail: '',\n password: ''\n };\n },\n computed: _objectSpread({}, (0, _vuex.mapState)(['User'])),\n methods: _objectSpread({}, (0, _vuex.mapActions)({\n getToken: 'User/getToken'\n }), {\n // usernameInputHandler(input) {\n // this.username = input;\n // },\n // passwordInputHandler(input) {\n // this.password = input;\n // },\n login: function login() {\n this.getToken({\n mail: this.mail,\n pass: this.password\n });\n }\n })\n};\nexports.default = _default;\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/components/User/Login.vue?./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options"); eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = void 0;\n\nvar _vuex = __webpack_require__(/*! vuex */ \"./node_modules/vuex/dist/vuex.esm.js\");\n\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\nvar _default = {\n data: function data() {\n return {\n mail: '',\n password: ''\n };\n },\n computed: _objectSpread({}, (0, _vuex.mapState)(['User'])),\n methods: _objectSpread({}, (0, _vuex.mapActions)({\n userLogin: 'User/userLogin'\n }), {\n // usernameInputHandler(input) {\n // this.username = input;\n // },\n // passwordInputHandler(input) {\n // this.password = input;\n // },\n login: function login() {\n this.userLogin({\n mail: this.mail,\n pass: this.password\n });\n }\n })\n};\nexports.default = _default;\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/components/User/Login.vue?./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options");
/***/ }), /***/ }),
@ -591,7 +591,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) *
/***/ (function(module, __webpack_exports__, __webpack_require__) { /***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict"; "use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"render\", function() { return render; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"staticRenderFns\", function() { return staticRenderFns; });\nvar render = function() {\n var _vm = this\n var _h = _vm.$createElement\n var _c = _vm._self._c || _h\n return _c(\"div\", { attrs: { id: \"user-tools\" } }, [\n _c(\"h4\", { staticClass: \"mdi mdi-account\" }, [_vm._v(_vm._s(_vm.mail))]),\n _vm._v(\" \"),\n _vm.isAdmin\n ? _c(\"a\", {\n staticClass: \"mdi mdi-settings\",\n attrs: { href: \"/admin/content/materials\" }\n })\n : _vm._e(),\n _vm._v(\" \"),\n _c(\"a\", {\n staticClass: \"mdi mdi-logout\",\n attrs: { href: \"/user/logout\", title: \"logout\" },\n on: {\n click: function($event) {\n $event.preventDefault()\n return _vm.onLogout()\n }\n }\n })\n ])\n}\nvar staticRenderFns = []\nrender._withStripped = true\n\n\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/components/User/UserTools.vue?./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options"); eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"render\", function() { return render; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"staticRenderFns\", function() { return staticRenderFns; });\nvar render = function() {\n var _vm = this\n var _h = _vm.$createElement\n var _c = _vm._self._c || _h\n return _c(\"div\", { attrs: { id: \"user-tools\" } }, [\n _c(\"h4\", { staticClass: \"mdi mdi-account\" }, [_vm._v(_vm._s(_vm.mail))]),\n _vm._v(\" \"),\n _vm.isAdmin\n ? _c(\"a\", {\n staticClass: \"mdi mdi-settings\",\n attrs: { href: \"/admin/content/materials\", title: \"admin\" }\n })\n : _vm._e(),\n _vm._v(\" \"),\n _c(\"a\", {\n staticClass: \"mdi mdi-logout\",\n attrs: { href: \"/user/logout\", title: \"logout\" },\n on: {\n click: function($event) {\n $event.preventDefault()\n return _vm.onLogout()\n }\n }\n })\n ])\n}\nvar staticRenderFns = []\nrender._withStripped = true\n\n\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/components/User/UserTools.vue?./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib??vue-loader-options");
/***/ }), /***/ }),
@ -900,7 +900,7 @@ eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n
/***/ (function(module, exports, __webpack_require__) { /***/ (function(module, exports, __webpack_require__) {
"use strict"; "use strict";
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = void 0;\n\nvar _restAxios = __webpack_require__(/*! vuejs/api/rest-axios */ \"./web/themes/custom/materiotheme/vuejs/api/rest-axios.js\");\n\nvar _jsonAxios = __webpack_require__(/*! vuejs/api/json-axios */ \"./web/themes/custom/materiotheme/vuejs/api/json-axios.js\");\n\nvar _querystring = _interopRequireDefault(__webpack_require__(/*! querystring */ \"./node_modules/querystring-es3/index.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nvar _default = {\n namespaced: true,\n // initial state\n state: {\n uid: null,\n // username: '',\n mail: '',\n token: null,\n logout_token: null,\n isloggedin: false,\n isAdmin: false\n },\n // getters\n getters: {},\n // mutations\n mutations: {\n setToken: function setToken(state, data) {\n state.uid = data.current_user.uid; // state.username = data.username\n\n state.mail = data.current_user.mail;\n state.token = data.csrf_token;\n state.isloggedin = true;\n state.logout_token = data.logout_token;\n },\n setUid: function setUid(state, uid) {\n state.uid = uid;\n state.isloggedin = true;\n },\n setUser: function setUser(state, data) {\n state.mail = data.mail[0].value;\n state.uuid = data.uuid[0].value;\n },\n setRoles: function setRoles(state, roles) {\n state.roles = [];\n\n for (var i = 0; i < roles.length; i++) {\n state.roles.push(roles[i].target_id);\n }\n\n if (state.roles.indexOf('admin') != -1) {\n console.log('is admin');\n state.isAdmin = true;\n }\n },\n setLoggedOut: function setLoggedOut(state) {\n state.uid = null;\n state.mail = '';\n state.token = null;\n state.isloggedin = false;\n state.asAdmin = false;\n state.logout_token = null;\n }\n },\n // actions\n actions: {\n getToken: function getToken(_ref, credentials) {\n var dispatch = _ref.dispatch,\n commit = _ref.commit,\n state = _ref.state;\n\n _restAxios.REST.post('/user/login?_format=json', credentials).then(function (_ref2) {\n var data = _ref2.data;\n console.log('user getToken data', data);\n commit('setToken', data);\n dispatch('getUser');\n }).catch(function (error) {\n console.warn('Issue with login', error);\n Promise.reject(error);\n });\n },\n getUser: function getUser(_ref3) {\n var dispatch = _ref3.dispatch,\n commit = _ref3.commit,\n state = _ref3.state;\n var params = {\n token: state.token\n };\n\n _restAxios.REST.get(\"/user/\".concat(state.uid, \"?_format=json\"), params).then(function (_ref4) {\n var data = _ref4.data;\n console.log('user REST getUser data', data);\n console.log('roles', data['roles']);\n commit('setUser', data);\n\n if (data.roles) {\n commit('setRoles', data.roles); // dispatch('checkLoginRole')\n } // dispatch('checkLoginRole')\n // JSONAPI.get(`/user/user/${state.uuid}?include=roles`)\n // .then(({ data }) => {\n // console.log('user JSONAPI getUser data', data)\n // })\n // .catch(( error ) => {\n // console.warn('Issue with getUser', error)\n // Promise.reject(error)\n // })\n\n }).catch(function (error) {\n console.warn('Issue with getUser', error);\n Promise.reject(error);\n });\n },\n // checkLoginRole ({ commit, state }){\n // console.log('checkLoginRole', state.roles, state.roles.indexOf('admin'))\n // if(state.roles.indexOf('admin') != -1){\n // console.log('is admin')\n // window.\n // }\n // },\n userLogout: function userLogout(_ref5) {\n var commit = _ref5.commit,\n state = _ref5.state;\n\n var credentials = _querystring.default.stringify({\n token: state.token\n });\n\n _restAxios.REST.post('/user/logout', credentials).then(function (resp) {\n console.log('resp', resp);\n commit('setLoggedOut');\n }).catch(function (error) {\n console.warn('Issue with logout', error);\n Promise.reject(error);\n });\n }\n }\n};\nexports.default = _default;\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/store/modules/user.js?"); eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.default = void 0;\n\nvar _restAxios = __webpack_require__(/*! vuejs/api/rest-axios */ \"./web/themes/custom/materiotheme/vuejs/api/rest-axios.js\");\n\nvar _jsonAxios = __webpack_require__(/*! vuejs/api/json-axios */ \"./web/themes/custom/materiotheme/vuejs/api/json-axios.js\");\n\nvar _querystring = _interopRequireDefault(__webpack_require__(/*! querystring */ \"./node_modules/querystring-es3/index.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nvar _default = {\n namespaced: true,\n // initial state\n state: {\n uid: null,\n // username: '',\n mail: '',\n token: null,\n logout_token: null,\n isloggedin: false,\n isAdmin: false\n },\n // getters\n getters: {},\n // mutations\n mutations: {\n setToken: function setToken(state, data) {\n state.uid = data.current_user.uid; // state.username = data.username\n\n state.mail = data.current_user.mail;\n state.token = data.csrf_token;\n state.isloggedin = true;\n state.logout_token = data.logout_token;\n },\n setUid: function setUid(state, uid) {\n state.uid = uid;\n state.isloggedin = true;\n },\n setUser: function setUser(state, data) {\n state.mail = data.mail[0].value;\n state.uuid = data.uuid[0].value;\n },\n setRoles: function setRoles(state, roles) {\n state.roles = [];\n\n for (var i = 0; i < roles.length; i++) {\n state.roles.push(roles[i].target_id);\n }\n\n if (state.roles.indexOf('admin') != -1) {\n console.log('is admin');\n state.isAdmin = true;\n }\n },\n setLoggedOut: function setLoggedOut(state) {\n state.uid = null;\n state.mail = '';\n state.token = null;\n state.isloggedin = false;\n state.logout_token = null;\n\n if (state.isAdmin) {\n window.location.reload(true);\n }\n\n state.asAdmin = false;\n }\n },\n // actions\n actions: {\n userLogin: function userLogin(_ref, credentials) {\n var dispatch = _ref.dispatch,\n commit = _ref.commit,\n state = _ref.state;\n dispatch('getToken', credentials).then(function () {\n dispatch('getUser').then(function (userdata) {\n console.log('User Loggedin');\n\n if (state.isAdmin) {\n window.location.reload(true);\n }\n });\n });\n },\n getToken: function getToken(_ref2, credentials) {\n var dispatch = _ref2.dispatch,\n commit = _ref2.commit,\n state = _ref2.state;\n return _restAxios.REST.post('/user/login?_format=json', credentials).then(function (_ref3) {\n var data = _ref3.data;\n console.log('user REST getToken data', data);\n commit('setToken', data);\n }).catch(function (error) {\n console.warn('Issue with getToken', error);\n Promise.reject(error);\n });\n },\n getUser: function getUser(_ref4) {\n var dispatch = _ref4.dispatch,\n commit = _ref4.commit,\n state = _ref4.state;\n var params = {\n token: state.token\n };\n return _restAxios.REST.get(\"/user/\".concat(state.uid, \"?_format=json\"), params).then(function (_ref5) {\n var data = _ref5.data;\n console.log('user REST getUser data', data);\n console.log('roles', data['roles']);\n commit('setUser', data);\n\n if (data.roles) {\n commit('setRoles', data.roles);\n } // JSONAPI.get(`/user/user/${state.uuid}?include=roles`)\n // .then(({ data }) => {\n // console.log('user JSONAPI getUser data', data)\n // })\n // .catch(( error ) => {\n // console.warn('Issue with getUser', error)\n // Promise.reject(error)\n // })\n\n }).catch(function (error) {\n console.warn('Issue with getUser', error);\n Promise.reject(error);\n });\n },\n // checkLoginRole ({ commit, state }){\n // console.log('checkLoginRole', state.roles, state.roles.indexOf('admin'))\n // if(state.roles.indexOf('admin') != -1){\n // console.log('is admin')\n // window.\n // }\n // },\n userLogout: function userLogout(_ref6) {\n var commit = _ref6.commit,\n state = _ref6.state;\n\n var credentials = _querystring.default.stringify({\n token: state.token\n });\n\n _restAxios.REST.post('/user/logout', credentials).then(function (resp) {\n console.log('resp', resp);\n commit('setLoggedOut');\n }).catch(function (error) {\n console.warn('Issue with logout', error);\n Promise.reject(error);\n });\n }\n }\n};\nexports.default = _default;\n\n//# sourceURL=webpack:///./web/themes/custom/materiotheme/vuejs/store/modules/user.js?");
/***/ }) /***/ })

View File

@ -46,7 +46,7 @@ export default {
}, },
methods: { methods: {
...mapActions({ ...mapActions({
getToken: 'User/getToken' userLogin: 'User/userLogin'
}), }),
// usernameInputHandler(input) { // usernameInputHandler(input) {
@ -57,7 +57,7 @@ export default {
// }, // },
login () { login () {
this.getToken({ this.userLogin({
mail: this.mail, mail: this.mail,
pass: this.password pass: this.password
}) })

View File

@ -6,6 +6,7 @@
<a href="/admin/content/materials" <a href="/admin/content/materials"
v-if="isAdmin" v-if="isAdmin"
class="mdi mdi-settings" class="mdi mdi-settings"
title="admin"
></a> ></a>
<a href="/user/logout" <a href="/user/logout"
@click.prevent="onLogout()" @click.prevent="onLogout()"

View File

@ -52,39 +52,50 @@ export default {
state.mail = '' state.mail = ''
state.token = null state.token = null
state.isloggedin = false state.isloggedin = false
state.asAdmin = false
state.logout_token = null state.logout_token = null
if (state.isAdmin){
window.location.reload(true);
}
state.asAdmin = false
} }
}, },
// actions // actions
actions : { actions : {
userLogin({ dispatch, commit, state }, credentials){
dispatch('getToken', credentials)
.then(() => {
dispatch('getUser').then((userdata) => {
console.log('User Loggedin');
if (state.isAdmin){
window.location.reload(true);
}
})
})
},
getToken ({ dispatch, commit, state }, credentials) { getToken ({ dispatch, commit, state }, credentials) {
REST.post('/user/login?_format=json', credentials) return REST.post('/user/login?_format=json', credentials)
.then(({ data }) => { .then(({ data }) => {
console.log('user getToken data', data) console.log('user REST getToken data', data)
commit('setToken', data) commit('setToken', data)
dispatch('getUser')
}) })
.catch(( error ) => { .catch(( error ) => {
console.warn('Issue with login', error) console.warn('Issue with getToken', error)
Promise.reject(error) Promise.reject(error)
}) })
}, },
getUser ({ dispatch, commit, state }) { getUser ({ dispatch, commit, state }) {
let params = { let params = {
token: state.token token: state.token
} }
REST.get(`/user/${state.uid}?_format=json`, params) return REST.get(`/user/${state.uid}?_format=json`, params)
.then(({ data }) => { .then(({ data }) => {
console.log('user REST getUser data', data) console.log('user REST getUser data', data)
console.log('roles', data['roles']) console.log('roles', data['roles'])
commit('setUser', data) commit('setUser', data)
if(data.roles){ if(data.roles){
commit('setRoles', data.roles) commit('setRoles', data.roles)
// dispatch('checkLoginRole')
} }
// dispatch('checkLoginRole')
// JSONAPI.get(`/user/user/${state.uuid}?include=roles`) // JSONAPI.get(`/user/user/${state.uuid}?include=roles`)
// .then(({ data }) => { // .then(({ data }) => {
// console.log('user JSONAPI getUser data', data) // console.log('user JSONAPI getUser data', data)