定制 grom/facebook-service-provider 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grom/facebook-service-provider

最新稳定版本:v1.1.0

Composer 安装命令:

composer require grom/facebook-service-provider

包简介

Facebook API and connect integration into your Silex applications

README 文档

README

The FacebookServiceProvider adds Facebook Connect and API to your applications.

It integrates FOSFacebookBundle into Silex.

Parameters

  • facebook.config: Configuration of your Facebook application. (appId, secret, fileUpload, )
  • facebook.permissions: List of permissions required to connect.
  • facebook.session_prefix: Prefix for Facebook data.

Services

Installation

Using composer :

composer require grom/facebook-service-provider

Registering

First, you must register the SecurityServiceProvider.

use Silex\Provider\FacebookServiceProvider;

$app->register(new FacebookServiceProvider(), array(
    'facebook.config' => array(
        'appId'      => 'YOUR_APP_ID',
        'secret'     => 'YOUR_APP_SECRET',
        'fileUpload' => false, // optional
    ),
    'facebook.permissions' => array('email'),
));

Authentication

To authenticate users with Facebook Connect, first you need to register the SecurityServiceProvider.

To enable Facebook authentication, just add a "facebook" option to your firewall configuration.

$app['security.firewalls'] = array(
    'private' => array(
        'pattern' => '^/',
        'facebook' => array(
            'check_path' => '/login_check',
            'login_path' => '/login',
        ),
        // Users are identified by their Facebook UID
        'users' => array(
            // This is Mark Zuckerberg
            '4' => array('ROLE_USER', null),
        ),
    ),
);

If you don't set a login_path, the user is redirected to Facebook.

Developers of embedded Facebook Applications may define app_url, server_url and display options.

Defining a custom User Provider and automatic user creation##

The UserProvider used to find Facebook user is similar to the username/password UserProvider. The differences are that users are identified by their Facebook UID instead of their username.

If the Facebook UID is not found in your database, the user provider can create the user automatically. You simply have to implements the method FOS\FacebookBundle\Security\User\UserProviderInterface::createUserFromUid

use FOS\FacebookBundle\Security\User\UserManagerInterface as FacebookUserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Doctrine\DBAL\Connection;

class FacebookUserProvider implements FacebookUserProviderInterface
{
    private $conn;

    public function __construct(Connection $conn)
    {
        $this->conn = $conn;
    }

    public function loadUserByUsername($uid)
    {
        $stmt = $this->conn->executeQuery('SELECT * FROM users WHERE username = ?', array($uid));

        if (!$user = $stmt->fetch()) {
            throw new UsernameNotFoundException(sprintf('Facebook UID "%s" does not exist.', $uid));
        }

        return new User($user['username'], null, explode(',', $user['roles']), true, true, true, true);
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof User) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    public function createUserFromUid($uid)
    {
        $this->conn->insert('users', array(
            'username' => $uid,
            'roles'    => 'ROLE_USER',
        ));

        return $this->loadUserByUsername($uid);
    }

    public function supportsClass($class)
    {
        return $class === 'Symfony\Component\Security\Core\User\User';
    }
}

Now use your custom user provider.

$app['security.firewalls'] = array(
    'default' => array(
        'facebook' => array(
        ),
        'users' => $app->share(function () use ($app) {
            return new FacebookUserProvider($app['db']);
        }),
    ),
);

Facebook Graph API

Once a user is authenticated with Facebook, you can make Facebook Graph API requests.

$app->get('/', function () use ($app) {
    $user = $app['facebook']->api('/me');

    return 'Welcome ' . $user['name'];
});

Look at the Facebook Graph Explorer.

grom/facebook-service-provider 适用场景与选型建议

grom/facebook-service-provider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.4k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2013 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 grom/facebook-service-provider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 grom/facebook-service-provider 我们能提供哪些服务?
定制开发 / 二次开发

基于 grom/facebook-service-provider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-19