2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2016-04-20 06:03:55 -04:00
|
|
|
// external modules
|
2017-03-08 05:45:51 -05:00
|
|
|
var Sequelize = require('sequelize')
|
2018-11-19 19:26:10 -05:00
|
|
|
var scrypt = require('@mlink/scrypt')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// core
|
2017-04-12 12:20:28 -04:00
|
|
|
var logger = require('../logger')
|
2018-04-12 07:14:42 -04:00
|
|
|
var {generateAvatarURL} = require('../letter-avatars')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
module.exports = function (sequelize, DataTypes) {
|
2017-03-08 05:45:51 -05:00
|
|
|
var User = sequelize.define('User', {
|
|
|
|
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: {
|
|
|
|
type: Sequelize.TEXT,
|
|
|
|
set: function (value) {
|
|
|
|
var hash = scrypt.kdfSync(value, scrypt.paramsSync(0.1)).toString('hex')
|
|
|
|
this.setDataValue('password', hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
instanceMethods: {
|
|
|
|
verifyPassword: function (attempt) {
|
2018-11-14 08:10:14 -05:00
|
|
|
if (scrypt.verifyKdfSync(Buffer.from(this.password, 'hex'), attempt)) {
|
2017-03-08 05:45:51 -05:00
|
|
|
return this
|
|
|
|
} else {
|
|
|
|
return false
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
classMethods: {
|
|
|
|
associate: function (models) {
|
|
|
|
User.hasMany(models.Note, {
|
|
|
|
foreignKey: 'ownerId',
|
|
|
|
constraints: false
|
|
|
|
})
|
|
|
|
User.hasMany(models.Note, {
|
|
|
|
foreignKey: 'lastchangeuserId',
|
|
|
|
constraints: false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
getProfile: function (user) {
|
2018-05-25 10:15:10 -04:00
|
|
|
if (!user) {
|
|
|
|
return null
|
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
return user.profile ? User.parseProfile(user.profile) : (user.email ? User.parseProfileByEmail(user.email) : null)
|
|
|
|
},
|
|
|
|
parseProfile: function (profile) {
|
|
|
|
try {
|
|
|
|
profile = JSON.parse(profile)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err)
|
|
|
|
profile = null
|
|
|
|
}
|
|
|
|
if (profile) {
|
|
|
|
profile = {
|
|
|
|
name: profile.displayName || profile.username,
|
|
|
|
photo: User.parsePhotoByProfile(profile),
|
|
|
|
biggerphoto: User.parsePhotoByProfile(profile, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return profile
|
|
|
|
},
|
|
|
|
parsePhotoByProfile: function (profile, bigger) {
|
|
|
|
var photo = null
|
|
|
|
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
|
2017-09-22 06:40:43 -04:00
|
|
|
if (photo) {
|
|
|
|
if (bigger) photo = photo.replace(/(\?s=)\d*$/i, '$1400')
|
|
|
|
else photo = photo.replace(/(\?s=)\d*$/i, '$196')
|
|
|
|
} else {
|
2018-04-12 07:14:42 -04:00
|
|
|
photo = generateAvatarURL(profile.username)
|
2017-09-22 06:40:43 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
2017-10-29 06:16:40 -04:00
|
|
|
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 {
|
2018-04-12 07:14:42 -04:00
|
|
|
photo = generateAvatarURL(profile.username)
|
2017-10-29 06:16:40 -04:00
|
|
|
}
|
|
|
|
break
|
2017-03-08 05:45:51 -05:00
|
|
|
case 'dropbox':
|
2018-06-23 17:40:46 -04:00
|
|
|
photo = generateAvatarURL('', profile.emails[0].value, bigger)
|
2017-03-08 05:45:51 -05:00
|
|
|
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':
|
2018-06-23 17:40:46 -04:00
|
|
|
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
2017-11-27 22:46:58 -05:00
|
|
|
case 'saml':
|
2018-06-23 17:40:46 -04:00
|
|
|
photo = generateAvatarURL(profile.username, profile.emails[0], bigger)
|
2017-11-27 22:46:58 -05:00
|
|
|
break
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
return photo
|
|
|
|
},
|
|
|
|
parseProfileByEmail: function (email) {
|
|
|
|
return {
|
|
|
|
name: email.substring(0, email.lastIndexOf('@')),
|
2018-06-23 17:40:46 -04:00
|
|
|
photo: generateAvatarURL('', email, false),
|
|
|
|
biggerphoto: generateAvatarURL('', email, true)
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-05-11 17:04:45 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
return User
|
|
|
|
}
|