diff --git a/README.md b/README.md index 61fb74e..2ffb582 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Plugin for PocketMine-MP that prevents people to impersonate an account, requeri * `/login ` * `/register ` -* `/unregister ` (TODO) -* For OPs: `/simpleauth [parameters...]` (TODO) +* `/unregister ` +* For OPs: `/simpleauth [parameters ...]` ## Configuration @@ -50,6 +50,7 @@ You can modify the _SimpleAuth/config.yml_ file on the _plugins_ directory once | simpleauth.lastip | true | Allows authenticating using the lastIP when enabled in the config | | simpleauth.command.register | true | Allows registering an account | | simpleauth.command.login | true | Allows logging into an account | +| simpleauth.command.simpleauth | op | Manage accounts | ## For developers diff --git a/plugin.yml b/plugin.yml index 76a6900..1b2d22c 100644 --- a/plugin.yml +++ b/plugin.yml @@ -16,6 +16,14 @@ commands: description: "Registers an account" usage: "/register " permission: simpleauth.command.register + unregister: + description: "Unregister an account" + usage: "/unregister " + permission: simpleauth.command.register + simpleauth: + description: "Manage accounts" + usage: "/simpleauth help|unregister [parameters ...]" + permission: simpleauth.command.simpleauth permissions: simpleauth: @@ -41,3 +49,6 @@ permissions: simpleauth.command.login: description: "Allows logging into an account" default: true + simpleauth.command.simpleauth: + description: "manage simpleauth accounts" + default: op diff --git a/resources/messages.yml b/resources/messages.yml index 221cc07..008ded4 100644 --- a/resources/messages.yml +++ b/resources/messages.yml @@ -24,4 +24,14 @@ login: error: password: "Incorrect password!" registered: "This account is not registered." - block: "Too many tries!" \ No newline at end of file + block: "Too many tries!" + ingame: "This command only works in-game." + +simpleauth: + unregistered: "%s unregistered" + notregistered: "You are no longer registered!" + help: "Use unregister to unregister players" + error: + unregister: "Unable to unregister %s" + usage1: "/simpleauth unregister player" + notfound: "Player %s not found" diff --git a/src/SimpleAuth/SimpleAuth.php b/src/SimpleAuth/SimpleAuth.php index 5824cd7..eb98f73 100644 --- a/src/SimpleAuth/SimpleAuth.php +++ b/src/SimpleAuth/SimpleAuth.php @@ -217,6 +217,21 @@ public function getDataProvider(){ return $this->provider; } + /** + * @api + * + * @param IPlayer $player + * @param string $password + * + * @return bool + */ + public function checkPassword(IPlayer $player, $password){ + if(($data = $this->provider->getPlayer($player)) === null){ + return false; + } + return hash_equals($data["hash"], $this->hash(strtolower($player->getName()), $password)); + } + /* -------------------------- Non-API part -------------------------- */ public function closePlayer(Player $player){ @@ -252,7 +267,7 @@ public function onCommand(CommandSender $sender, Command $command, $label, array $password = implode(" ", $args); - if(hash_equals($data["hash"], $this->hash(strtolower($sender->getName()), $password)) and $this->authenticatePlayer($sender)){ + if($this->checkPassword($sender, $password) and $this->authenticatePlayer($sender)){ return true; }else{ $this->tryAuthenticatePlayer($sender); @@ -261,7 +276,7 @@ public function onCommand(CommandSender $sender, Command $command, $label, array return true; } }else{ - $sender->sendMessage(TextFormat::RED . "This command only works in-game."); + $sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame")); return true; } @@ -287,13 +302,78 @@ public function onCommand(CommandSender $sender, Command $command, $label, array return true; } }else{ - $sender->sendMessage(TextFormat::RED . "This command only works in-game."); + $sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame")); return true; } break; - } + case "unregister": + if($sender instanceof Player){ + if(!$this->isPlayerRegistered($sender) or ($data = $this->provider->getPlayer($sender)) === null){ + $sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.registered")); + + return true; + } + if(count($args) !== 1){ + $sender->sendMessage(TextFormat::RED . "Usage: " . $command->getUsage()); + return true; + } + $password = implode(" ", $args); + + if($this->checkPassword($sender, $password)){ + if ($this->deauthenticatePlayer($sender) and $this->unregisterPlayer($sender)){ + $sender->sendMessage(TextFormat::RED . $this->getMessage("join.register")); + }else{ + $sender->sendMessage(TextFormat::RED . $this->getMessage("register.error.general")); + } + return true; + }else{ + $sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.password")); + + return true; + } + }else{ + $sender->sendMessage(TextFormat::RED . $this->getMessage("login.error.ingame")); + + return true; + } + break; + case "simpleauth": + if(count($args) < 1){ + return false; + } + $subcmd = strtolower(array_shift($args)); + switch($subcmd){ + case "unregister": + if(count($args) < 1){ + $sender->sendMessage(TextFormat::RED . $this->getMessage("simpleauth.error.usage1")); + return true; + } + foreach($args as $name){ + $player = $this->getServer()->getOfflinePlayer($name); + if(!$this->isPlayerRegistered($player)){ + $sender->sendMessage(TextFormat::RED. sprintf($this->getMessage("simpleauth.error.notfound"), $name)); + continue; + } + if($this->unregisterPlayer($player)){ + $sender->sendMessage(TextFormat::GREEN . sprintf($this->getMessage("simpleauth.unregistered"), $name)); + if($player instanceof Player){ + $player->sendMessage(TextFormat::YELLOW.$this->getMessage("simpleauth.notregistered")); + $this->deauthenticatePlayer($player); + } + }else{ + $sender->sendMessage(TextFormat::RED . sprintf($this->getMessage("simpleauth.error.unregister"), $name)); + } + } + return true; + break; + case "help": + $sender->sendMessage(TextFormat::GREEN . $this->getMessage("simpleauth.help")); + return true; + } + break; + } return false; }