2021-03-04 21:28:02 +02:00
|
|
|
const config = require("../config");
|
|
|
|
|
|
|
|
const mongoose = require("mongoose");
|
|
|
|
|
|
|
|
const userSchema = new mongoose.Schema({
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
email: String,
|
|
|
|
role: String,
|
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.method("getPublicObject", function() {
|
|
|
|
return {
|
|
|
|
username: this.username,
|
|
|
|
permissionLevel: config.roleMap[this.role],
|
|
|
|
role: this.role,
|
|
|
|
_id: this._id
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
userSchema.method("getFullObject", function() {
|
|
|
|
return {
|
|
|
|
username: this.username,
|
|
|
|
password: this.password,
|
|
|
|
email: this.email,
|
|
|
|
permissionLevel: config.roleMap[this.role],
|
|
|
|
role: this.role,
|
|
|
|
_id: this._id
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const User = mongoose.model("User", userSchema);
|
|
|
|
|
|
|
|
User.findByUsername = async function(username) {
|
|
|
|
return await User.findOne({ username }).exec();
|
|
|
|
};
|
|
|
|
|
2021-03-17 03:01:11 +02:00
|
|
|
User.getPulicFields = function(isPartial=false) {
|
2021-10-21 22:22:35 +03:00
|
|
|
if (isPartial)
|
|
|
|
return "username _id";
|
|
|
|
return "username role _id";
|
2021-03-04 21:28:02 +02:00
|
|
|
};
|
|
|
|
|
2020-10-05 20:36:03 +03:00
|
|
|
module.exports = User;
|