简体   繁体   English

在Symfony2中设置NEO4j

[英]Setting up NEO4j in Symfony2

I am new to Symfony and have started reading and toying about. 我是Symfony的新手,已经开始阅读和玩弄玩具。 I was able to install Symfony2 and a NEO4j database. 我能够安装Symfony2和NEO4j数据库。 I was also able to include bootstrap into my Symfony project and display a basic homepage. 我还能够将引导程序包含到我的Symfony项目中并显示一个基本主页。

I would now like to connect my project to the database. 我现在想将我的项目连接到数据库。 I would like to start with basic functionallity such as registration and user system. 我想从注册和用户系统等基本功能入手。

I am currently using following (installed via composer): 我目前正在使用以下文件(通过composer安装):

    "everyman/neo4jphp": "dev-master",
    "hirevoice/neo4jphp-ogm": "dev-master",
    "klaussilveira/neo4j-ogm-bundle": "dev-master"

These should give me the functionality I need. 这些应该给我我需要的功能。 I have then created a UserBundle under src with following folder structure: 然后,我在src下使用以下文件夹结构创建了一个UserBundle:

src    
|-Controller    
|-DependencyInjection
|-Entity
|-Form
|-Ressources
|-UserBundle.php

I would now like to go through the tutorial at http://symfony.com/doc/current/cookbook/doctrine/registration_form.html but instead of using their database, I would like to use Neo4j. 我现在想浏览一下http://symfony.com/doc/current/cookbook/doctrine/registration_form.html上的教程,但是我不想使用他们的数据库,而是想使用Neo4j。 Could someone point out where and how I should manipulate the code of the tutorial to be able to register a user into the neo4j graph? 有人可以指出我应该在哪里以及如何操纵本教程的代码才能将用户注册到neo4j图形中? I have tried myself but the documentation is scarse of the neo4j-ogm and I have only produced errors upon errors. 我已经尝试过了,但是文档缺乏neo4j-ogm的介绍,并且仅在出错时产生错误。 I have now deleted the Bundle and created it again to start anew. 现在,我删除了捆绑软件,并再次创建它以重新开始。

I am especially interessted if their are steps that are assumed to be common knowledge and are thus left out? 如果他们的步骤被认为是公知常识,因此被遗漏了,我会特别感兴趣。

I have now been able to set up everything so that the registration form is shown. 现在,我已经能够进行所有设置,以便显示注册表。 But when submitting, following error is shown: 但是提交时会显示以下错误:

Unable to commit batch [401]:
Headers: Array
(
[Date] => Sun, 05 Jul 2015 13:08:20 GMT
[Content-Type] => application/json; charset=UTF-8
[WWW-Authenticate] => None
[Content-Length] => 139
[Server] => Jetty(9.2.4.v20141103)
)
Body: Array
(
[errors] => Array
(
[0] => Array
(
[message] => No authorization header supplied.
[code] => Neo.ClientError.Security.AuthorizationFailed
)

)

)
500 Internal Server Error - Exception 

I am assuming that my connection authentication is not working. 我假设我的连接身份验证无效。 In my config.yml I have defined following connection: 在我的config.yml中,我定义了以下连接:

# Neo4j config   
neo4j_ogm:
    transport: 'curl'
    host: 'localhost'
    port: 7474
    username: 'foo'
    password: 'bar'
    debug: true

But this does not seem to work. 但这似乎不起作用。 Where else do I have to set up the connection details? 我还必须在哪里设置连接详细信息?

Update 更新

Thank you Christophe Willemsen for your answer. 谢谢Christophe Willemsen的回答。

The error was produced because the authentication did not work. 由于身份验证不起作用而产生了错误。 By navigating to the Neo4j installation folder and editing the "neo4j-server.properties" file to allow all access, I am now able to save simple data to the graph. 通过导航到Neo4j安装文件夹并编辑“ neo4j-server.properties”文件以允许所有访问,我现在能够将简单数据保存到图形中。 I have not tried to implement any checks or passwort encryptions, but the very basic "create a node with properties" now works. 我没有尝试实现任何检查或密码加密,但是现在可以使用非常基本的“使用属性创建节点”。

