-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
46 lines (37 loc) · 1.27 KB
/
user.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
const mongoose = require('mongoose');
const { authorize } = require('../../../../lib');
const policies = require('../policies');
const { Schema } = mongoose;
const UserSchema = mongoose.Schema({
name: { type: String, required: true },
username: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
roles: [{ type: Schema.Types.ObjectId, ref: 'Role' }],
});
UserSchema.virtual('permissions').get(async function f() {
const Role = mongoose.model('Role');
const roles = await Role.find({ _id: { $in: this.roles.toObject() } });
let permissions = [];
roles.forEach((role) => {
permissions = permissions.concat(role.permissions);
});
return permissions;
});
/**
* This method can be used to conveniently check whether the user can perform
* a given action on an entity. This can prove useful if you still need to
* perform an authorization check without necessary doing it at the
* routing level.
*
* @param action
* @param entity
* @param req
* @returns {Promise<boolean>}
*/
UserSchema.methods.can = async function f(action, entity, req = null) {
return (
authorize(action, entity, await this.permissions, policies, req) === true
);
};
module.exports = mongoose.model('User', UserSchema);