<?php

use Vn\Web;

class Supplant extends Vn\Web\JsonRequest {
	const PARAMS = ['supplantUser'];

	function run($db) {
		$userId = $db->getValue(
			'SELECT id FROM account.user WHERE `name` = #',
			[$_REQUEST['supplantUser']]
		);

		$isClient = $db->getValue(
			'SELECT COUNT(*) > 0 FROM vn.client WHERE id = #',
			[$userId]
		);
		if (!$isClient)
			throw new Web\ForbiddenException(s('The user is not a client'));

		$isWorker = $db->getValue(
			'SELECT COUNT(*) > 0 FROM vn.worker WHERE id = #',
			[$userId]
		);
		if ($isWorker)
			throw new Web\ForbiddenException(s('Workers cannot be supplanted'));

		return $this->service->createToken($_REQUEST['supplantUser']);
	}
}