Skip to content

Commit

Permalink
Merge develop to master for 4.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
rowasc committed Sep 25, 2019
2 parents 09bc482 + 6451f3a commit 6a0889d
Show file tree
Hide file tree
Showing 24 changed files with 1,903 additions and 580 deletions.
38 changes: 38 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,44 @@
"contributions": [
"code"
]
},
{
"login": "Obadha2",
"name": "Obadha2",
"avatar_url": "https://avatars1.githubusercontent.com/u/38259840?v=4",
"profile": "https://www.linkedin.com/in/walterobadha/",
"contributions": [
"bug"
]
},
{
"login": "justinscherer",
"name": "Justin Scherer",
"avatar_url": "https://avatars1.githubusercontent.com/u/29209303?v=4",
"profile": "https://www.justinscherer.xyz",
"contributions": [
"design",
"ideas",
"userTesting"
]
},
{
"login": "CeciliaHinga",
"name": "CeciliaHinga",
"avatar_url": "https://avatars1.githubusercontent.com/u/20906968?v=4",
"profile": "https://github.com/CeciliaHinga",
"contributions": [
"blog"
]
},
{
"login": "trendspotter",
"name": "trendspotter",
"avatar_url": "https://avatars1.githubusercontent.com/u/15286128?v=4",
"profile": "https://github.com/trendspotter",
"contributions": [
"bug"
]
}
],
"projectName": "platform",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Puppetfile.lock
phpunit.xml
phpspec.yml
phinx.yml
bootstrap/gwcheck.enabled

# Homestead / Laravel files
Homestead.json
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ushahidi/php-fpm-nginx:php-7.0
FROM ushahidi/php-fpm-nginx:php-7.1

WORKDIR /var/www
COPY composer.json ./
Expand Down
72 changes: 72 additions & 0 deletions app/Console/Commands/EnvironmentVerify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Ushahidi\App\Console\Commands;

use Illuminate\Console\Command;

use Ushahidi\App\Tools\OutputText;
use Composer\Script\Event;
use Composer\Installer\PackageEvent;

class EnvironmentVerify extends Command
{

/**
* The console command name.
*
* @var string
*/
protected $name = 'environment:verify';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Verify the environment setup.';
protected $signature = 'environment:verify';

private static $NO_ENV = "No environment file found. Please copy the .env.example file to create a new .env file.";
private static $REQUIRED_ENV_KEYS = [
"DB_CONNECTION" => "Please set `DB_CONNECTION=mysql` in the .env file.",
"DB_HOST" => "Please set the address of your database in the DB_HOST key",
"DB_PORT" => "Please set the port of your database in the DB_PORT key",
"DB_DATABASE" => "Please set the name of your database in the DB_DATABASE key",
"DB_USERNAME" => "Please set the username to connect to your database in the DB_USERNAME key",
"DB_PASSWORD" => "Please set the password to connect to your database in the DB_PASSWORD key",
"CACHE_DRIVER" => "Please set the CACHE_DRIVER according to your environment." .
"See https://laravel.com/docs/5.8/cache#driver-prerequisites for more information on cache drivers.",
"QUEUE_DRIVER" => "Please set the QUEUE_DRIVER according to your environment." .
"See https://laravel.com/docs/5.8/queues for more information on queue drivers.",
];

public static function verifyRequirements($console = true)
{
$env = new \Ushahidi\App\PlatformVerifier\Env();
return $env->verifyRequirements(true);
}

public function verifyDB()
{
$db = new \Ushahidi\App\PlatformVerifier\Database();
return $db->verifyRequirements(true);
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
echo OutputText::info("Running ENV configuration checks");

$env = $this->verifyRequirements(true);

echo OutputText::info("Running DB connectivity verification");

$db = $this->verifyDB(true);
if (isset($db['errors'])||isset($env['errors'])) {
throw new \Exception("Verification Failed.");
}
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Kernel extends ConsoleKernel
Commands\MigrateStatusCommand::class,
Commands\SeedMakeCommand::class,
Commands\SeedCommand::class,
Commands\EnvironmentVerify::class,
\Ushahidi\Console\Command\ApikeySet::class,
\Ushahidi\Console\Command\ConfigSet::class,
\Ushahidi\Console\Command\ConfigGet::class,
Expand All @@ -34,6 +35,7 @@ class Kernel extends ConsoleKernel
\Ushahidi\Console\Command\Webhook::class,
\Ushahidi\Console\Command\ObfuscateData::class,
Commands\TestMultisiteJob::class,

];

/**
Expand Down
66 changes: 66 additions & 0 deletions app/Http/Controllers/API/VerifyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* Ushahidi REST Base Controller
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application\Controllers
* @copyright 2013 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

namespace Ushahidi\App\Http\Controllers\API;

use Ushahidi\App\Http\Controllers\RESTController;

use Ushahidi\Factory\UsecaseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use League\OAuth2\Server\Exception\OAuth2Exception;
use League\OAuth2\Server\Exception\MissingAccessTokenException;
use Ushahidi\App\Exceptions\ValidationException;
use Ushahidi\App\Multisite\MultisiteManager;

class VerifyController extends RESTController
{

protected function getResource()
{
return 'verifier';
}

/**
* @var array List of HTTP methods which may be cached
*/
protected $cacheableMethods = [];

/**
* Get current api version
*/
public static function version()
{
return self::$version;
}

public function db(\Illuminate\Http\Request $request)
{
if (!\Ushahidi\App\PlatformVerifier\DebugMode::isEnabled()) {
return (new Response(null, 204))
->header('X-Ushahidi-Platform-Install-Debug-Mode', 'off');
}

$output = new \Ushahidi\App\PlatformVerifier\Database();
return $output->verifyRequirements(false);
}

public function conf(\Illuminate\Http\Request $request)
{
if (!\Ushahidi\App\PlatformVerifier\DebugMode::isEnabled()) {
return (new Response(null, 204))
->header('X-Ushahidi-Platform-Install-Debug-Mode', 'off');
}

$output = new \Ushahidi\App\PlatformVerifier\Env();
return $output->verifyRequirements(false);
}
}
45 changes: 45 additions & 0 deletions app/PlatformVerifier/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Ushahidi\App\PlatformVerifier;

