-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Refactor how permissions get serialized for sessions into…
… using a new strategy
- Loading branch information
1 parent
3777555
commit 2929b64
Showing
16 changed files
with
154 additions
and
29 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
39 changes: 39 additions & 0 deletions
39
packages/core/src/config/auth/channel-role-permission-resolver-strategy.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,39 @@ | ||
import { Injector } from '../../common'; | ||
import { TransactionalConnection } from '../../connection'; | ||
import { User } from '../../entity'; | ||
import { ChannelRole } from '../../entity/channel-role/channel-role'; | ||
import { UserChannelPermissions } from '../../service/helpers/utils/get-user-channels-permissions'; | ||
|
||
import { RolePermissionResolverStrategy } from './role-permission-resolver-strategy'; | ||
|
||
export class ChannelRolePermissionResolverStrategy implements RolePermissionResolverStrategy { | ||
private connection: TransactionalConnection; | ||
|
||
async init(injector: Injector) { | ||
this.connection = injector.get(TransactionalConnection); | ||
} | ||
|
||
async resolvePermissions(user: User): Promise<UserChannelPermissions[]> { | ||
console.log('---- BEGIN RESOLVE'); | ||
const channelRoleEntries = await this.connection.rawConnection.getRepository(ChannelRole).find({ | ||
where: { user: { id: user.id } }, | ||
relations: ['user', 'channel', 'role'], | ||
}); | ||
console.log('---- RESOLVE -- ENTRIES:', JSON.stringify(channelRoleEntries)); | ||
|
||
const channelRolePermissions = new Array<UserChannelPermissions>(channelRoleEntries.length); | ||
for (let i = 0; i < channelRoleEntries.length; i++) { | ||
channelRolePermissions[i] = { | ||
id: channelRoleEntries[i].channel.id, | ||
token: channelRoleEntries[i].channel.token, | ||
code: channelRoleEntries[i].channel.code, | ||
permissions: channelRoleEntries[i].role.permissions, | ||
}; | ||
} | ||
channelRoleEntries.sort((a, b) => (a.id < b.id ? -1 : 1)); | ||
console.log('---- RESOLVE -- OUTPUT:', channelRolePermissions); | ||
|
||
console.log('---- END RESOLVE'); | ||
return channelRolePermissions; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/core/src/config/auth/default-role-permission-resolver-strategy.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 '../../entity'; | ||
import { | ||
getChannelPermissions, | ||
UserChannelPermissions, | ||
} from '../../service/helpers/utils/get-user-channels-permissions'; | ||
|
||
import { RolePermissionResolverStrategy } from './role-permission-resolver-strategy'; | ||
|
||
export class DefaultRolePermissionResolverStrategy implements RolePermissionResolverStrategy { | ||
async resolvePermissions(user: User): Promise<UserChannelPermissions[]> { | ||
return getChannelPermissions(user.roles); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/core/src/config/auth/role-permission-resolver-strategy.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,10 @@ | ||
import { InjectableStrategy } from '../../common'; | ||
import { User } from '../../entity'; | ||
import { UserChannelPermissions } from '../../service/helpers/utils/get-user-channels-permissions'; | ||
|
||
/** | ||
* @description TODO | ||
*/ | ||
export interface RolePermissionResolverStrategy extends InjectableStrategy { | ||
resolvePermissions(user: User): Promise<UserChannelPermissions[]>; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { DeepPartial } from '@vendure/common/lib/shared-types'; | ||
import { Column, Entity, ManyToOne } from 'typeorm'; | ||
|
||
import { HasCustomFields } from '../../config'; | ||
import { VendureEntity } from '../base/base.entity'; | ||
import { Channel } from '../channel/channel.entity'; | ||
import { CustomChannelRoleFields } from '../custom-entity-fields'; | ||
import { Role } from '../role/role.entity'; | ||
import { User } from '../user/user.entity'; | ||
|
||
/** | ||
* @description | ||
* TODO | ||
* | ||
* @docsCategory entities | ||
*/ | ||
@Entity() | ||
export class ChannelRole extends VendureEntity implements HasCustomFields { | ||
constructor(input?: DeepPartial<ChannelRole>) { | ||
super(input); | ||
} | ||
|
||
@Column(type => CustomChannelRoleFields) | ||
customFields: CustomChannelRoleFields; | ||
|
||
@ManyToOne(type => User) | ||
user: User; | ||
|
||
@ManyToOne(type => Channel) | ||
channel: Channel; | ||
|
||
@ManyToOne(type => Role) | ||
role: Role; | ||
} |
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
16 changes: 16 additions & 0 deletions
16
packages/core/src/service/services/channel-role.service.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,16 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { TransactionalConnection } from '../../connection'; | ||
|
||
/** | ||
* @description | ||
* Contains methods relating to {@link ChannelRole} entities. | ||
* | ||
* @todo TODO | ||
* | ||
* @docsCategory services | ||
*/ | ||
@Injectable() | ||
export class ChannelRoleService { | ||
constructor(private connection: TransactionalConnection) {} | ||
} |
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.