Skip to content

Commit

Permalink
ZF -> Laravel: Public / IXP Graphs (#90)
Browse files Browse the repository at this point in the history
This commit converts the old double implemented IXP/Infrastructure, trunk and switch graph pages into a single implemented version in Laravel.

It also improves this by turning POSTs into GETs allowing easier sharing of specific graph links.

NB: entry point URLs have changed.

* ZF -> Laravel: Migrate public graphs

* Improve public IXP stats - all GET requests wiu=th sharable urls

* Migrate switch and separate IXP and infrastructure

* Migrate trunks and delete old files

* Update menu links

* Per graph authorisation

* Depreacted in favour of grapher authorisation
  • Loading branch information
barryo authored Nov 4, 2017
1 parent 67658aa commit 9d11d00
Show file tree
Hide file tree
Showing 30 changed files with 709 additions and 745 deletions.
166 changes: 155 additions & 11 deletions app/Http/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@

use App, D2EM;

use IXP\Services\Grapher\Graph;

use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Support\Facades\Cache;

use Entities\{
Customer as CustomerEntity,
Infrastructure as InfrastructureEntity,
IXP as IXPEntity,
Switcher as SwitchEntity,
VirtualInterface as VIEntity,
Vlan as VlanEntity,
VlanInterface as VlanInterfaceEntity
};


use Illuminate\View\View;
use Illuminate\Support\Facades\Cache;

use IXP\Http\Requests\StatisticsRequest;
use IXP\Services\Grapher\Graph;
use IXP\Utils\View\Alert\Alert;
use IXP\Utils\View\Alert\Container as AlertContainer;


/**
* Statistics Controller
* @author Barry O'Donovan <[email protected]>
Expand All @@ -53,21 +59,161 @@ class StatisticsController extends Controller
*
* These are safe for use from the request.
*
* @param \Illuminate\Http\Request $r
* @param StatisticsRequest $r
*/
private function processGraphParams( Request $r ) {
private function processGraphParams( StatisticsRequest $r ) {
$r->period = Graph::processParameterPeriod( $r->input( 'period', '' ) );
$r->category = Graph::processParameterCategory( $r->input( 'category', '' ) );
$r->protocol = Graph::processParameterProtocol( $r->input( 'protocol', '' ) );
$r->type = Graph::processParameterType( $r->input( 'type', '' ) );
}


/**
* Show overall IXP graphs
*
* @param StatisticsRequest $r
* @param string $category Category of graph to show (e.g. bits / pkts)
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function ixp( StatisticsRequest $r, string $category = Graph::CATEGORY_BITS )
{
$ixp = D2EM::getRepository( IXPEntity::class )->getDefault();
$grapher = App::make('IXP\Services\Grapher');
$category = Graph::processParameterCategory( $category, true );

$graph = $grapher->ixp( $ixp )->setType( Graph::TYPE_PNG )->setProtocol( Graph::PROTOCOL_ALL )->setCategory( $category );
$graph->authorise();

return view( 'statistics/ixp' )->with([
'graph' => $graph,
'category' => $category,
]);
}

/**
* Show IXP infrastructure graphs
*
* @param StatisticsRequest $r
* @param int $infraid ID of the infrastructure to show the graph of
* @param string $category Category of graph to show (e.g. bits / pkts)
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function infrastructure( StatisticsRequest $r, int $infraid = 0, string $category = Graph::CATEGORY_BITS )
{
/** @var InfrastructureEntity[] $eInfras */
$eInfras = D2EM::getRepository( InfrastructureEntity::class )->findBy( [], [ 'name' => 'ASC' ] );
$grapher = App::make('IXP\Services\Grapher');
$category = Graph::processParameterCategory( $category, true );

$infras = [];
foreach( $eInfras as $i ) {
$infras[ $i->getId() ] = $i->getName();
}

$infraid = isset( $infras[ $infraid ] ) ? $infraid : array_keys( $infras )[0];
$infra = D2EM::getRepository( InfrastructureEntity::class )->find( $infraid );
$graph = $grapher->infrastructure( $infra )->setType( Graph::TYPE_PNG )->setProtocol( Graph::PROTOCOL_ALL )->setCategory( $category );

$graph->authorise();

return view( 'statistics/infrastructure' )->with([
'infras' => $infras,
'infraid' => $infraid,
'infra' => $infra,
'graph' => $graph,
'category' => $category,
]);
}

/**
* Show IXP switch graphs
*
* @param StatisticsRequest $r
* @param int $switchid ID of the switch to show the graph of
* @param string $category Category of graph to show (e.g. bits / pkts)
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function switch( StatisticsRequest $r, int $switchid = 0, string $category = Graph::CATEGORY_BITS )
{
/** @var SwitchEntity[] $eSwitches */
$eSwitches = D2EM::getRepository( SwitchEntity::class )->getAndCache( true, SwitchEntity::TYPE_SWITCH );
$grapher = App::make('IXP\Services\Grapher');
$category = Graph::processParameterCategory( $category, true );

$switches = [];
foreach( $eSwitches as $s ) {
$switches[ $s->getId() ] = $s->getName();
}

$switchid = isset( $switches[ $switchid ] ) ? $switchid : array_keys( $switches )[0];
$switch = D2EM::getRepository( SwitchEntity::class )->find( $switchid );
$graph = $grapher->switch( $switch )->setType( Graph::TYPE_PNG )->setProtocol( Graph::PROTOCOL_ALL )->setCategory( $category );

$graph->authorise();

return view( 'statistics/switch' )->with([
'switches' => $switches,
'switchid' => $switchid,
'switch' => $switch,
'graph' => $graph,
'category' => $category,
]);
}