use Ushahidi\App\Tools\OutputText;
use Composer\Script\Event;
use Composer\Installer\PackageEvent;
use Illuminate\Support\Facades\DB as DB;

use Ushahidi\App\Facades\Features;

class Database
{
private static $errors = [
// SQLSTATE[HY000] [2002] Connection refused
'2002' => 'Check that your MySQL server is installed and running, ' .
'and that the right DB_HOST and DB_PORT are set up in the .env file',
'1049' => 'Check that the database in the .env file variable DB_DATABASE exists' .
'and that the database user in DB_USERNAME has permissions to access it.',
'1045' => 'Check that DB_USERNAME, DB_PASSWORD and DB_USERNAME are correct in the .env file. ' .
'Verify that accessing mysql through a CLI with `mysql -u YOUR_DB_USERNAME -p ' .
'YOUR_DB_NAME and entering the password on prompt results in a successful connection to mysql.'
];

public function verifyRequirements(bool $console = true, \Illuminate\Database\MySqlConnection $connection = null)
{
/*
* Enable calling this with a mocked connection from unit tests, or using the regular class.
* We can't inject it always because not everything that calls this has access to Illuminate\Support\Facades\DB
*/
if (!$connection) {
$connectTo = getenv('MULTISITE_DOMAIN') ? 'multisite' : 'mysql';
$connection = \Illuminate\Support\Facades\DB::connection($connectTo);
}
try {
$connection = $connection->getPdo();
return Respond::successResponse('We were able to connect to the DB. Well done!', '', $console);
} catch (\Exception $e) {
$code = $e->getCode();
$explainer = isset(self::$errors[$code]) ? self::$errors[$code] : '';
$errors = [['message' => $e->getMessage(), 'explainer' => $explainer]];
return Respond::errorResponse($errors, $console);
}
}
}
62 changes: 62 additions & 0 deletions app/PlatformVerifier/DebugMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Ushahidi\App\PlatformVerifier;

use Ushahidi\App\Tools\OutputText;
use Composer\Script\Event;
use Composer\Installer\PackageEvent;

