change_user.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const Command = require('./command.js');
  3. const Packets = require('../packets/index.js');
  4. const ClientHandshake = require('./client_handshake.js');
  5. const CharsetToEncoding = require('../constants/charset_encodings.js');
  6. class ChangeUser extends Command {
  7. constructor(options, callback) {
  8. super();
  9. this.onResult = callback;
  10. this.user = options.user;
  11. this.password = options.password;
  12. this.database = options.database;
  13. this.passwordSha1 = options.passwordSha1;
  14. this.charsetNumber = options.charsetNumber;
  15. this.currentConfig = options.currentConfig;
  16. }
  17. start(packet, connection) {
  18. const newPacket = new Packets.ChangeUser({
  19. flags: connection.config.clientFlags,
  20. user: this.user,
  21. database: this.database,
  22. charsetNumber: this.charsetNumber,
  23. password: this.password,
  24. passwordSha1: this.passwordSha1,
  25. authPluginData1: connection._handshakePacket.authPluginData1,
  26. authPluginData2: connection._handshakePacket.authPluginData2
  27. });
  28. this.currentConfig.user = this.user;
  29. this.currentConfig.password = this.password;
  30. this.currentConfig.database = this.database;
  31. this.currentConfig.charsetNumber = this.charsetNumber;
  32. connection.clientEncoding = CharsetToEncoding[this.charsetNumber];
  33. // reset prepared statements cache as all statements become invalid after changeUser
  34. connection._statements.reset();
  35. connection.writePacket(newPacket.toPacket());
  36. return ChangeUser.prototype.handshakeResult;
  37. }
  38. }
  39. ChangeUser.prototype.handshakeResult =
  40. ClientHandshake.prototype.handshakeResult;
  41. ChangeUser.prototype.calculateNativePasswordAuthToken =
  42. ClientHandshake.prototype.calculateNativePasswordAuthToken;
  43. module.exports = ChangeUser;