简体   繁体   中英

Ruby on Rails - Active Record Associations for Multi-tenant Account and User

Basically I want to scope resources by Account (account_id), so I have created a method and helper called current_account in my accounts base_controller.

I originally was using a subdomain as the unique identifier but now I want to drop using subdomain and have every user, including my account owner user, to be associated by account_id.

The issue I'm having is I can't figure out the right method in my Accounts Controller to build the account and attribute the account_id to the owner user. I think it might have something to do with the way that I'm already building a Owner at the same time. The account owner is important as they have the rights to add/invite new users to the account.

Can anyone help?

Background

  • The first user who signs up becomes the account owner.
  • Account owners can then invite other users to the account.
  • I'm using the devise gem. Resources are scoped by account so that only users linked to an account can see the records belonging to that account.

Account Model

class Account < ActiveRecord::Base
  belongs_to :owner, class_name: "User"
  accepts_nested_attributes_for :owner

  validates :subdomain, presence: true, uniqueness: true

  has_many :users    
  has_many :contacts
  has_many :invitations
end

User Model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
  belongs_to :account
end

Account Controller

def new
    @account = Account.new
    @account.build_owner
end
def create
    @user = current_user
    @account = @user.account.build_user(account_params)  
    if @account.save
        sign_in(@account.owner)
        flash[:notice] = "Your account has been created."
        redirect_to dashboard_index_path(current_user)
        # redirect_to root_url(subdomain: @account.subdomain)
    else
        flash.now[:alert] = "Sorry, your account could not be created."
        render :new 
    end
end

Schema

create_table "accounts", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "owner_id"
  t.string   "subdomain"
end

add_index "accounts", ["subdomain"], name: "index_accounts_on_subdomain"

create_table "invitations", force: true do |t|
  t.string   "email"
  t.integer  "account_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "token"
end

add_index "invitations", ["account_id"], name: "index_invitations_on_account_id"
add_index "invitations", ["token"], name: "index_invitations_on_token"

create_table "users", force: true do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "account_id"
end

add_index "users", ["account_id"], name: "index_users_on_account_id"
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

I have a similar app. Substitute account for organization. Here are my models/controllers...

organization.rb

class Organization < ApplicationRecord
    has_many :users
    has_many :tasks, through: :users
end

user.rb

class User < ApplicationRecord
    belongs_to :organization
    has_many :tasks, dependent: :destroy
  accepts_nested_attributes_for :organization
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def self.get_users(current_org)
    current_org.users
  end
end

Rather than creating the organization (account) in the controller as it looks like you are doing I do this when a user signs up. When a new user signs up they enter an organization name. Is the the views/devise/registrations/new view

<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <%= f.fields_for :organization do |org| %>
    <div><%= 'Organization or Company Name' %><br />
    <%= org.text_field :name %></div>
  <% end %>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

This first user that signs up is set as an admin user for the organization and has access to a user dashboard which allows for creation of other users within the organization.

Here is a working example of the app along with a readme.pdf in the root folder:

https://github.com/marklocklear/devise_multitenant_rails5

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM