KoolCoder

Admin Controllers

Jun 9th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. app/classes/controller/base:
  2.  
  3. Controller_Base_Base:
  4. <?php
  5.  
  6. class Controller_Base_Base extends \Controller_Hybrid
  7. {
  8.  
  9.     public function before()
  10.     {
  11.         parent::before();
  12.  
  13.         // Assign current_user to the instance so controllers can use it
  14.         $this->current_user = \Auth::check()
  15.             ? (\Config::get('auth.driver', 'Simpleauth') == 'Ormauth'
  16.                 ? \Model\Auth_User::find_by_username(\Auth::get_screen_name())
  17.                 : \Model_User::find_by_username(\Auth::get_screen_name()))
  18.             : null;
  19.  
  20.         // Set a global variables so views can use it
  21.         \View::set_global('current_user', $this->current_user);
  22.         \View::set_global('site_name', 'Nikita CMS');
  23.  
  24.     }
  25.  
  26. }
  27.  
  28. <?php
  29.  
  30. class Controller_Base_Admin extends \Controller_Base_Base
  31. {
  32.  
  33.     public function before()
  34.     {
  35.         parent::before();
  36.  
  37.  
  38.     }
  39.  
  40. }
  41.  
  42. <?php
  43.  
  44. class Controller_Base_Public extends \Controller_Base_Base
  45. {
  46.  
  47.     public function before()
  48.     {
  49.         parent::before();
  50.  
  51.  
  52.     }
  53.  
  54. }
  55.  
  56. Module Admin:
  57.  
  58. modules/admin/classes/controller:
  59.  
  60. <?php
  61.  
  62. namespace Admin;
  63.  
  64. class Controller_Base extends \Controller_Base_Admin
  65. {
  66.  
  67.     public function before()
  68.     {
  69.         parent::before();
  70.  
  71.     }
  72.  
  73. }
  74.  
  75.  
  76. <?php
  77.  
  78. namespace Admin;
  79.  
  80. class Controller_Admin extends Controller_Base
  81. {
  82.     // Set the template name.
  83.     public $template = 'admin/template';
  84.  
  85.     /**
  86.      * The before method.
  87.      *
  88.      * @access  public
  89.      * @return  void
  90.      */
  91.     public function before()
  92.     {
  93.         parent::before();
  94.  
  95.  
  96.         // Check to see if user is logged in
  97.         if (\Request::active()->controller !== 'Controller_Admin' or ! in_array(\Request::active()->action, array('login', 'logout')))
  98.         {
  99.  
  100.             if (\Auth::check())
  101.             {
  102.                 $admin_group_id = \Config::get('auth.driver', 'Simpleauth') == 'Ormauth' ? 6 : 100;
  103.  
  104.                 if ( ! \Auth::member($admin_group_id))
  105.                 {
  106.                     \Session::set_flash('error', e('You don\'t have access to the admin panel'));
  107.                     \Response::redirect('/');
  108.                 }
  109.             }
  110.  
  111.             // Not logged in send user to login
  112.             else
  113.             {
  114.                 //echo 'module admin before';
  115.                 //exit;
  116.  
  117.                 \Response::redirect('admin/login');
  118.             }
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * The login action.
  124.      *
  125.      * @access  public
  126.      * @return  void
  127.      */
  128.     public function action_login()
  129.     {
  130.         // Already logged in
  131.         \Auth::check() and \Response::redirect('admin');
  132.  
  133.         //echo 'action_login';
  134.         //exit;
  135.  
  136.         $val = \Validation::forge();
  137.  
  138.         if (\Input::method() == 'POST')
  139.         {
  140.             // Validation form rules
  141.             $val->add('email', 'Email or Username')
  142.                 ->add_rule('required');
  143.  
  144.             $val->add('password', 'Password')
  145.                 ->add_rule('required');
  146.  
  147.             if ($val->run())
  148.             {
  149.                 $auth = \Auth::instance();
  150.  
  151.                 //echo 'module admin before';
  152.                 //exit;
  153.  
  154.                 // check the credentials. This assumes that you have the previous table created
  155.                 if (\Auth::check() or $auth->login(\Input::post('email'), \Input::post('password')))
  156.                 {
  157.                     // credentials ok, go right in, it is a Ormauth login
  158.                     if (\Config::get('auth.driver', 'Simpleauth') == 'Ormauth')
  159.                     {
  160.                         $current_user = \Model\Auth_User::find_by_username(\Auth::get_screen_name());
  161.                     }
  162.  
  163.                     // It is a Simpleauth login.
  164.                     //else
  165.                     //{
  166.                     //  $current_user = Model\User::find_by_username(\Auth::get_screen_name());
  167.                     //}
  168.  
  169.                     // Set the flash message an the current user name
  170.                     \Session::set_flash('success', e('Welcome, '.$current_user->username));
  171.                     \Response::redirect('admin');
  172.                 }
  173.  
  174.                 // Else it is an Error!
  175.                 else
  176.                 {
  177.                     $this->template->set_global('login_error', 'Fail');
  178.                 }
  179.             }
  180.         }
  181.  
  182.         //echo 'module admin before';
  183.         //exit;
  184.  
  185.         $this->template->title     = 'Login';
  186.         $this->template->content   = \View::forge('admin/login', array('val' => $val), false);
  187.     }
  188.  
  189.     /**
  190.      * The register action.
  191.      *
  192.      * @access  public
  193.      * @return  void
  194.      */
  195.     public function action_register()
  196.     {
  197.  
  198.  
  199.     }
  200.  
  201.     /**
  202.      * The logout action.
  203.      *
  204.      * @access  public
  205.      * @return  void
  206.      */
  207.     public function action_logout()
  208.     {
  209.         \Auth::logout();
  210.  
  211.         //Response::redirect('admin');
  212.         \Response::redirect('/');
  213.     }
  214.  
  215.     /**
  216.      * The index action.
  217.      *
  218.      * @access  public
  219.      * @return  void
  220.      */
  221.     public function action_index()
  222.     {
  223.         $this->template->title     = 'Dashboard';
  224.         $this->template->content   = \View::forge('admin/dashboard');
  225.     }
  226.  
  227. }
  228.  
  229. /* End of file admin.php */
Advertisement
Add Comment
Please, Sign In to add comment