我在NodeJS(使用TypeScript)和MongoDB(使用mongoose)中构建一个web应用程序。用户权限的实现
在我的应用程序中,我有6种类型的权限。 为了简单起见,我们使用P1,P2,...,P6来表示我的权限。
此外,该应用程序有几个组织。
应用程序中的每个用户都属于一个组织,并且可以为每个组织(不一定是他所属的组织)拥有多个权限(P1-P6)。
例如:
- 用户 'U1' 属于 'O1' 的组织,有权限P1,P3在组织O1'和权限 P2,P6在组织 'O2'。
- 用户'U2'属于'O2'组织,在组织'O2'中拥有权限P1。
- 用户'U3'属于'O1'组织,没有权限。
我想在用户架构(使用mongoose模式)中实现这个结构,并且需要关于我的实现的一些意见。
这是我的用户模式:
var userSchema: mongoose.Schema = new mongoose.Schema({
_id: {
type: String
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
mail: {
type: String,
required: true,
unique: true
},
organization: { // User's organization
type: mongoose.Schema.Types.ObjectId,
ref: 'Organization'
},
permissions: [{
organization: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Organization'
},
permissions: [{
type: String,
enum: [
Permission.P1,
Permission.P2,
Permission.P3,
Permission.P4,
Permission.P5,
Permission.P6,
]
}]
}]
});
对于权限枚举我用:
export enum Permission {
P1 = <any>'P1',
P2 = <any>'P2',
P3 = <any>'P3',
P4 = <any>'P4',
P5 = <any>'P5',
P6 = <any>'P6',
}
我使用这个枚举的字符串值,因为我认为这是最好有的字符串值许可,而不仅仅是枚举数值(1-6)。 当然,许可名称(P1-P6)被替换为真实的许可名称 - 例如P1是Administrator。
我不确定我目前的执行情况,并希望得到一些意见,不管我做得好还是应该改变一些事情。
请随时批评我的代码和思维方式。
谢谢,
Ron。
每个组织可以更改权限它的用户,因此这种方法不是很适合我... – Ron537