From: Chocobozzz Date: Sat, 1 Oct 2016 07:12:02 +0000 (+0200) Subject: Server: when we remove a user, remove the oauthtokens too X-Git-Tag: v0.0.1-alpha~719 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=b81929a0148c8a6a3213acb41b5bd4d1936b66b5;p=github%2FChocobozzz%2FPeerTube.git Server: when we remove a user, remove the oauthtokens too --- 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 ------------------------------