-
Notifications
You must be signed in to change notification settings - Fork 1
/
authorization.js
240 lines (172 loc) · 7.8 KB
/
authorization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const _ = require('lodash');
const crypto = require('crypto');
module.exports = function (Model, options) {
const userModelName = options.userModelName || 'user';
const roleModelName = options.roleModelName || 'role';
const adminRoleName = options.adminRoleName || 'admin';
const dataSourcesName = options.dataSourcesName || 'db';
const guestAccess = options.guestAccess || false;
Model.getApp((err, app) => {
const modelBuilder = app.dataSources[dataSourcesName].modelBuilder;
let role, user = app.models[userModelName];
modelBuilder.models[roleModelName].on('attached', roleModelSetup);
Model.beforeRemote('**', checkModelAccessControl);
function checkModelAccessControl(ctx, unused, next) {
checkLogin(ctx.req.accessToken).then((accessToken) => {
return getUserInstanceRole(accessToken.userId);
}).then((userRoles) => {
if (isUserAdmin(userRoles)) {
throw 'The token is admin user.';
} else {
return getModelInstanceRoles(ctx.instance ? ctx.instance : ctx.args.id, userRoles);
}
}).then((roles) => {
return getMatchedRole(roles);
}).then((matchedRole) => {
matchedRole.save();
return checkAccessControl(ctx.methodString, matchedRole);
}).then((result) => {
throw result;
}).catch((err) => {
if (err.status !== 401) {
next();
} else {
ctx.res.status(err.status);
next(new Error(err.message));
}
});
}
function checkLogin(accessToken) {
return new Promise((resolve, reject) => {
if (accessToken) {
resolve(accessToken);
} else {
guestAccess ? reject('User not login.') : reject({message: 'User not login.', status: 401});
}
})
}
function checkAccessControl(methodString, matchedRole) {
return new Promise((resolve, reject) => {
if (matchedRole.acl[methodString.replace(/\./g, '_')] === true) {
resolve(`${matchedRole.name} has the premission of ${methodString}.`)
} else {
reject({message: `"${matchedRole.name}" cannot get "${methodString}".`, status: 401})
}
})
}
function getMatchedRole(roles = [[], []]) {
const userRoles = roles[0], modelRoles = roles[1];
// Promise返回user在该Model实例中的角色
return new Promise((resolve, reject) => {
for (const role of modelRoles) {
const roleResult = _.find(userRoles, role);
if (roleResult) {
resolve(roleResult)
}
}
// 如果没有,则返回空
guestAccess ? reject('No matched roles.') : reject({message: 'No matched roles.', status: 401})
})
}
function getModelInstanceRoles(modelInstance, userRoles) {
return new Promise((resolve, reject) => {
if (typeof modelInstance === 'string') {
// 如果没有实例但有实例id的,获取Model实例
return getModelInstance(modelInstance).then((modelInstance) => {
// 获取Model实例的所有角色
return getModelRoles(modelInstance)
}).then((modelRoles) => {
// 并且获取user的所有角色,返回给最终Promise
resolve([userRoles, modelRoles])
})
} else if (modelInstance) {
// 如果有实例的,获取Model实例的所有角色
return getModelRoles(modelInstance).then((modelRoles) => {
// 并且获取user的所有角色
resolve([userRoles, modelRoles])
})
} else {
// 如果没有Model实例的,交给ACL控制
reject('No Model Instance');
}
})
}
function getModelInstance(id) {
return Model.findOne({where: {id: id}})
}
function getModelRoles(modelInstance) {
return modelInstance.roles({})
}
function isUserAdmin(userRoles) {
return _.find(userRoles, {'name': adminRoleName})
}
function getUserRole(userInstance) {
return userInstance.roles({})
}
function getUserInstanceRole(userId) {
return user.findOne({where: {id: userId}}).then((userInstance) => {
return getUserRole(userInstance)
})
}
function roleModelSetup() {
role = app.models[roleModelName];
role.observe('before save', (ctx, next) => {
const methodListHash = getMethodListHash(app);
if (ctx.isNewInstance && ctx.instance.name !== adminRoleName) {
ctx.instance.acl = getDefaultModelAcl(app);
console.log(`Auto create role model Acl.`);
} else if (ctx.instance.name !== adminRoleName && ctx.instance.acl.aclHash !== methodListHash) {
const defaultModelAcl = getDefaultModelAcl(app);
for (const oldMethodName in ctx.instance.acl) {
if (defaultModelAcl[oldMethodName] === undefined) {
delete ctx.instance.acl[oldMethodName];
}
}
for (const newMethodName in defaultModelAcl) {
if (ctx.instance.acl[newMethodName] === undefined) {
ctx.instance.acl[newMethodName] = false;
}
}
ctx.instance.acl.aclHash = methodListHash;
console.log(`Auto refresh role model Acl.`);
}
next();
});
// 定义当前Model有多个role
Model.hasMany(role, {as: `${roleModelName}s`, foreignKey: `${Model.modelName}Id`});
// 定义role只属于一个Model
role.belongsTo(Model, {foreignKey: `${Model.modelName}Id`});
}
function getDefaultModelAcl(app) {
const hash = crypto.createHash('sha256');
let defaultMethodAcl = {}, methodList = '';
app.models().forEach((Model) => {
if (isModelHasAuthorizationMixins(Model)) {
for (const method of Model.sharedClass.methods()) {
methodList += method.stringName;
defaultMethodAcl[method.stringName.replace(/\./g, '_')] = false;
}
}
});
hash.update(methodList);
defaultMethodAcl.aclHash = hash.digest('base64');
return defaultMethodAcl;
}
function getMethodListHash(app) {
let methodList = '';
const hash = crypto.createHash('sha256');
app.models().forEach((Model) => {
if (isModelHasAuthorizationMixins(Model)) {
for (const method of Model.sharedClass.methods()) {
methodList += method.stringName;
}
}
});
hash.update(methodList);
return hash.digest('base64');
}
function isModelHasAuthorizationMixins(Model) {
return Model.settings.mixins && Model.settings.mixins.Authorization;
}
})
};