@bixi/ac
Version:
520 lines (511 loc) • 12.9 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Directive, ViewContainerRef, TemplateRef, ChangeDetectorRef, Input, NgModule } from '@angular/core';
import { BehaviorSubject, Subscription, combineLatest, of } from 'rxjs';
import * as i2 from '@angular/router';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common';
/**
* @fileoverview added by tsickle
* Generated from: src/ac.type.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @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 {?} */
const isArray = Array.isArray;
/** @type {?} */
const toArray = (/**
* @param {?} strOrArr
* @return {?}
*/
(strOrArr) => {
if (!strOrArr)
return [];
return isArray(strOrArr) ? strOrArr : [strOrArr];
});
const ɵ0 = toArray;
/** @type {?} */
const partInArray = (/**
* @param {?=} all
* @param {?=} sub
* @return {?}
*/
(all = [], sub = []) => {
return !!sub.find((/**
* @param {?} s
* @return {?}
*/
s => all.includes(s)));
});
const ɵ1 = partInArray;
/** @type {?} */
const allInArray = (/**
* @param {?=} all
* @param {?=} sub
* @return {?}
*/
(all = [], sub = []) => {
return !!sub.every((/**
* @param {?} s
* @return {?}
*/
s => all.includes(s)));
});
const ɵ2 = allInArray;
/** @type {?} */
const partNotInArray = (/**
* @param {?=} all
* @param {?=} sub
* @return {?}
*/
(all = [], sub = []) => {
return !!sub.find((/**
* @param {?} s
* @return {?}
*/
s => !all.includes(s)));
});
const ɵ3 = partNotInArray;
/** @type {?} */
const allNotInArray = (/**
* @param {?=} all
* @param {?=} sub
* @return {?}
*/
(all = [], sub = []) => {
return sub.every((/**
* @param {?} s
* @return {?}
*/
s => !all.includes(s)));
});
const ɵ4 = allNotInArray;
class BixiACService {
constructor() {
this._permissions = [];
this._roles = [];
this._roles$ = new BehaviorSubject([]);
this._permissions$ = new BehaviorSubject([]);
}
/**
* @return {?}
*/
get roles$() {
return this._roles$;
}
/**
* @return {?}
*/
get permissions$() {
return this._permissions$;
}
/**
* @param {?} data
* @param {?=} options
* @return {?}
*/
ac(data, options = { isRole: false }) {
return allInArray(options.isRole ? this._roles : this._permissions, toArray(data));
}
/**
* @param {?} data
* @param {?=} options
* @return {?}
*/
acAny(data, options = { isRole: false }) {
return partInArray(options.isRole ? this._roles : this._permissions, toArray(data));
}
/**
* @param {?} data
* @param {?=} options
* @return {?}
*/
ace(data, options = { isRole: false }) {
return allNotInArray(options.isRole ? this._roles : this._permissions, toArray(data));
}
/**
* @param {?} data
* @param {?=} options
* @return {?}
*/
aceAny(data, options = { isRole: false }) {
return partNotInArray(options.isRole ? this._roles : this._permissions, toArray(data));
}
/**
* @param {?} permissions
* @return {?}
*/
setPermissions(permissions) {
this._permissions = toArray(permissions);
this._permissions$.next(this._permissions);
}
/**
* @param {?} roles
* @return {?}
*/
setRoles(roles) {
this._roles = toArray(roles);
this._roles$.next(this._roles);
}
/**
* @return {?}
*/
getPermissions() {
return this._permissions || [];
}
/**
* @return {?}
*/
getRoles() {
return this._roles || [];
}
/**
* @return {?}
*/
clear() {
this.setPermissions([]);
this.setRoles([]);
}
}
BixiACService.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */ BixiACService.ɵprov = i0.ɵɵ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
*/
class BixiACDirective {
/**
* @param {?} viewContainer
* @param {?} templateRef
* @param {?} acService
* @param {?} changeDetector
*/
constructor(viewContainer, templateRef, acService, changeDetector) {
this.viewContainer = viewContainer;
this.templateRef = templateRef;
this.acService = acService;
this.changeDetector = changeDetector;
this.subscription = Subscription.EMPTY;
this._isRole = false;
this.subscription = combineLatest([this.acService.permissions$, this.acService.roles$])
.subscribe((/**
* @return {?}
*/
() => {
this.checkView();
}));
}
/**
* @param {?} isRole
* @return {?}
*/
set isRole(isRole) {
this._isRole = isRole;
this.checkView();
}
/**
* @param {?} value
* @return {?}
*/
set ac(value) {
this._ac = value;
this.checkView();
}
/**
* @param {?} value
* @return {?}
*/
set acAny(value) {
this._acAny = value;
this.checkView();
}
/**
* @param {?} value
* @return {?}
*/
set ace(value) {
this._ace = value;
this.checkView();
}
/**
* @param {?} value
* @return {?}
*/
set aceAny(value) {
this._aceAny = value;
this.checkView();
}
/**
* @private
* @return {?}
*/
checkView() {
this.viewContainer.clear();
if (this.hasPermissions()) {
if (this.templateRef) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.changeDetector.markForCheck();
}
}
}
/**
* @private
* @return {?}
*/
hasPermissions() {
/** @type {?} */
const isRole = this._isRole;
if (this._ac) {
return this.acService.ac(this._ac, { isRole });
}
if (this._acAny) {
return this.acService.acAny(this._acAny, { isRole });
}
if (this._ace) {
return this.acService.ace(this._ace, { isRole });
}
if (this._aceAny) {
return this.acService.aceAny(this._aceAny, { isRole });
}
return false;
}
/**
* @return {?}
*/
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
BixiACDirective.decorators = [
{ type: Directive, args: [{
selector: '[ac],[acAny],[ace],[aceAny]'
},] }
];
/** @nocollapse */
BixiACDirective.ctorParameters = () => [
{ type: ViewContainerRef },
{ type: TemplateRef },
{ type: BixiACService },
{ type: ChangeDetectorRef }
];
BixiACDirective.propDecorators = {
isRole: [{ type: Input }],
ac: [{ type: Input }],
acAny: [{ type: Input }],
ace: [{ type: Input }],
aceAny: [{ type: 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
*/
class BixiACGuard {
/**
* @param {?} acService
* @param {?} router
*/
constructor(acService, router) {
this.acService = acService;
this.router = router;
}
/**
* @param {?} route
* @return {?}
*/
canActivate(route) {
return of(this.hasPermissions(route));
}
/**
* @param {?} childRoute
* @return {?}
*/
canActivateChild(childRoute) {
return this.canActivate(childRoute);
}
/**
* @param {?} route
* @return {?}
*/
canLoad(route) {
return of(this.hasPermissions(route));
}
/**
* @private
* @param {?} route
* @return {?}
*/
hasPermissions(route) {
if (!(route && route.data && route.data.ac)) {
return true;
}
/** @type {?} */
const acData = route.data.ac || {};
const { isRole = false, ac, acAny, ace, aceAny, redirectTo } = acData;
if ([ac, acAny, ace, aceAny].filter((/**
* @param {?} s
* @return {?}
*/
s => !!s)).length > 1) {
console.error(`[/ac] 同一个路由只能使用 ['ac', 'acAny', 'ace', 'aceAny'] 中一条规则`);
return false;
}
/** @type {?} */
let hasPermission = false;
if (ac) {
hasPermission = this.acService.ac(ac, { isRole });
}
if (acAny) {
hasPermission = this.acService.acAny(acAny, { isRole });
}
if (ace) {
hasPermission = this.acService.ace(ace, { isRole });
}
if (aceAny) {
hasPermission = this.acService.aceAny(aceAny, { isRole });
}
if (hasPermission)
return hasPermission;
if (redirectTo) {
console.log(`[/ac] 没有路由访问权限,重定向至 -> ${redirectTo}`, route);
this.router.navigateByUrl(redirectTo);
return hasPermission;
}
else {
console.warn(`[/ac] 没有路由访问权限,无重定向路由`, route);
return hasPermission;
}
}
}
BixiACGuard.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */
BixiACGuard.ctorParameters = () => [
{ type: BixiACService },
{ type: Router }
];
/** @nocollapse */ BixiACGuard.ɵprov = i0.ɵɵdefineInjectable({ factory: function BixiACGuard_Factory() { return new BixiACGuard(i0.ɵɵinject(BixiACService), i0.ɵɵinject(i2.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
*/
class BixiACModule {
}
BixiACModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
declarations: [BixiACDirective],
exports: [BixiACDirective]
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public-api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: ac.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { BixiACDirective, BixiACGuard, BixiACModule, BixiACService };
//# sourceMappingURL=ac.js.map