2017-05-15 15:22:03 -05:00
|
|
|
import { values } from 'lodash'
|
2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-06-16 02:45:46 -05:00
|
|
|
import { getSort } from '../utils'
|
|
|
|
import { USER_ROLES } from '../../initializers'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
cryptPassword,
|
|
|
|
comparePassword,
|
|
|
|
isUserPasswordValid,
|
|
|
|
isUserUsernameValid,
|
|
|
|
isUserDisplayNSFWValid
|
2017-06-16 02:45:46 -05:00
|
|
|
} from '../../helpers'
|
2016-08-04 15:32:36 -05:00
|
|
|
|
2017-06-16 02:45:46 -05:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 13:58:25 -05:00
|
|
|
import {
|
|
|
|
UserClass,
|
|
|
|
UserInstance,
|
|
|
|
UserAttributes,
|
|
|
|
|
|
|
|
UserMethods
|
|
|
|
} from './user-interface'
|
|
|
|
|
|
|
|
let User: Sequelize.Model<UserInstance, UserAttributes>
|
|
|
|
let isPasswordMatch: UserMethods.IsPasswordMatch
|
|
|
|
let toFormatedJSON: UserMethods.ToFormatedJSON
|
|
|
|
let isAdmin: UserMethods.IsAdmin
|
|
|
|
let countTotal: UserMethods.CountTotal
|
|
|
|
let getByUsername: UserMethods.GetByUsername
|
|
|
|
let list: UserMethods.List
|
|
|
|
let listForApi: UserMethods.ListForApi
|
|
|
|
let loadById: UserMethods.LoadById
|
|
|
|
let loadByUsername: UserMethods.LoadByUsername
|
|
|
|
let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
|
|
|
|
|
2017-06-11 10:35:32 -05:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
User = sequelize.define<UserInstance, UserAttributes>('User',
|
2016-12-11 14:50:51 -06:00
|
|
|
{
|
|
|
|
password: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
passwordValid: function (value) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const res = isUserPasswordValid(value)
|
2016-12-28 08:49:23 -06:00
|
|
|
if (res === false) throw new Error('Password not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
username: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
usernameValid: function (value) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const res = isUserUsernameValid(value)
|
2016-12-28 08:49:23 -06:00
|
|
|
if (res === false) throw new Error('Username not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
2017-02-18 02:29:59 -06:00
|
|
|
email: {
|
2017-02-18 04:56:28 -06:00
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 02:29:59 -06:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
|
|
|
},
|
2017-04-03 14:24:36 -05:00
|
|
|
displayNSFW: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: false,
|
|
|
|
validate: {
|
|
|
|
nsfwValid: function (value) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const res = isUserDisplayNSFWValid(value)
|
2017-04-03 14:24:36 -05:00
|
|
|
if (res === false) throw new Error('Display NSFW is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-12-11 14:50:51 -06:00
|
|
|
role: {
|
2017-05-15 15:22:03 -05:00
|
|
|
type: DataTypes.ENUM(values(USER_ROLES)),
|
2016-12-28 08:49:23 -06:00
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 02:33:28 -06:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 12:24:34 -06:00
|
|
|
fields: [ 'username' ],
|
|
|
|
unique: true
|
2017-02-18 02:29:59 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'email' ],
|
|
|
|
unique: true
|
2016-12-29 02:33:28 -06:00
|
|
|
}
|
|
|
|
],
|
2016-12-11 14:50:51 -06:00
|
|
|
hooks: {
|
|
|
|
beforeCreate: beforeCreateOrUpdate,
|
|
|
|
beforeUpdate: beforeCreateOrUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
getByUsername,
|
|
|
|
list,
|
|
|
|
listForApi,
|
|
|
|
loadById,
|
|
|
|
loadByUsername,
|
|
|
|
loadByUsernameOrEmail
|
|
|
|
]
|
|
|
|
const instanceMethods = [
|
|
|
|
isPasswordMatch,
|
|
|
|
toFormatedJSON,
|
|
|
|
isAdmin
|
|
|
|
]
|
|
|
|
addMethodsToModel(User, classMethods, instanceMethods)
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return User
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function beforeCreateOrUpdate (user: UserInstance) {
|
2017-05-22 13:58:25 -05:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
cryptPassword(user.password, function (err, hash) {
|
|
|
|
if (err) return reject(err)
|
2016-08-25 10:57:37 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
user.password = hash
|
2016-08-25 10:57:37 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return resolve()
|
|
|
|
})
|
2016-08-25 10:57:37 -05:00
|
|
|
})
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2016-08-25 10:57:37 -05:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-06-16 02:54:59 -05:00
|
|
|
isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) {
|
2017-05-15 15:22:03 -05:00
|
|
|
return comparePassword(password, this.password, callback)
|
2016-08-25 10:57:37 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 04:02:35 -05:00
|
|
|
toFormatedJSON = function (this: UserInstance) {
|
2016-08-25 10:57:37 -05:00
|
|
|
return {
|
2016-12-11 14:50:51 -06:00
|
|
|
id: this.id,
|
2016-08-25 10:57:37 -05:00
|
|
|
username: this.username,
|
2017-02-18 02:29:59 -06:00
|
|
|
email: this.email,
|
2017-04-03 14:24:36 -05:00
|
|
|
displayNSFW: this.displayNSFW,
|
2016-09-23 10:19:57 -05:00
|
|
|
role: this.role,
|
2016-12-11 14:50:51 -06:00
|
|
|
createdAt: this.createdAt
|
2016-08-25 10:57:37 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 14:22:10 -05:00
|
|
|
|
2017-06-16 02:54:59 -05:00
|
|
|
isAdmin = function (this: UserInstance) {
|
2017-05-15 15:22:03 -05:00
|
|
|
return this.role === USER_ROLES.ADMIN
|
2017-04-26 14:22:10 -05:00
|
|
|
}
|
|
|
|
|
2016-08-25 10:57:37 -05:00
|
|
|
// ------------------------------ STATICS ------------------------------
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
function associate (models) {
|
2017-05-22 13:58:25 -05:00
|
|
|
User.hasOne(models.Author, {
|
2016-12-29 03:33:36 -06:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
User.hasMany(models.OAuthToken, {
|
2016-12-11 14:50:51 -06:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
countTotal = function (callback: UserMethods.CountTotalCallback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.count().asCallback(callback)
|
2016-08-16 14:51:35 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
getByUsername = function (username: string) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username: username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.findOne(query)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
list = function (callback: UserMethods.ListCallback) {
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.find().asCallback(callback)
|
2016-09-26 15:36:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
listForApi = function (start: number, count: number, sort: string, callback: UserMethods.ListForApiCallback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-05-15 15:22:03 -05:00
|
|
|
order: [ getSort(sort) ]
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.findAndCountAll(query).asCallback(function (err, result) {
|
2016-12-11 14:50:51 -06:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, result.rows, result.count)
|
|
|
|
})
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
loadById = function (id: number, callback: UserMethods.LoadByIdCallback) {
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.findById(id).asCallback(callback)
|
2016-08-09 14:44:45 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
loadByUsername = function (username: string, callback: UserMethods.LoadByUsernameCallback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username: username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.findOne(query).asCallback(callback)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
2017-02-18 02:29:59 -06:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
loadByUsernameOrEmail = function (username: string, email: string, callback: UserMethods.LoadByUsernameOrEmailCallback) {
|
2017-02-18 02:29:59 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
$or: [ { username }, { email } ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
return User.findOne(query).asCallback(callback)
|
2017-02-18 02:29:59 -06:00
|
|
|
}
|