one does not simply get admin rights

Adding admin and beta attributes to Devise User accounts in Rails

So I actually do have an application that I developed this year that my current employer is using.  It is, in a nutshell, a custom CRM for our company and industry type.  The owner of the company wants the simplest and quickest tool available for the employees to be able to check if they can call a certain company or not, and doesn’t want any of the bells or whistles that usually accompany a typical CRM system.

So I need an admin class of user so that we can control who can update the database of clients.  I’m also adding some of the more useful features to the application for my own use, but don’t want every user and the owner to see these features and get confused – so I’ll also need to create a beta attribute for certain users that will allow them to access these new features.

Since I’ve already set up the users table using the Devise Gem, I just need to add the appropriate columns to the clients table.  We’ll get started in the command line of our Cloud9 IDE:

Command Line
rails g migration AddAdminAndBetaColumnsToClients

Now we’ll add our columns in the migration rails just generated for us.  We’ll need to add a beta column and an admin column.  Since people either “are” or “aren’t” beta users and/or admins, then we’ll make these columns boolean.  Finally we’ll make the default value false so that users must manually be added to either category and cannot just sign themselves up.

def change
  add_column :users, :admin, :boolean, :default => false
  add_column :users, :beta, :boolean, :default => false

We’ll then make our changes in the database:

Command Line
$ rake db:migrate

While this sets up our development environment we still need to update our Git and Heroku repositories.
Command Line
$ git add -A
$ git commit -m "added admin and beta boolean columns to users table, default values for both is false"
$ git push
$ git push heroku master

And we’re ready to actually activate the appropriate attributes for the appropriate user accounts. Now I use Heroku to host my application, so I’ll be using the rails console through there.

Command Line
$ heroku run rails c

Now we just need to find the appropriate user accounts and update their beta and admin values to true. My account needs to have both values updated while an account for another user “tim” needs to have just his beta value updated.

Command Line
> myAccount = User.where(email: "").first
=> #>
> myAccount.admin = true
=> true
> myAccount.beta = true
=> true
=> true

We then would go through the same steps for “tim” substituting his email address for my own and then only changing beta to true before .save!.

With this now in place we can enable and disable certain actions and views depending on the user type. I’ll go through several of these methods in future posts.

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *