X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fuser.js;h=a19de7072cd77ae5e881297e775f25c829ab45aa;hb=8eb7d0fa0974995d3cfb8a440d72222cc18cd77d;hp=0bbd638d497bce4ad060732b5d4c5a55493af71c;hpb=9bd2662976a75d3b03364cdbe6419e57c80f99a6;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/user.js b/server/models/user.js index 0bbd638d4..a19de7072 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -1,10 +1,18 @@ const mongoose = require('mongoose') const customUsersValidators = require('../helpers/custom-validators').users +const modelUtils = require('./utils') +const peertubeCrypto = require('../helpers/peertube-crypto') + +const OAuthToken = mongoose.model('OAuthToken') // --------------------------------------------------------------------------- const UserSchema = mongoose.Schema({ + createdDate: { + type: Date, + default: Date.now + }, password: String, username: String, role: String @@ -15,35 +23,76 @@ UserSchema.path('username').required(customUsersValidators.isUserUsernameValid) UserSchema.path('role').validate(customUsersValidators.isUserRoleValid) UserSchema.methods = { - toFormatedJSON: toFormatedJSON + isPasswordMatch, + toFormatedJSON } UserSchema.statics = { - getByUsernameAndPassword: getByUsernameAndPassword, - list: list, - loadByUsername: loadByUsername + countTotal, + getByUsername, + list, + listForApi, + loadById, + loadByUsername } -mongoose.model('User', UserSchema) +UserSchema.pre('save', function (next) { + const user = this -// --------------------------------------------------------------------------- + peertubeCrypto.cryptPassword(this.password, function (err, hash) { + if (err) return next(err) -function getByUsernameAndPassword (username, password) { - return this.findOne({ username: username, password: password }) -} + user.password = hash -function list (callback) { - return this.find(callback) -} + return next() + }) +}) -function loadByUsername (username, callback) { - return this.findOne({ username: username }, callback) +UserSchema.pre('remove', function (next) { + const user = this + + OAuthToken.removeByUserId(user._id, next) +}) + +mongoose.model('User', UserSchema) + +// ------------------------------ METHODS ------------------------------ + +function isPasswordMatch (password, callback) { + return peertubeCrypto.comparePassword(password, this.password, callback) } function toFormatedJSON () { return { id: this._id, username: this.username, - role: this.role + role: this.role, + createdDate: this.createdDate } } +// ------------------------------ STATICS ------------------------------ + +function countTotal (callback) { + return this.count(callback) +} + +function getByUsername (username) { + return this.findOne({ username: username }) +} + +function list (callback) { + return this.find(callback) +} + +function listForApi (start, count, sort, callback) { + const query = {} + return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) +} + +function loadById (id, callback) { + return this.findById(id, callback) +} + +function loadByUsername (username, callback) { + return this.findOne({ username: username }, callback) +}