【问题标题】:Ruby on Rails - Active Record Associations for Multi-tenant Account and UserRuby on Rails - 多租户帐户和用户的 Active Record 关联
【发布时间】:2017-01-26 04:04:07
【问题描述】:

基本上我想通过 Account (account_id) 来确定资源的范围,所以我在我的 accounts base_controller 中创建了一个名为 current_account 的方法和助手。

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

我遇到的问题是我无法在帐户控制器中找到正确的方法来构建帐户并将 account_id 属性分配给所有者用户。我认为这可能与我同时构建所有者的方式有关。帐户所有者很重要,因为他们有权向帐户添加/邀请新用户。

谁能帮忙?

背景

  • 第一个注册的用户成为帐户所有者。
  • 然后帐户所有者可以邀请其他用户加入该帐户。
  • 我正在使用设计 gem。资源按帐户划分,因此只有与帐户关联的用户才能看到属于该帐户的记录。

帐户模型

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

【问题讨论】:

    标签: ruby-on-rails activerecord devise multi-tenant user-accounts


    【解决方案1】:

    我有一个类似的应用程序。组织的替代帐户。这是我的模型/控制器...

    组织.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
    

    当用户注册时,我会在控制器中创建组织(帐户),而不是像您正在做的那样。当新用户注册时,他们输入组织名称。是视图/设计/注册/新视图

    <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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多