UNPKG

@lxlib/acl

Version:

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.

982 lines (971 loc) 27.3 kB
import { __assign, __spread, __values, __decorate, __metadata } from 'tslib'; import { Injectable, ɵɵdefineInjectable, Directive, TemplateRef, ViewContainerRef, Input, ElementRef, Renderer2, ɵɵinject, NgModule } from '@angular/core'; import { BehaviorSubject, Observable, of } from 'rxjs'; import { InputBoolean, LxlibUtilModule } from '@lxlib/util'; import { filter, map, tap } from 'rxjs/operators'; import { Router } from '@angular/router'; import { CommonModule } from '@angular/common'; /** * @fileoverview added by tsickle * Generated from: src/acl.config.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var LxlibACLConfig = /** @class */ (function () { function LxlibACLConfig() { /** * Router URL when guard fail, default: `/403` */ this.guard_url = '/403'; } LxlibACLConfig.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ LxlibACLConfig.ɵprov = ɵɵdefineInjectable({ factory: function LxlibACLConfig_Factory() { return new LxlibACLConfig(); }, token: LxlibACLConfig, providedIn: "root" }); return LxlibACLConfig; }()); if (false) { /** * Router URL when guard fail, default: `/403` * @type {?} */ LxlibACLConfig.prototype.guard_url; /** * `can` before execution callback * @type {?} */ LxlibACLConfig.prototype.preCan; } /** * @fileoverview added by tsickle * Generated from: src/acl.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * ACL 控制服务,[在线文档](https://ng-alain.com/acl) * * 务必在根目录注册 `LxlibACLModule.forRoot()` 才能使用服务 */ var ACLService = /** @class */ (function () { function ACLService(options) { this.options = options; this.roles = []; this.abilities = []; this.full = false; this.aclChange = new BehaviorSubject(null); } Object.defineProperty(ACLService.prototype, "change", { /** ACL变更通知 */ get: /** * ACL变更通知 * @return {?} */ function () { return this.aclChange.asObservable(); }, enumerable: true, configurable: true }); Object.defineProperty(ACLService.prototype, "data", { /** 获取所有数据 */ get: /** * 获取所有数据 * @return {?} */ function () { return { full: this.full, roles: this.roles, abilities: this.abilities, }; }, enumerable: true, configurable: true }); /** * @private * @param {?} val * @return {?} */ ACLService.prototype.parseACLType = /** * @private * @param {?} val * @return {?} */ function (val) { /** @type {?} */ var t; if (typeof val === 'number') { t = { ability: [val] }; } else if (Array.isArray(val) && val.length > 0 && typeof val[0] === 'number') { t = { ability: val }; } else if (typeof val === 'object' && !Array.isArray(val)) { t = __assign({}, val); } else if (Array.isArray(val)) { t = { role: (/** @type {?} */ (val)) }; } else { t = { role: val == null ? [] : [val] }; } return __assign({ except: false }, t); }; /** * 设置当前用户角色或权限能力(会先清除所有) */ /** * 设置当前用户角色或权限能力(会先清除所有) * @param {?} value * @return {?} */ ACLService.prototype.set = /** * 设置当前用户角色或权限能力(会先清除所有) * @param {?} value * @return {?} */ function (value) { this.abilities = []; this.roles = []; this.add(value); this.aclChange.next(value); }; /** * 标识当前用户为全量,即不受限 */ /** * 标识当前用户为全量,即不受限 * @param {?} val * @return {?} */ ACLService.prototype.setFull = /** * 标识当前用户为全量,即不受限 * @param {?} val * @return {?} */ function (val) { this.full = val; this.aclChange.next(val); }; /** * 设置当前用户权限能力(会先清除所有) */ /** * 设置当前用户权限能力(会先清除所有) * @param {?} abilities * @return {?} */ ACLService.prototype.setAbility = /** * 设置当前用户权限能力(会先清除所有) * @param {?} abilities * @return {?} */ function (abilities) { this.set((/** @type {?} */ ({ ability: abilities }))); }; /** * 设置当前用户角色(会先清除所有) */ /** * 设置当前用户角色(会先清除所有) * @param {?} roles * @return {?} */ ACLService.prototype.setRole = /** * 设置当前用户角色(会先清除所有) * @param {?} roles * @return {?} */ function (roles) { this.set((/** @type {?} */ ({ role: roles }))); }; /** * 为当前用户增加角色或权限能力 */ /** * 为当前用户增加角色或权限能力 * @param {?} value * @return {?} */ ACLService.prototype.add = /** * 为当前用户增加角色或权限能力 * @param {?} value * @return {?} */ function (value) { var _a, _b; if (value.role && value.role.length > 0) { (_a = this.roles).push.apply(_a, __spread(value.role)); } if (value.ability && value.ability.length > 0) { (_b = this.abilities).push.apply(_b, __spread(value.ability)); } }; /** * 为当前用户附加角色 */ /** * 为当前用户附加角色 * @param {?} roles * @return {?} */ ACLService.prototype.attachRole = /** * 为当前用户附加角色 * @param {?} roles * @return {?} */ function (roles) { var e_1, _a; try { for (var roles_1 = __values(roles), roles_1_1 = roles_1.next(); !roles_1_1.done; roles_1_1 = roles_1.next()) { var val = roles_1_1.value; if (!this.roles.includes(val)) { this.roles.push(val); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (roles_1_1 && !roles_1_1.done && (_a = roles_1.return)) _a.call(roles_1); } finally { if (e_1) throw e_1.error; } } this.aclChange.next(this.data); }; /** * 为当前用户附加权限 */ /** * 为当前用户附加权限 * @param {?} abilities * @return {?} */ ACLService.prototype.attachAbility = /** * 为当前用户附加权限 * @param {?} abilities * @return {?} */ function (abilities) { var e_2, _a; try { for (var abilities_1 = __values(abilities), abilities_1_1 = abilities_1.next(); !abilities_1_1.done; abilities_1_1 = abilities_1.next()) { var val = abilities_1_1.value; if (!this.abilities.includes(val)) { this.abilities.push(val); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (abilities_1_1 && !abilities_1_1.done && (_a = abilities_1.return)) _a.call(abilities_1); } finally { if (e_2) throw e_2.error; } } this.aclChange.next(this.data); }; /** * 为当前用户移除角色 */ /** * 为当前用户移除角色 * @param {?} roles * @return {?} */ ACLService.prototype.removeRole = /** * 为当前用户移除角色 * @param {?} roles * @return {?} */ function (roles) { var e_3, _a; try { for (var roles_2 = __values(roles), roles_2_1 = roles_2.next(); !roles_2_1.done; roles_2_1 = roles_2.next()) { var val = roles_2_1.value; /** @type {?} */ var idx = this.roles.indexOf(val); if (idx !== -1) { this.roles.splice(idx, 1); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (roles_2_1 && !roles_2_1.done && (_a = roles_2.return)) _a.call(roles_2); } finally { if (e_3) throw e_3.error; } } this.aclChange.next(this.data); }; /** * 为当前用户移除权限 */ /** * 为当前用户移除权限 * @param {?} abilities * @return {?} */ ACLService.prototype.removeAbility = /** * 为当前用户移除权限 * @param {?} abilities * @return {?} */ function (abilities) { var e_4, _a; try { for (var abilities_2 = __values(abilities), abilities_2_1 = abilities_2.next(); !abilities_2_1.done; abilities_2_1 = abilities_2.next()) { var val = abilities_2_1.value; /** @type {?} */ var idx = this.abilities.indexOf(val); if (idx !== -1) { this.abilities.splice(idx, 1); } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (abilities_2_1 && !abilities_2_1.done && (_a = abilities_2.return)) _a.call(abilities_2); } finally { if (e_4) throw e_4.error; } } this.aclChange.next(this.data); }; /** * 当前用户是否有对应角色,其实 `number` 表示Ability * * - 当 `full: true` 或参数 `null` 时返回 `true` * - 若使用 `ACLType` 参数,可以指定 `mode` 校验模式 */ /** * 当前用户是否有对应角色,其实 `number` 表示Ability * * - 当 `full: true` 或参数 `null` 时返回 `true` * - 若使用 `ACLType` 参数,可以指定 `mode` 校验模式 * @param {?} roleOrAbility * @return {?} */ ACLService.prototype.can = /** * 当前用户是否有对应角色,其实 `number` 表示Ability * * - 当 `full: true` 或参数 `null` 时返回 `true` * - 若使用 `ACLType` 参数,可以指定 `mode` 校验模式 * @param {?} roleOrAbility * @return {?} */ function (roleOrAbility) { var _this = this; var preCan = this.options.preCan; if (preCan) { roleOrAbility = preCan((/** @type {?} */ (roleOrAbility))); } /** @type {?} */ var t = this.parseACLType(roleOrAbility); /** @type {?} */ var result = false; if (this.full === true || !roleOrAbility) { result = true; } else { if (t.role && t.role.length > 0) { if (t.mode === 'allOf') { result = t.role.every((/** * @param {?} v * @return {?} */ function (v) { return _this.roles.includes(v); })); } else { result = t.role.some((/** * @param {?} v * @return {?} */ function (v) { return _this.roles.includes(v); })); } } if (t.ability && t.ability.length > 0) { if (t.mode === 'allOf') { result = ((/** @type {?} */ (t.ability))).every((/** * @param {?} v * @return {?} */ function (v) { return _this.abilities.includes(v); })); } else { result = ((/** @type {?} */ (t.ability))).some((/** * @param {?} v * @return {?} */ function (v) { return _this.abilities.includes(v); })); } } } return t.except === true ? !result : result; }; /** @inner */ /** * \@inner * @param {?} value * @return {?} */ ACLService.prototype.parseAbility = /** * \@inner * @param {?} value * @return {?} */ function (value) { if (typeof value === 'number' || typeof value === 'string' || Array.isArray(value)) { value = (/** @type {?} */ ({ ability: Array.isArray(value) ? value : [value] })); } delete value.role; return value; }; /** * 当前用户是否有对应权限点 */ /** * 当前用户是否有对应权限点 * @param {?} value * @return {?} */ ACLService.prototype.canAbility = /** * 当前用户是否有对应权限点 * @param {?} value * @return {?} */ function (value) { return this.can(this.parseAbility(value)); }; ACLService.decorators = [ { type: Injectable } ]; /** @nocollapse */ ACLService.ctorParameters = function () { return [ { type: LxlibACLConfig } ]; }; return ACLService; }()); if (false) { /** * @type {?} * @private */ ACLService.prototype.roles; /** * @type {?} * @private */ ACLService.prototype.abilities; /** * @type {?} * @private */ ACLService.prototype.full; /** * @type {?} * @private */ ACLService.prototype.aclChange; /** * @type {?} * @private */ ACLService.prototype.options; } /** * @fileoverview added by tsickle * Generated from: src/acl-if.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var ACLIfDirective = /** @class */ (function () { function ACLIfDirective(templateRef, srv, _viewContainer) { var _this = this; this.srv = srv; this._viewContainer = _viewContainer; this._thenTemplateRef = null; this._elseTemplateRef = null; this._thenViewRef = null; this._elseViewRef = null; this.except = false; this._change$ = this.srv.change.pipe(filter((/** * @param {?} r * @return {?} */ function (r) { return r != null; }))).subscribe((/** * @return {?} */ function () { return _this._updateView(); })); this._thenTemplateRef = templateRef; } Object.defineProperty(ACLIfDirective.prototype, "aclIf", { set: /** * @param {?} value * @return {?} */ function (value) { this._value = value; this._updateView(); }, enumerable: true, configurable: true }); Object.defineProperty(ACLIfDirective.prototype, "aclIfThen", { set: /** * @param {?} templateRef * @return {?} */ function (templateRef) { this._thenTemplateRef = templateRef; this._thenViewRef = null; this._updateView(); }, enumerable: true, configurable: true }); Object.defineProperty(ACLIfDirective.prototype, "aclIfElse", { set: /** * @param {?} templateRef * @return {?} */ function (templateRef) { this._elseTemplateRef = templateRef; this._elseViewRef = null; this._updateView(); }, enumerable: true, configurable: true }); /** * @protected * @return {?} */ ACLIfDirective.prototype._updateView = /** * @protected * @return {?} */ function () { /** @type {?} */ var res = this.srv.can(this._value); if ((res && !this.except) || (!res && this.except)) { if (!this._thenViewRef) { this._viewContainer.clear(); this._elseViewRef = null; if (this._thenTemplateRef) { this._thenViewRef = this._viewContainer.createEmbeddedView(this._thenTemplateRef); } } } else { if (!this._elseViewRef) { this._viewContainer.clear(); this._thenViewRef = null; if (this._elseTemplateRef) { this._elseViewRef = this._viewContainer.createEmbeddedView(this._elseTemplateRef); } } } }; /** * @return {?} */ ACLIfDirective.prototype.ngOnDestroy = /** * @return {?} */ function () { this._change$.unsubscribe(); }; ACLIfDirective.decorators = [ { type: Directive, args: [{ selector: '[aclIf]', exportAs: 'aclIf', },] } ]; /** @nocollapse */ ACLIfDirective.ctorParameters = function () { return [ { type: TemplateRef }, { type: ACLService }, { type: ViewContainerRef } ]; }; ACLIfDirective.propDecorators = { aclIf: [{ type: Input }], aclIfThen: [{ type: Input }], aclIfElse: [{ type: Input }], except: [{ type: Input }] }; __decorate([ InputBoolean(), __metadata("design:type", Object) ], ACLIfDirective.prototype, "except", void 0); return ACLIfDirective; }()); if (false) { /** * @type {?} * @private */ ACLIfDirective.prototype._value; /** * @type {?} * @private */ ACLIfDirective.prototype._change$; /** * @type {?} * @private */ ACLIfDirective.prototype._thenTemplateRef; /** * @type {?} * @private */ ACLIfDirective.prototype._elseTemplateRef; /** * @type {?} * @private */ ACLIfDirective.prototype._thenViewRef; /** * @type {?} * @private */ ACLIfDirective.prototype._elseViewRef; /** @type {?} */ ACLIfDirective.prototype.except; /** * @type {?} * @private */ ACLIfDirective.prototype.srv; /** * @type {?} * @private */ ACLIfDirective.prototype._viewContainer; } /** * @fileoverview added by tsickle * Generated from: src/acl.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var ACLDirective = /** @class */ (function () { function ACLDirective(el, renderer, srv) { var _this = this; this.el = el; this.renderer = renderer; this.srv = srv; this.change$ = this.srv.change.pipe(filter((/** * @param {?} r * @return {?} */ function (r) { return r != null; }))).subscribe((/** * @return {?} */ function () { return _this.set(_this._value); })); } Object.defineProperty(ACLDirective.prototype, "acl", { set: /** * @param {?} value * @return {?} */ function (value) { this.set(value); }, enumerable: true, configurable: true }); Object.defineProperty(ACLDirective.prototype, "ability", { set: /** * @param {?} value * @return {?} */ function (value) { this.set(this.srv.parseAbility(value)); }, enumerable: true, configurable: true }); /** * @private * @param {?} value * @return {?} */ ACLDirective.prototype.set = /** * @private * @param {?} value * @return {?} */ function (value) { this._value = value; /** @type {?} */ var CLS = 'acl__hide'; /** @type {?} */ var el = this.el.nativeElement; if (this.srv.can(this._value)) { this.renderer.removeClass(el, CLS); } else { this.renderer.addClass(el, CLS); } }; /** * @return {?} */ ACLDirective.prototype.ngOnDestroy = /** * @return {?} */ function () { this.change$.unsubscribe(); }; ACLDirective.decorators = [ { type: Directive, args: [{ selector: '[acl]', exportAs: 'acl', },] } ]; /** @nocollapse */ ACLDirective.ctorParameters = function () { return [ { type: ElementRef }, { type: Renderer2 }, { type: ACLService } ]; }; ACLDirective.propDecorators = { acl: [{ type: Input, args: ['acl',] }], ability: [{ type: Input, args: ['acl-ability',] }] }; return ACLDirective; }()); if (false) { /** * @type {?} * @private */ ACLDirective.prototype._value; /** * @type {?} * @private */ ACLDirective.prototype.change$; /** * @type {?} * @private */ ACLDirective.prototype.el; /** * @type {?} * @private */ ACLDirective.prototype.renderer; /** * @type {?} * @protected */ ACLDirective.prototype.srv; } /** * @fileoverview added by tsickle * Generated from: src/acl.type.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function ACLType() { } if (false) { /** * 角色 * @type {?|undefined} */ ACLType.prototype.role; /** * 权限点 * @type {?|undefined} */ ACLType.prototype.ability; /** * 校验模式,默认:`oneOf` * - `allOf` 表示必须满足所有角色或权限点数组算有效 * - `oneOf` 表示只须满足角色或权限点数组中的一项算有效 * @type {?|undefined} */ ACLType.prototype.mode; /** * 是否取反,即结果为 `true` 时表示未授权 * @type {?|undefined} */ ACLType.prototype.except; /* Skipping unhandled member: [key: string]: NzSafeAny;*/ } /** * @fileoverview added by tsickle * Generated from: src/acl-guard.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng-alain.com/acl). * * ```ts * data: { * path: 'home', * canActivate: [ ACLGuard ], * data: { guard: 'user1' } * } * ``` */ var ACLGuard = /** @class */ (function () { function ACLGuard(srv, router, options) { this.srv = srv; this.router = router; this.options = options; } /** * @private * @param {?} data * @return {?} */ ACLGuard.prototype.process = /** * @private * @param {?} data * @return {?} */ function (data) { var _this = this; data = __assign({ guard: null, guard_url: this.options.guard_url }, data); /** @type {?} */ var guard = data.guard; return (guard && guard instanceof Observable ? guard : of(guard != null ? ((/** @type {?} */ (guard))) : null)).pipe(map((/** * @param {?} v * @return {?} */ function (v) { return _this.srv.can(v); })), tap((/** * @param {?} v * @return {?} */ function (v) { if (v) return; _this.router.navigateByUrl(data.guard_url); }))); }; // lazy loading // lazy loading /** * @param {?} route * @return {?} */ ACLGuard.prototype.canLoad = // lazy loading /** * @param {?} route * @return {?} */ function (route) { return this.process((/** @type {?} */ (route.data))); }; // all children route // all children route /** * @param {?} childRoute * @param {?} state * @return {?} */ ACLGuard.prototype.canActivateChild = // all children route /** * @param {?} childRoute * @param {?} state * @return {?} */ function (childRoute, state) { return this.canActivate(childRoute, state); }; // route // route /** * @param {?} route * @param {?} _state * @return {?} */ ACLGuard.prototype.canActivate = // route /** * @param {?} route * @param {?} _state * @return {?} */ function (route, _state) { return this.process(route.data); }; ACLGuard.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ ACLGuard.ctorParameters = function () { return [ { type: ACLService }, { type: Router }, { type: LxlibACLConfig } ]; }; /** @nocollapse */ ACLGuard.ɵprov = ɵɵdefineInjectable({ factory: function ACLGuard_Factory() { return new ACLGuard(ɵɵinject(ACLService), ɵɵinject(Router), ɵɵinject(LxlibACLConfig)); }, token: ACLGuard, providedIn: "root" }); return ACLGuard; }()); if (false) { /** * @type {?} * @private */ ACLGuard.prototype.srv; /** * @type {?} * @private */ ACLGuard.prototype.router; /** * @type {?} * @private */ ACLGuard.prototype.options; } /** * @fileoverview added by tsickle * Generated from: src/acl.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var COMPONENTS = [ACLDirective, ACLIfDirective]; var LxlibACLModule = /** @class */ (function () { function LxlibACLModule() { } /** * @return {?} */ LxlibACLModule.forRoot = /** * @return {?} */ function () { return { ngModule: LxlibACLModule, providers: [ACLService], }; }; LxlibACLModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, LxlibUtilModule], declarations: __spread(COMPONENTS), exports: __spread(COMPONENTS), },] } ]; return LxlibACLModule; }()); /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: acl.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { ACLDirective, ACLGuard, ACLIfDirective, ACLService, LxlibACLConfig, LxlibACLModule }; //# sourceMappingURL=acl.js.map