UNPKG

@delon-fork/acl

Version:

A simple role-based access control.

21 lines (19 loc) 9.94 kB
/** * @license ng-alain(cipchk@qq.com) v8.8.0-beta6 * (c) 2019 cipchk https://ng-alain.com/ * License: MIT */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("@delon/util"),require("rxjs/operators"),require("@angular/router"),require("@angular/common")):"function"==typeof define&&define.amd?define("@delon/acl",["exports","@angular/core","rxjs","@delon/util","rxjs/operators","@angular/router","@angular/common"],t):t(((e=e||self).delon=e.delon||{},e.delon.acl={}),e.ng.core,e.rxjs,e.delon.util,e.rxjs.operators,e.ng.router,e.ng.common)}(this,(function(e,t,r,n,i,o,a){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */var l=function(){return(l=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)};function s(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function c(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}}function u(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)a.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(i)throw i.error}}return a}function p(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(u(arguments[t]));return e}var f=function(){function e(){this.guard_url="/403"}return e.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],e.ngInjectableDef=t.ɵɵdefineInjectable({factory:function(){return new e},token:e,providedIn:"root"}),e}();var h=function(){function e(e){this.options=e,this.roles=[],this.abilities=[],this.full=!1,this.aclChange=new r.BehaviorSubject(null)}return Object.defineProperty(e.prototype,"change",{get:function(){return this.aclChange.asObservable()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"data",{get:function(){return{full:this.full,roles:this.roles,abilities:this.abilities}},enumerable:!0,configurable:!0}),e.prototype.parseACLType=function(e){var t;return t="number"==typeof e?{ability:[e]}:Array.isArray(e)&&e.length>0&&"number"==typeof e[0]?{ability:e}:"object"!=typeof e||Array.isArray(e)?Array.isArray(e)?{role:e}:{role:null==e?[]:[e]}:l({},e),l({except:!1},t)},e.prototype.set=function(e){this.abilities=[],this.roles=[],this.add(e),this.aclChange.next(e)},e.prototype.setFull=function(e){this.full=e,this.aclChange.next(e)},e.prototype.setAbility=function(e){this.set({ability:e})},e.prototype.setRole=function(e){this.set({role:e})},e.prototype.add=function(e){var t,r;e.role&&e.role.length>0&&(t=this.roles).push.apply(t,p(e.role)),e.ability&&e.ability.length>0&&(r=this.abilities).push.apply(r,p(e.ability))},e.prototype.attachRole=function(e){var t,r;try{for(var n=c(e),i=n.next();!i.done;i=n.next()){var o=i.value;this.roles.includes(o)||this.roles.push(o)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.attachAbility=function(e){var t,r;try{for(var n=c(e),i=n.next();!i.done;i=n.next()){var o=i.value;this.abilities.includes(o)||this.abilities.push(o)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.removeRole=function(e){var t,r;try{for(var n=c(e),i=n.next();!i.done;i=n.next()){var o=i.value,a=this.roles.indexOf(o);-1!==a&&this.roles.splice(a,1)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.removeAbility=function(e){var t,r;try{for(var n=c(e),i=n.next();!i.done;i=n.next()){var o=i.value,a=this.abilities.indexOf(o);-1!==a&&this.abilities.splice(a,1)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.can=function(e){var t=this,r=this.options.preCan;r&&(e=r(e));var n=this.parseACLType(e),i=!1;return!0!==this.full&&e?(n.role&&n.role.length>0&&(i="allOf"===n.mode?n.role.every((function(e){return t.roles.includes(e)})):n.role.some((function(e){return t.roles.includes(e)}))),n.ability&&n.ability.length>0&&(i="allOf"===n.mode?n.ability.every((function(e){return t.abilities.includes(e)})):n.ability.some((function(e){return t.abilities.includes(e)})))):i=!0,!0===n.except?!i:i},e.prototype.parseAbility=function(e){return("number"==typeof e||"string"==typeof e||Array.isArray(e))&&(e={ability:Array.isArray(e)?e:[e]}),delete e.role,e},e.prototype.canAbility=function(e){return this.can(this.parseAbility(e))},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:f}]},e}();var y=function(){function e(e,t,r){var n=this;this.srv=t,this._viewContainer=r,this._thenTemplateRef=null,this._elseTemplateRef=null,this._thenViewRef=null,this._elseViewRef=null,this.except=!1,this._change$=this.srv.change.pipe(i.filter((function(e){return null!=e}))).subscribe((function(){return n._updateView()})),this._thenTemplateRef=e}return Object.defineProperty(e.prototype,"aclIf",{set:function(e){this._value=e,this._updateView()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"aclIfThen",{set:function(e){this._thenTemplateRef=e,this._thenViewRef=null,this._updateView()},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"aclIfElse",{set:function(e){this._elseTemplateRef=e,this._elseViewRef=null,this._updateView()},enumerable:!0,configurable:!0}),e.prototype._updateView=function(){var e=this.srv.can(this._value);e&&!this.except||!e&&this.except?this._thenViewRef||(this._viewContainer.clear(),this._elseViewRef=null,this._thenTemplateRef&&(this._thenViewRef=this._viewContainer.createEmbeddedView(this._thenTemplateRef))):this._elseViewRef||(this._viewContainer.clear(),this._thenViewRef=null,this._elseTemplateRef&&(this._elseViewRef=this._viewContainer.createEmbeddedView(this._elseTemplateRef)))},e.prototype.ngOnDestroy=function(){this._change$.unsubscribe()},e.decorators=[{type:t.Directive,args:[{selector:"[aclIf]",exportAs:"aclIf"}]}],e.ctorParameters=function(){return[{type:t.TemplateRef},{type:h},{type:t.ViewContainerRef}]},e.propDecorators={aclIf:[{type:t.Input}],aclIfThen:[{type:t.Input}],aclIfElse:[{type:t.Input}],except:[{type:t.Input}]},function(e,t,r,n){var i,o=arguments.length,a=o<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,n);else for(var l=e.length-1;l>=0;l--)(i=e[l])&&(a=(o<3?i(a):o>3?i(t,r,a):i(t,r))||a);o>3&&a&&Object.defineProperty(t,r,a)}([n.InputBoolean(),s("design:type",Object)],e.prototype,"except",void 0),e}();var d=function(){function e(e,t,r){var n=this;this.el=e,this.renderer=t,this.srv=r,this.change$=this.srv.change.pipe(i.filter((function(e){return null!=e}))).subscribe((function(){return n.set(n._value)}))}return Object.defineProperty(e.prototype,"acl",{set:function(e){this.set(e)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"ability",{set:function(e){this.set(this.srv.parseAbility(e))},enumerable:!0,configurable:!0}),e.prototype.set=function(e){this._value=e;var t=this.el.nativeElement;this.srv.can(this._value)?this.renderer.removeClass(t,"acl__hide"):this.renderer.addClass(t,"acl__hide")},e.prototype.ngOnDestroy=function(){this.change$.unsubscribe()},e.decorators=[{type:t.Directive,args:[{selector:"[acl]",exportAs:"acl"}]}],e.ctorParameters=function(){return[{type:t.ElementRef},{type:t.Renderer2},{type:h}]},e.propDecorators={acl:[{type:t.Input,args:["acl"]}],ability:[{type:t.Input,args:["acl-ability"]}]},e}();var b=function(){function e(e,t,r){this.srv=e,this.router=t,this.options=r}return e.prototype.process=function(e){var t=this,n=(e=l({guard:null,guard_url:this.options.guard_url},e)).guard;return(n&&n instanceof r.Observable?n:r.of(null!=n?n:null)).pipe(i.map((function(e){return t.srv.can(e)})),i.tap((function(r){r||t.router.navigateByUrl(e.guard_url)})))},e.prototype.canLoad=function(e){return this.process(e.data)},e.prototype.canActivateChild=function(e,t){return this.canActivate(e,t)},e.prototype.canActivate=function(e,t){return this.process(e.data)},e.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],e.ctorParameters=function(){return[{type:h},{type:o.Router},{type:f}]},e.ngInjectableDef=t.ɵɵdefineInjectable({factory:function(){return new e(t.ɵɵinject(h),t.ɵɵinject(o.Router),t.ɵɵinject(f))},token:e,providedIn:"root"}),e}();var v=[d,y],g=function(){function e(){}return e.forRoot=function(){return{ngModule:e,providers:[h]}},e.decorators=[{type:t.NgModule,args:[{imports:[a.CommonModule,n.DelonUtilModule],declarations:p(v),exports:p(v)}]}],e}();e.ACLDirective=d,e.ACLGuard=b,e.ACLIfDirective=y,e.ACLService=h,e.DelonACLConfig=f,e.DelonACLModule=g,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=acl.umd.min.js.map