What I would like to accomplish now would be: 我现在想完成的是:

  • Make it possible to password protect the Neo4j connection and have the connection information stored in a config.yml file 可以使用密码保护Neo4j连接并将连接信息存储在config.yml文件中
  • Create and implement various checks (is email address already in DB / Is password strong enough...) 创建并实施各种检查(数据库中是否已存在电子邮件地址/密码是否足够强大...)
  • Create and implement password encryption 创建并实施密码加密
  • Create member-system which gets data out of the neo4j database 创建成员系统,该系统从neo4j数据库中获取数据

If you have any tips on where I can get more information on this topic or if you have already done something similar, help would be appreciated. 如果您有关于在何处可以获得有关此主题的更多信息的提示,或者您已经做过类似的操作,将不胜感激。

Have added the files to my post below in case these are useful for someone else. 如果这些文件对其他人有用,请将它们添加到下面的我的帖子中。 Be aware: I have only just begun learning Symfony and Neo4j and the code could contain errors or major vulnerabilitie s. 请注意:我才刚刚开始学习Symfony和Neo4j,并且代码中可能包含错误或严重的漏洞 I will also try to update this post if I have any luck implementing further neo4j related things. 如果我在实现更多与neo4j相关的事情上有运气,我还将尝试更新此帖子。

In the file "neo4j-server.properties" (located under "/etc/neo4j" on my install) edit following lines as such: 在文件“ neo4j-server.properties”(位于我的安装中的“ / etc / neo4j”下)中,编辑以下几行:

# Let the webserver only listen on the specified IP. Default is localhost (only
# accept local connections). Uncomment to allow any connection. Please see the
# security section in the neo4j manual before modifying this.
org.neo4j.server.webserver.address=0.0.0.0

# Require (or disable the requirement of) auth to access Neo4j
dbms.security.auth_enabled=false

In my UserBundle, following files could be of interesst to others: "/src/UserBundle/Controller/AccountController.php" 在我的UserBundle中,以下文件可能会引起他人的兴趣“ /src/UserBundle/Controller/AccountController.php”

<?php
// src/Acme/AccountBundle/Controller/AccountController.php
namespace UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use UserBundle\Form\Type\RegistrationType;
use UserBundle\Form\Model\Registration;

use Symfony\Component\HttpFoundation\Request;

// use HireVoice\Neo4j\EntityManager as EntityManager;

class AccountController extends Controller
{
    public function registerAction()
    {
        $registration = new Registration();
        $form = $this->createForm(new RegistrationType(), $registration, array(
            'action' => $this->generateUrl('account_create'),
        ));

        return $this->render(
            'UserBundle:Account:register.html.twig',
            array('form' => $form->createView())
        );
    }

    public function createAction(Request $request)
    {   


        $em = $this->container->get('neo4j.manager');
        //$repo = $em->getRepository('Entity\\User');

        $form = $this->createForm(new RegistrationType(), new Registration());        

        $form->handleRequest($request);

        if ($form->isValid()) {

            $registration = $form->getData();


            $em->persist($registration->getUser());
            $em->flush();

            return $this->redirectToRoute('account_success');
           // return $this->redirect('http://4740.hostserv.eu/twohive/web/app_dev.php/register/success');

        }

        return $this->render(
            'UserBundle:Account:register.html.twig',
            array('form' => $form->createView())
        );
    }

    public function successAction()
    {
        return $this->render('UserBundle:Account:success.html.twig');
    }
}

"/src/UserBundle/Entity/User.php" “/src/UserBundle/Entity/User.php”

<?php
// src/UserBundle/Entity/User.php
namespace UserBundle\Entity;


use HireVoice\Neo4j\Annotation as OGM;

/**
 * @OGM\Entity
 */
class User
{
    /**
     * @OGM\Auto
     */
    protected $id;

    /**
     * @OGM\Property
     * @OGM\Index
     */
    protected $email;

    /**
     * @OGM\Property
     */
    protected $plainPassword;

    public function getId()
    {
        return $this->id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }
}

"/src/UserBundle/Form/Model/Registration.php" (As described in the Symfony-Book) “ /src/UserBundle/Form/Model/Registration.php”(如Symfony书中所述)

