Ruby 在 Rails - 多租户帐户和用户的活动记录关联

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

基本上我想按帐户 (account_id) 确定资源范围,所以我在我的帐户 base_controller.

中创建了一个名为 current_account 的方法和助手

我最初使用子域作为唯一标识符,但现在我想放弃使用子域并让每个用户(包括我的帐户所有者用户)通过 account_id 关联。

我遇到的问题是我无法在我的帐户控制器中找出正确的方法来构建帐户并将 account_id 归因于所有者用户。我认为这可能与我已经同时建立所有者的方式有关。帐户所有者很重要,因为他们有权 add/invite 新用户访问该帐户。

有人能帮忙吗?

背景

账户模型

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

用户模型

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

账户管理员

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

架构

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

我有一个类似的应用程序。组织的替代帐户。这是我的 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

我没有像您那样在控制器中创建组织(帐户),而是在用户注册时执行此操作。当新用户注册时,他们输入一个组织名称。是views/devise/registrations/new视图

<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" %>

第一个注册的用户被设置为组织的管理员用户,并有权访问允许在组织内创建其他用户的用户仪表板。

这是应用程序的工作示例以及根文件夹中的 readme.pdf:

https://github.com/marklocklear/devise_multitenant_rails5

希望对您有所帮助!