/**
* Show IXP trunk graphs
*
* @param StatisticsRequest $r
* @param string $trunkid ID of the trunk to show the graph of
* @param string $category Category of graph to show (e.g. bits / pkts)
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function trunk( StatisticsRequest $r, string $trunkid = null, string $category = Graph::CATEGORY_BITS )
{
if( !is_array( config('grapher.backends.mrtg.trunks') ) || !count( config('grapher.backends.mrtg.trunks') ) ) {
AlertContainer::push(
"Trunk graphs have not been configured. Please see <a href=\"http://docs.ixpmanager.org/features/grapher/\">this documentation</a> for instructions.",
Alert::DANGER
);
return redirect('');
}

$grapher = App::make('IXP\Services\Grapher');

// get the available graphs
$images = [];
$graphs = [];
foreach( config('grapher.backends.mrtg.trunks') as $g ) {
$images[] = $g['name'];
$graphs[$g['name']] = $g['title'];
}

if( !in_array( $trunkid, $images ) ) {
$trunkid = $images[ 0 ];
}

$graph = $grapher->trunk( $trunkid )->setType( Graph::TYPE_PNG )->setProtocol( Graph::PROTOCOL_ALL )->setCategory( Graph::CATEGORY_BITS );
$graph->authorise();

return view( 'statistics/trunk' )->with([
'graphs' => $graphs,
'trunkid' => $trunkid,
'graph' => $graph,
'category' => $category,
]);
}



/**
* Display all member graphs
*
* @param StatisticsRequest $r
* @return View
*/
public function members( Request $r ) : View {
public function members( StatisticsRequest $r ) : View {

$grapher = App::make('IXP\Services\Grapher');
$this->processGraphParams($r);
Expand All @@ -87,8 +233,6 @@ public function members( Request $r ) : View {
$r->protocol = Graph::PROTOCOL_ALL;
}



$graphs = [];
foreach( $targets as $t ) {
if( $infra ) {
Expand Down
53 changes: 53 additions & 0 deletions app/Http/Requests/StatisticsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace IXP\Http\Requests;

/*
* Copyright (C) 2009-2017 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/

use Auth;

use Illuminate\Foundation\Http\FormRequest;


class StatisticsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// authorisation done on a per action basis using Grapher::Graph->authorise() in the controller
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [];
}
}
4 changes: 2 additions & 2 deletions app/Services/Grapher/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ public static function processParameterProtocol( string $v ): string {
* @param string $v The user input value
* @return string The verified / sanitised / default value
*/
public static function processParameterCategory( string $v ): string {
if( !isset( self::CATEGORIES[ $v ] ) ) {
public static function processParameterCategory( string $v, $bits_pkts_only = false ): string {
if( ( $bits_pkts_only && !isset( self::CATEGORIES_BITS_PKTS[$v] ) ) || ( !$bits_pkts_only && !isset( self::CATEGORIES[ $v ] ) ) ) {
$v = self::CATEGORY_DEFAULT;
}
return $v;
Expand Down
11 changes: 9 additions & 2 deletions app/Services/Grapher/Graph/IXP.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

use IXP\Exceptions\Services\Grapher\{BadBackendException,CannotHandleRequestException,ConfigurationException,ParameterException};

use Entities\IXP as IXPEntity;
use Entities\IXP as IXPEntity;
use Entities\User as UserEntity;

use Auth;

Expand Down Expand Up @@ -139,7 +140,13 @@ public function authorise(): bool {
return $this->deny();
}

return $this->allow();
if( config( 'grapher.access.ixp', -1 ) == UserEntity::AUTH_PUBLIC ) {
return $this->allow();
} else if( Auth::check() && Auth::user()->getPrivs() >= config( 'grapher.access.ixp', 0 ) ) {
return $this->allow();
}

return $this->deny();
}

/**
Expand Down
9 changes: 8 additions & 1 deletion app/Services/Grapher/Graph/Infrastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use IXP\Exceptions\Services\Grapher\{BadBackendException,CannotHandleRequestException,ConfigurationException,ParameterException};

use Entities\Infrastructure as InfrastructureEntity;
use Entities\User as UserEntity;

use Auth;

Expand Down Expand Up @@ -115,7 +116,13 @@ public function authorise(): bool {
return $this->deny();
}

return $this->allow();
if( config( 'grapher.access.infrastructure', -1 ) == UserEntity::AUTH_PUBLIC ) {
return $this->allow();
} else if( Auth::check() && Auth::user()->getPrivs() >= config( 'grapher.access.infrastructure', 0 ) ) {
return $this->allow();
}

return $this->deny();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions app/Services/Grapher/Graph/Switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use IXP\Exceptions\Services\Grapher\{BadBackendException,CannotHandleRequestException,ConfigurationException,ParameterException};

use Entities\Switcher as SwitchEntity;
use Entities\User as UserEntity;

use Auth;

Expand Down Expand Up @@ -111,8 +112,13 @@ public function authorise(): bool {
return $this->allow();
}

// public access to switch graphs
return $this->allow();
if( config( 'grapher.access.switch', -1 ) == UserEntity::AUTH_PUBLIC ) {
return $this->allow();
} else if( Auth::check() && Auth::user()->getPrivs() >= config( 'grapher.access.switch', 0 ) ) {
return $this->allow();
}

return $this->deny();
}

/**
Expand Down
15 changes: 12 additions & 3 deletions app/Services/Grapher/Graph/Trunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use IXP\Exceptions\Services\Grapher\{BadBackendException,CannotHandleRequestException,ConfigurationException,ParameterException};

use Entities\Switcher as SwitchEntity;
use Entities\User as UserEntity;

use Auth;

Expand Down Expand Up @@ -108,8 +108,17 @@ public function identifier(): string {
* @return bool
*/
public function authorise(): bool {
// public access to trunk graphs
return $this->allow();
if( Auth::check() && Auth::user()->isSuperUser() ) {
return $this->allow();
}

if( config( 'grapher.access.trunk', -1 ) == UserEntity::AUTH_PUBLIC ) {
return $this->allow();
} else if( Auth::check() && Auth::user()->getPrivs() >= config( 'grapher.access.trunk', 0 ) ) {
return $this->allow();
}

return $this->deny();
}

/**
Expand Down
Loading

0 comments on commit 9d11d00

Please sign in to comment.