Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Not Found redirection improvement #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default new AclCreate({
},
middleware: async acl => {
await timeout(2000) // call your api
acl.change('admin')
acl.change('admin', { name: 'page-not-authorized' })
}
})
```
Expand Down
67 changes: 32 additions & 35 deletions dist/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ var EventBus = new _vue2.default();
var currentGlobal = [];
var not = false;

var register = exports.register = function register(initial, acceptLocalRules, globalRules, router, notfound, middleware) {
var register = exports.register = function register(initial, acceptLocalRules, globalRules, router, notFoundOptions, middleware) {
currentGlobal = Array.isArray(initial) ? initial : [initial];

if (router !== null) {
router.beforeEach(function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(to, from, next) {
var notFoundPath, routePermission;
var forwardQueryParams, notFound, routePermission;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
Expand All @@ -40,31 +40,33 @@ var register = exports.register = function register(initial, acceptLocalRules, g
_context.next = 3;
return middleware({
change: function change(a) {
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : notFoundOptions;

currentGlobal = a;
notFoundOptions = b;
}
});

case 3:
forwardQueryParams = notFoundOptions.forwardQueryParams;
notFound = router.resolve(notFoundOptions).resolved;

// to be backwards compatible (notfound could be string)
notFoundPath = notfound.path || notfound;

if (!(to.path === notFoundPath)) {
_context.next = 6;
if (!(to.path === notFound.path)) {
_context.next = 7;
break;
}

return _context.abrupt('return', next());

case 6:
case 7:
if ('rule' in to.meta) {
_context.next = 8;
_context.next = 9;
break;
}

return _context.abrupt('return', console.error('[vue-acl] ' + to.path + ' not have rule'));

case 8:
case 9:
routePermission = to.meta.rule;


Expand All @@ -73,24 +75,24 @@ var register = exports.register = function register(initial, acceptLocalRules, g
}

if ((0, _checker.testPermission)(currentGlobal, routePermission)) {
_context.next = 14;
_context.next = 15;
break;
}

if (!notfound.forwardQueryParams) {
_context.next = 13;
break;
// check if forwardQueryParams is set
if (forwardQueryParams) {
notFoundOptions.query = to.query;
notFound = router.resolve(notFoundOptions).resolved;
}

return _context.abrupt('return', next({ path: notFoundPath, query: to.query }));
notFound.meta.from = to;

case 13:
return _context.abrupt('return', next(notFoundPath));
return _context.abrupt('return', next(notFound));

case 14:
case 15:
return _context.abrupt('return', next());

case 15:
case 16:
case 'end':
return _context.stop();
}
Expand All @@ -109,8 +111,6 @@ var register = exports.register = function register(initial, acceptLocalRules, g
* Called before create component
*/
beforeCreate: function beforeCreate() {
var _this = this;

var self = this;

this.$acl = {
Expand Down Expand Up @@ -166,25 +166,22 @@ var register = exports.register = function register(initial, acceptLocalRules, g
return false;
}
};

EventBus.$on('vueacl-permission-changed', function (newPermission) {
currentGlobal = newPermission;
if ('onChange' in _this.$acl) {
_this.$acl.onChange(currentGlobal);
}
_this.$forceUpdate();
});
},
created: function created() {
EventBus.$on('vueacl-permission-changed', this.vue_aclOnChange);
},
destroyed: function destroyed() {
var _this2 = this;
EventBus.$off('vueacl-permission-changed', this.vue_aclOnChange);
},

EventBus.$off('vueacl-permission-changed', function (newPermission) {
methods: {
vue_aclOnChange: function vue_aclOnChange(newPermission) {
currentGlobal = newPermission;
if ('onChange' in _this2.$acl) {
_this2.$acl.onChange(currentGlobal);
if ('onChange' in this.$acl) {
this.$acl.onChange(currentGlobal);
}
_this2.$forceUpdate();
});
this.$forceUpdate();
}
}
};
};
2 changes: 1 addition & 1 deletion dist/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
// @ts-check
var upRouter = exports.upRouter = function upRouter(router, currentGlobal, notfound) {
if (router === null) return;
router.beforeEach(function (to, from, next) {
router.beforeRouteEnter(function (to, from, next) {

/** @type {Array} */
var routePermission = to.meta.rule;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-acl",
"version": "4.1.10",
"version": "4.2.0",
"description": "Plugin of Access Control List from Vue JS 2",
"main": "dist/index.js",
"scripts": {
Expand Down
22 changes: 14 additions & 8 deletions source/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ let currentGlobal = []
let not = false


export const register = (initial, acceptLocalRules, globalRules, router, notfound, middleware) => {
export const register = (initial, acceptLocalRules, globalRules, router, notFoundOptions, middleware) => {
currentGlobal = Array.isArray(initial) ? initial : [initial]

if (router !== null) {
router.beforeEach(async (to, from, next) => {
if (middleware) {
await middleware({change (a) {
await middleware({change (a, b=notFoundOptions) {
currentGlobal = a
notFoundOptions = b
}})
}

// to be backwards compatible (notfound could be string)
const notFoundPath = notfound.path || notfound;
if (to.path === notFoundPath) return next()
const forwardQueryParams = notFoundOptions.forwardQueryParams
let notFound = router.resolve(notFoundOptions).resolved

if (to.path === notFound.path) return next()

/** @type {Array} */
if (!('rule' in to.meta)) {
Expand All @@ -39,10 +41,14 @@ export const register = (initial, acceptLocalRules, globalRules, router, notfoun

if (!testPermission(currentGlobal, routePermission)) {
// check if forwardQueryParams is set
if (notfound.forwardQueryParams) {
return next({path: notFoundPath, query: to.query})
if (forwardQueryParams) {
notFoundOptions.query = to.query
notFound = router.resolve(notFoundOptions).resolved
}
return next(notFoundPath)

notFound.meta.from = to

return next(notFound)
}
return next()
})
Expand Down