From b81929a0148c8a6a3213acb41b5bd4d1936b66b5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 09:12:02 +0200 Subject: Server: when we remove a user, remove the oauthtokens too --- server/models/oauth-token.js | 7 ++++++- server/models/user.js | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/server/models/oauth-token.js b/server/models/oauth-token.js index f6a814c36..d53fdcf31 100644 --- a/server/models/oauth-token.js +++ b/server/models/oauth-token.js @@ -20,7 +20,8 @@ OAuthTokenSchema.path('user').required(true) OAuthTokenSchema.statics = { getByRefreshTokenAndPopulateClient: getByRefreshTokenAndPopulateClient, getByTokenAndPopulateUser: getByTokenAndPopulateUser, - getByRefreshToken: getByRefreshToken + getByRefreshToken: getByRefreshToken, + removeByUserId: removeByUserId } mongoose.model('OAuthToken', OAuthTokenSchema) @@ -53,3 +54,7 @@ function getByTokenAndPopulateUser (bearerToken) { function getByRefreshToken (refreshToken) { return this.findOne({ refreshToken: refreshToken }).exec() } + +function removeByUserId (userId, callback) { + return this.remove({ user: userId }, callback) +} diff --git a/server/models/user.js b/server/models/user.js index c2c8807f0..91e8aeae1 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -4,6 +4,8 @@ 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({ @@ -46,6 +48,12 @@ UserSchema.pre('save', function (next) { }) }) +UserSchema.pre('remove', function (next) { + const user = this + + OAuthToken.removeByUserId(user._id, next) +}) + mongoose.model('User', UserSchema) // ------------------------------ METHODS ------------------------------ -- cgit v1.2.3