<commit_before><?php
namespace App\Shell;

use App\Model\Entity\Player;
use App\Utility\AuthState;
use App\Utility\CheckConfig;
use Cake\Auth\Storage;
use Cake\Console\Shell;
use Cake\Controller\Component\AuthComponent;

class AdminShell extends Shell
{
	public function initialize()
	{
		AuthState::setAuth($this, -2);
	}

	public function user($field)
	{
		switch($field) {
		case 'id':
			return -2;
		case 'role':
			return 'Super';
		default:
			return NULL;
		}
	}

	public function checks()
	{
		$status = CheckConfig::installation();
		foreach($status as $msg => $ok) {
			if($ok) {
				$this->out($msg);
				continue;
			}
			$this->err($msg);
		}
	}

	public function auth($plin = null, $role = null)
	{
		if(isset($plin)) {
			return $this->authPlayer($plin, $role);
		}

		$this->loadModel('Players');
		$perms = $this->Players->find('list',
					[ 'valueField' => 'id'
					, 'groupField' => 'role'
					])->toArray();

		foreach(array_reverse(Player::roleValues()) as $role) {
			$count = isset($perms[$role]) ? count($perms[$role]) : 0;
			$this->out(sprintf('<warning>%s</warning> (%d)', $role, $count));
			if($count == 0 || $count > 100) {
				continue;
			}

			foreach($perms[$role] as $plin) {
				$player = $this->Players->get($plin);
				$this->out(sprintf('<info>%4d</info> %s', $plin, $player->fullName));
			}
		}
	}

	private function authPlayer($plin, $role)
	{
		$this->loadModel('Players');
		$player = $this->Players->findById($plin)->first();
		if(is_null($player) || strcmp($player->id, $plin)) {
			$this->abort(sprintf('No player found with plin `%s`.', $plin));
		}

		if(isset($role)) {
			$this->Players->patchEntity($player, ['role' => $role]);
			$this->Players->save($player);
			$errors = $player->errors('role');
			if(!empty($errors)) {
				foreach($errors as $error) {
					$this->err($error);
				}
				return 1;
			}
		}

		$this->out(sprintf('<warning>%s</warning> <info>%d</info> %s', $player->role, $player->id, $player->fullName));
	}

	public function getOptionParser()
	{
		$parser = parent::getOptionParser();
		$parser->setDescription(['Admin operations'])
			->addSubcommand('checks',
				[ 'help' => 'Run application configuration and setup checks.'
				])
			->addSubcommand('auth',
				[ 'help' => 'Show/modify user authorizations.'
				, 'parser' =>
					[ 'arguments' =>
						[ 'plin' =>
							[ 'help' => '<plin> of the player to view/modify.'
							, 'required' => false
							]
						, 'role' =>
							[ 'help' => 'The new authorization role to assign to player <plin>.'
							, 'required' => false
							, 'choice' => Player::roleValues()
				]	]	]	])
			->removeOption('verbose');

		foreach($parser->subcommands() as $sub) {
			$sub = $sub->parser();
			if(!$sub) continue;

			$sub->removeOption('verbose');
		}

		return $parser;
	}
}<commit_msg>AdminShell: add player password set/removed<commit_after><?php
namespace App\Shell;

use App\Model\Entity\Player;
use App\Utility\AuthState;
use App\Utility\CheckConfig;
use Cake\Auth\Storage;
use Cake\Console\Shell;
use Cake\Controller\Component\AuthComponent;

class AdminShell extends Shell
{
	public function initialize()
	{
		AuthState::setAuth($this, -2);
	}

	public function user($field)
	{
		switch($field) {
		case 'id':
			return -2;
		case 'role':
			return 'Super';
		default:
			return NULL;
		}
	}

	public function checks()
	{
		$status = CheckConfig::installation();
		foreach($status as $msg => $ok) {
			if($ok) {
				$this->out($msg);
				continue;
			}
			$this->err($msg);
		}
	}

	public function auth($plin = null, $role = null)
	{
		if(isset($plin)) {
			return $this->authPlayer($plin, $role);
		}

		$this->loadModel('Players');
		$perms = $this->Players->find('list',
					[ 'valueField' => 'id'
					, 'groupField' => 'role'
					])->toArray();

		foreach(array_reverse(Player::roleValues()) as $role) {
			$count = isset($perms[$role]) ? count($perms[$role]) : 0;
			$this->out(sprintf('<warning>%s</warning> (%d)', $role, $count));
			if($count == 0 || $count > 100) {
				continue;
			}

			foreach($perms[$role] as $plin) {
				$player = $this->Players->get($plin);
				$this->out(sprintf('<info>%4d</info> %s', $plin, $player->fullName));
			}
		}
	}

	private function authPlayer($plin, $role)
	{
		$this->loadModel('Players');
		$player = $this->Players->findById($plin)->first();
		if(is_null($player) || strcmp($player->id, $plin)) {
			$this->abort(sprintf('No player found with plin `%s`.', $plin));
		}

		if(isset($role)) {
			$this->Players->patchEntity($player, ['role' => $role]);
			$this->Players->save($player);
			$errors = $player->errors('role');
			if(!empty($errors)) {
				foreach($errors as $error) {
					$this->err($error);
				}
				return 1;
			}
		}

		$this->out(sprintf('<warning>%s</warning> <info>%d</info> %s', $player->role, $player->id, $player->fullName));
	}

	public function password($plin)
	{
		$this->loadModel('Players');
		$player = $this->Players->findById($plin)->first();
		if(is_null($player) || strcmp($player->id, $plin)) {
			$this->abort(sprintf('No player found with plin `%s`.', $plin));
		}

		if($this->params['remove']) {
			$player->password = <PASSWORD>;
			$msg = 'Password removed';
		} else {
			$player->set('password', $this->in('Password?'));
			$msg = 'Password set';
		}

		$this->Players->save($player);
		$errors = $player->errors('password');
		if(!empty($errors)) {
			foreach($errors as $error) {
				$this->err($error);
			}
			return 1;
		}

		$this->out(sprintf('<info>%4d</info> %s: <warning>%s</warning>', $player->id, $player->fullName, $msg));
	}

	public function getOptionParser()
	{
		$parser = parent::getOptionParser();
		$parser->setDescription(['Admin operations'])
			->addSubcommand('checks',
				[ 'help' => 'Run application configuration and setup checks.'
				])
			->addSubcommand('auth',
				[ 'help' => 'Show/modify user authorizations.'
				, 'parser' =>
					[ 'arguments' =>
						[ 'plin' =>
							[ 'help' => '<plin> of the player to view/modify.'
							, 'required' => false
							]
						, 'role' =>
							[ 'help' => 'The new authorization role to assign to player <plin>.'
							, 'required' => false
							, 'choice' => Player::roleValues()
				]	]	]	])
			->addSubcommand('password',
				[ 'help' => 'Set/remove user password.'
				, 'parser' =>
					[ 'options' =>
						[ 'remove' =>
							[ 'help' => 'Remove password instead of setting it.'
							, 'required' => false
							, 'boolean' => true
						]	]
					, 'arguments' =>
						[ 'plin' =>
							[ 'help' => '<plin> of the player to view/modify.'
							, 'required' => true
				]	]	]	])
			->removeOption('verbose');

		foreach($parser->subcommands() as $sub) {
			$sub = $sub->parser();
			if(!$sub) continue;

			$sub->removeOption('verbose');
		}

		return $parser;
	}
}
{'fraction_non_alphanumeric': 0.21364505219820615, 'fraction_numerical': 0.002940744008234083, 'mean_word_length': 3.37427652733119, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 160, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6167370', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2390, 'n_words': 458}