Paradiso


Aerobatic pilot, software architect, explorer, and researcher focused on all things distributed.


Multi-User Ghost Woes

So I've recently begun setting up a Ghost blogging system internally at work. Its great for the team to capture what they're doing and present it to the rest of the community. The problem was that, with the advent of Ghost 0.5 and the ability to have multiple users, we weren't able to leverage the feature because we were in a constricted environment.

The problem

So what really am I talking about? Well here's the issue:

  1. We are on a machine that can only be accessed inbound or outbound through a limited set of ports
  2. The Ghost blog wants to email out (i.e. have a the mail tag setup properly) for each new user you want to generate for the system

Between those two constraints (and a few others I'm omitting) there was no way I could get an email sent out to each of my team members. So...

The solution

Since we're using a simple MySQL backend I figured, heck, why not try and insert the columns directly that Ghost needs! Well, my friends, we've done just that. Below is the code that I've found which allows you to, without emailing out anything, insert a new user into the Ghost blogging system.

#!/bin/bash

GHOST_DB_USER=ghost  
GHOST_DB_PROD_TABLE=ghost

ID=`mysql -u ${GHOST_DB_USER} -p << EOF  
  use ${GHOST_DB_PROD_TABLE};
  select count(*) from users;
EOF`

MOD_ID=$(echo $(echo $ID | cut -d' ' -f2) + 1 | bc)

## Arg1 - Slug for new user
mysql -u ${GHOST_DB_USER} -p << EOF  
  use ${GHOST_DB_PROD_TABLE};
  insert into users (id, 
                     uuid,
                     name, 
                     slug, 
                     password, 
                     email, 
                     image, 
                     cover, 
                     bio, 
                     website, 
                     location, 
                     accessibility, 
                     status, 
                     language, 
                     meta_title, 
                     meta_description, 
                     last_login, 
                     created_at, 
                     created_by, 
                     updated_at, 
                     updated_by)
              values ('$MOD_ID',
                      '`cat /proc/sys/kernel/random/uuid`',
                      '$1', 
                      '$1',
                      '<previous-passwd-hash>',
                      '$1@mydomain.com', 
                      NULL, 
                      NULL, 
                      NULL, 
                      '', 
                      '', 
                      NULL, 
                      'active', 
                      'en_US', 
                      NULL, 
                      NULL, 
                      '2014-10-18 17:12:40', 
                      '2014-10-18 12:55:44', 
                      '1', 
                      '2014-10-18 17:12:40', 
                      '1');
  insert into roles_users (role_id, user_id) 
                   values ('3', '$MOD_ID');
EOF  

This script takes a single argument, the name of the new user you want in the Ghost site. I wouldn't recommend using spaces and, in reality, any singlely-identifiable word will suffice because you'll need to edit that information later anyway.

Explanation

So to explain what exactly is going on here its broken down into, essentially, three parts. First, I'm assuming you already have access to your Ghost blog database because, in the end, you're going to need a previous password hash (maybe not?) as a starting point. To get that I just did:

use ${GHOST_DB_PROD_TABLE}  
select password from users limit 1;  

Now, since there was only one user in the database already (myself) I knew what this password was. You'll need to know the same. Honestly, you might be able to get away with putting whatever you want in this column and skipping this step (reset the password in the browser), but I never tried. If anyone tries I'd love to hear about it!

Next, to generate the correct id for our user, we issue a count command in the database, parse it with bash, and increment by one. This will give us (ideally) the next available id for our new user. We'll need this because that id is used in two different tables.

Once we have the correct id for the new user we just populate all the fields necessary with either previous or NULL elements from the initial user and issue the INSERT statement into the users table. Like I said above though, we'll also need to INSERT into the roles_users table as well to ensure we don't completely unmangle the database (don't worry I've tested that part!). The line:

insert into roles_users (role_id, user_id)  
                 values ('3', '$MOD_ID');

is what does this for us. The '3' here represents the default author role that Ghost will assign to a new user.

Congratulations!

Once this is done you'll be able to head back into the Ghost Admin UI, refresh the users page, and you should see your new user up and ready. From there you can edit all the previous information we injected and you should be off to the races!

Congrats on adding a user without ever having to send out that pesky invite email!

Happy ghosting =)

comments powered by Disqus