1. <?php
  2.  
  3. Route::controller('home');
  4. Route::get('/', function()
  5. {
  6.     return Redirect::to('home');
  7. });
  8.  
  9. Event::listen('404', function()
  10. {
  11.     return Response::error('404');
  12. });
  13.  
  14. Event::listen('500', function()
  15. {
  16.     return Response::error('500');
  17. });
  18.  
  19. Route::filter('before', function()
  20. {
  21.     // Do stuff before every request to your application...
  22. });
  23.  
  24. Route::filter('after', function($response)
  25. {
  26.     // Do stuff after every request to your application...
  27. });
  28.  
  29. Route::filter('csrf', function()
  30. {
  31.     if (Request::forged()) return Response::error('500');
  32. });
  33.  
  34. Route::filter('auth', function()
  35. {
  36.     if (Auth::guest()) return Redirect::to('login');
  37. });
  38.  
  39. Route::get('login', array('uses' => 'login@index'));
  40.  
  41. Route::post('login', function() {
  42.     //return "login form sent";
  43.  
  44.     //Get the person data from login
  45.     $credentials = array(
  46.         'username' => Input::get('username'),
  47.         'password' => Input::get('password')
  48.     );
  49.     if(Auth::attempt($credentials)):
  50.         //We are logged in and sending the user to home
  51.         return Redirect::to('admin');
  52.     else:
  53.         //Auth fail and we are sending user back so he can log in
  54.         return Redirect::to('login')
  55.             ->with('login_errors', true);
  56.     endif;
  57. });
  58.  
  59. use Admin\Libraries\ModelHelper;
  60. use Admin\Libraries\Fields\Field;
  61. use Admin\Libraries\Column;
  62. use Admin\Libraries\Sort;
  63.  
  64. //admin index view
  65. View::composer('admin.index', function($view)
  66. {
  67.     //get a model instance that we'll use for constructing stuff
  68.     $modelInstance = ModelHelper::getModel($view->modelName);
  69.  
  70.  
  71.     $columns = Column::getColumns($modelInstance);
  72.     $editFields = Field::getEditFields($modelInstance);
  73.  
  74.     //add the view fields
  75.     $view->modelTitle = Config::get('administrator.models.'.$view->modelName.'.title', $view->modelName);
  76.     $view->modelSingle = Config::get('administrator.models.'.$view->modelName.'.single', $view->modelTitle);
  77.     $view->columns = $columns['columns'];
  78.     $view->includedColumns = $columns['includedColumns'];
  79.     $view->primaryKey = $modelInstance::$key;
  80.     $view->sort = Sort::get($modelInstance)->toArray();
  81.     $view->rows = ModelHelper::getRows($modelInstance, $view->sort);
  82.     $view->editFields = $editFields['arrayFields'];
  83.     $view->dataModel = $editFields['dataModel'];
  84.     $view->filters = ModelHelper::getFilters($modelInstance);
  85.     $view->baseUrl = URL::to_route('admin_index');
  86.     $view->bundleHandles = Config::get('administrator.handle');
  87.     $view->expandWidth = ModelHelper::getExpandWidth($modelInstance);
  88.     $view->modelInstance = $modelInstance;
  89.     $view->model = isset($view->model) ? $view->model : false;
  90.  
  91. });
  92.  
  93. //validate_admin filter
  94. Route::filter('validate_admin', function ()
  95. {
  96.     //get the admin check closure that should be supplied in the config
  97.     $authCheck = Config::get('administrator.auth_check');
  98.  
  99.     if (!$authCheck())
  100.     {
  101.         $loginUrl = URL::to(Config::get('administrator.login_path', 'user/login'));
  102.         $redirectKey = Config::get('administrator.login_redirect_key', 'redirect');
  103.         $redirectUri = URL::to_route('admin_dashboard');
  104.  
  105.         return Redirect::to($loginUrl)->with($redirectKey, $redirectUri);
  106.     }
  107. });
  108.  
  109. //validate_model filter
  110. Route::filter('validate_model', function ()
  111. {
  112.     $modelName = URI::segment(2);
  113.     $model = ModelHelper::getModelInstance($modelName);
  114.  
  115.     //if the model doesn't exist at all, redirect to 404
  116.     if (!$model)
  117.     {
  118.         return Response::error('404');
  119.     }
  120.  
  121.     //if the model does exist, check if this user has permission to access it
  122.     if (!ModelHelper::checkPermission($modelName))
  123.     {
  124.         Redirect::to_route('admin_dashboard');
  125.     }
  126. });
  127.  
  128. /**
  129.  * Admin Routes
  130.  */
  131.  
  132. Route::get('admin', array(
  133.     'as' => 'admin_dashboard',
  134.     'uses' => 'admin@dashboard',
  135.     'before' => 'validate_admin', //only needs to validate admin and add assets
  136. ));
  137.  
  138. //The route group for all other requests needs to validate admin, model, and add assets
  139. Route::group(array('before' => 'validate_admin|validate_model'), function()
  140. {
  141.     //Model Index
  142.     Route::get('admin/(:any)', array(
  143.         'as' => 'admin_index',
  144.         'uses' => 'administrator::admin@index'
  145.     ));
  146.  
  147.     //Get Item
  148.     Route::get('admin/(:any)/(:num)', array(
  149.         'as' => 'admin_get_item',
  150.         'uses' => 'admin@item'
  151.     ));
  152.  
  153.     //New Item
  154.     Route::get('admin/(:any)/new', array(
  155.         'as' => 'admin_new_item',
  156.         'uses' => 'admin@item'
  157.     ));
  158.  
  159.     //Upload Item
  160.     Route::post('admin/(:any)/upload', array(
  161.         'as' => 'admin_upload_item',
  162.         'uses' => 'admin@upload'
  163.     ));
  164.  
  165.     //Model Images
  166.     Route::get('admin/images', array(
  167.         'as' => 'admin_images_manager',
  168.         'uses' => 'admin@images_manager'
  169.     ));
  170.  
  171.     //Search Relationship Items
  172.     Route::get('admin/(:any)/search_relation/(:any)/(:any)', array(
  173.         'as' => 'admin_search_relation',
  174.         'uses' => 'admin@search_relation'
  175.     ));
  176.  
  177.     //CSRF protection in forms
  178.     Route::group(array('before' => 'csrf'), function()
  179.     {
  180.         //Save Item
  181.         Route::post('admin/(:any)/(:num?)/(:num?)/save', array(
  182.             'as' => 'admin_save_item',
  183.             'uses' => 'admin@save'
  184.         ));
  185.  
  186.         //Delete Item
  187.         Route::post('admin/(:any)/(:num)/delete', array(
  188.             'as' => 'admin_delete_item',
  189.             'uses' => 'admin@delete'
  190.         ));
  191.  
  192.         //Get results
  193.         Route::post('admin/(:any)/results', array(
  194.             'as' => 'admin_get_results',
  195.             'uses' => 'admin@results'
  196.         ));
  197.     });
  198.  
  199. });
  200.  
  201.  Route::get('logout', function() {
  202.     Auth::logout();
  203.     return Redirect::to('login');
  204. });