wawa
wawa

Reputation: 5066

Symfony create User manually with Friends Of Symfony

So I'm using this bundle for the Users: https://github.com/FriendsOfSymfony/FOSUserBundle

This works perfect and on my local machine (where I develop) every thing works fine. I created an Admin User over the CLI interface the bundle provides.

Unfortunately the hoster where my client is doesn't allow MySQL access from Command Line. So I'm looking for a was to create a admin user without CLI. I have full access to the code with FTP and I can access the DB with PHPmySQL.

Any suggestions?

Upvotes: 1

Views: 2468

Answers (3)

devilcius
devilcius

Reputation: 1884

You can use a controller also:

app/config/routing.yml:

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

src/ACME/DemoBundle/Controller/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 = "[email protected]";
        $isActive = true;
        $isSuperAdmin = true;        
        $userManipulator->create($adminUsername, $adminPassword, $adminEmail, $isActive, $isSuperAdmin);

        return new Response();
    }
}  

And invoke the url http://yourhost:<port>/adminuser/create

Upvotes: 1

sjagr
sjagr

Reputation: 16502

Fastest way without having to bring databases to your local environment and back: on your local environment, run php app/console fos:user:create -vvv. This runs debug verbose mode so the INSERT query is spewed out:

$ 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:[email protected]
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":"[email protected]","4":"[email protected]","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

All you really need from that long debug INSERT output are the values for salt, roles, and password - you can do the rest from your favourite MySQL client with little issue. If your host isn't providing you with a way to access your MySQL database, you can install PHPMyAdmin somewhere in your web/ folder and gain access through there. I probably wouldn't leave it installed though.

Upvotes: 1

spekulatius
spekulatius

Reputation: 1499

You could export the 'User' and 'UserGroup' tables locally and import them on your hosters environment via phpMyAdmin.

But sure to make a backup of your database before.

Upvotes: 0

Related Questions