khalid_hussain

Controller_Member

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