Refactored controllers

This commit is contained in:
Graham Campbell
2015-01-01 15:45:04 +00:00
parent 1e85398a4b
commit 1bc73873ce
14 changed files with 118 additions and 63 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace CachetHQ\Cachet\Controllers
use GrahamCampbell\Throttle\Facades\Throttle;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
/**
* Logs users into their account.
*/
class AuthController extends Controller
{
/**
* Shows the login view.
*
* @return \Illuminate\View\View
*/
public function showLogin()
{
return View::make('auth.login');
}
/**
* Logs the user in.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postLogin()
{
if (Auth::attempt(Input::only(['email', 'password']))) {
return Redirect::intended('dashboard');
}
Throttle::hit(Request::instance(), 10, 10);
return Redirect::back()
->withInput(Input::except('password'))
->with('error', 'Invalid email or password');
}
/**
* Logs the user out, deleting their session etc.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function logoutAction()
{
Auth::logout();
return Redirect::to('/');
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Exception;
use Component;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
class DashAPIController extends Controller
{
/**
* Updates a component with the entered info.
*
* @param \Component $component
*
* @throws \Exception
*
* @return \Component
*/
public function postUpdateComponent(Component $component)
{
if (!$component->update(Input::except(['_token']))) {
throw new Exception('Failed to update the component.');
}
return $component;
}
/**
* Updates a components ordering.
*
* @return array
*/
public function postUpdateComponentOrder()
{
$componentData = Input::all();
unset($componentData['component'][0]); // Remove random 0 index.
foreach ($componentData['component'] as $componentId => $order) {
$component = Component::find($componentId);
$component->update(['order' => $order]);
}
return $componentData;
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Component;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
class DashComponentController extends Controller
{
/**
* Shows the components view.
*
* @return \Illuminate\View\View
*/
public function showComponents()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
return View::make('dashboard.components.index')->with([
'pageTitle' => 'Components - Dashboard',
'components' => $components,
]);
}
/**
* Shows the edit component view.
*
* @param \Component $component
*
* @return \Illuminate\View\View
*/
public function showEditComponent(Component $component)
{
return View::make('dashboard.components.edit')->with([
'pageTitle' => 'Editing "'.$component->name.'" Component - Dashboard',
'component' => $component,
]);
}
/**
* Updates a component.
*
* @param \Component $component
*
* @return \Illuminate\Http\RedirectResponse
*/
public function updateComponentAction(Component $component)
{
$_component = Input::get('component');
$component->update($_component);
return Redirect::back()->with('savedComponent', $component);
}
/**
* Shows the add component view.
*
* @return \Illuminate\View\View
*/
public function showAddComponent()
{
return View::make('dashboard.components.add')->with([
'pageTitle' => 'Add Component - Dashboard',
]);
}
/**
* Creates a new component.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createComponentAction()
{
$_component = Input::get('component');
$component = Component::create($_component);
return Redirect::back()->with('component', $component);
}
/**
* Deletes a given component.
*
* @param \Component $component
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteComponentAction(Component $component)
{
$component->delete();
return Redirect::back();
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
use Incident;
use IncidentTemplate;
class DashIncidentController extends Controller
{
/**
* Shows the incidents view.
*
* @return \Illuminate\View\View
*/
public function showIncidents()
{
$incidents = Incident::orderBy('created_at', 'desc')->get();
return View::make('dashboard.incidents.index')->with([
'pageTitle' => 'Incidents - Dashboard',
'incidents' => $incidents,
]);
}
/**
* Shows the add incident view.
*
* @return \Illuminate\View\View
*/
public function showAddIncident()
{
return View::make('dashboard.incidents.add')->with([
'pageTitle' => 'Add Incident - Dashboard',
]);
}
/**
* Shows the add incident template view.
*
* @return \Illuminate\View\View
*/
public function showAddIncidentTemplate()
{
return View::make('dashboard.incidents.incident-template')->with([
'pageTitle' => 'Add Incident Template - Dashboard',
]);
}
/**
* Creates a new incident template.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createIncidentTemplateAction()
{
$_template = Input::get('template');
$template = IncidentTemplate::create($_template);
return Redirect::back()->with('template', $template);
}
/**
* Creates a new incident.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function createIncidentAction()
{
$_incident = Input::get('incident');
$incident = Incident::create($_incident);
return Redirect::back()->with('incident', $incident);
}
/**
* Deletes a given incident.
*
* @param \Incident $incident
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteIncidentAction(Incident $incident)
{
$incident->delete();
return Redirect::back();
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Exception;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
use Setting;
class DashSettingsController extends Controller
{
protected $subMenu = [];
protected $subTitle = 'Settings';
public function __construct()
{
$this->subMenu = [
'setup' => [
'title' => 'Application Setup',
'url' => '/dashboard/settings/setup',
'icon' => 'ion-gear-b',
'active' => false,
],
'security' => [
'title' => 'Security',
'url' => '/dashboard/settings/security',
'icon' => 'ion-lock-combination',
'active' => false,
],
'theme' => [
'title' => 'Theme',
'url' => '/dashboard/settings/theme',
'icon' => 'ion-paintbrush',
'active' => false,
],
'stylesheet' => [
'title' => 'Stylesheet',
'url' => '/dashboard/settings/stylesheet',
'icon' => 'ion-paintbucket',
'active' => false,
],
];
View::share('subTitle', $this->subTitle);
View::share('subMenu', $this->subMenu);
}
/**
* Shows the settings setup view.
*
* @return \Illuminate\View\View
*/
public function showSetupView()
{
$this->subMenu['setup']['active'] = true;
return View::make('dashboard.settings.app-setup')->with([
'pageTitle' => 'Application Setup - Dashboard',
'subMenu' => $this->subMenu,
]);
}
/**
* Shows the settings theme view.
*
* @return \Illuminate\View\View
*/
public function showThemeView()
{
$this->subMenu['theme']['active'] = true;
return View::make('dashboard.settings.theme')->with([
'pageTitle' => 'Theme - Dashboard',
'subMenu' => $this->subMenu,
]);
}
/**
* Shows the settings security view.
*
* @return \Illuminate\View\View
*/
public function showSecurityView()
{
$this->subMenu['security']['active'] = true;
return View::make('dashboard.settings.security')->with([
'pageTitle' => 'Security - Dashboard',
'subMenu' => $this->subMenu,
]);
}
/**
* Shows the settings stylesheet view.
*
* @return \Illuminate\View\View
*/
public function showStylesheetView()
{
$this->subMenu['stylesheet']['active'] = true;
return View::make('dashboard.settings.stylesheet')->with([
'pageTitle' => 'Stylesheet - Dashboard',
'subMenu' => $this->subMenu,
]);
}
/**
* Updates the status page settings.
*
* @return \Illuminate\View\View
*/
public function postSettings()
{
if (Input::get('remove_banner') == "1") {
$setting = Setting::where('name', 'app_banner');
$setting->delete();
}
if (Input::hasFile('app_banner')) {
$file = Input::file('app_banner');
// Image Validation.
// Image size in bytes.
$maxSize = $file->getMaxFilesize();
if ($file->getSize() > $maxSize) {
return Redirect::back()->withErrorMessage('You need to upload an image that is less than '.$maxSize.'.');
}
if (!$file->isValid() || $file->getError()) {
return Redirect::back()->withErrorMessage($file->getErrorMessage());
}
if (strpos($file->getMimeType(), 'image/') !== 0) {
return Redirect::back()->withErrorMessage('Only images may be uploaded.');
}
// Store the banner.
Setting::firstOrCreate([
'name' => 'app_banner',
])->update([
'value' => base64_encode(file_get_contents($file->getRealPath())),
]);
// Store the banner type
Setting::firstOrCreate([
'name' => 'app_banner_type',
])->update([
'value' => $file->getMimeType(),
]);
}
try {
foreach (Input::except(['app_banner', 'remove-banner']) as $settingName => $settingValue) {
$setting = Setting::firstOrCreate([
'name' => $settingName,
])->update([
'value' => $settingValue,
]);
}
} catch (Exception $e) {
return Redirect::back()->withSaved(false);
}
return Redirect::back()->withSaved(true);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
class DashUserController extends Controller
{
/**
* Shows the user view.
*
* @return \Illuminate\View\View
*/
public function showUser()
{
return View::make('dashboard.user.index')->with([
'pageTitle' => 'User - Dashboard',
]);
}
/**
* Updates the current user.
*
* @return \Illuminate\View\View
*/
public function postUser()
{
$items = Input::all();
$updated = Auth::user()->update($items);
return Redirect::back()->with('updated', $updated);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Component;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
class DashboardController extends Controller
{
/**
* Shows the dashboard view.
*
* @return \Illuminate\View\View
*/
public function showDashboard()
{
// TODO: Find steps needed to complete setup.
$components = Component::all();
return View::make('dashboard.index')->with([
'components' => $components,
]);
}
/**
* Shows the metrics view.
*
* @return \Illuminate\View\View
*/
public function showMetrics()
{
return View::make('dashboard.metrics.index')->with([
'pageTitle' => 'Metrics - Dashboard',
]);
}
/**
* Shows the notifications view.
*
* @return \Illuminate\View\View
*/
public function showNotifications()
{
return View::make('dashboard.notifications.index')->with([
'pageTitle' => 'Notifications - Dashboard',
]);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Component;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
use Setting;
class HomeController extends Controller
{
/**
* Returns the rendered Blade templates.
*
* @return \Illuminate\View\View
*/
public function showIndex()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
return View::make('index', [
'components' => $components,
'pageTitle' => Setting::get('app_name')
]);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Illuminate\Support\Facades\Response;
use Illuminate\Routing\Controller;
use Incident;
use Setting;
use Thujohn\Rss\RssFacade;
class RssController extends Controller
{
/**
* Generates an RSS feed of all incidents.
*
* @return \Illuminate\Http\Response
*/
public function feedAction()
{
$feed = RssFacade::feed('2.0', 'UTF-8');
$feed->channel([
'title' => Setting::get('app_name'),
'description' => 'Status Feed',
'link' => Setting::get('app_domain'),
]);
Incident::get()->map(function ($incident) use ($feed) {
if ($incident->component) {
$componentName = $incident->component->name;
} else {
$componentName = null;
}
$feed->item([
'title' => $incident->name,
'message' => $incident->message,
'component' => $componentName,
'status' => $incident->humanStatus,
'created_at' => $incident->created_at,
'updated_at' => $incident->updated_at,
]);
});
return Response::make($feed, 200, ['Content-Type' => 'text/xml']);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace CachetHQ\Cachet\Controllers
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
use Setting;
use User;
class SetupController extends Controller
{
/**
* Create a new setup controller instance.
*
* @return void
*/
public function __construct()
{
$this->beforeFilter('csrf', ['only' => ['postCachet']]);
}
/**
* Returns the setup page.
*
* @return \Illuminate\View\View
*/
public function getIndex()
{
return View::make('setup')->with([
'pageTitle' => 'Setup',
]);
}
/**
* Handles the actual app setup.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postIndex()
{
$postData = Input::get();
$v = Validator::make($postData, [
'settings.app_name' => 'required',
'settings.app_domain' => 'required',
'settings.show_support' => 'boolean',
'user.username' => 'alpha_dash|required',
'user.email' => 'email|required',
'user.password' => 'required'
]);
if ($v->passes()) {
// Pull the user details out.
$userDetails = array_pull($postData, 'user');
// TODO: Do we want to just use Model::unguard() here?
$user = User::create([
'username' => $userDetails['username'],
'email' => $userDetails['email'],
'password' => $userDetails['password'],
'level' => 1,
]);
Auth::login($user);
$settings = array_get($postData, 'settings');
foreach ($settings as $settingName => $settingValue) {
$setting = new Setting();
$setting->name = $settingName;
$setting->value = $settingValue;
$setting->save();
}
return Redirect::to('dashboard');
} else {
// No good, let's try that again.
return Redirect::back()->withInput()->with('errors', $v->messages());
}
}
}