UNPKG

@bixi/ac

Version:
586 lines (575 loc) 19.1 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('@angular/router'), require('@angular/common')) : typeof define === 'function' && define.amd ? define('@bixi/ac', ['exports', '@angular/core', 'rxjs', '@angular/router', '@angular/common'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.bixi = global.bixi || {}, global.bixi.ac = {}), global.ng.core, global.rxjs, global.ng.router, global.ng.common)); })(this, (function (exports, i0, rxjs, i2, common) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var i2__namespace = /*#__PURE__*/_interopNamespace(i2); /** * @fileoverview added by tsickle * Generated from: src/ac.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function IOptions() { } if (false) { /** @type {?} */ IOptions.prototype.isRole; } /** @type {?} */ var isArray = Array.isArray; /** @type {?} */ var toArray = ( /** * @param {?} strOrArr * @return {?} */function (strOrArr) { if (!strOrArr) return []; return isArray(strOrArr) ? strOrArr : [strOrArr]; }); var ɵ0 = toArray; /** @type {?} */ var partInArray = ( /** * @param {?=} all * @param {?=} sub * @return {?} */function (all, sub) { if (all === void 0) { all = []; } if (sub === void 0) { sub = []; } return !!sub.find(( /** * @param {?} s * @return {?} */function (/** * @param {?} s * @return {?} */ s) { return all.includes(s); })); }); var ɵ1 = partInArray; /** @type {?} */ var allInArray = ( /** * @param {?=} all * @param {?=} sub * @return {?} */function (all, sub) { if (all === void 0) { all = []; } if (sub === void 0) { sub = []; } return !!sub.every(( /** * @param {?} s * @return {?} */function (/** * @param {?} s * @return {?} */ s) { return all.includes(s); })); }); var ɵ2 = allInArray; /** @type {?} */ var partNotInArray = ( /** * @param {?=} all * @param {?=} sub * @return {?} */function (all, sub) { if (all === void 0) { all = []; } if (sub === void 0) { sub = []; } return !!sub.find(( /** * @param {?} s * @return {?} */function (/** * @param {?} s * @return {?} */ s) { return !all.includes(s); })); }); var ɵ3 = partNotInArray; /** @type {?} */ var allNotInArray = ( /** * @param {?=} all * @param {?=} sub * @return {?} */function (all, sub) { if (all === void 0) { all = []; } if (sub === void 0) { sub = []; } return sub.every(( /** * @param {?} s * @return {?} */function (/** * @param {?} s * @return {?} */ s) { return !all.includes(s); })); }); var ɵ4 = allNotInArray; var BixiACService = /** @class */ (function () { function BixiACService() { this._permissions = []; this._roles = []; this._roles$ = new rxjs.BehaviorSubject([]); this._permissions$ = new rxjs.BehaviorSubject([]); } Object.defineProperty(BixiACService.prototype, "roles$", { /** * @return {?} */ get: function () { return this._roles$; }, enumerable: false, configurable: true }); Object.defineProperty(BixiACService.prototype, "permissions$", { /** * @return {?} */ get: function () { return this._permissions$; }, enumerable: false, configurable: true }); /** * @param {?} data * @param {?=} options * @return {?} */ BixiACService.prototype.ac = function (data, options) { if (options === void 0) { options = { isRole: false }; } return allInArray(options.isRole ? this._roles : this._permissions, toArray(data)); }; /** * @param {?} data * @param {?=} options * @return {?} */ BixiACService.prototype.acAny = function (data, options) { if (options === void 0) { options = { isRole: false }; } return partInArray(options.isRole ? this._roles : this._permissions, toArray(data)); }; /** * @param {?} data * @param {?=} options * @return {?} */ BixiACService.prototype.ace = function (data, options) { if (options === void 0) { options = { isRole: false }; } return allNotInArray(options.isRole ? this._roles : this._permissions, toArray(data)); }; /** * @param {?} data * @param {?=} options * @return {?} */ BixiACService.prototype.aceAny = function (data, options) { if (options === void 0) { options = { isRole: false }; } return partNotInArray(options.isRole ? this._roles : this._permissions, toArray(data)); }; /** * @param {?} permissions * @return {?} */ BixiACService.prototype.setPermissions = function (permissions) { this._permissions = toArray(permissions); this._permissions$.next(this._permissions); }; /** * @param {?} roles * @return {?} */ BixiACService.prototype.setRoles = function (roles) { this._roles = toArray(roles); this._roles$.next(this._roles); }; /** * @return {?} */ BixiACService.prototype.getPermissions = function () { return this._permissions || []; }; /** * @return {?} */ BixiACService.prototype.getRoles = function () { return this._roles || []; }; /** * @return {?} */ BixiACService.prototype.clear = function () { this.setPermissions([]); this.setRoles([]); }; return BixiACService; }()); BixiACService.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ BixiACService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function BixiACService_Factory() { return new BixiACService(); }, token: BixiACService, providedIn: "root" }); if (false) { /** * @type {?} * @private */ BixiACService.prototype._permissions; /** * @type {?} * @private */ BixiACService.prototype._roles; /** * @type {?} * @private */ BixiACService.prototype._roles$; /** * @type {?} * @private */ BixiACService.prototype._permissions$; } /** * @fileoverview added by tsickle * Generated from: src/ac.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var BixiACDirective = /** @class */ (function () { /** * @param {?} viewContainer * @param {?} templateRef * @param {?} acService * @param {?} changeDetector */ function BixiACDirective(viewContainer, templateRef, acService, changeDetector) { var _this = this; this.viewContainer = viewContainer; this.templateRef = templateRef; this.acService = acService; this.changeDetector = changeDetector; this.subscription = rxjs.Subscription.EMPTY; this._isRole = false; this.subscription = rxjs.combineLatest([this.acService.permissions$, this.acService.roles$]) .subscribe(( /** * @return {?} */function () { _this.checkView(); })); } Object.defineProperty(BixiACDirective.prototype, "isRole", { /** * @param {?} isRole * @return {?} */ set: function (isRole) { this._isRole = isRole; this.checkView(); }, enumerable: false, configurable: true }); Object.defineProperty(BixiACDirective.prototype, "ac", { /** * @param {?} value * @return {?} */ set: function (value) { this._ac = value; this.checkView(); }, enumerable: false, configurable: true }); Object.defineProperty(BixiACDirective.prototype, "acAny", { /** * @param {?} value * @return {?} */ set: function (value) { this._acAny = value; this.checkView(); }, enumerable: false, configurable: true }); Object.defineProperty(BixiACDirective.prototype, "ace", { /** * @param {?} value * @return {?} */ set: function (value) { this._ace = value; this.checkView(); }, enumerable: false, configurable: true }); Object.defineProperty(BixiACDirective.prototype, "aceAny", { /** * @param {?} value * @return {?} */ set: function (value) { this._aceAny = value; this.checkView(); }, enumerable: false, configurable: true }); /** * @private * @return {?} */ BixiACDirective.prototype.checkView = function () { this.viewContainer.clear(); if (this.hasPermissions()) { if (this.templateRef) { this.viewContainer.createEmbeddedView(this.templateRef); this.changeDetector.markForCheck(); } } }; /** * @private * @return {?} */ BixiACDirective.prototype.hasPermissions = function () { /** @type {?} */ var isRole = this._isRole; if (this._ac) { return this.acService.ac(this._ac, { isRole: isRole }); } if (this._acAny) { return this.acService.acAny(this._acAny, { isRole: isRole }); } if (this._ace) { return this.acService.ace(this._ace, { isRole: isRole }); } if (this._aceAny) { return this.acService.aceAny(this._aceAny, { isRole: isRole }); } return false; }; /** * @return {?} */ BixiACDirective.prototype.ngOnDestroy = function () { this.subscription.unsubscribe(); }; return BixiACDirective; }()); BixiACDirective.decorators = [ { type: i0.Directive, args: [{ selector: '[ac],[acAny],[ace],[aceAny]' },] } ]; /** @nocollapse */ BixiACDirective.ctorParameters = function () { return [ { type: i0.ViewContainerRef }, { type: i0.TemplateRef }, { type: BixiACService }, { type: i0.ChangeDetectorRef } ]; }; BixiACDirective.propDecorators = { isRole: [{ type: i0.Input }], ac: [{ type: i0.Input }], acAny: [{ type: i0.Input }], ace: [{ type: i0.Input }], aceAny: [{ type: i0.Input }] }; if (false) { /** @type {?} */ BixiACDirective.prototype.subscription; /** * @type {?} * @private */ BixiACDirective.prototype._isRole; /** * @type {?} * @private */ BixiACDirective.prototype._ac; /** * @type {?} * @private */ BixiACDirective.prototype._acAny; /** * @type {?} * @private */ BixiACDirective.prototype._ace; /** * @type {?} * @private */ BixiACDirective.prototype._aceAny; /** * @type {?} * @private */ BixiACDirective.prototype.viewContainer; /** * @type {?} * @private */ BixiACDirective.prototype.templateRef; /** * @type {?} * @private */ BixiACDirective.prototype.acService; /** * @type {?} * @private */ BixiACDirective.prototype.changeDetector; } /** * @fileoverview added by tsickle * Generated from: src/ac.guard.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var BixiACGuard = /** @class */ (function () { /** * @param {?} acService * @param {?} router */ function BixiACGuard(acService, router) { this.acService = acService; this.router = router; } /** * @param {?} route * @return {?} */ BixiACGuard.prototype.canActivate = function (route) { return rxjs.of(this.hasPermissions(route)); }; /** * @param {?} childRoute * @return {?} */ BixiACGuard.prototype.canActivateChild = function (childRoute) { return this.canActivate(childRoute); }; /** * @param {?} route * @return {?} */ BixiACGuard.prototype.canLoad = function (route) { return rxjs.of(this.hasPermissions(route)); }; /** * @private * @param {?} route * @return {?} */ BixiACGuard.prototype.hasPermissions = function (route) { if (!(route && route.data && route.data.ac)) { return true; } /** @type {?} */ var acData = route.data.ac || {}; var _a = acData.isRole, isRole = _a === void 0 ? false : _a, ac = acData.ac, acAny = acData.acAny, ace = acData.ace, aceAny = acData.aceAny, redirectTo = acData.redirectTo; if ([ac, acAny, ace, aceAny].filter(( /** * @param {?} s * @return {?} */function (/** * @param {?} s * @return {?} */ s) { return !!s; })).length > 1) { console.error("[@bixi/ac] \u540C\u4E00\u4E2A\u8DEF\u7531\u53EA\u80FD\u4F7F\u7528 ['ac', 'acAny', 'ace', 'aceAny'] \u4E2D\u4E00\u6761\u89C4\u5219"); return false; } /** @type {?} */ var hasPermission = false; if (ac) { hasPermission = this.acService.ac(ac, { isRole: isRole }); } if (acAny) { hasPermission = this.acService.acAny(acAny, { isRole: isRole }); } if (ace) { hasPermission = this.acService.ace(ace, { isRole: isRole }); } if (aceAny) { hasPermission = this.acService.aceAny(aceAny, { isRole: isRole }); } if (hasPermission) return hasPermission; if (redirectTo) { console.log("[@bixi/ac] \u6CA1\u6709\u8DEF\u7531\u8BBF\u95EE\u6743\u9650\uFF0C\u91CD\u5B9A\u5411\u81F3 -> " + redirectTo, route); this.router.navigateByUrl(redirectTo); return hasPermission; } else { console.warn("[@bixi/ac] \u6CA1\u6709\u8DEF\u7531\u8BBF\u95EE\u6743\u9650\uFF0C\u65E0\u91CD\u5B9A\u5411\u8DEF\u7531", route); return hasPermission; } }; return BixiACGuard; }()); BixiACGuard.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ BixiACGuard.ctorParameters = function () { return [ { type: BixiACService }, { type: i2.Router } ]; }; /** @nocollapse */ BixiACGuard.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function BixiACGuard_Factory() { return new BixiACGuard(i0__namespace.ɵɵinject(BixiACService), i0__namespace.ɵɵinject(i2__namespace.Router)); }, token: BixiACGuard, providedIn: "root" }); if (false) { /** * @type {?} * @private */ BixiACGuard.prototype.acService; /** * @type {?} * @private */ BixiACGuard.prototype.router; } /** * @fileoverview added by tsickle * Generated from: src/ac.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var BixiACModule = /** @class */ (function () { function BixiACModule() { } return BixiACModule; }()); BixiACModule.decorators = [ { type: i0.NgModule, args: [{ imports: [common.CommonModule], declarations: [BixiACDirective], exports: [BixiACDirective] },] } ]; /** * @fileoverview added by tsickle * Generated from: ac.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ exports.BixiACDirective = BixiACDirective; exports.BixiACGuard = BixiACGuard; exports.BixiACModule = BixiACModule; exports.BixiACService = BixiACService; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ac.umd.js.map