user1648020
user1648020

Reputation: 97

Devise: Creating Admins Users and Accounts

Using this question and answer ( Use both Account and User tables with Devise ) I have successfully set up for my app the ability for a user who signs up to create an account at the same time. Currently, I have two models: users and accounts. In the user model I have an account_id field.

I am struggling now with how I can make this user (ie the first one who creates the account) be defaulted as the admin. I have an admin field in my users model (this is for use with ActiveAdmin which has been set up to use a single Users Model).

Secondly, I know there are multiple posts to figure out how the admin user can create other users (which I am still trying to get to work with Devise), but can someone guide me on the easiest way so that the other users all are assigned the same account_id. I plan on using CanCan to control what the admin and non admins have access to in both ActiveAdmin and in the application in general.

Any asistance would be greatly appreciated.

My current models are:

Account Model

class Account < ActiveRecord::Base   
  has_many :users, :inverse_of => :account, :dependent => :destroy      
  accepts_nested_attributes_for :users   
  attr_accessible :name, :users_attributes
end

User Model

class User < ActiveRecord::Base
  belongs_to :account, :inverse_of => :users   
  validates :account, :presence => true
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

My controllers are:

Account Controller

class AccountsController < ApplicationController    
  def new     
    @accounts = Account.new     
    @accounts.users.build  
  end    
  def create     
    @account = Account.new(params[:account])     
    if @account.save       
      flash[:success] = "Account created"       
      redirect_to accounts_path     
    else       
      render 'new'     
    end   
  end  
end

User Controller

class UsersController < ApplicationController   
  before_filter :authenticate_user!   
  load_and_authorize_resource # CanCan
  def new     
    @user = User.new   
  end    
  def create         
    @user.skip_confirmation! # confirm immediately--don't require email confirmation     
    if @user.save       
      flash[:success] = "User added and activated."       
      redirect_to users_path # list of all users     
    else       
      render 'new'     
    end   
  end
end 

Upvotes: 1

Views: 2410

Answers (1)

Taryn East
Taryn East

Reputation: 27747

If all you want to do is force the first user to be an Admin, then try this:

class Account < ActiveRecord::Base
   after_create :make_first_user_an_admin

   def make_first_user_an_admin
      return true unless self.users.present?
      self.users.first.update_attribute(:admin, true)
   end
end

It'll only run once - at the time the account is first created. I'd also recommend a validation that there are some users on the account.

Upvotes: 3

Related Questions