public function index() { $app_id = Config::get('registration::social.fb.api_id'); $app_secret = Config::get('registration::social.fb.secret_key'); $my_url = "http://" . $_SERVER['HTTP_HOST'] . "/auth_soc/face_res"; $code = Input::get("code"); $state = Input::get("state"); if (empty($code)) { Session::put('state', md5(uniqid(rand(), TRUE))); $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=public_profile,publish_actions,email&state=" . Session::get('state') . "&fields=email,first_name,last_name,id,gender"; header("Location: {$dialog_url}"); } if ($state == Session::get('state')) { $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code . "&fields=email,first_name,last_name,id,gender"; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token'] . "&fields=email,first_name,last_name,id,gender"; $user = json_decode(file_get_contents($graph_url)); $first_name = $user->first_name; $last_name = $user->last_name; $fb_id = $user->id; if (isset($user->email)) { $user_email = $user->email; } else { $user_email = $fb_id; } //проверка юзера if ($user_email && $fb_id) { $user = DB::table("users")->where("id_fb", $fb_id)->first(); if (!$user['id']) { $user = DB::table("users")->where("email", "like", $user_email)->first(); } if (!$user['id']) { $new_pass = str_random(6); $user = Sentry::register(array('email' => $user_email, 'password' => $new_pass, 'id_fb' => $fb_id, 'activated' => "1", 'first_name' => $first_name, 'last_name' => $last_name)); $user_auth = Sentry::findUserById($user->id); Sentry::login($user_auth, Config::get('registration::social.fb.remember')); } else { $user_auth = Sentry::findUserById($user['id']); Sentry::login($user_auth, Config::get('registration::social.fb.remember')); } $redirect = Session::get('url_previous', "/"); Session::forget('url_previous'); //if not empty redirect_url if (Config::get('registration::social.fb.redirect_url')) { $redirect = Config::get('registration::social.fb.redirect_url'); Session::flash('id_user', $user_auth->id); } else { $redirect = Session::get('url_previous', "/"); Session::forget('url_previous'); } return Redirect::to($redirect); } } }
public function index() { if (Input::get("code")) { $api_id = Config::get('registration::social.vk.api_id'); $secret_key = Config::get('registration::social.vk.secret_key'); $params = array('client_id' => $api_id, 'client_secret' => $secret_key, 'code' => Input::get("code"), 'redirect_uri' => "http://" . $_SERVER['HTTP_HOST'] . "/auth_soc/vk_res"); $url = 'https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params)); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); $data = json_decode($result, true); if (isset($data['access_token'])) { $str = "https://api.vkontakte.ru/method/getProfiles?uid=" . $data['user_id'] . "&fields=photo_big&access_token=" . $data['access_token']; $resp2 = file_get_contents($str); $el = json_decode($resp2, true); $first_name = $el['response'][0]['first_name']; $last_name = $el['response'][0]['last_name']; $id_user = $el['response'][0]['uid']; $user = DB::table("users")->where("id_vk", $id_user)->first(); if (!isset($user['id'])) { $new_pass = str_random(6); $user = Sentry::register(array('email' => $id_user, 'password' => $new_pass, 'id_vk' => $id_user, 'activated' => "1", 'first_name' => $first_name, 'last_name' => $last_name)); //качаем аватарку юзера if ($el['response'][0]['photo_big'] && Config::get('registration::social.vk.foto')) { $id_one = substr($user->id, 0, 1); $destinationPath = "/storage/users/{$id_one}/{$user->id}/"; $path_server = public_path() . $destinationPath; File::makeDirectory($path_server, $mode = 0777, true, true); $foto_resource = file_get_contents($el['response'][0]['photo_big']); $foto_user = time() . basename($el['response'][0]['photo_big']); $f = fopen($_SERVER['DOCUMENT_ROOT'] . $destinationPath . $foto_user, 'w'); fwrite($f, $foto_resource); fclose($f); $user->photo = $destinationPath . $foto_user; $user->save(); } $user_auth = Sentry::findUserById($user->id); Sentry::login($user_auth, Config::get('registration::social.vk.remember')); } else { $user_auth = Sentry::findUserById($user['id']); Sentry::login($user_auth, Config::get('registration::social.vk.remember')); } //if not empty redirect_url if (Config::get('registration::social.vk.redirect_url')) { $redirect = Config::get('registration::social.vk.redirect_url'); Session::flash('id_user', $user_auth->id); } else { $redirect = Session::get('url_previous', "/"); Session::forget('url_previous'); } return Redirect::to($redirect); } } }
/** * Log the given user ID into the application. * * @param mixed $id * @param bool $remember * @return \Illuminate\Auth\UserInterface */ public function loginUsingId($id, $remember = false) { try { // Find the user using the user id $user = Sentry::getUserProvider()->findById($id); // Log the user in Sentry::login($user, $remember); } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) { } catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) { } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) { } return false; }
public function oauth2callback() { if (Input::get("code")) { $params = array('client_id' => Config::get('registration::social.google.api_id'), 'client_secret' => Config::get('registration::social.google.secret_key'), 'redirect_uri' => Config::get('registration::social.google.redirect_oauth2callback'), 'grant_type' => 'authorization_code', 'code' => Input::get("code")); $url = 'https://accounts.google.com/o/oauth2/token'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params))); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($curl); curl_close($curl); $tokenInfo = json_decode($result, true); if (isset($tokenInfo['access_token'])) { $params['access_token'] = $tokenInfo['access_token']; $userInfo = json_decode(file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo' . '?' . urldecode(http_build_query($params))), true); if ($userInfo["id"]) { $email = trim($userInfo['email']); $user = DB::table("users")->where("email", "like", $email)->first(); if (!$user['id']) { $new_pass = str_random(6); $user = Sentry::register(array('email' => $email, 'password' => $new_pass, 'activated' => "1", 'first_name' => $userInfo['given_name'], 'last_name' => $userInfo['family_name'])); $user_auth = Sentry::findUserById($user->id); Sentry::login($user_auth, Config::get('registration::social.google.remember')); } else { $user_auth = Sentry::findUserById($user['id']); Sentry::login($user_auth, Config::get('registration::social.google.remember')); } $redirect = Session::get('url_previous', "/"); Session::forget('url_previous'); //if not empty redirect_url if (Config::get('registration::social.google.redirect_url')) { $redirect = Config::get('registration::social.google.redirect_url'); Session::flash('id_user', $user_auth->id); } else { $redirect = Session::get('url_previous', "/"); Session::forget('url_previous'); } return Redirect::to($redirect); } } } }
public function doActivatingUser() { $email = Input::get("email"); $code = Input::get("code"); $status = "error"; if ($email && $code) { try { $user = Sentry::findUserByLogin($email); // Attempt to activate the user if ($user->attemptActivation($code)) { $result = "Пользователь активирован"; $status = "success"; Sentry::login($user, false); } else { $result = "Ошибка. Пользователя код активации не подходит"; } } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) { $result = "Пользователь не найден"; } catch (\Cartalyst\Sentry\Users\UserAlreadyActivatedException $e) { $result = "Пользователь уже активирован"; } return View::make('registration::activatingUser', compact("result", "status")); } else { $result = "Неверные входные данные. Email или код активации неверные "; return View::make('registration::activatingUser', compact("result")); } }