nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
42 lines (41 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const SecurityException_1 = require("./SecurityException");
const SecurityManager_1 = require("./SecurityManager");
exports.SECURITY_AUTHORIZE_METADATA_KEY = Symbol('security:authorize');
exports.SECURITY_SECURITY_MANAGER_METADATA_KEY = Symbol('security:security-manager');
function authorize(options) {
const context = this.context;
const that = this;
that.context = context;
return (target, propertyKey, descriptor) => {
const method = descriptor.value;
const settings = options || {};
settings.type = settings.type || 'hasAny';
descriptor.value = function () {
const args = arguments;
settings.providerName = settings.providerName || SecurityManager_1.SecurityManager.DefaultProviderName;
const sp = SecurityManager_1.SecurityManager.getSecurityProvider(settings.providerName) || SecurityManager_1.SecurityManager.DefaultProvider;
if (!sp) {
throw new SecurityException_1.SecurityException(-4004, 'Security provider is not exists.');
}
const pr = SecurityManager_1.authorized.call(target, options);
if (pr && pr instanceof Promise) {
return pr
.then(result => {
if (result === true) {
return method.apply(target, args);
}
else {
throw new SecurityException_1.SecurityException(-4009, 'Authorization failed.');
}
})
.catch(error => {
throw error;
});
}
};
};
}
exports.authorize = authorize;