Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: read model for change request access checking (#3380)
- Loading branch information
Showing
18 changed files
with
184 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/lib/features/change-request-access-service/change-request-access-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import User from '../../types/user'; | ||
|
||
export interface IChangeRequestAccessReadModel { | ||
canBypassChangeRequest( | ||
project: string, | ||
environment: string, | ||
user?: User, | ||
): Promise<boolean>; | ||
isChangeRequestsEnabled( | ||
project: string, | ||
environment: string, | ||
): Promise<boolean>; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/lib/features/change-request-access-service/createChangeRequestAccessReadModel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Db, IUnleashConfig } from 'lib/server-impl'; | ||
import { ChangeRequestAccessReadModel } from './sql-change-request-access-read-model'; | ||
import { createAccessService } from '../access/createAccessService'; | ||
import { FakeChangeRequestAccessReadModel } from './fake-change-request-access-read-model'; | ||
import { IChangeRequestAccessReadModel } from './change-request-access-read-model'; | ||
|
||
export const createChangeRequestAccessReadModel = ( | ||
db: Db, | ||
config: IUnleashConfig, | ||
): IChangeRequestAccessReadModel => { | ||
const accessService = createAccessService(db, config); | ||
|
||
return new ChangeRequestAccessReadModel(db, accessService); | ||
}; | ||
|
||
export const createFakeChangeRequestAccessService = | ||
(): IChangeRequestAccessReadModel => { | ||
return new FakeChangeRequestAccessReadModel(); | ||
}; |
22 changes: 22 additions & 0 deletions
22
src/lib/features/change-request-access-service/fake-change-request-access-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { IChangeRequestAccessReadModel } from './change-request-access-read-model'; | ||
|
||
export class FakeChangeRequestAccessReadModel | ||
implements IChangeRequestAccessReadModel | ||
{ | ||
private canBypass: boolean; | ||
|
||
private isChangeRequestEnabled: boolean; | ||
|
||
constructor(canBypass = true, isChangeRequestEnabled = true) { | ||
this.canBypass = canBypass; | ||
this.isChangeRequestEnabled = isChangeRequestEnabled; | ||
} | ||
|
||
public async canBypassChangeRequest(): Promise<boolean> { | ||
return this.canBypass; | ||
} | ||
|
||
public async isChangeRequestsEnabled(): Promise<boolean> { | ||
return this.isChangeRequestEnabled; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/lib/features/change-request-access-service/sql-change-request-access-read-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { SKIP_CHANGE_REQUEST } from '../../types'; | ||
import { Db } from '../../db/db'; | ||
import { AccessService } from '../../services'; | ||
import User from '../../types/user'; | ||
import { IChangeRequestAccessReadModel } from './change-request-access-read-model'; | ||
|
||
export class ChangeRequestAccessReadModel | ||
implements IChangeRequestAccessReadModel | ||
{ | ||
private db: Db; | ||
|
||
private accessService: AccessService; | ||
|
||
constructor(db: Db, accessService: AccessService) { | ||
this.db = db; | ||
this.accessService = accessService; | ||
} | ||
|
||
public async canBypassChangeRequest( | ||
project: string, | ||
environment: string, | ||
user?: User, | ||
): Promise<boolean> { | ||
const [canSkipChangeRequest, changeRequestEnabled] = await Promise.all([ | ||
user | ||
? this.accessService.hasPermission( | ||
user, | ||
SKIP_CHANGE_REQUEST, | ||
project, | ||
environment, | ||
) | ||
: Promise.resolve(false), | ||
this.isChangeRequestsEnabled(project, environment), | ||
]); | ||
return !(changeRequestEnabled && !canSkipChangeRequest); | ||
} | ||
|
||
public async isChangeRequestsEnabled( | ||
project: string, | ||
environment: string, | ||
): Promise<boolean> { | ||
const result = await this.db.raw( | ||
`SELECT EXISTS(SELECT 1 | ||
FROM change_request_settings | ||
WHERE environment = ? | ||
and project = ?) AS present`, | ||
[environment, project], | ||
); | ||
const { present } = result.rows[0]; | ||
return present; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.