<?php
// src/Acme/AccountBundle/Form/Model/Registration.php
namespace UserBundle\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;

use UserBundle\Entity\User;

class Registration
{
    /**
     * @Assert\Type(type="UserBundle\Entity\User")
     * @Assert\Valid()
     */
    protected $user;

    /**
     * @Assert\NotBlank()
     * @Assert\True()
     */
    protected $termsAccepted;

    public function setUser(User $user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function getTermsAccepted()
    {
        return $this->termsAccepted;
    }

    public function setTermsAccepted($termsAccepted)
    {
        $this->termsAccepted = (bool) $termsAccepted;
    }
}

"/src/UserBundle/Form/Type/RegistrationType.php" (As described in the Symfony-Book) “ /src/UserBundle/Form/Type/RegistrationType.php”(如Symfony书中所述)

<?php
// UserBundle/Form/Type/RegistrationType.php
namespace UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType());
        $builder->add(
            'terms',
            'checkbox',
            array('property_path' => 'termsAccepted')
        );
        $builder->add('Register', 'submit');
    }

    public function getName()
    {
        return 'registration';
    }
}

"/src/UserBundle/Form/Type/UserType.php" (As described in the Symfony-Book) “ /src/UserBundle/Form/Type/UserType.php”(如Symfony书中所述)

<?php
// UserBundle/Form/Type/UserType.php
namespace UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'email');
        $builder->add('plainPassword', 'repeated', array(
           'first_name'  => 'password',
           'second_name' => 'confirm',
           'type'        => 'password',
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'UserBundle\Entity\User'
        ));
    }

    public function getName()
    {
        return 'user';
    }
}

"/src/UserBundle/Resources/config/routing.yml" “/src/UserBundle/Resources/config/routing.yml”

account_register:
    path:     /register
    defaults: { _controller: UserBundle:Account:register }

account_create:
    path:     /register/create
    defaults: { _controller: UserBundle:Account:create }

account_success:
    path:     /register/success
    defaults: { _controller: UserBundle:Account:success }

I did this implementation some time ago (~ two years) but didn't updated it due to the fact I don't use these vendors anymore. 我前一段时间(〜两年)做了这个实现,但是由于我不再使用这些供应商而没有更新它。

This was splitted in two bundles, and that might give you some ideas and examples : 它分为两个捆绑,这可能会给您一些想法和示例:

https://github.com/ikwattro/KwattroNeo4jOGMBundle https://github.com/ikwattro/KwattroNeo4jOGMBundle

https://github.com/ikwattro/Neo4jUserBundle https://github.com/ikwattro/Neo4jUserBundle

Basically steps are common and lot of information can be found in the Symfony documentation. 基本上,步骤很常见,可以在Symfony文档中找到很多信息。

  1. Create your user entity base on the neo4jphp-ogm annotations 根据neo4jphp-ogm批注创建用户实体

  2. Create a User provider -> security 创建一个用户提供者->安全性

  3. Create a User Manager that will retrieve the users when the authentication happens 创建一个用户管理器,该用户管理器将在身份验证发生时检索用户

Concerning your last error concerning the authentication, by looking at the klaussilvera/neo4j-ogm-bundle last commit date, chance is high that it doesn't support neo4j authentication added in 2.2. 关于身份验证的最后错误,通过查看klaussilvera/neo4j-ogm-bundle最后提交日期,很可能它不支持2.2中添加的neo4j身份验证。

You might want to disable the neo4j authentication for your first tests or fork the current repository and create a PR adding the authentication parameters in the ConfigDefinition of the bundle. 您可能想为首次测试禁用neo4j身份验证,或者派生当前存储库并创建PR,在束的ConfigDefinition中添加身份验证参数。

I have now created a new repository which seems to work up till now: 我现在创建了一个新的存储库,该存储库似乎可以使用到现在:

https://github.com/JoranBeaufort/Neo4jUserBundle https://github.com/JoranBeaufort/Neo4jUserBundle

Be careful: Like I stated, I am not an expert in Symfony or programming in general. 注意:就像我说的那样,我一般不是Symfony或编程专家。 The Bundle may have serious security issues. 捆绑软件可能存在严重的安全问题。 Hints of improvement are welcome. 欢迎改进的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM