|
| 1 | +panel.plugin('sylvainjule/bouncer', { |
| 2 | + created(Vue) { |
| 3 | + Vue.$router.onReady(function() { |
| 4 | + getCurrentUser(true, function() { |
| 5 | + checkRedirect(Vue.$router.currentRoute, undefined); |
| 6 | + }); |
| 7 | + }); |
| 8 | + |
| 9 | + Vue.$router.beforeResolve((to, from, next) => { |
| 10 | + if(from.path == '/login') Vue.$data.user = undefined; |
| 11 | + |
| 12 | + if(!Vue.$data.user) { |
| 13 | + getCurrentUser(false, function() { |
| 14 | + checkRedirect(to, next); |
| 15 | + }); |
| 16 | + } |
| 17 | + else { |
| 18 | + checkRedirect(to, next); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + |
| 23 | + function checkRedirect(to, next, firstLoad = false) { |
| 24 | + var user = Vue.$data.user; |
| 25 | + |
| 26 | + if(user && user.restriction) { |
| 27 | + if(!isAllowed(to, user)) { |
| 28 | + if(next) { next(user.restriction.path); } |
| 29 | + else { Vue.$router.push(user.restriction.path); } |
| 30 | + } |
| 31 | + else { |
| 32 | + if(next) { |
| 33 | + if(to.path == '/logout' || to.path == '/login') Vue.$data.user = undefined; |
| 34 | + next(); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + else { |
| 39 | + if(next) { next(); } |
| 40 | + } |
| 41 | + } |
| 42 | + function getCurrentUser(safety = false, callback = false) { |
| 43 | + if(safety && (Vue.$router.currentRoute.path == '/login' || Vue.$router.currentRoute.path == '/')) return false; |
| 44 | + |
| 45 | + Vue.$api |
| 46 | + .get('current-user') |
| 47 | + .then(user => { |
| 48 | + Vue.$data.user = user; |
| 49 | + if(callback) callback(); |
| 50 | + }); |
| 51 | + } |
| 52 | + function isAllowed(to, user) { |
| 53 | + return to.name == 'Account' || |
| 54 | + to.path == '/logout' || |
| 55 | + to.path == '/login' || |
| 56 | + to.path.slice(0, user.restriction.path.length) == user.restriction.path; |
| 57 | + } |
| 58 | + }, |
| 59 | +}); |
0 commit comments