# Methods to check whether the installation debug mode is enabled , as well
# to create and delete the file that enables it
#
# This would be a nice as just commands in the composer.json file,
# but that wouldn't be portable across platforms
class DebugMode
{
private static $SWITCH_FILE = "bootstrap/install_debug_mode.enabled";
private static $SWITCH_FILE_PATH = __DIR__ . "/../../bootstrap/install_debug_mode.enabled";

private static function getLastErrorMessage()
{
$error = error_get_last();
if ($error === null) {
return '[no PHP error]';
} else {
return $error['message'];
}
}

public static function isEnabled()
{
return file_exists(self::$SWITCH_FILE_PATH) ||
($_ENV['USH_PLATFORM_INSTALL_DEBUG_MODE_ENABLED'] ?? null);
}

public static function enable()
{
if (!file_exists(self::$SWITCH_FILE_PATH)) {
// create the file
if (touch(self::$SWITCH_FILE_PATH)) {
echo OutputText::success(self::$SWITCH_FILE . " created");
} else {
echo OutputText::error("Creating " . self::$SWITCH_FILE . ": " . self::getLastErrorMessage());
}
} else {
echo OutputText::info("The file " . self::$SWITCH_FILE . " already exists: no action taken.");
}
}

public static function disable()
{
if (file_exists(self::$SWITCH_FILE_PATH)) {
// delete the file
if (unlink(self::$SWITCH_FILE_PATH)) {
echo OutputText::success(self::$SWITCH_FILE . " deleted");
} else {
echo OutputText::error("Deleting " . self::$SWITCH_FILE . ": " . self::getLastErrorMessage());
}
} else {
echo OutputText::info("The file " . self::$SWITCH_FILE . " didn't exist: no action taken.");
}
}
}
65 changes: 65 additions & 0 deletions app/PlatformVerifier/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Ushahidi\App\PlatformVerifier;

use Ushahidi\App\Tools\OutputText;
use Composer\Script\Event;
use Composer\Installer\PackageEvent;

class Env
{
private static $NO_ENV = "Required environment variables missing and no environment file found.";
private static $NO_ENV_EXPLAINER = "Please copy the '.env.example' file into a file named '.env' and set your missing variables.";
private static $REQUIRED_ENV_KEYS = [
"DB_CONNECTION" => "Please set `DB_CONNECTION=mysql` in the environment or .env file.",
"DB_HOST" => "Please set the address of your database in the DB_HOST key",
"DB_PORT" => "Please set the port of your database in the DB_PORT key",
"DB_DATABASE" => "Please set the name of your database in the DB_DATABASE key",
"DB_USERNAME" => "Please set the username to connect to your database in the DB_USERNAME key",
"DB_PASSWORD" => "Please set the password to connect to your database in the DB_PASSWORD key",
"CACHE_DRIVER" => "Please set the CACHE_DRIVER according to your environment." .
"See https://laravel.com/docs/5.8/cache#driver-prerequisites for more information on cache drivers.",
"QUEUE_DRIVER" => "Please set the QUEUE_DRIVER according to your environment." .
"See https://laravel.com/docs/5.8/queues for more information on queue drivers.",
];

public function envExists()
{
return file_exists(__DIR__ . "/../../.env");
}

public function isMissingEnvKey($key)
{
return !getenv($key);
}
public function verifyRequirements($console = true)
{
$ok = "Good job! you have configured your system environment and/or .env file with all the required keys.";
$info = "We will check the database connectivity next.";
$errors = [];
$success = [];

if ($this->envExists()) {
// load DotEnv for this script
(new \Dotenv\Dotenv(__DIR__."/../../"))->load();
}

$failures = false;
foreach (self::$REQUIRED_ENV_KEYS as $key => $value) {
if ($this->isMissingEnvKey($key)) {
$failures = true;
$message = [
"message" => "$key is missing in the environment or .env file.",
"explainer" => $value
];
array_push($errors, $message);
}
}
// If there have been errors and the .env file is missing, point out that creating it
// is a convenient way of solving those errors
if (!empty($errors) && !$this->envExists()) {
array_push($errors, ["message" => self::$NO_ENV, "explainer" => self::$NO_ENV_EXPLAINER], $console);
}
return $failures ? Respond::errorResponse($errors, $console) : Respond::successResponse($ok, $info, $console);
}
}
Loading

0 comments on commit 6a0889d

Please sign in to comment.