public function postSignIn()
 {
     $validator = Validator::make(Input::all(), array('email' => 'required', 'password' => 'required'));
     if ($validator->fails()) {
         return Redirect::route('user-sign-in')->withErrors($validator)->withInput();
     } else {
         $remember = Input::has('remember') ? true : false;
         $auth = Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password'), 'permissions' => 1), $remember);
         if ($auth) {
             $active = Auth::user()->active;
             if ($active == 0) {
                 Auth::logout();
                 return Redirect::route('user-sign-in')->with('global', 'Account Not Activated. Activate it.');
             } else {
                 if ($active == 1) {
                     //log into the users_login_info table
                     $prev_user_info = UsersLoginInfo::where('user_id', '=', Auth::user()->id)->get();
                     if ($prev_user_info->count() == 0) {
                         //if count is 0 then send to set initial sessions page
                         $user_info = new UsersLoginInfo();
                         $user_info->user_id = Auth::user()->id;
                         $user_info->school_id = Auth::user()->school_id;
                         // other properties according to the ip
                         $user_info->save();
                         return Redirect::intended('/user/class/set/intial');
                     } else {
                         $user_info = new UsersLoginInfo();
                         $user_info->user_id = Auth::user()->id;
                         $user_info->school_id = Auth::user()->school_id;
                         // other properties according to the ip
                         $user_info->save();
                         return Redirect::intended('/user/home');
                     }
                 }
             }
         } else {
             return Redirect::route('user-sign-in')->with('global', 'Email Address or Password Wrong');
         }
     }
     return Redirect::route('user-sign-in')->with('global', 'account not activated');
 }
 public function postSignIn()
 {
     $inputs = array('identity' => Input::get('identity'), 'password' => Input::get('password'));
     //Since user can enter username,email we cannot have email validator
     $rules = array('identity' => 'required|min:4|max:32', 'password' => 'required|min:6');
     //Find is that username or password and change identity validation rules
     //Lets use regular expressions
     if (filter_var(Input::get('identity'), FILTER_VALIDATE_EMAIL)) {
         //It is email
         $rules['identity'] = 'required|min:4|max:32|email';
     } else {
         //It is username . Check if username exist in profile table
         if (UserDetails::where('username', Input::get('identity'))->count() > 0) {
             //User exist so get email address
             $user = UserDetails::where('username', Input::get('identity'))->first();
             $inputs['identity'] = $user->email;
         } else {
             Session::flash('global', 'User does not exist');
             return Redirect::to('/user/sign/in')->withInput(Input::except('password'));
         }
     }
     $v = Validator::make($inputs, $rules);
     if ($v->fails()) {
         return Redirect::to('/user/sign/in')->withErrors($v)->withInput(Input::except('password'));
     } else {
         try {
             //Try to authenticate user
             $user = Sentry::getUserProvider()->findByLogin(Input::get('identity'));
             $throttle = Sentry::getThrottleProvider()->findByUserId($user->id);
             $throttle->check();
             //Authenticate user
             $credentials = array('email' => Input::get('identity'), 'password' => Input::get('password'));
             //For now auto activate users
             $user = Sentry::authenticate($credentials, false);
             //At this point we may get many exceptions lets handle all user management and throttle exceptions
         } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
             Session::flash('global', 'Login field is required.');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
             Session::flash('global', 'Password field is required.');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
             Session::flash('global', 'Wrong password, try again.');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
             Session::flash('global', 'User was not found.');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
             Session::flash('global', 'User is not activated.');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
             Session::flash('global', 'User is suspended ');
             return Redirect::to('/user/sign/in');
         } catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
             Session::flash('global', 'User is banned.');
             return Redirect::to('/user/sign/in');
         }
         $users_login_info = UsersLoginInfo::where('user_id', '=', $user->id)->get();
         Session::flash('global', 'Loggedin Successfully');
         if ($users_login_info->count() > 0) {
             $school_id = $user->school_id;
             $school_session = SchoolSession::where('school_id', '=', $school_id)->where('current_session', '=', 1)->get()->first();
             $user_registered_to_session = UsersToClass::where('session_id', '=', $school_session->id)->where('user_id', '=', Sentry::getUser()->id)->get();
             if ($user_registered_to_session->count() > 0) {
                 return Redirect::to(route('user-home'));
             } else {
                 Session::flash('global', 'Loggedin Successfully.<br>You Have to Register For new School Session first');
                 return Redirect::to(route('user-class-set-initial'));
             }
         } else {
             return Redirect::to(route('user-welcome-settings'));
         }
     }
 }