@yelon/acl 17.3.1 → 18.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fesm2022/acl.mjs CHANGED
@@ -1,416 +1,416 @@
1
- import * as i0 from '@angular/core';
2
- import { Injectable, inject, ViewContainerRef, TemplateRef, Directive, Input, ElementRef, Renderer2, Injector, NgModule } from '@angular/core';
3
- import { BehaviorSubject, filter, Observable, of, map, tap } from 'rxjs';
4
- import * as i1 from '@yelon/util/config';
5
- import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
6
- import { Router } from '@angular/router';
7
- import { CommonModule } from '@angular/common';
8
-
9
- const ACL_DEFAULT_CONFIG = {
10
- guard_url: `/403`
11
- };
12
-
13
- /**
14
- * ACL 控制服务,[在线文档](https://ng.yunzainfo.com/acl)
15
- */
16
- class ACLService {
17
- /** ACL变更通知 */
18
- get change() {
19
- return this.aclChange.asObservable();
20
- }
21
- /** 获取所有数据 */
22
- get data() {
23
- return {
24
- full: this.full,
25
- roles: this.roles,
26
- abilities: this.abilities
27
- };
28
- }
29
- get guard_url() {
30
- return this.options.guard_url;
31
- }
32
- constructor(configSrv) {
33
- this.roles = [];
34
- this.abilities = [];
35
- this.full = false;
36
- this.aclChange = new BehaviorSubject(null);
37
- this.options = configSrv.merge('acl', ACL_DEFAULT_CONFIG);
38
- }
39
- parseACLType(val) {
40
- let t;
41
- if (typeof val === 'number') {
42
- t = { ability: [val] };
43
- }
44
- else if (Array.isArray(val) && val.length > 0 && typeof val[0] === 'number') {
45
- t = { ability: val };
46
- }
47
- else if (typeof val === 'object' && !Array.isArray(val)) {
48
- t = { ...val };
49
- }
50
- else if (Array.isArray(val)) {
51
- t = { role: val };
52
- }
53
- else {
54
- t = { role: val == null ? [] : [val] };
55
- }
56
- return { except: false, ...t };
57
- }
58
- /**
59
- * 设置当前用户角色或权限能力(会先清除所有)
60
- */
61
- set(value) {
62
- this.full = false;
63
- this.abilities = [];
64
- this.roles = [];
65
- this.add(value);
66
- this.aclChange.next(value);
67
- }
68
- /**
69
- * 标识当前用户为全量,即不受限
70
- */
71
- setFull(val) {
72
- this.full = val;
73
- this.aclChange.next(val);
74
- }
75
- /**
76
- * 设置当前用户权限能力(会先清除所有)
77
- */
78
- setAbility(abilities) {
79
- this.set({ ability: abilities });
80
- }
81
- /**
82
- * 设置当前用户角色(会先清除所有)
83
- */
84
- setRole(roles) {
85
- this.set({ role: roles });
86
- }
87
- /**
88
- * 为当前用户增加角色或权限能力
89
- */
90
- add(value) {
91
- if (value.role && value.role.length > 0) {
92
- this.roles.push(...value.role);
93
- }
94
- if (value.ability && value.ability.length > 0) {
95
- this.abilities.push(...value.ability);
96
- }
97
- }
98
- /**
99
- * 为当前用户附加角色
100
- */
101
- attachRole(roles) {
102
- for (const val of roles) {
103
- if (!this.roles.includes(val)) {
104
- this.roles.push(val);
105
- }
106
- }
107
- this.aclChange.next(this.data);
108
- }
109
- /**
110
- * 为当前用户附加权限
111
- */
112
- attachAbility(abilities) {
113
- for (const val of abilities) {
114
- if (!this.abilities.includes(val)) {
115
- this.abilities.push(val);
116
- }
117
- }
118
- this.aclChange.next(this.data);
119
- }
120
- /**
121
- * 为当前用户移除角色
122
- */
123
- removeRole(roles) {
124
- for (const val of roles) {
125
- const idx = this.roles.indexOf(val);
126
- if (idx !== -1) {
127
- this.roles.splice(idx, 1);
128
- }
129
- }
130
- this.aclChange.next(this.data);
131
- }
132
- /**
133
- * 为当前用户移除权限
134
- */
135
- removeAbility(abilities) {
136
- for (const val of abilities) {
137
- const idx = this.abilities.indexOf(val);
138
- if (idx !== -1) {
139
- this.abilities.splice(idx, 1);
140
- }
141
- }
142
- this.aclChange.next(this.data);
143
- }
144
- /**
145
- * 当前用户是否有对应角色,其实 `number` 表示Ability
146
- *
147
- * - 当 `full: true` 或参数 `null` 时返回 `true`
148
- * - 若使用 `ACLType` 参数,可以指定 `mode` 校验模式
149
- */
150
- can(roleOrAbility) {
151
- const { preCan } = this.options;
152
- if (preCan) {
153
- roleOrAbility = preCan(roleOrAbility);
154
- }
155
- const t = this.parseACLType(roleOrAbility);
156
- let result = false;
157
- if (this.full === true || !roleOrAbility) {
158
- result = true;
159
- }
160
- else {
161
- if (t.role && t.role.length > 0) {
162
- if (t.mode === 'allOf') {
163
- result = t.role.every(v => this.roles.includes(v));
164
- }
165
- else {
166
- result = t.role.some(v => this.roles.includes(v));
167
- }
168
- }
169
- if (t.ability && t.ability.length > 0) {
170
- if (t.mode === 'allOf') {
171
- result = t.ability.every(v => this.abilities.includes(v));
172
- }
173
- else {
174
- result = t.ability.some(v => this.abilities.includes(v));
175
- }
176
- }
177
- }
178
- return t.except === true ? !result : result;
179
- }
180
- /** @inner */
181
- parseAbility(value) {
182
- if (typeof value === 'number' || typeof value === 'string' || Array.isArray(value)) {
183
- value = { ability: Array.isArray(value) ? value : [value] };
184
- }
185
- delete value.role;
186
- return value;
187
- }
188
- /**
189
- * 当前用户是否有对应权限点
190
- */
191
- canAbility(value) {
192
- return this.can(this.parseAbility(value));
193
- }
194
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLService, deps: [{ token: i1.YunzaiConfigService }], target: i0.ɵɵFactoryTarget.Injectable }); }
195
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLService, providedIn: 'root' }); }
196
- }
197
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLService, decorators: [{
198
- type: Injectable,
199
- args: [{ providedIn: 'root' }]
200
- }], ctorParameters: () => [{ type: i1.YunzaiConfigService }] });
201
-
202
- class ACLIfDirective {
203
- constructor() {
204
- this.srv = inject(ACLService);
205
- this._viewContainer = inject(ViewContainerRef);
206
- this._thenTemplateRef = inject((TemplateRef));
207
- this._elseTemplateRef = null;
208
- this._thenViewRef = null;
209
- this._elseViewRef = null;
210
- this._except = false;
211
- this._change$ = this.srv.change
212
- .pipe(takeUntilDestroyed(), filter(r => r != null))
213
- .subscribe(() => this._updateView());
214
- }
215
- set aclIf(value) {
216
- this._value = value;
217
- this._updateView();
218
- }
219
- set aclIfThen(templateRef) {
220
- this._thenTemplateRef = templateRef;
221
- this._thenViewRef = null;
222
- this._updateView();
223
- }
224
- set aclIfElse(templateRef) {
225
- this._elseTemplateRef = templateRef;
226
- this._elseViewRef = null;
227
- this._updateView();
228
- }
229
- set except(value) {
230
- this._except = value != null && `${value}` !== 'false';
231
- }
232
- get except() {
233
- return this._except;
234
- }
235
- _updateView() {
236
- const res = this.srv.can(this._value);
237
- if ((res && !this.except) || (!res && this.except)) {
238
- if (!this._thenViewRef) {
239
- this._viewContainer.clear();
240
- this._elseViewRef = null;
241
- if (this._thenTemplateRef) {
242
- this._thenViewRef = this._viewContainer.createEmbeddedView(this._thenTemplateRef);
243
- }
244
- }
245
- }
246
- else {
247
- if (!this._elseViewRef) {
248
- this._viewContainer.clear();
249
- this._thenViewRef = null;
250
- if (this._elseTemplateRef) {
251
- this._elseViewRef = this._viewContainer.createEmbeddedView(this._elseTemplateRef);
252
- }
253
- }
254
- }
255
- }
256
- ngOnDestroy() {
257
- this._change$.unsubscribe();
258
- }
259
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLIfDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
260
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.0", type: ACLIfDirective, isStandalone: true, selector: "[aclIf]", inputs: { aclIf: "aclIf", aclIfThen: "aclIfThen", aclIfElse: "aclIfElse", except: "except" }, exportAs: ["aclIf"], ngImport: i0 }); }
261
- }
262
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLIfDirective, decorators: [{
263
- type: Directive,
264
- args: [{
265
- selector: '[aclIf]',
266
- exportAs: 'aclIf',
267
- standalone: true
268
- }]
269
- }], ctorParameters: () => [], propDecorators: { aclIf: [{
270
- type: Input
271
- }], aclIfThen: [{
272
- type: Input
273
- }], aclIfElse: [{
274
- type: Input
275
- }], except: [{
276
- type: Input
277
- }] } });
278
-
279
- class ACLDirective {
280
- set acl(value) {
281
- this.set(value);
282
- }
283
- set ability(value) {
284
- this.set(this.srv.parseAbility(value));
285
- }
286
- set(value) {
287
- this._value = value;
288
- const CLS = 'acl__hide';
289
- const el = this.el;
290
- if (this.srv.can(this._value)) {
291
- this.renderer.removeClass(el, CLS);
292
- }
293
- else {
294
- this.renderer.addClass(el, CLS);
295
- }
296
- }
297
- constructor() {
298
- this.el = inject(ElementRef).nativeElement;
299
- this.renderer = inject(Renderer2);
300
- this.srv = inject(ACLService);
301
- this.change$ = this.srv.change
302
- .pipe(takeUntilDestroyed(), filter(r => r != null))
303
- .subscribe(() => this.set(this._value));
304
- }
305
- ngOnDestroy() {
306
- this.change$.unsubscribe();
307
- }
308
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
309
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.0", type: ACLDirective, isStandalone: true, selector: "[acl]", inputs: { acl: "acl", ability: ["acl-ability", "ability"] }, exportAs: ["acl"], ngImport: i0 }); }
310
- }
311
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLDirective, decorators: [{
312
- type: Directive,
313
- args: [{
314
- selector: '[acl]',
315
- exportAs: 'acl',
316
- standalone: true
317
- }]
318
- }], ctorParameters: () => [], propDecorators: { acl: [{
319
- type: Input,
320
- args: ['acl']
321
- }], ability: [{
322
- type: Input,
323
- args: ['acl-ability']
324
- }] } });
325
-
326
- /**
327
- * NOTE:`ACLType` 类型可能会被其他类库所引用,为了减少类库间彼此的依赖性,其他类库会以复制的形式存在
328
- * 当这里有变化时,请务必同步更新,涉及:`MenuService.acl`、`util.YunzaiACLType`
329
- * TODO: 尝试增加 `@yelon/core` 类库用于处理这种通用型
330
- */
331
-
332
- class ACLGuardService {
333
- constructor() {
334
- this.srv = inject(ACLService);
335
- this.router = inject(Router);
336
- this.injector = inject(Injector);
337
- }
338
- process(data) {
339
- data = {
340
- guard: null,
341
- guard_url: this.srv.guard_url,
342
- ...data
343
- };
344
- let guard = data.guard;
345
- if (typeof guard === 'function')
346
- guard = guard(this.srv, this.injector);
347
- return (guard && guard instanceof Observable ? guard : of(guard != null ? guard : null)).pipe(map(v => this.srv.can(v)), tap(v => {
348
- if (v)
349
- return;
350
- this.router.navigateByUrl(data.guard_url);
351
- }));
352
- }
353
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLGuardService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
354
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLGuardService, providedIn: 'root' }); }
355
- }
356
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: ACLGuardService, decorators: [{
357
- type: Injectable,
358
- args: [{ providedIn: 'root' }]
359
- }] });
360
- /**
361
- * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
362
- *
363
- * ```ts
364
- * data: {
365
- * path: 'home',
366
- * canActivate: [ aclCanActivate ],
367
- * data: { guard: 'user1' }
368
- * }
369
- * ```
370
- */
371
- const aclCanActivate = route => inject(ACLGuardService).process(route.data);
372
- /**
373
- * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
374
- *
375
- * ```ts
376
- * data: {
377
- * path: 'home',
378
- * canActivateChild: [ aclCanActivateChild ],
379
- * data: { guard: 'user1' }
380
- * }
381
- * ```
382
- */
383
- const aclCanActivateChild = route => inject(ACLGuardService).process(route.data);
384
- /**
385
- * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
386
- *
387
- * ```ts
388
- * data: {
389
- * path: 'home',
390
- * canMatch: [ aclCanMatch ],
391
- * data: { guard: 'user1' }
392
- * }
393
- * ```
394
- */
395
- const aclCanMatch = route => inject(ACLGuardService).process(route.data);
396
-
397
- const COMPONENTS = [ACLDirective, ACLIfDirective];
398
- class YelonACLModule {
399
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: YelonACLModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
400
- static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.0", ngImport: i0, type: YelonACLModule, imports: [CommonModule, ACLDirective, ACLIfDirective], exports: [ACLDirective, ACLIfDirective] }); }
401
- static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: YelonACLModule, imports: [CommonModule] }); }
402
- }
403
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: YelonACLModule, decorators: [{
404
- type: NgModule,
405
- args: [{
406
- imports: [CommonModule, ...COMPONENTS],
407
- exports: COMPONENTS
408
- }]
409
- }] });
410
-
411
- /**
412
- * Generated bundle index. Do not edit.
413
- */
414
-
415
- export { ACLDirective, ACLGuardService, ACLIfDirective, ACLService, ACL_DEFAULT_CONFIG, YelonACLModule, aclCanActivate, aclCanActivateChild, aclCanMatch };
416
- //# sourceMappingURL=acl.mjs.map
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, inject, ViewContainerRef, TemplateRef, Directive, Input, ElementRef, Renderer2, Injector, NgModule } from '@angular/core';
3
+ import { BehaviorSubject, filter, Observable, of, map, tap } from 'rxjs';
4
+ import * as i1 from '@yelon/util/config';
5
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
6
+ import { Router } from '@angular/router';
7
+ import { CommonModule } from '@angular/common';
8
+
9
+ const ACL_DEFAULT_CONFIG = {
10
+ guard_url: `/403`
11
+ };
12
+
13
+ /**
14
+ * ACL 控制服务,[在线文档](https://ng.yunzainfo.com/acl)
15
+ */
16
+ class ACLService {
17
+ /** ACL变更通知 */
18
+ get change() {
19
+ return this.aclChange.asObservable();
20
+ }
21
+ /** 获取所有数据 */
22
+ get data() {
23
+ return {
24
+ full: this.full,
25
+ roles: this.roles,
26
+ abilities: this.abilities
27
+ };
28
+ }
29
+ get guard_url() {
30
+ return this.options.guard_url;
31
+ }
32
+ constructor(configSrv) {
33
+ this.roles = [];
34
+ this.abilities = [];
35
+ this.full = false;
36
+ this.aclChange = new BehaviorSubject(null);
37
+ this.options = configSrv.merge('acl', ACL_DEFAULT_CONFIG);
38
+ }
39
+ parseACLType(val) {
40
+ let t;
41
+ if (typeof val === 'number') {
42
+ t = { ability: [val] };
43
+ }
44
+ else if (Array.isArray(val) && val.length > 0 && typeof val[0] === 'number') {
45
+ t = { ability: val };
46
+ }
47
+ else if (typeof val === 'object' && !Array.isArray(val)) {
48
+ t = { ...val };
49
+ }
50
+ else if (Array.isArray(val)) {
51
+ t = { role: val };
52
+ }
53
+ else {
54
+ t = { role: val == null ? [] : [val] };
55
+ }
56
+ return { except: false, ...t };
57
+ }
58
+ /**
59
+ * 设置当前用户角色或权限能力(会先清除所有)
60
+ */
61
+ set(value) {
62
+ this.full = false;
63
+ this.abilities = [];
64
+ this.roles = [];
65
+ this.add(value);
66
+ this.aclChange.next(value);
67
+ }
68
+ /**
69
+ * 标识当前用户为全量,即不受限
70
+ */
71
+ setFull(val) {
72
+ this.full = val;
73
+ this.aclChange.next(val);
74
+ }
75
+ /**
76
+ * 设置当前用户权限能力(会先清除所有)
77
+ */
78
+ setAbility(abilities) {
79
+ this.set({ ability: abilities });
80
+ }
81
+ /**
82
+ * 设置当前用户角色(会先清除所有)
83
+ */
84
+ setRole(roles) {
85
+ this.set({ role: roles });
86
+ }
87
+ /**
88
+ * 为当前用户增加角色或权限能力
89
+ */
90
+ add(value) {
91
+ if (value.role && value.role.length > 0) {
92
+ this.roles.push(...value.role);
93
+ }
94
+ if (value.ability && value.ability.length > 0) {
95
+ this.abilities.push(...value.ability);
96
+ }
97
+ }
98
+ /**
99
+ * 为当前用户附加角色
100
+ */
101
+ attachRole(roles) {
102
+ for (const val of roles) {
103
+ if (!this.roles.includes(val)) {
104
+ this.roles.push(val);
105
+ }
106
+ }
107
+ this.aclChange.next(this.data);
108
+ }
109
+ /**
110
+ * 为当前用户附加权限
111
+ */
112
+ attachAbility(abilities) {
113
+ for (const val of abilities) {
114
+ if (!this.abilities.includes(val)) {
115
+ this.abilities.push(val);
116
+ }
117
+ }
118
+ this.aclChange.next(this.data);
119
+ }
120
+ /**
121
+ * 为当前用户移除角色
122
+ */
123
+ removeRole(roles) {
124
+ for (const val of roles) {
125
+ const idx = this.roles.indexOf(val);
126
+ if (idx !== -1) {
127
+ this.roles.splice(idx, 1);
128
+ }
129
+ }
130
+ this.aclChange.next(this.data);
131
+ }
132
+ /**
133
+ * 为当前用户移除权限
134
+ */
135
+ removeAbility(abilities) {
136
+ for (const val of abilities) {
137
+ const idx = this.abilities.indexOf(val);
138
+ if (idx !== -1) {
139
+ this.abilities.splice(idx, 1);
140
+ }
141
+ }
142
+ this.aclChange.next(this.data);
143
+ }
144
+ /**
145
+ * 当前用户是否有对应角色,其实 `number` 表示Ability
146
+ *
147
+ * - 当 `full: true` 或参数 `null` 时返回 `true`
148
+ * - 若使用 `ACLType` 参数,可以指定 `mode` 校验模式
149
+ */
150
+ can(roleOrAbility) {
151
+ const { preCan } = this.options;
152
+ if (preCan) {
153
+ roleOrAbility = preCan(roleOrAbility);
154
+ }
155
+ const t = this.parseACLType(roleOrAbility);
156
+ let result = false;
157
+ if (this.full === true || !roleOrAbility) {
158
+ result = true;
159
+ }
160
+ else {
161
+ if (t.role && t.role.length > 0) {
162
+ if (t.mode === 'allOf') {
163
+ result = t.role.every(v => this.roles.includes(v));
164
+ }
165
+ else {
166
+ result = t.role.some(v => this.roles.includes(v));
167
+ }
168
+ }
169
+ if (t.ability && t.ability.length > 0) {
170
+ if (t.mode === 'allOf') {
171
+ result = t.ability.every(v => this.abilities.includes(v));
172
+ }
173
+ else {
174
+ result = t.ability.some(v => this.abilities.includes(v));
175
+ }
176
+ }
177
+ }
178
+ return t.except === true ? !result : result;
179
+ }
180
+ /** @inner */
181
+ parseAbility(value) {
182
+ if (typeof value === 'number' || typeof value === 'string' || Array.isArray(value)) {
183
+ value = { ability: Array.isArray(value) ? value : [value] };
184
+ }
185
+ delete value.role;
186
+ return value;
187
+ }
188
+ /**
189
+ * 当前用户是否有对应权限点
190
+ */
191
+ canAbility(value) {
192
+ return this.can(this.parseAbility(value));
193
+ }
194
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLService, deps: [{ token: i1.YunzaiConfigService }], target: i0.ɵɵFactoryTarget.Injectable }); }
195
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLService, providedIn: 'root' }); }
196
+ }
197
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLService, decorators: [{
198
+ type: Injectable,
199
+ args: [{ providedIn: 'root' }]
200
+ }], ctorParameters: () => [{ type: i1.YunzaiConfigService }] });
201
+
202
+ class ACLIfDirective {
203
+ constructor() {
204
+ this.srv = inject(ACLService);
205
+ this._viewContainer = inject(ViewContainerRef);
206
+ this._thenTemplateRef = inject((TemplateRef));
207
+ this._elseTemplateRef = null;
208
+ this._thenViewRef = null;
209
+ this._elseViewRef = null;
210
+ this._except = false;
211
+ this._change$ = this.srv.change
212
+ .pipe(takeUntilDestroyed(), filter(r => r != null))
213
+ .subscribe(() => this._updateView());
214
+ }
215
+ set aclIf(value) {
216
+ this._value = value;
217
+ this._updateView();
218
+ }
219
+ set aclIfThen(templateRef) {
220
+ this._thenTemplateRef = templateRef;
221
+ this._thenViewRef = null;
222
+ this._updateView();
223
+ }
224
+ set aclIfElse(templateRef) {
225
+ this._elseTemplateRef = templateRef;
226
+ this._elseViewRef = null;
227
+ this._updateView();
228
+ }
229
+ set except(value) {
230
+ this._except = value != null && `${value}` !== 'false';
231
+ }
232
+ get except() {
233
+ return this._except;
234
+ }
235
+ _updateView() {
236
+ const res = this.srv.can(this._value);
237
+ if ((res && !this.except) || (!res && this.except)) {
238
+ if (!this._thenViewRef) {
239
+ this._viewContainer.clear();
240
+ this._elseViewRef = null;
241
+ if (this._thenTemplateRef) {
242
+ this._thenViewRef = this._viewContainer.createEmbeddedView(this._thenTemplateRef);
243
+ }
244
+ }
245
+ }
246
+ else {
247
+ if (!this._elseViewRef) {
248
+ this._viewContainer.clear();
249
+ this._thenViewRef = null;
250
+ if (this._elseTemplateRef) {
251
+ this._elseViewRef = this._viewContainer.createEmbeddedView(this._elseTemplateRef);
252
+ }
253
+ }
254
+ }
255
+ }
256
+ ngOnDestroy() {
257
+ this._change$.unsubscribe();
258
+ }
259
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLIfDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
260
+ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.0.6", type: ACLIfDirective, isStandalone: true, selector: "[aclIf]", inputs: { aclIf: "aclIf", aclIfThen: "aclIfThen", aclIfElse: "aclIfElse", except: "except" }, exportAs: ["aclIf"], ngImport: i0 }); }
261
+ }
262
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLIfDirective, decorators: [{
263
+ type: Directive,
264
+ args: [{
265
+ selector: '[aclIf]',
266
+ exportAs: 'aclIf',
267
+ standalone: true
268
+ }]
269
+ }], ctorParameters: () => [], propDecorators: { aclIf: [{
270
+ type: Input
271
+ }], aclIfThen: [{
272
+ type: Input
273
+ }], aclIfElse: [{
274
+ type: Input
275
+ }], except: [{
276
+ type: Input
277
+ }] } });
278
+
279
+ class ACLDirective {
280
+ set acl(value) {
281
+ this.set(value);
282
+ }
283
+ set ability(value) {
284
+ this.set(this.srv.parseAbility(value));
285
+ }
286
+ set(value) {
287
+ this._value = value;
288
+ const CLS = 'acl__hide';
289
+ const el = this.el;
290
+ if (this.srv.can(this._value)) {
291
+ this.renderer.removeClass(el, CLS);
292
+ }
293
+ else {
294
+ this.renderer.addClass(el, CLS);
295
+ }
296
+ }
297
+ constructor() {
298
+ this.el = inject(ElementRef).nativeElement;
299
+ this.renderer = inject(Renderer2);
300
+ this.srv = inject(ACLService);
301
+ this.change$ = this.srv.change
302
+ .pipe(takeUntilDestroyed(), filter(r => r != null))
303
+ .subscribe(() => this.set(this._value));
304
+ }
305
+ ngOnDestroy() {
306
+ this.change$.unsubscribe();
307
+ }
308
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
309
+ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.0.6", type: ACLDirective, isStandalone: true, selector: "[acl]", inputs: { acl: "acl", ability: ["acl-ability", "ability"] }, exportAs: ["acl"], ngImport: i0 }); }
310
+ }
311
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLDirective, decorators: [{
312
+ type: Directive,
313
+ args: [{
314
+ selector: '[acl]',
315
+ exportAs: 'acl',
316
+ standalone: true
317
+ }]
318
+ }], ctorParameters: () => [], propDecorators: { acl: [{
319
+ type: Input,
320
+ args: ['acl']
321
+ }], ability: [{
322
+ type: Input,
323
+ args: ['acl-ability']
324
+ }] } });
325
+
326
+ /**
327
+ * NOTE:`ACLType` 类型可能会被其他类库所引用,为了减少类库间彼此的依赖性,其他类库会以复制的形式存在
328
+ * 当这里有变化时,请务必同步更新,涉及:`MenuService.acl`、`util.YunzaiACLType`
329
+ * TODO: 尝试增加 `@yelon/core` 类库用于处理这种通用型
330
+ */
331
+
332
+ class ACLGuardService {
333
+ constructor() {
334
+ this.srv = inject(ACLService);
335
+ this.router = inject(Router);
336
+ this.injector = inject(Injector);
337
+ }
338
+ process(data) {
339
+ data = {
340
+ guard: null,
341
+ guard_url: this.srv.guard_url,
342
+ ...data
343
+ };
344
+ let guard = data.guard;
345
+ if (typeof guard === 'function')
346
+ guard = guard(this.srv, this.injector);
347
+ return (guard && guard instanceof Observable ? guard : of(guard != null ? guard : null)).pipe(map(v => this.srv.can(v)), tap(v => {
348
+ if (v)
349
+ return;
350
+ this.router.navigateByUrl(data.guard_url);
351
+ }));
352
+ }
353
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLGuardService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
354
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLGuardService, providedIn: 'root' }); }
355
+ }
356
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: ACLGuardService, decorators: [{
357
+ type: Injectable,
358
+ args: [{ providedIn: 'root' }]
359
+ }] });
360
+ /**
361
+ * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
362
+ *
363
+ * ```ts
364
+ * data: {
365
+ * path: 'home',
366
+ * canActivate: [ aclCanActivate ],
367
+ * data: { guard: 'user1' }
368
+ * }
369
+ * ```
370
+ */
371
+ const aclCanActivate = route => inject(ACLGuardService).process(route.data);
372
+ /**
373
+ * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
374
+ *
375
+ * ```ts
376
+ * data: {
377
+ * path: 'home',
378
+ * canActivateChild: [ aclCanActivateChild ],
379
+ * data: { guard: 'user1' }
380
+ * }
381
+ * ```
382
+ */
383
+ const aclCanActivateChild = route => inject(ACLGuardService).process(route.data);
384
+ /**
385
+ * Routing guard prevent unauthorized users visit the page, [ACL Document](https://ng.yunzainfo.com/acl).
386
+ *
387
+ * ```ts
388
+ * data: {
389
+ * path: 'home',
390
+ * canMatch: [ aclCanMatch ],
391
+ * data: { guard: 'user1' }
392
+ * }
393
+ * ```
394
+ */
395
+ const aclCanMatch = route => inject(ACLGuardService).process(route.data);
396
+
397
+ const COMPONENTS = [ACLDirective, ACLIfDirective];
398
+ class YelonACLModule {
399
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: YelonACLModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
400
+ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.0.6", ngImport: i0, type: YelonACLModule, imports: [CommonModule, ACLDirective, ACLIfDirective], exports: [ACLDirective, ACLIfDirective] }); }
401
+ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: YelonACLModule, imports: [CommonModule] }); }
402
+ }
403
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.6", ngImport: i0, type: YelonACLModule, decorators: [{
404
+ type: NgModule,
405
+ args: [{
406
+ imports: [CommonModule, ...COMPONENTS],
407
+ exports: COMPONENTS
408
+ }]
409
+ }] });
410
+
411
+ /**
412
+ * Generated bundle index. Do not edit.
413
+ */
414
+
415
+ export { ACLDirective, ACLGuardService, ACLIfDirective, ACLService, ACL_DEFAULT_CONFIG, YelonACLModule, aclCanActivate, aclCanActivateChild, aclCanMatch };
416
+ //# sourceMappingURL=acl.mjs.map