nodrew/exceptional-bundle

此包已被废弃,不再维护。没有建议的替代包。
无法获取此包最新版本(dev-master)的许可证信息。

Symfony2 Bundle 用于与 Exceptional 服务协作

安装: 7

依赖者: 0

建议者: 0

安全: 0

星星: 3

关注者: 2

分支: 0

类型:symfony-bundle

dev-master 2012-09-04 21:41 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:21:19 UTC


README

用于与位于: http://www.getexceptional.com 的 Exceptional 服务协作

安装说明

  1. 下载 NodrewExceptionalBundle
  2. 配置自动加载器
  3. 启用 Bundle
  4. 添加您的 Exceptional API 密钥

第 1 步:下载 NodrewExceptionalBundle

最终,NodrewExceptionalBundle 文件应下载到 vendor/bundles/Nodrew/Bundle/ExceptionalBundle 目录。

这可以通过几种方式完成,具体取决于您的偏好。第一种方法是标准的 Symfony2 方法。

使用供应商脚本

在您的 deps 文件中添加以下行

[NodrewExceptionalBundle]
    git=http://github.com/nodrew/NodrewExceptionalBundle.git
    target=/bundles/Nodrew/Bundle/ExceptionalBundle

现在,运行供应商脚本来下载 Bundle

$ php bin/vendors install

使用子模块

如果您更愿意使用 git 子模块,请运行以下命令

$ git submodule add http://github.com/nodrew/NodrewExceptionalBundle.git vendor/bundles/Nodrew/Bundle/ExceptionalBundle
$ git submodule update --init

第 2 步:配置自动加载器

<?php
// app/autoload.php

$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    // ...
    'Nodrew'   => __DIR__.'/../vendor/bundles',
));

第 3 步:启用 Bundle

最后,在 kernel 中启用 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Nodrew\Bundle\ExceptionalBundle\NodrewExceptionalBundle(),
    );
}

第 4 步:添加您的 Exceptional 提供者密钥

# app/config/config.yml
nodrew_exceptional:
    api_key:   [your api key]

可选配置

以下选项可以添加到配置中。

// app/config/config.yml
nodrew_exceptional:
    use_ssl:  false
    context_id: ~
    blacklist:
        - password
        - ssn

use_ssl

类型:布尔型

此命令用于开启 SSL 处理。默认情况下是关闭的,因为许多系统没有安装适当的 openssl 库。但是,如果您有它并且您对传输中可能包含敏感数据表示任何担忧,我强烈建议启用此功能。

blacklist

类型:数组

这可能是您用户安全性的最重要的一部分。这将允许您添加将被过滤掉并从发送到 Exceptional 的 GET 和 POST 数据中过滤的参数。这对于确保密码、ssn 和信用卡号码不会以纯文本形式出现在您的错误日志中非常有用。请,为了我们大家的利益,使用这个功能。

示例

// app/config/config.yml
nodrew_exceptional:
    blacklist:
        - password
        - ssn
        - credit_card_number

在这种情况下,以下内容将被转换

原始

{'password':'secret', 'password2':'secret', 'ssn':'111-111-1111', 'credit_card_number': '1111111111111111', 'name':'joe', 'zip':'10001'} 

将转换成以下内容

{'password':'[PROTECTED]', 'password2':'[PROTECTED]', 'ssn':'[PROTECTED]', 'credit_card_number': '[PROTECTED]', 'name':'joe', 'zip':'10001'} 

如果您注意,字段 password2 也匹配了,因为它包含单词 password。这对于所有这些都适用。任何包含过滤密钥的键都将被过滤。

context_id

这可能是有用的功能。Exceptional 提供了一种向错误日志添加额外上下文数据的方法。在这种情况下,我们通过服务处理器来实现,这将使您能够将自定义数据加载到生成的任何响应中。为此,让我们创建一个简单的处理器来添加已登录用户的 Symfony2 用户名和 userId。

步骤 1:创建您的上下文处理器类。

<?php
// src/Acme/DemoBundle/Handler/ExceptionalContextHandler.php

namespace Acme\DemoBundle\Handler;

use Nodrew\Bundle\ExceptionalBundle\Handler\ContextHandlerInterface,
    Symfony\Component\Security\Core\SecurityContext;

class ExceptionalContextHandler implements ContextHandlerInterface
{
    protected $securityContext;
    
    /**
     * @param Symfony\Component\Security\Core\SecurityContext $securityContext
     */
    public function __construct(SecurityContext $securityContext)
    {
        $this->securityContext = $securityContext;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext()
    {
        $context = array();
        $token   = $this->securityContext->getToken();

        if ($token && $token->isAuthenticated()) {
            $context['userId']   = $token->getUser()->getId();
            $context['username'] = $token->getUser()->getUsername();
        }

        return $context;
    }
}

步骤 2:创建您的服务 ID

// src/Acme/DemoBundle/Resources/config/services.xml
<?xml version="1.0" ?>

<container xmlns="https://symfony.ac.cn/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://symfony.ac.cn/schema/dic/services https://symfony.ac.cn/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="acme.exceptional.context.handler.class">Acme\DemoBundle\Handler\ExceptionalContextHandler</parameter>
    </parameters>

    <services>
        <service id="acme.exceptional.context.handler" class="%acme.exceptional.context.handler.class%">
            <argument type="service" id="security.context" />
        </service>
    </services>
</container>

步骤 3:将您的上下文 ID 添加到配置中

// app/config/config.yml
nodrew_exceptional:
    context_id: acme.exceptional.context.handler

就这些了。现在您应该会在 exceptional 的 params 中看到添加的用户名和 userId,如果用户已登录。如果没有,它将是空的。当然,您可以在该数组中添加自己的参数。只需确保它返回一个数组。如果返回其他任何内容,则它将被跳过。

待办事项

  • 添加 404 处理,以便它们被解析并添加到 Exceptional 后端中的 404 列表中。