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(route('teacher-sign-in'))->withInput(Input::except('password'));
         }
     }
     $v = Validator::make($inputs, $rules);
     if ($v->fails()) {
         return Redirect::to(route('teacher-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(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
             Session::flash('global', 'Password field is required.');
             return Redirect::to(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
             Session::flash('global', 'Wrong password, try again.');
             return Redirect::to(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
             Session::flash('global', 'User was not found.');
             return Redirect::to(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
             Session::flash('global', 'User is not activated.');
             return Redirect::to(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
             Session::flash('global', 'User is suspended ');
             return Redirect::to(route('teacher-sign-in'));
         } catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
             Session::flash('global', 'User is banned.');
             return Redirect::to(route('teacher-sign-in'));
         }
         //    $users_login_info = UsersLoginInfo::where('user_id', '=', $user->id)->get();
         Session::flash('global', 'Loggedin Successfully');
         if ($user->school_id != null && $user->last_login != null) {
             $school_id = $user->school_id;
             $users_login_info = new UsersLoginInfo();
             $users_login_info->user_id = $user->id;
             $users_login_info->school_id = $school_id;
             if ($users_login_info->save()) {
                 Session::flash('global', 'Loggedin Successfully.');
                 return Redirect::to(route('teacher-home'));
             }
         } else {
             return Redirect::to(route('teacher-welcome-settings'));
         }
     }
 }
 public function postValidateSchool()
 {
     $registration_code = Input::get('registration_code');
     $group_id = Input::get('group_id');
     try {
         // Find the group using the group id
         $group = Sentry::findGroupById($group_id);
         // Get the group permissions
         $groupPermissions = $group->getPermissions();
     } catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) {
         echo 'Group does not exist.';
     }
     if ($group->count() > 0) {
         if ($group->id == 3) {
             $code_for_teachers = Input::get('code_for_teachers');
             $school = Schools::where('registration_code', '=', $registration_code)->where('code_for_teachers', '=', $code_for_teachers)->get()->first();
         } elseif ($group_id == 2) {
             $code_for_students = Input::get('code_for_students');
             $school = Schools::where('registration_code', '=', $registration_code)->where('code_for_students', '=', $code_for_students)->get()->first();
         } elseif ($group_id == 1) {
             $code_for_admin = Input::get('code_for_admin');
             $school = Schools::where('registration_code', '=', $registration_code)->where('code_for_admin', '=', $code_for_admin)->get()->first();
         }
         if ($school->count() > 0) {
             $user = Sentry::getUser();
             $user->school_id = $school->id;
             $user->save();
             $users_login_info = new UsersLoginInfo();
             $users_login_info->user_id = $user->id;
             $users_login_info->school_id = $school->id;
             $users_login_info->save();
             $response = array('status' => 'success', 'result' => array('school' => $school));
             return Response::json($response);
         } else {
             $response = array('status' => 'failed', 'result' => array('school' => null));
             return Response::json($response);
         }
     }
 }