2020-10-12 21:28:44 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const crypto = require("crypto");
|
2020-10-12 21:35:34 +02:00
|
|
|
const privateKey = process.env.NODE_ENV === "test" ? require("../config/config_example.json") : require("../config/config.json").passwordPrivateKey;
|
2020-10-12 21:28:44 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
Model
|
|
|
|
} = require("sequelize");
|
|
|
|
module.exports = (sequelize, DataTypes) => {
|
|
|
|
function hash(password, email) {
|
2020-12-12 18:28:44 +01:00
|
|
|
const cipher = crypto.createCipheriv(
|
2020-10-12 21:28:44 +02:00
|
|
|
"aes-256-cbc",
|
|
|
|
privateKey,
|
|
|
|
crypto.createHash("md5").update(email).digest("base64").slice(0, 16)
|
|
|
|
);
|
|
|
|
return cipher.update(password, "utf8", "base64") + cipher.final("base64");
|
|
|
|
}
|
|
|
|
|
|
|
|
class User extends Model {
|
|
|
|
static associate(models) {
|
2020-11-22 16:48:06 +01:00
|
|
|
User.belongsToMany(models.Group, {through: "UserGroup"});
|
2020-12-12 18:20:02 +01:00
|
|
|
User.belongsToMany(models.Event, {through: "EventTeacher"});
|
2020-12-12 22:49:09 +01:00
|
|
|
User.belongsToMany(models.Evaluation, {through: "EvaluationTeacher"});
|
2020-11-22 16:48:06 +01:00
|
|
|
User.belongsToMany(models.UE, {through: "UEUser"});
|
2020-10-12 21:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
checkPassword(password) {
|
|
|
|
return hash(password, this.email) === this.passwordHash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
User.init({
|
|
|
|
email: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
},
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
emailVerified : {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
defaultValue: false,
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
emailToken: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
firstName: {
|
|
|
|
type: DataTypes.STRING,
|
2020-10-12 23:24:59 +02:00
|
|
|
allowNull: false
|
2020-10-12 21:28:44 +02:00
|
|
|
},
|
|
|
|
lastName: {
|
|
|
|
type: DataTypes.STRING,
|
2020-10-12 23:24:59 +02:00
|
|
|
allowNull: false
|
2020-10-12 21:28:44 +02:00
|
|
|
},
|
|
|
|
passwordHash: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
set(value) {
|
|
|
|
if (value)
|
|
|
|
this.setDataValue("passwordHash", hash(value, this.email));
|
|
|
|
}
|
|
|
|
},
|
2020-11-02 17:04:20 +01:00
|
|
|
passwordToken: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
passwordTokenDate: {
|
|
|
|
type: DataTypes.DATE
|
|
|
|
},
|
2020-10-12 21:28:44 +02:00
|
|
|
permissions: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
defaultValue: 0,
|
|
|
|
allowNull: false
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sequelize,
|
|
|
|
modelName: "User",
|
|
|
|
});
|
|
|
|
return User;
|
|
|
|
};
|