UNPKG

@lxlib/acl

Version:

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

16 lines (14 loc) 9.94 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("@lxlib/util"),require("rxjs/operators"),require("@angular/router"),require("@angular/common")):"function"==typeof define&&define.amd?define("@lxlib/acl",["exports","@angular/core","rxjs","@lxlib/util","rxjs/operators","@angular/router","@angular/common"],t):t(((e=e||self).lxlib=e.lxlib||{},e.lxlib.acl={}),e.ng.core,e.rxjs,e.lxlib.util,e.rxjs.operators,e.ng.router,e.ng.common)}(this,(function(e,t,r,i,n,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,i=arguments.length;r<i;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)};function s(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function u(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],i=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&i>=e.length&&(e=void 0),{value:e&&e[i++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var i,n,o=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(i=o.next()).done;)a.push(i.value)}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return a}function p(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(c(arguments[t]));return e}var f=function(){function e(){this.guard_url="/403"}return e.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],e.ɵprov=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 i=u(e),n=i.next();!n.done;n=i.next()){var o=n.value;this.roles.includes(o)||this.roles.push(o)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.attachAbility=function(e){var t,r;try{for(var i=u(e),n=i.next();!n.done;n=i.next()){var o=n.value;this.abilities.includes(o)||this.abilities.push(o)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.removeRole=function(e){var t,r;try{for(var i=u(e),n=i.next();!n.done;n=i.next()){var o=n.value,a=this.roles.indexOf(o);-1!==a&&this.roles.splice(a,1)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(t)throw t.error}}this.aclChange.next(this.data)},e.prototype.removeAbility=function(e){var t,r;try{for(var i=u(e),n=i.next();!n.done;n=i.next()){var o=n.value,a=this.abilities.indexOf(o);-1!==a&&this.abilities.splice(a,1)}}catch(e){t={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}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 i=this.parseACLType(e),n=!1;return!0!==this.full&&e?(i.role&&i.role.length>0&&(n="allOf"===i.mode?i.role.every((function(e){return t.roles.includes(e)})):i.role.some((function(e){return t.roles.includes(e)}))),i.ability&&i.ability.length>0&&(n="allOf"===i.mode?i.ability.every((function(e){return t.abilities.includes(e)})):i.ability.some((function(e){return t.abilities.includes(e)})))):n=!0,!0===i.except?!n:n},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 i=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(n.filter((function(e){return null!=e}))).subscribe((function(){return i._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,i){var n,o=arguments.length,a=o<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,i);else for(var l=e.length-1;l>=0;l--)(n=e[l])&&(a=(o<3?n(a):o>3?n(t,r,a):n(t,r))||a);o>3&&a&&Object.defineProperty(t,r,a)}([i.InputBoolean(),s("design:type",Object)],e.prototype,"except",void 0),e}();var d=function(){function e(e,t,r){var i=this;this.el=e,this.renderer=t,this.srv=r,this.change$=this.srv.change.pipe(n.filter((function(e){return null!=e}))).subscribe((function(){return i.set(i._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,i=(e=l({guard:null,guard_url:this.options.guard_url},e)).guard;return(i&&i instanceof r.Observable?i:r.of(null!=i?i:null)).pipe(n.map((function(e){return t.srv.can(e)})),n.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.ɵprov=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,i.LxlibUtilModule],declarations:p(v),exports:p(v)}]}],e}();e.ACLDirective=d,e.ACLGuard=b,e.ACLIfDirective=y,e.ACLService=h,e.LxlibACLConfig=f,e.LxlibACLModule=g,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=acl.umd.min.js.map