Start working on the changepassword api method

TODO: fix unit testing
This commit is contained in:
sgdc3
2017-10-04 20:12:53 +02:00
parent 816e751fe7
commit e268c3a624
5 changed files with 65 additions and 36 deletions
@@ -9,6 +9,7 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -30,7 +31,13 @@ public class AsyncChangePassword implements AsynchronousProcess {
AsyncChangePassword() {
}
/**
* Change password for an online player
*
* @param player the player
* @param oldPassword the old password used by the player
* @param newPassword the new password chosen by the player
*/
public void changePassword(final Player player, String oldPassword, String newPassword) {
final String name = player.getName().toLowerCase();
PlayerAuth auth = playerCache.getAuth(name);
@@ -50,5 +57,38 @@ public class AsyncChangePassword implements AsynchronousProcess {
commonService.send(player, MessageKey.WRONG_PASSWORD);
}
}
}
/**
* Change a user's password as an administrator, without asking for the previous one
*
* @param sender who is performing the operation, null if called by other plugins
* @param playerName the player name
* @param newPassword the new password chosen for the player
*/
public void changePasswordAsAdmin(CommandSender sender, final String playerName, String newPassword) {
final String lowerCaseName = playerName.toLowerCase();
if (!(playerCache.isAuthenticated(lowerCaseName) || dataSource.isAuthAvailable(lowerCaseName))) {
if(sender == null) {
ConsoleLogger.warning("Tried to change password for user " + lowerCaseName + " but it doesn't exist!");
} else {
commonService.send(sender, MessageKey.UNKNOWN_USER);
}
return;
}
HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, lowerCaseName);
if (dataSource.updatePassword(lowerCaseName, hashedPassword)) {
if(sender != null) {
commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(sender.getName() + " changed password of " + lowerCaseName);
} else {
ConsoleLogger.info("Changed password of " + lowerCaseName);
}
} else {
if(sender != null) {
commonService.send(sender, MessageKey.ERROR);
}
ConsoleLogger.warning("An error occurred while changing password for user " + lowerCaseName + "!");
}
}
}