2016-07-01 09:03:53 -05:00
|
|
|
const mongoose = require('mongoose')
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
const customUsersValidators = require('../helpers/custom-validators').users
|
2016-08-16 15:31:45 -05:00
|
|
|
const modelUtils = require('./utils')
|
2016-08-25 10:57:37 -05:00
|
|
|
const peertubeCrypto = require('../helpers/peertube-crypto')
|
2016-08-04 15:32:36 -05:00
|
|
|
|
2016-10-01 02:12:02 -05:00
|
|
|
const OAuthToken = mongoose.model('OAuthToken')
|
|
|
|
|
2016-07-01 09:03:53 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const UserSchema = mongoose.Schema({
|
2016-08-16 15:31:45 -05:00
|
|
|
createdDate: {
|
|
|
|
type: Date,
|
|
|
|
default: Date.now
|
|
|
|
},
|
2016-07-01 09:03:53 -05:00
|
|
|
password: String,
|
2016-08-04 15:32:36 -05:00
|
|
|
username: String,
|
|
|
|
role: String
|
2016-07-01 09:03:53 -05:00
|
|
|
})
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
UserSchema.path('password').required(customUsersValidators.isUserPasswordValid)
|
|
|
|
UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
|
|
|
|
UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
|
|
|
|
|
|
|
|
UserSchema.methods = {
|
2016-08-25 10:57:37 -05:00
|
|
|
isPasswordMatch: isPasswordMatch,
|
2016-08-04 15:32:36 -05:00
|
|
|
toFormatedJSON: toFormatedJSON
|
|
|
|
}
|
2016-07-01 09:03:53 -05:00
|
|
|
|
|
|
|
UserSchema.statics = {
|
2016-08-16 15:31:45 -05:00
|
|
|
countTotal: countTotal,
|
2016-08-25 10:57:37 -05:00
|
|
|
getByUsername: getByUsername,
|
2016-09-26 15:36:36 -05:00
|
|
|
list: list,
|
2016-08-16 15:31:45 -05:00
|
|
|
listForApi: listForApi,
|
2016-08-09 14:44:45 -05:00
|
|
|
loadById: loadById,
|
2016-08-04 15:32:36 -05:00
|
|
|
loadByUsername: loadByUsername
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
2016-08-25 10:57:37 -05:00
|
|
|
UserSchema.pre('save', function (next) {
|
|
|
|
const user = this
|
|
|
|
|
|
|
|
peertubeCrypto.cryptPassword(this.password, function (err, hash) {
|
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
user.password = hash
|
|
|
|
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-01 02:12:02 -05:00
|
|
|
UserSchema.pre('remove', function (next) {
|
|
|
|
const user = this
|
|
|
|
|
|
|
|
OAuthToken.removeByUserId(user._id, next)
|
|
|
|
})
|
|
|
|
|
2016-07-01 09:03:53 -05:00
|
|
|
mongoose.model('User', UserSchema)
|
|
|
|
|
2016-08-25 10:57:37 -05:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function isPasswordMatch (password, callback) {
|
|
|
|
return peertubeCrypto.comparePassword(password, this.password, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
return {
|
|
|
|
id: this._id,
|
|
|
|
username: this.username,
|
2016-09-23 10:19:57 -05:00
|
|
|
role: this.role,
|
|
|
|
createdDate: this.createdDate
|
2016-08-25 10:57:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
2016-07-01 09:03:53 -05:00
|
|
|
|
2016-08-16 15:31:45 -05:00
|
|
|
function countTotal (callback) {
|
2016-08-16 14:51:35 -05:00
|
|
|
return this.count(callback)
|
|
|
|
}
|
|
|
|
|
2016-08-25 10:57:37 -05:00
|
|
|
function getByUsername (username) {
|
|
|
|
return this.findOne({ username: username })
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2016-09-26 15:36:36 -05:00
|
|
|
function list (callback) {
|
|
|
|
return this.find(callback)
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:31:45 -05:00
|
|
|
function listForApi (start, count, sort, callback) {
|
|
|
|
const query = {}
|
|
|
|
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
|
2016-07-01 09:03:53 -05:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:44:45 -05:00
|
|
|
function loadById (id, callback) {
|
|
|
|
return this.findById(id, callback)
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:32:36 -05:00
|
|
|
function loadByUsername (username, callback) {
|
|
|
|
return this.findOne({ username: username }, callback)
|
|
|
|
}
|