]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/user.js
Server: Remove unused console log
[github/Chocobozzz/PeerTube.git] / server / models / user.js
index 351ffef86244900e2484a1ec5b7ea3f459bc942c..a19de7072cd77ae5e881297e775f25c829ab45aa 100644 (file)
@@ -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,28 +23,72 @@ UserSchema.path('username').required(customUsersValidators.isUserUsernameValid)
 UserSchema.path('role').validate(customUsersValidators.isUserRoleValid)
 
 UserSchema.methods = {
-  toFormatedJSON: toFormatedJSON
+  isPasswordMatch,
+  toFormatedJSON
 }
 
 UserSchema.statics = {
-  getByUsernameAndPassword: getByUsernameAndPassword,
-  list: list,
-  loadById: loadById,
-  loadByUsername: loadByUsername
+  countTotal,
+  getByUsername,
+  list,
+  listForApi,
+  loadById,
+  loadByUsername
 }
 
+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()
+  })
+})
+
+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,
+    createdDate: this.createdDate
+  }
+}
+// ------------------------------ STATICS ------------------------------
 
-function getByUsernameAndPassword (username, password) {
-  return this.findOne({ username: username, password: password })
+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)
 }
@@ -44,11 +96,3 @@ function loadById (id, callback) {
 function loadByUsername (username, callback) {
   return this.findOne({ username: username }, callback)
 }
-
-function toFormatedJSON () {
-  return {
-    id: this._id,
-    username: this.username,
-    role: this.role
-  }
-}