2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2016-04-20 06:03:55 -04:00
|
|
|
// external modules
|
2019-05-13 06:55:03 -04:00
|
|
|
const Sequelize = require('sequelize')
|
|
|
|
const scrypt = require('scrypt-kdf')
|
2021-05-09 09:35:06 -04:00
|
|
|
const filterXSS = require('xss')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// core
|
2019-05-13 06:55:03 -04:00
|
|
|
const logger = require('../logger')
|
|
|
|
const { generateAvatarURL } = require('../letter-avatars')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
module.exports = function (sequelize, DataTypes) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const User = sequelize.define('User', {
|
2017-03-08 05:45:51 -05:00
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
primaryKey: true,
|
|
|
|
defaultValue: Sequelize.UUIDV4
|
|
|
|
},
|
|
|
|
profileid: {
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
profile: {
|
|
|
|
type: DataTypes.TEXT
|
|
|
|
},
|
|
|
|
history: {
|
|
|
|
type: DataTypes.TEXT
|
|
|
|
},
|
|
|
|
accessToken: {
|
2017-07-10 14:09:59 -04:00
|
|
|
type: DataTypes.TEXT
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
|
|
|
refreshToken: {
|
2017-07-10 14:09:59 -04:00
|
|
|
type: DataTypes.TEXT
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
2018-05-25 12:19:31 -04:00
|
|
|
deleteToken: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: Sequelize.UUIDV4
|
|
|
|
},
|
2017-03-08 05:45:51 -05:00
|
|
|
email: {
|
|
|
|
type: Sequelize.TEXT,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
password: {
|
2019-05-13 04:55:37 -04:00
|
|
|
type: Sequelize.TEXT
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
User.prototype.verifyPassword = function (attempt) {
|
|
|
|
return scrypt.verify(Buffer.from(this.password, 'hex'), attempt)
|
|
|
|
}
|
|
|
|
|
|
|
|
User.associate = function (models) {
|
|
|
|
User.hasMany(models.Note, {
|
|
|
|
foreignKey: 'ownerId',
|
|
|
|
constraints: false
|
|
|
|
})
|
|
|
|
User.hasMany(models.Note, {
|
|
|
|
foreignKey: 'lastchangeuserId',
|
|
|
|
constraints: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
User.getProfile = function (user) {
|
|
|
|
if (!user) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null)
|
|
|
|
}
|
|
|
|
User.parseProfile = function (profile) {
|
|
|
|
try {
|
|
|
|
profile = JSON.parse(profile)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err)
|
|
|
|
profile = null
|
|
|
|
}
|
|
|
|
if (profile) {
|
|
|
|
profile = {
|
2021-05-09 09:35:06 -04:00
|
|
|
name: filterXSS(profile.displayName || profile.username),
|
2019-04-12 00:05:32 -04:00
|
|
|
photo: User.parsePhotoByProfile(profile),
|
|
|
|
biggerphoto: User.parsePhotoByProfile(profile, true)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
User.parsePhotoByProfile = function (profile, bigger) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let photo = null
|
2019-04-12 00:05:32 -04:00
|
|
|
switch (profile.provider) {
|
|
|
|
case 'facebook':
|
|
|
|
photo = 'https://graph.facebook.com/' + profile.id + '/picture'
|
|
|
|
if (bigger) photo += '?width=400'
|
|
|
|
else photo += '?width=96'
|
|
|
|
break
|
|
|
|
case 'twitter':
|
|
|
|
photo = 'https://twitter.com/' + profile.username + '/profile_image'
|
|
|
|
if (bigger) photo += '?size=original'
|
|
|
|
else photo += '?size=bigger'
|
|
|
|
break
|
|
|
|
case 'github':
|
|
|
|
photo = 'https://avatars.githubusercontent.com/u/' + profile.id
|
|
|
|
if (bigger) photo += '?s=400'
|
|
|
|
else photo += '?s=96'
|
|
|
|
break
|
|
|
|
case 'gitlab':
|
|
|
|
photo = profile.avatarUrl
|
|
|
|
if (photo) {
|
|
|
|
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400')
|
|
|
|
else photo = photo.replace(/(\?s=)\d*$/i, '$196')
|
|
|
|
} else {
|
|
|
|
photo = generateAvatarURL(profile.username)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
break
|
|
|
|
case 'mattermost':
|
|
|
|
photo = profile.avatarUrl
|
|
|
|
if (photo) {
|
|
|
|
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400')
|
|
|
|
else photo = photo.replace(/(\?s=)\d*$/i, '$196')
|
|
|
|
} else {
|
|
|
|
photo = generateAvatarURL(profile.username)
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
break
|
|
|
|
case 'dropbox':
|
|
|
|
photo = generateAvatarURL('', profile.emails[0].value, bigger)
|
|
|
|
break
|
|
|
|
case 'google':
|
|
|
|
photo = profile.photos[0].value
|
|
|
|
if (bigger) photo = photo.replace(/(\?sz=)\d*$/i, '$1400')
|
|
|
|
else photo = photo.replace(/(\?sz=)\d*$/i, '$196')
|
|
|
|
break
|
|
|
|
case 'ldap':
|
|
|
|
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
|
|
|
break
|
|
|
|
case 'saml':
|
|
|
|
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
photo = generateAvatarURL(profile.username)
|
|
|
|
break
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2021-05-09 09:35:06 -04:00
|
|
|
return filterXSS(photo)
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
User.parseProfileByEmail = function (email) {
|
|
|
|
return {
|
|
|
|
name: email.substring(0, email.lastIndexOf('@')),
|
|
|
|
photo: generateAvatarURL('', email, false),
|
|
|
|
biggerphoto: generateAvatarURL('', email, true)
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 17:04:45 -04:00
|
|
|
|
2019-04-12 00:05:32 -04:00
|
|
|
function updatePasswordHashHook (user, options) {
|
2019-05-13 04:55:37 -04:00
|
|
|
// suggested way to hash passwords to be able to do this asynchronously:
|
|
|
|
// @see https://github.com/sequelize/sequelize/issues/1821#issuecomment-44265819
|
2019-05-13 05:51:05 -04:00
|
|
|
|
2019-04-12 00:05:32 -04:00
|
|
|
if (!user.changed('password')) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
return scrypt.kdf(user.getDataValue('password'), { logN: 15 }).then(keyBuf => {
|
2019-05-13 05:51:05 -04:00
|
|
|
user.setDataValue('password', keyBuf.toString('hex'))
|
|
|
|
})
|
2019-05-13 04:55:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
User.beforeCreate(updatePasswordHashHook)
|
|
|
|
User.beforeUpdate(updatePasswordHashHook)
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
return User
|
|
|
|
}
|