Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-4942 - authorization reference service #4413

Merged
merged 96 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 80 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
2ddbba4
Move referenceLoader
CeEv Sep 6, 2023
1935cff
Create authorizableReferenceService.
CeEv Sep 6, 2023
80d5578
switch interface and move types inside of authorization module
CeEv Sep 6, 2023
a651dfa
cleanup imports in files-storage module
CeEv Sep 6, 2023
fb5d251
Add short cut for loading user to authorization service
CeEv Sep 6, 2023
d7901dd
Fix tools by adding reference service as additional dependency.
CeEv Sep 6, 2023
508db9a
remove method
CeEv Sep 18, 2023
73c85dc
remove try catch and fix type imports
CeEv Sep 18, 2023
e0632f0
Add wrapper interface from main branch ...useage must be checked afte…
CeEv Sep 18, 2023
32321b3
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 18, 2023
30ac13d
Replace imports from AuthorizableReferenceType with invalid deep links
CeEv Sep 18, 2023
4bf55dc
Update README.md
CeEv Sep 18, 2023
c0029c8
Switch course.copy.uc to reference. Fix issues in tests.
CeEv Sep 18, 2023
1e9eeca
change authorization service to reference in course-export
CeEv Sep 19, 2023
5159463
fix linter
CeEv Sep 19, 2023
5fbb2dc
cleanup authorization in lesson-copy and fix test issolations and str…
CeEv Sep 19, 2023
7a00b9d
Remove authorization references from school.uc.
CeEv Sep 19, 2023
2dbeb88
Add reference service to share-token.uc.
CeEv Sep 19, 2023
18bb2a1
Replace authorization reference methodes. Fix test issolations .
CeEv Sep 19, 2023
f40aafa
fix test for authorization.getUserWithPermissions()
CeEv Sep 19, 2023
594c9f8
linter
CeEv Sep 19, 2023
afcadfd
Add missing id creations
CeEv Sep 19, 2023
9ceacd2
Remove reference service from tool-permissions. Improve tests.
CeEv Sep 20, 2023
757c9d7
Replace authorisation reference methods in video conference. Exclude…
CeEv Sep 22, 2023
32f0bd4
Add comments for needed next steps.
CeEv Sep 22, 2023
4909f78
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 22, 2023
51bb6bd
Fix merge
CeEv Sep 22, 2023
4cc9f67
Improve comments
CeEv Sep 22, 2023
36ea61a
Split module for authorisation and auth reference service
CeEv Sep 22, 2023
5d1b3d7
Add reference service for legacy vidio conference.
CeEv Sep 22, 2023
8507380
Add missing dependencies for resolving modules.
CeEv Sep 22, 2023
d766b6d
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 22, 2023
768d076
Change depended module
CeEv Sep 22, 2023
2c4c75b
linter and fix test
CeEv Sep 22, 2023
5a9831f
Merge branch 'BC-4942-authoriazation-reference-service' of https://gi…
CeEv Sep 22, 2023
7ac618e
Add reference hack to solve dependecy issue
CeEv Sep 22, 2023
72eb47e
remove unnessasary comments
CeEv Sep 22, 2023
9bc1155
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 22, 2023
cc812c9
Fix test setups
CeEv Sep 26, 2023
cae4569
Fix test setup
CeEv Sep 26, 2023
b6cc28a
rewrite sharing lookup token api tests
CeEv Sep 26, 2023
f1d1df3
Fix test for sharing-create-token api
CeEv Sep 26, 2023
7480616
fix test
CeEv Sep 26, 2023
ebdcbb7
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 26, 2023
3f4f777
Throw if error is occured by getJwtFromResponse to get this error inf…
CeEv Sep 26, 2023
7331d3e
Add additional informations to comments
CeEv Sep 26, 2023
7593aec
Fix unstable test
CeEv Sep 27, 2023
5e59774
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Sep 28, 2023
b71eabf
Add missing test for coverage
CeEv Sep 28, 2023
34af759
Merge branch 'BC-4942-authoriazation-reference-service' of https://gi…
CeEv Sep 28, 2023
ef30505
resort files in authorisation module
CeEv Oct 4, 2023
6e2d9ca
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 5, 2023
6faf54f
fix import
CeEv Oct 5, 2023
32cb80d
fix linter
CeEv Oct 5, 2023
5cace57
Solve sonar hint
CeEv Oct 5, 2023
097d4a8
Not needed after refactoring.
CeEv Oct 5, 2023
3a92bb2
Remove comments, change documentation for cleaner example
CeEv Oct 5, 2023
e2e5335
rename methodes
CeEv Oct 5, 2023
6c5262b
rename private methodes to use check
CeEv Oct 5, 2023
cdd8323
rename private methods to use check
CeEv Oct 5, 2023
c01a6f2
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 6, 2023
264af52
Use courseService instead of repo
CeEv Oct 9, 2023
7d9d1c5
Use different notation in test to hold AAA Pattern.
CeEv Oct 9, 2023
582e62e
Cleanup authorisation folder strcuture and resolve dependency issues..
CeEv Oct 10, 2023
e0ec4bc
Fix test
CeEv Oct 11, 2023
c408193
Move rules to authorisation module to solve js import issues that res…
CeEv Oct 11, 2023
6ed15db
Remove forwardRef is not nessaary after moving rules to authorisation…
CeEv Oct 11, 2023
14cbe3e
cleanup moved rules
CeEv Oct 11, 2023
69766b9
move builder to mapper , rename folder to singular
CeEv Oct 11, 2023
b27b5d3
Cleanup import / exports
CeEv Oct 11, 2023
da2e0b7
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 11, 2023
f2b7383
remove comment
CeEv Oct 11, 2023
3621256
Merge branch 'BC-4942-authoriazation-reference-service' of https://gi…
CeEv Oct 11, 2023
295928f
cleanup more import / exports
CeEv Oct 11, 2023
514ba0f
cleanup import / exports
CeEv Oct 11, 2023
fcfb3e3
remove comment
CeEv Oct 11, 2023
3fee5fb
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 11, 2023
95a230c
move types to type folder
CeEv Oct 11, 2023
06bdbce
Merge branch 'BC-4942-authoriazation-reference-service' of https://gi…
CeEv Oct 11, 2023
b77cb9b
replace comment
CeEv Oct 11, 2023
1104baa
change test description
CeEv Oct 11, 2023
88a8237
change test desciptions
CeEv Oct 11, 2023
97d9460
move rules folder one level up
CeEv Oct 12, 2023
5d5eb2c
linter
CeEv Oct 12, 2023
5bc9c7b
Fix last commit in linter conform way, but hold logic like they are b…
CeEv Oct 12, 2023
96a6a6b
Change error case
CeEv Oct 12, 2023
a2ad301
remove typo
CeEv Oct 12, 2023
b12313d
Add test for coverage
CeEv Oct 12, 2023
6ea835b
add missing test for test coverage
CeEv Oct 12, 2023
533cacc
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 12, 2023
2242973
Fix import order
CeEv Oct 12, 2023
ce59660
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 18, 2023
7e99942
remove helper from export it is not needed for now
CeEv Oct 18, 2023
0731245
remove comment
CeEv Oct 18, 2023
5c5bb2f
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 19, 2023
ff47e5f
Merge branch 'main' into BC-4942-authoriazation-reference-service
CeEv Oct 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 44 additions & 57 deletions apps/server/src/modules/authorization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,7 @@ When calling other internal micro service for already authorized operations plea
// next orchestration steps
```

### Example 2 - Execute a Single Operation with Loading Resources

```javascript
// If you don't have an entity but an entity type and id, you can check permission by reference
await this.authorizationService.checkPermissionByReferences(userId, AllowedEntity.course, courseId, AuthorizationContextBuilder.read([]));
// or
await this.authorizationService.hasPermissionByReferences(userId, AllowedEntity.course, courseId, AuthorizationContextBuilder.read([]));
// next orchestration steps
```

### Example 3 - Set Permission(s) of User as Required
### Example 2 - Set Permission(s) of User as Required

```javascript
// Multiple permissions can be added. For a successful authorization, the user need all of them.
Expand Down Expand Up @@ -173,14 +163,13 @@ this.authorizationService.hasPermission(userId, course, PermissionContexts.creat
```ts
async createSchoolBySuperhero(userId: EntityId, params: { name: string }) {

const user = this.authorizationService.getUserWithPermissions(userId);
this.authorizationService.hasAllPermissions(user, [Permission.SCHOOL_CREATE]);

const school = new School(params);
const user = this.authorizationService.getUserWithPermissions(userId);
this.authorizationService.hasAllPermissions(user, [Permission.SCHOOL_CREATE]);

await this.schoolService.save(school);
const school = new School(params);
await this.schoolService.save(school);

return true;
return true;
}

```
Expand All @@ -191,15 +180,15 @@ async createSchoolBySuperhero(userId: EntityId, params: { name: string }) {

async createUserByAdmin(userId: EntityId, params: { email: string, firstName: string, lastName: string, schoolId: EntityId }) {

const user = this.authorizationService.getUserWithPermissions(userId);

await this.authorizationService.checkPermissionByReferences(userId, AllowedEntity.school, schoolId, AuthorizationContextBuilder.write([Permission.INSTANCE, Permission.CREATE_USER]));

const newUser = new User(params)
const user = this.authorizationService.getUserWithPermissions(userId);

const context = AuthorizationContextBuilder.write([Permission.INSTANCE, Permission.CREATE_USER])
await this.authorizationService.checkPermission(user, school, context);

await this.userService.save(newUser);
const newUser = new User(params)
await this.userService.save(newUser);

return true;
return true;
}

```
Expand All @@ -210,18 +199,17 @@ async createUserByAdmin(userId: EntityId, params: { email: string, firstName: st
// admin
async editCourseByAdmin(userId: EntityId, params: { courseId: EntityId, description: string }) {

const course = this.courseService.getCourse(params.courseId);
const user = this.authorizationService.getUserWithPermissions(userId);

const school = course.school

this.authorizationService.hasPermissions(user, school, [Permission.INSTANCE, Permission.COURSE_EDIT]);
const course = this.courseService.getCourse(params.courseId);
const user = this.authorizationService.getUserWithPermissions(userId);
const school = course.school;

course.description = params.description;
const context = AuthorizationContextBuilder.write([Permission.INSTANCE, Permission.CREATE_USER]);
this.authorizationService.checkPermissions(user, school, context);

await this.courseService.save(course);
course.description = params.description;
await this.courseService.save(course);

return true;
return true;
}

```
Expand All @@ -234,18 +222,17 @@ async createCourse(userId: EntityId, params: { schoolId: EntityId }) {
const user = this.authorizationService.getUserWithPermissions(userId);
const school = this.schoolService.getSchool(params.schoolId);

this.authorizationService.checkPermission(user, school
{
action: Actions.write,
requiredPermissions: [Permission.COURSE_CREATE],
}
);
this.authorizationService.checkPermission(user, school
{
action: Actions.write,
requiredPermissions: [Permission.COURSE_CREATE],
}
);

const course = new Course({ school });
const course = new Course({ school });
await this.courseService.saveCourse(course);

await this.courseService.saveCourse(course);

return course;
return course;
}

```
Expand All @@ -255,21 +242,20 @@ async createCourse(userId: EntityId, params: { schoolId: EntityId }) {
```ts
// User can create a lesson to course, so you have a courseId
async createLesson(userId: EntityId, params: { courseId: EntityId }) {
const course = this.courseService.getCourse(params.courseId);
const user = this.authorizationService.getUserWithPermissions(userId);
const course = this.courseService.getCourse(params.courseId);
const user = this.authorizationService.getUserWithPermissions(userId);
// check authorization for user and course
this.authorizationService.checkPermission(user, course
{
action: Actions.write,
requiredPermissions: [Permission.COURSE_EDIT],
}
);

const lesson = new Lesson({course});
this.authorizationService.checkPermission(user, course
{
action: Actions.write,
requiredPermissions: [Permission.COURSE_EDIT],
}
);

await this.lessonService.saveLesson(lesson);
const lesson = new Lesson({course});
await this.lessonService.saveLesson(lesson);

return true;
return true;
}
```

Expand Down Expand Up @@ -345,8 +331,9 @@ The authorization module is the core of authorization. It collects all needed in

### Reference.loader

For situations where only the id and the domain object (string) type is known, it is possible to use the \*ByReferences methods.
They load the reference directly.
It should be use only inside of the authorization module.
It is use to load registrated ressouces by the id and name of the ressource.
This is needed to solve the API requests from external services. (API implementation is missing for now)

> Please keep in mind that it can have an impact on the performance if you use it wrongly.
> We keep it as a seperate method to avoid the usage in areas where the domain object should exist, because we see the risk that a developer could be tempted by the ease of only passing the id.
Expand Down
CeEv marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { forwardRef, Module } from '@nestjs/common';
import {
CourseGroupRepo,
CourseRepo,
LessonRepo,
SchoolExternalToolRepo,
LegacySchoolRepo,
SubmissionRepo,
TaskRepo,
TeamsRepo,
UserRepo,
} from '@shared/repo';
import { ToolModule } from '@src/modules/tool';
import { LoggerModule } from '@src/core/logger';
import { BoardModule } from '@src/modules/board';
import { ReferenceLoader, AuthorizationReferenceService, AuthorizationHelper } from './domain';
import { AuthorizationModule } from './authorization.module';

/**
* This module is part of an intermediate state. In the future it should be replaced by an AuthorizationApiModule.
* For now it is used where the authorization itself needs to load data from the database.
* Avoid using this module and load the needed data in your use cases and then use the normal AuthorizationModule!
*/
@Module({
// TODO: remove forwardRef to TooModule N21-1055
imports: [AuthorizationModule, forwardRef(() => ToolModule), forwardRef(() => BoardModule), LoggerModule],
providers: [
AuthorizationHelper,
ReferenceLoader,
UserRepo,
CourseRepo,
CourseGroupRepo,
TaskRepo,
LegacySchoolRepo,
LessonRepo,
TeamsRepo,
SubmissionRepo,
SchoolExternalToolRepo,
AuthorizationReferenceService,
],
exports: [AuthorizationReferenceService],
})
export class AuthorizationReferenceModule {}
71 changes: 32 additions & 39 deletions apps/server/src/modules/authorization/authorization.module.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
import { forwardRef, Module } from '@nestjs/common';
import { ALL_RULES } from '@shared/domain/rules';
import { Module } from '@nestjs/common';
import { UserRepo } from '@shared/repo';
import { LoggerModule } from '@src/core/logger';
import { FeathersModule } from '@shared/infra/feathers';
import {
CourseGroupRepo,
CourseRepo,
LessonRepo,
SchoolExternalToolRepo,
LegacySchoolRepo,
SubmissionRepo,
TaskRepo,
TeamsRepo,
UserRepo,
} from '@shared/repo';
import { LoggerModule } from '@src/core/logger';
import { LegacySchoolModule } from '@src/modules/legacy-school';
import { ToolModule } from '@src/modules/tool';
import { BoardModule } from '../board';
import { AuthorizationHelper } from './authorization.helper';
import { AuthorizationService } from './authorization.service';
BoardDoRule,
ContextExternalToolRule,
CourseGroupRule,
CourseRule,
LessonRule,
SchoolExternalToolRule,
SubmissionRule,
TaskRule,
TeamRule,
UserRule,
UserLoginMigrationRule,
LegacySchoolRule,
} from './domain/service/rules';
import { AuthorizationHelper, AuthorizationService, RuleManager } from './domain';
import { FeathersAuthorizationService, FeathersAuthProvider } from './feathers';
import { ReferenceLoader } from './reference.loader';
import { RuleManager } from './rule-manager';

@Module({
// TODO: remove forwardRef to TooModule N21-1055
imports: [
FeathersModule,
LoggerModule,
LegacySchoolModule,
forwardRef(() => ToolModule),
forwardRef(() => BoardModule),
],
imports: [FeathersModule, LoggerModule],
providers: [
FeathersAuthorizationService,
FeathersAuthProvider,
AuthorizationService,
...ALL_RULES,
ReferenceLoader,
UserRepo,
CourseRepo,
CourseGroupRepo,
TaskRepo,
LegacySchoolRepo,
LessonRepo,
TeamsRepo,
SubmissionRepo,
SchoolExternalToolRepo,
RuleManager,
AuthorizationHelper,
// rules
BoardDoRule,
ContextExternalToolRule,
CourseGroupRule,
CourseRule,
LessonRule,
SchoolExternalToolRule,
SubmissionRule,
TaskRule,
TeamRule,
UserRule,
UserLoginMigrationRule,
LegacySchoolRule,
],
exports: [FeathersAuthorizationService, AuthorizationService],
exports: [FeathersAuthorizationService, AuthorizationService, AuthorizationHelper],
CeEv marked this conversation as resolved.
Show resolved Hide resolved
})
export class AuthorizationModule {}
100 changes: 0 additions & 100 deletions apps/server/src/modules/authorization/authorization.service.ts

This file was deleted.

Loading