Symfony使用Friends of Symfony手动创建用户

时间:2014-12-04 20:02:57

标签: symfony fosuserbundle

所以我正在为用户使用此捆绑包:https://github.com/FriendsOfSymfony/FOSUserBundle

这很完美,在我的本地机器上(我开发的地方)每件事都很好。我通过捆绑包提供的CLI界面创建了管理员用户。

不幸的是,我的客户端所在的托管服务商不允许从命令行访问MySQL。所以我正在寻找一个没有CLI创建管理员用户的方法。我可以使用FTP完全访问代码,我可以使用PHPmySQL访问数据库。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

最快的方式,无需将数据库带到您的本地环境并返回:在您的本地环境中,运行php app/console fos:user:create -vvv。这会运行调试详细模式,以便INSERT查询出来:

$ php app/console fos:user:create -vvv
[2014-12-04 15:49:45] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand". [] []
Please choose a username:test
Please choose an email:test@test.com
Please choose a password:test
[2014-12-04 15:49:55] doctrine.DEBUG: "START TRANSACTION" [] []
[2014-12-04 15:49:55] doctrine.DEBUG: INSERT INTO fos_user_user (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, created_at, updated_at, date_of_birth, firstname, lastname, website, biography, gender, locale, timezone, phone, facebook_uid, facebook_name, facebook_data, twitter_uid, twitter_name, twitter_data, gplus_uid, gplus_name, gplus_data, token, two_step_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) {"1":"test","2":"test","3":"test@test.com","4":"test@test.com","5":true,"6":"caah6vuazqosss48w8cwsssog080k8k","7":"9frgaqCv4zJQky6YUTLR5UzebY [...]","8":null,"9":false,"10":false,"11":null,"12":null,"13":null,"14":[],"15":false,"16":null,"17":"2014-12-04 15:49:55","18":"2014-12-04 15:49:55","19":null,"20":null,"21":null,"22":null,"23":null,"24":"u","25":null,"26":null,"27":null,"28":null,"29":null,"30":null,"31":null,"32":null,"33":null,"34":null,"35":null,"36":null,"37":null,"38":null} []
[2014-12-04 15:49:55] doctrine.DEBUG: "COMMIT" [] []
Created user test

从那个长调试INSERT输出中你真正需要的是saltrolespassword的值 - 你可以从你最喜欢的MySQL客户端完成剩下的工作几乎没有问题。如果您的主机没有为您提供访问MySQL数据库的方法,您可以install PHPMyAdmin somewhere in your web/ folder并通过那里获得访问权限。我可能不会安装它。

答案 1 :(得分:1)

您也可以使用控制器:

应用程序/配置/ routing.yml中

create_admin_user:
    pattern: adminuser/create
    defaults: {_controller: ACMEDemoBundle:Any:createAdmin}  

的src / ACME / DemoBundle /控制器/ AnyController.php

<?php
namespace ACME\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AnyController extends Controller
{

    /**
     * Creates admin user.
     *
     * @return Response
     */
    public function createAdminAction()
    {
        $userManipulator = $this->get('fos_user.util.user_manipulator');
        $adminPassword = "supersecret";
        $adminUsername = "superadmin";
        $adminEmail = "super@admin.com";
        $isActive = true;
        $isSuperAdmin = true;        
        $userManipulator->create($adminUsername, $adminPassword, $adminEmail, $isActive, $isSuperAdmin);

        return new Response();
    }
}  

并调用网址http://yourhost:<port>/adminuser/create

答案 2 :(得分:0)

您可以导出&#39;用户&#39;和&#39; UserGroup&#39;本地表,并通过phpMyAdmin在您的主机环境中导入它们。

但请务必先备份数据库。