@@ -21,6 +21,10 @@ Then on your app.module.ts
21
21
}),
22
22
inject: [ConfigService ]
23
23
}),
24
+ ],
25
+ providers: [
26
+ { provide: APP_GUARD , useClass: AuthGuard },
27
+ { provide: APP_GUARD , useClass: ResourceGuard }
24
28
]
25
29
})
26
30
```
@@ -30,21 +34,66 @@ Then on your app.module.ts
30
34
By default nestjs-keycloak-admin supports User Managed Access for managing your resources.
31
35
32
36
``` typescript
33
- class Organization () {
37
+ @Controller ()
38
+ @DefineResource (' organization' )
39
+ class OrganizationController () {
34
40
constructor (private readonly adminProvider : KeycloakAdminService ) {}
35
41
36
- async findAll(): UMAResource [] {
37
- return this .adminProvider .resourceManager .findAll ()
42
+ @Get (' /hello' )
43
+ @Public ()
44
+ hello(): string {
45
+ return ' life is short'
38
46
}
39
47
40
- async create(payload : payload ): Promise <UMAResource > {
41
- const resource = new UMAResource (payload )
48
+ @Get (' /)
49
+ @FetchResources ()
50
+ findAll (@Request (' resources' ): Resource []): Resource [] {
51
+ return resources
52
+ }
53
+
54
+ @Get (' /:id' )
55
+ @DefineScope (' read' ) // this will check organization:read permission
56
+ @DefineResourceEnforcer ({
57
+ id : (req : any ) => req .params .id
58
+ })
59
+ findOne (@Request (' resource' ): Resource ): Resource ) {
60
+ return resource
61
+ }
62
+
63
+ @Get (' /slug/:slug' )
64
+ @DefineScope (' read' )
65
+ @DefineResourceEnforcer ({
66
+ id : async (req : any , context : ExecutionContext ) => {
67
+ const class = context .getClass <OrganizationController >()
68
+ const org = await class .typeormProvider .findBySlug (req .params .slug )
69
+ return org .keycloakId
70
+ }
71
+ })
72
+ findBySlug(@Request (' resource' ): Resource ): Resource ) {
73
+ return resource
74
+ }
75
+
76
+ @Post (' /' )
77
+ @DefineScope (' create' )
78
+ async create((): Promise <Resource > {
79
+ let resource = new Resource ({
80
+ name: ' resource' ,
81
+ displayName: ' My Resource'
82
+ })
42
83
.setOwner (1 )
43
- .addScope ( ' organization:create ' )
44
- .setType (' organization' )
84
+ .addScopes ([ new Scope ( ' organization:read ' ), new Scope ( ' organization:write ' )] )
85
+ .setType (' urn:resource-server:type: organization' )
45
86
.setUri (' /organization/123' )
87
+ .setAttributes ({
88
+ valid: true ,
89
+ types: [' customer' , ' any' ]
90
+ })
91
+
92
+ resource = await this .adminProvider .create (resource )
93
+
94
+ // create organization on your resource server and add link to resource.id, to access it later.
46
95
47
- return this . adminProvider . create ( resource )
96
+ return resource
48
97
}
49
98
}
50
99
```
0 commit comments