2009-06-18 01:14:40 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-03-21 04:22:23 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DatabaseAdmin;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
use Exception;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
/**
|
2014-08-06 06:29:52 +02:00
|
|
|
* Base class for development tools.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2014-08-06 06:29:52 +02:00
|
|
|
* Configured in framework/_config/dev.yml, with the config key registeredControllers being
|
|
|
|
* used to generate the list of links for /dev.
|
2009-06-18 01:14:40 +02:00
|
|
|
*
|
|
|
|
* @todo documentation for how to add new unit tests and tasks
|
2014-08-06 06:29:52 +02:00
|
|
|
* @todo do we need buildDefaults and generatesecuretoken? if so, register in the list
|
|
|
|
* @todo cleanup errors() it's not even an allowed action, so can go
|
|
|
|
* @todo cleanup index() html building
|
2009-06-18 01:14:40 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class DevelopmentAdmin extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $url_handlers = array(
|
|
|
|
'' => 'index',
|
|
|
|
'build/defaults' => 'buildDefaults',
|
|
|
|
'generatesecuretoken' => 'generatesecuretoken',
|
|
|
|
'$Action' => 'runRegisteredController',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'buildDefaults',
|
|
|
|
'runRegisteredController',
|
|
|
|
'generatesecuretoken',
|
|
|
|
);
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Assume that CLI equals admin permissions
|
|
|
|
* If set to false, normal permission model will apply even in CLI mode
|
|
|
|
* Applies to all development admin tasks (E.g. TaskRunner, DatabaseAdmin)
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $allow_all_cli = true;
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected function init()
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
// Special case for dev/build: Defer permission checks to DatabaseAdmin->init() (see #4957)
|
|
|
|
$requestedDevBuild = (stripos($this->getRequest()->getURL(), 'dev/build') === 0)
|
|
|
|
&& (stripos($this->getRequest()->getURL(), 'dev/build/defaults') === false);
|
|
|
|
|
|
|
|
// We allow access to this controller regardless of live-status or ADMIN permission only
|
|
|
|
// if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
|
2017-06-22 12:50:45 +02:00
|
|
|
$allowAllCLI = static::config()->get('allow_all_cli');
|
2016-11-29 00:31:16 +01:00
|
|
|
$canAccess = (
|
|
|
|
$requestedDevBuild
|
|
|
|
|| Director::isDev()
|
2017-06-22 12:50:45 +02:00
|
|
|
|| (Director::is_cli() && $allowAllCLI)
|
2016-11-29 00:31:16 +01:00
|
|
|
// Its important that we don't run this check if dev/build was requested
|
|
|
|
|| Permission::check("ADMIN")
|
|
|
|
);
|
|
|
|
if (!$canAccess) {
|
|
|
|
Security::permissionFailure($this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backwards compat: Default to "draft" stage, which is important
|
|
|
|
// for tasks like dev/build which call DataObject->requireDefaultRecords(),
|
|
|
|
// but also for other administrative tasks which have assumptions about the default stage.
|
2017-03-21 04:22:23 +01:00
|
|
|
if (class_exists(Versioned::class)) {
|
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
// Web mode
|
|
|
|
if (!Director::is_cli()) {
|
|
|
|
$renderer = DebugView::create();
|
|
|
|
echo $renderer->renderHeader();
|
|
|
|
echo $renderer->renderInfo("SilverStripe Development Tools", Director::absoluteBaseURL());
|
|
|
|
$base = Director::baseURL();
|
|
|
|
|
|
|
|
echo '<div class="options"><ul>';
|
|
|
|
$evenOdd = "odd";
|
|
|
|
foreach (self::get_links() as $action => $description) {
|
|
|
|
echo "<li class=\"$evenOdd\"><a href=\"{$base}dev/$action\"><b>/dev/$action:</b>"
|
|
|
|
. " $description</a></li>\n";
|
|
|
|
$evenOdd = ($evenOdd == "odd") ? "even" : "odd";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $renderer->renderFooter();
|
|
|
|
|
|
|
|
// CLI mode
|
|
|
|
} else {
|
|
|
|
echo "SILVERSTRIPE DEVELOPMENT TOOLS\n--------------------------\n\n";
|
|
|
|
echo "You can execute any of the following commands:\n\n";
|
|
|
|
foreach (self::get_links() as $action => $description) {
|
|
|
|
echo " sake dev/$action: $description\n";
|
|
|
|
}
|
|
|
|
echo "\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runRegisteredController(HTTPRequest $request)
|
|
|
|
{
|
|
|
|
$controllerClass = null;
|
|
|
|
|
|
|
|
$baseUrlPart = $request->param('Action');
|
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
|
|
|
if (isset($reg[$baseUrlPart])) {
|
|
|
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($controllerClass && class_exists($controllerClass)) {
|
|
|
|
return $controllerClass::create();
|
|
|
|
}
|
|
|
|
|
2018-01-16 19:39:30 +01:00
|
|
|
$msg = 'Error: no controller registered in ' . __CLASS__ . ' for: ' . $request->param('Action');
|
2016-11-29 00:31:16 +01:00
|
|
|
if (Director::is_cli()) {
|
|
|
|
// in CLI we cant use httpError because of a bug with stuff being in the output already, see DevAdminControllerTest
|
|
|
|
throw new Exception($msg);
|
|
|
|
} else {
|
2018-04-18 01:37:31 +02:00
|
|
|
$this->httpError(404, $msg);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-12-14 02:18:41 +01:00
|
|
|
* Internal methods
|
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return array of url => description
|
|
|
|
*/
|
|
|
|
protected static function get_links()
|
|
|
|
{
|
2018-04-24 12:34:37 +02:00
|
|
|
$links = [];
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
|
|
|
foreach ($reg as $registeredController) {
|
2018-04-24 12:34:37 +02:00
|
|
|
if (isset($registeredController['links'])) {
|
|
|
|
foreach ($registeredController['links'] as $url => $desc) {
|
|
|
|
$links[$url] = $desc;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $links;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected function getRegisteredController($baseUrlPart)
|
|
|
|
{
|
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if (isset($reg[$baseUrlPart])) {
|
|
|
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
|
|
|
return $controllerClass;
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/*
|
2017-12-14 02:18:41 +01:00
|
|
|
* Unregistered (hidden) actions
|
|
|
|
*/
|
2010-10-15 01:57:02 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Build the default data, calling requireDefaultRecords on all
|
|
|
|
* DataObject classes
|
|
|
|
* Should match the $url_handlers rule:
|
|
|
|
* 'build/defaults' => 'buildDefaults',
|
|
|
|
*/
|
|
|
|
public function buildDefaults()
|
|
|
|
{
|
|
|
|
$da = DatabaseAdmin::create();
|
|
|
|
|
|
|
|
$renderer = null;
|
|
|
|
if (!Director::is_cli()) {
|
|
|
|
$renderer = DebugView::create();
|
|
|
|
echo $renderer->renderHeader();
|
|
|
|
echo $renderer->renderInfo("Defaults Builder", Director::absoluteBaseURL());
|
2017-05-16 18:04:10 +02:00
|
|
|
echo "<div class=\"build\">";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$da->buildDefaults();
|
|
|
|
|
|
|
|
if (!Director::is_cli()) {
|
|
|
|
echo "</div>";
|
|
|
|
echo $renderer->renderFooter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a secure token which can be used as a crypto key.
|
|
|
|
* Returns the token and suggests PHP configuration to set it.
|
|
|
|
*/
|
|
|
|
public function generatesecuretoken()
|
|
|
|
{
|
|
|
|
$generator = Injector::inst()->create('SilverStripe\\Security\\RandomGenerator');
|
|
|
|
$token = $generator->randomToken('sha1');
|
|
|
|
$body = <<<TXT
|
2014-05-02 01:52:47 +02:00
|
|
|
Generated new token. Please add the following code to your YAML configuration:
|
2012-12-06 16:25:45 +01:00
|
|
|
|
2014-05-02 01:52:47 +02:00
|
|
|
Security:
|
|
|
|
token: $token
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-02 01:52:47 +02:00
|
|
|
TXT;
|
2016-11-29 00:31:16 +01:00
|
|
|
$response = new HTTPResponse($body);
|
|
|
|
return $response->addHeader('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function errors()
|
|
|
|
{
|
|
|
|
$this->redirect("Debug_");
|
|
|
|
}
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|