示例#1
0
 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);
         }
     }
 }
示例#2
0
 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);
         }
     }
 }
 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 doRegistration()
 {
     parse_str(Input::get('filds'), $filds);
     //check password
     if ($filds['password'] != $filds['re_password']) {
         return Response::json(array('status' => 'error', "errors_messages" => "Ошибка. Пароли не совпадают"));
     }
     $validator = Validator::make($filds, $this->reg_rules, $this->messages);
     if ($validator->fails()) {
         return Response::json(array('status' => 'error', "errors_messages" => implode("<br>", $validator->messages()->all())));
     }
     try {
         $user = Sentry::register(array('email' => $filds['email'], 'password' => $filds['password'], 'first_name' => $filds['name']));
         $mail = new MailT(Config::get('registration::registration.template_mail'), ["login" => $filds['email'], "password" => $filds['password'], "activationcode" => $user->getActivationCode()]);
         $mail->to = $filds['email'];
         $mail->send();
         return Response::json(array("status" => "ok", "ok_messages" => "Вы успешно зарегистрированы. На почту выслана ссылка для активации"));
     } catch (\Cartalyst\Sentry\Users\UserExistsException $e) {
         return Response::json(array('status' => 'error', "errors_messages" => $this->messages['email.unique']));
     }
 }
示例#5
0
 /**
  * 添加用户
  * @return mixed
  */
 public function store()
 {
     $user['email'] = $user['username'] = Input::get('email');
     $user['password'] = Input::get('password0');
     $mail_url = (new Uinfo())->getMail($user['email']);
     //判断用户是否存在
     $user_exist = User::where('email', $user['email'])->first();
     if ($user_exist) {
         return Redirect::back()->withErrors('邮箱已被占用!');
     }
     $errors = '';
     //添加用户
     try {
         $user = Sentry::register($user);
         //添加默认用户组
         $genealGroup = Sentry::findGroupById(2);
         $user->addGroup($genealGroup);
         //添加用户对应信息
         Uinfo::create(['uid' => $user->id]);
         //登陆该用户
         $data['activationCode'] = $user->getActivationCode();
         $data['uid'] = $user->id;
         Mail::send('emails.auth.active', $data, function ($message) use($user) {
             $message->to($user->email, '尊敬的柚皮会员')->subject('欢迎注册柚皮网');
         });
     } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
         $errors = '请输入完整注册信息.';
     } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
         $errors = '请输入密码.';
     } catch (Cartalyst\Sentry\Users\UserExistsException $e) {
         $errors = '用户已存在.';
     }
     if ($errors) {
         return Redirect::back()->withErrors($errors);
     } else {
         return View::make('auth.active')->withEmail($mail_url);
     }
 }
示例#6
0
 public function postUsers(Request $request)
 {
     $outputMessage = array();
     if ($request->has('action')) {
         switch ($request->get('action')) {
             case 'new':
                 $email = $request->get('email');
                 $password = $request->get('password');
                 $firstname = $request->get('firstname');
                 $lastname = $request->get('lastname');
                 //$groupName = $request->get('group');
                 $groupID = $request->get('group');
                 try {
                     $user = Sentry::register(array('email' => $email, 'first_name' => $firstname, 'last_name' => $lastname, 'password' => $password, 'activated' => true));
                     if ($user) {
                         $outputMessage[] = array("type" => "success", "msg" => "User created successfuly.");
                         if ($groupID == 1) {
                             $group = Sentry::findGroupByName('user');
                             $user->addGroup($group);
                             $outputMessage[] = array("type" => "alert", "msg" => "The God is unique.User is added to User group ;)");
                         } else {
                             $group = Sentry::findGroupById($groupID);
                             if ($group) {
                                 $user->addGroup($group);
                                 $outputMessage[] = array("type" => "success", "msg" => "User added to " . $group->name . " successfuly");
                             } else {
                                 $group = Sentry::findGroupByName('user');
                                 $user->addGroup($group);
                                 $outputMessage[] = array("type" => "alert", "msg" => "Group " . $group->name . " not found.User is added to User group ;)");
                             }
                         }
                     }
                 } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Login field is required.");
                 } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Login field is required.");
                 } catch (\Cartalyst\Sentry\Users\UserExistsException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "User with this login already exists.");
                 } catch (\Cartalyst\Sentry\Groups\GroupNotFoundException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Group was not found.");
                 }
                 break;
         }
     }
     $groups = Sentry::findAllGroups();
     $users = User::select(array('id', 'first_name', 'last_name', 'email'))->paginate(20);
     return view('admin.users')->with('users', $users)->with('groups', $groups)->with('messages', $outputMessage);
 }
示例#7
0
 /**
  * Registers a user through Sentry.
  *
  * @param array $data
  * @param array $groups
  *
  * @return mixed
  */
 public function registerUser(array $data, array $groups = [])
 {
     try {
         $insert = ['first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'email' => $data['email'], 'username' => $data['username'], 'password' => $data['password']];
         $user = Sentry::register($insert);
         $this->addGroupsToUser($user, $groups);
     } catch (UserExistsException $e) {
         $loginAttribute = config('cartalyst.sentry.users.login_attribute');
         $user = Sentry::findUserByLogin($data[$loginAttribute]);
     }
     return $user;
 }
示例#8
0
 /**
  * Register a new user.
  * @param  array $user
  * @return bool
  */
 public function register(array $user)
 {
     return Sentry::register($user);
 }