unplugged_web Posted September 4, 2014 Share Posted September 4, 2014 (edited) I'm not sure why this isn't working. I've tried to add 'check if user is in database' function to Google login. The login worked before I add the db check. I've gone through the error logs and no errors are being thrown but when I try to go in I just get returned to the login page. If somebody isn't authorised to log in then they will be returned to the login page but should see the site if they are permitted. I've got this: $app->match('/auth/callback', function (Request $request) use ($app, $googleService) { // This was a callback request from google, get the token $googleService->requestAccessToken($request->get('code')); // Send a request with it $user = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true); if (substr($user['email'],-11)==='@domain.com') {function findPkSimple($key, $con) {$sql = 'SELECT COUNT(*) AS count FROM users WHERE ID = :p0'; try { $stmt = $con->prepare($sql); $stmt->bindValue(':p0', $key, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); if ($result[0]["count"] > 0) { // User Exist $user['logged_in_date'] = date('Y-m-d'); $app['session']->set('userinfo', $user); $log = new employeeUserLoggedinLog(); $log->setEmail($user['email']); $log->setDate(time()); $log->save(); } else { $message = Swift_Message::newInstance() ->setSubject('Alert :: unauthorized login') ->setFrom(array('alert@domain.com' => 'robot')) ->setTo(array('adim@domain.com')) ->setBody(sprintf("The following email [%s] tried to login from IP [%s]", $user['email'], $request->getClientIp())); $app['mailer']->send($message); } } catch (Exception $ex) { Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e);}} } else { $message = Swift_Message::newInstance() ->setSubject('Alert :: unauthorized login') ->setFrom(array('alert@domain.com' => 'robot')) ->setTo(array('adim@domain.com')) ->setBody(sprintf("The following email [%s] tried to login from IP [%s]", $user['email'], $request->getClientIp())); $app['mailer']->send($message); } return $app->redirect('/');}); Edited September 5, 2014 by thehappyappy Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2014 Share Posted September 4, 2014 Your if statement defines a function but doesn't do anything else. You probably want to execute the function and figure out what variables to pass to it. Note if that code runs in a loop you're going to get an error about redefining that function, that's an odd place to define a function. Link to comment Share on other sites More sharing options...
unplugged_web Posted September 5, 2014 Author Share Posted September 5, 2014 (edited) I've changed the code so that it now looks like this: $app->match('/auth/callback', function (Request $request) use ($app, $googleService) {// This was a callback request from google, get the token$googleService->requestAccessToken($request->get('code'));// Send a request with it$user = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);if (substr($user['email'],-11)==='@domain.com') { $host = 'localhost';$dbname = 'xxxxxxx';$username = 'root';$password = 'xxxxxxxxxxxx';$con = new PDO("mysql:host={$host};dbname={$dbname}", $username, $password); // assumes you're using the MySQL database $sql = 'SELECT COUNT(*) AS count FROM users WHERE email = :email';try {$stmt = $con->prepare($sql);$stmt->bindValue(':email', $user['email']);$stmt->execute();$result = $stmt->fetchAll();if ($result[0]['count'] > 0) {// User Exist$user['logged_in_date'] = date('Y-m-d');$app['session']->set('userinfo', $user);$log = new employeeUserLoggedinLog();$log->setEmail($user['email']);$log->setDate(time());$log->save();} else {$message = Swift_Message::newInstance()->setSubject('Alert :: unauthorized login')->setFrom(array('alert@domain.com' => 'robot'))->setTo(array('admin@domain.com'))->setBody(sprintf("The following ex-employee [%s] tried to login from IP [%s]", $user['email'], $request->getClientIp()));$app['mailer']->send($message);}} catch (Exception $ex) {Propel::log($e->getMessage(), Propel::LOG_ERR);throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e);} } else {$message = Swift_Message::newInstance()->setSubject('Alert :: unauthorized login')->setFrom(array('alert@domain.com' => 'robot'))->setTo(array('admin@domain.com'))->setBody(sprintf("The following email [%s] tried to login from IP [%s]", $user['email'], $request->getClientIp()));$app['mailer']->send($message);}return $app->redirect('/');}); And now it works perfectly, I just thought I'd post that incase anybody else needed help with it in the future Edited September 5, 2014 by thehappyappy Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now