public function postLogin()
 {
     // get input parameters
     //
     $username = Input::get('username');
     $password = Input::get('password');
     // validate user
     //
     $user = User::getByUsername($username);
     if ($user) {
         if (User::isValidPassword($password, $user->password)) {
             if ($user->isEnabled()) {
                 $res = Response::json(array('user_uid' => $user->user_uid));
                 Session::set('timestamp', time());
                 Session::set('user_uid', $user->user_uid);
                 return $res;
             } else {
                 return Response::make('User has not been approved.', 401);
             }
         } else {
             return Response::make('Incorrect username or password.', 401);
         }
     } else {
         return Response::make('Incorrect username or password.', 401);
     }
 }
 public function changePassword($userUid)
 {
     $currentUser = User::getIndex(Session::get('user_uid'));
     // current user is an admin
     //
     if ($currentUser->isAdmin()) {
         $newPassword = Input::get('new_password');
         // change password
         //
         $user = User::getIndex($userUid);
         $user->modifyPassword($newPassword);
         $cfg = array('url' => Config::get('app.cors_url') ?: '', 'user' => $user);
         Mail::send('emails.password-changed', $cfg, function ($message) use($user) {
             $message->to($user->email, $user->getFullName());
             $message->subject('SWAMP Password Changed');
         });
         return Response::json(array('success' => true));
         // current user is not an admin
         //
     } else {
         if ($userUid == $currentUser->user_uid) {
             $oldPassword = Input::get('old_password');
             if (User::isValidPassword($oldPassword, $currentUser->password)) {
                 $newPassword = Input::get('new_password');
                 // change password
                 //
                 $currentUser->modifyPassword($newPassword);
                 $cfg = array('url' => Config::get('app.cors_url') ?: '', 'user' => $user);
                 Mail::send('emails.password-changed', $cfg, function ($message) use($user) {
                     $message->to($user->email, $user->getFullName());
                     $message->subject('SWAMP Password Changed');
                 });
                 return Response::json(array('success' => true));
             } else {
                 // old password is not valid
                 //
                 return Response::make('Old password is incorrect.', 404);
             }
             // current user is not the target user
             //
         } else {
             return Response::make("You must be an admin to change a user's password", 403);
         }
     }
 }
 public function githubLink()
 {
     $username = Input::get('username');
     $password = Input::get('password');
     $user = User::getByUsername($username);
     if ($user) {
         if (User::isValidPassword($password, $user->password)) {
             if ($user->hasBeenVerified()) {
                 if ($user->isEnabled()) {
                     // Attempt to load the github account the user is currently logged in as.
                     //
                     if (!Session::has('github_access_token') || !Session::has('github_access_time')) {
                         return Response::make('Unauthorized GitHub access.', 401);
                     }
                     if (gmdate('U') - Session::get('github_access_time') > 15 * 60) {
                         return Response::make('GitHub access has expired.  If you would like to link a GitHub account to an existing SWAMP account, please click "Sign In" and select "Sign in With GitHub."', 401);
                     }
                     $token = Session::get('github_access_token');
                     $ch = curl_init('https://api.github.com/user');
                     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token {$token}"));
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
                     curl_setopt($ch, CURLOPT_USERAGENT, 'SWAMP');
                     $response = curl_exec($ch);
                     $github_user = json_decode($response);
                     if (!property_exists($github_user, 'id')) {
                         return Response::make('Unable to authenticate with GitHub.', 401);
                     }
                     // Make sure they don't already have an account
                     //
                     $account = LinkedAccount::where('user_uid', '=', $user->user_uid)->where('linked_account_provider_code', '=', 'github')->first();
                     if ($account && !(Input::has('confirmed') && Input::get('confirmed') === 'true')) {
                         return Response::json(array('error' => 'EXISTING_ACCOUNT', 'username' => $user->username, 'login' => $github_user->login), 401);
                     }
                     // Verify they are logged in as the account they are attempting to link to.
                     //
                     if ($github_user->id != Input::get('github_id')) {
                         return Response::make('Unauthorized GitHub access.', 401);
                     }
                     // Remove any old entries
                     LinkedAccount::where('user_uid', '=', $user->user_uid)->where('linked_account_provider_code', '=', 'github')->delete();
                     // Link the accounts
                     //
                     $linkedAccount = new LinkedAccount(array('linked_account_provider_code' => 'github', 'user_external_id' => Input::get('github_id'), 'enabled_flag' => 1, 'user_uid' => $user->user_uid, 'create_date' => gmdate('Y-m-d H:i:s')));
                     $linkedAccount->save();
                     $userEvent = new UserEvent(array('user_uid' => $user->user_uid, 'event_type' => 'linkedAccountCreated', 'value' => json_encode(array('linked_account_provider_code' => 'github', 'user_external_id' => $linkedAccount->user_external_id, 'user_ip' => $_SERVER['REMOTE_ADDR']))));
                     $userEvent->save();
                     Response::make('User account linked!');
                 } else {
                     return Response::make('User has not been approved.', 401);
                 }
             } else {
                 return Response::make('User email has not been verified.', 401);
             }
         } else {
             return Response::make('Incorrect username or password.', 401);
         }
     } else {
         return Response::make('Incorrect username or password.', 401);
     }
 }
 public function postResend()
 {
     // get input parameters
     //
     $username = Input::get('username');
     $password = Input::get('password');
     // validate user
     //
     $user = User::getByUsername($username);
     if ($user) {
         if (User::isValidPassword($password, $user->password)) {
             // get email verification
             //
             $emailVerification = $user->getEmailVerification();
             // resend
             //
             $emailVerification->send('#register/verify-email');
         }
     }
 }