khalid_hussain

Controller_Admin

Nov 4th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2. class Controller_Admin extends Controller_Base {
  3.  
  4.     private $user_group_id = 1;
  5.     private $admin_group_id = 100;
  6.     public $template = 'admin/template';
  7.  
  8.     public function before() {
  9.         parent::before();
  10.  
  11.         if (Request::active()->controller !== 'Controller_Admin' or ! in_array(Request::active()->action, array('login', 'logout'))) {
  12.             if (Auth::check()) {
  13.                 // $admin_group_id = Config::get('auth.driver', 'Simpleauth') == 'Ormauth' ? 6 : 100;              
  14.  
  15.                 if (!Auth::member($this->admin_group_id)) {
  16.                     Session::set_flash('error', e('You don\'t have access to the admin panel'));
  17.                     Response::redirect('/');
  18.                 }
  19.                 if (Auth::member($this->user_group_id)) {
  20.                     Response::redirect('main');
  21.                 }
  22.             }
  23.             else {
  24.                 if (Auth::member($this->user_group_id)) {
  25.                     Response::redirect('member/login');
  26.                 }
  27.                 elseif (Auth::member($this->admin_group_id)) {
  28.                     Reponse::redirect('admin/login');
  29.                 }
  30.             }
  31.         }
  32.     }
  33.  
  34.     public function action_login() {
  35.         // Already logged in
  36.         Auth::check() and Response::redirect('admin');
  37.  
  38.         $val = Validation::forge();
  39.  
  40.         if (Input::method() == 'POST') {
  41.             $val->add('email', 'Email or Username')
  42.                     ->add_rule('required');
  43.             $val->add('password', 'Password')
  44.                     ->add_rule('required');
  45.  
  46.             if ($val->run()) {
  47.                 $auth = Auth::instance();
  48.  
  49.                 // check the credentials. This assumes that you have the previous table created
  50.                 if (Auth::check() or $auth->login(Input::post('email'), Input::post('password'))) {
  51.                     // credentials ok, go right in
  52.                     if (Config::get('auth.driver', 'Simpleauth') == 'Ormauth') {
  53.                         $current_user = Model\Auth_User::find_by_username(Auth::get_screen_name());
  54.                     }
  55.                     else {
  56.                         $current_user = Model_User::find_by_username(Auth::get_screen_name());
  57.                     }
  58.                     Session::set_flash('success', e('Welcome, ' . $current_user->username));
  59.                     Response::redirect('admin');
  60.                 }
  61.                 else {
  62.                     $this->template->set_global('login_error', 'Fail');
  63.                 }
  64.             }
  65.         }
  66.  
  67.         $this->template->title = 'Login';
  68.         $this->template->content = View::forge('admin/login', array('val' => $val), false);
  69.     }
  70.  
  71.     /**
  72.      * The logout action.
  73.      *
  74.      * @access  public
  75.      * @return  void
  76.      */
  77.     public function action_logout() {
  78.         Auth::logout();
  79.         Response::redirect('main');
  80.     }
  81.  
  82.     /**
  83.      * The index action.
  84.      *
  85.      * @access  public
  86.      * @return  void
  87.      */
  88.     public function action_index() {
  89.         if (Auth::check()) {
  90.             if (Auth::member($this->admin_group_id)) {
  91.                 $this->template->title = 'Dashboard';
  92.                 $this->template->user_type = 'admin';
  93.                
  94.                 $this->template->content = View::forge('admin/dashboard');
  95.             }
  96.             else
  97.                 Response::redirect('member');
  98.         }
  99.         else
  100.             Response::redirect('admin/login');
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment