Account.sole → Current.account

and some other de-tenant changes, including removing the controller
tenanting concerns
This commit is contained in:
Mike Dalessio
2025-11-10 07:58:40 -05:00
parent 6705b5225f
commit d41d50d52b
40 changed files with 67 additions and 96 deletions
+3 -2
View File
@@ -10,8 +10,9 @@ module ApplicationCable
private
def set_current_user
if session = find_session_by_cookie
membership = session.identity.memberships.find_by!(tenant: current_tenant)
self.current_user = membership.user if membership.user.active?
# # TODO:PLANB: not sure how to fix this
# membership = session.identity.memberships.find_by!(tenant: current_tenant)
# self.current_user = membership.user if membership.user.active?
end
end
@@ -2,7 +2,7 @@ class Account::EntropiesController < ApplicationController
before_action :ensure_admin
def update
Account.sole.entropy.update!(entropy_params)
Current.account.entropy.update!(entropy_params)
redirect_to account_settings_path, notice: "Account updated"
end
@@ -13,7 +13,7 @@ class Account::SettingsController < ApplicationController
private
def set_account
@account = Account.sole
@account = Current.account
end
def account_params
+1 -20
View File
@@ -2,8 +2,6 @@ module Authentication
extend ActiveSupport::Concern
included do
# Checking for tenant must happen first so we redirect before trying to access the db.
before_action :require_tenant
prepend_before_action :clear_old_scoped_session_cookies
before_action :require_authentication
@@ -25,11 +23,6 @@ module Authentication
before_action :resume_session, **options
allow_unauthorized_access **options
end
def require_untenanted_access(**options)
skip_before_action :require_tenant, **options
before_action :redirect_tenanted_request, **options
end
end
private
@@ -37,12 +30,6 @@ module Authentication
Current.session.present?
end
def require_tenant
if ApplicationRecord.current_tenant.blank?
redirect_to session_menu_url(script_name: nil)
end
end
def require_authentication
resume_session || request_authentication
end
@@ -65,9 +52,7 @@ module Authentication
end
def request_authentication
if ApplicationRecord.current_tenant.present?
session[:return_to_after_authenticating] = request.url
end
session[:return_to_after_authenticating] = request.url
redirect_to_login_url
end
@@ -80,10 +65,6 @@ module Authentication
redirect_to root_url if authenticated?
end
def redirect_tenanted_request
redirect_to root_url if ApplicationRecord.current_tenant
end
def start_new_session_for(identity)
identity.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
set_current_session session
+1 -1
View File
@@ -2,7 +2,7 @@ module Authorization
extend ActiveSupport::Concern
included do
before_action :ensure_can_access_account, if: -> { ApplicationRecord.current_tenant && authenticated? }
before_action :ensure_can_access_account, if: -> { authenticated? }
end
class_methods do
+3 -3
View File
@@ -1,5 +1,4 @@
class JoinCodesController < ApplicationController
require_untenanted_access
allow_unauthenticated_access
before_action :set_join_code
before_action :ensure_join_code_is_valid
@@ -7,7 +6,7 @@ class JoinCodesController < ApplicationController
layout "public"
def new
@account_name = ApplicationRecord.with_tenant(tenant) { Account.sole.name }
@account_name = Current.account.name
end
def create
@@ -30,7 +29,8 @@ class JoinCodesController < ApplicationController
end
def set_join_code
@join_code ||= ApplicationRecord.with_tenant(tenant) { Account::JoinCode.active.find_by(code: code) }
# TODO:PLANB: this find should be scoped by account
@join_code ||= Account::JoinCode.active.find_by(code: code)
end
def tenant
@@ -1,5 +1,4 @@
class Memberships::EmailAddresses::ConfirmationsController < ApplicationController
require_untenanted_access
allow_unauthenticated_access
before_action :set_membership
@@ -1,6 +1,4 @@
class Memberships::EmailAddressesController < ApplicationController
require_untenanted_access
layout "public"
before_action :set_membership
@@ -1,5 +1,4 @@
class Memberships::UnlinkController < ApplicationController
require_untenanted_access
before_action :set_membership
def show
-1
View File
@@ -1,5 +1,4 @@
class PwaController < ApplicationController
require_untenanted_access
skip_forgery_protection
# We need a stable URL at the root, so we can't use the regular asset path here.
@@ -1,5 +1,4 @@
class Sessions::MagicLinksController < ApplicationController
require_untenanted_access
require_unauthenticated_access
rate_limit to: 10, within: 15.minutes, only: :create, with: -> { redirect_to session_magic_link_path, alert: "Try again in 15 minutes." }
@@ -1,6 +1,4 @@
class Sessions::MenusController < ApplicationController
require_untenanted_access
before_action(if: :render_as_menu_section?) { request.variant = :menu_section }
layout "public"
@@ -1,5 +1,4 @@
class Sessions::TransfersController < ApplicationController
require_untenanted_access
require_unauthenticated_access
def show
-1
View File
@@ -4,7 +4,6 @@ class SessionsController < ApplicationController
SIGNUP_PASSWORD = Rails.env.local? ? "testpassword" : Rails.application.credentials.account_signup_http_basic_auth.password
http_basic_authenticate_with name: SIGNUP_USERNAME, password: SIGNUP_PASSWORD, realm: "Fizzy Signup", only: :create, unless: -> { Identity.exists?(email_address: email_address) }
require_untenanted_access
require_unauthenticated_access except: :destroy
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: "Try again later." }
+2 -2
View File
@@ -1,7 +1,7 @@
module ApplicationHelper
def page_title_tag
account_name = if ApplicationRecord.current_tenant && Current.session&.identity&.memberships&.many?
Account.sole&.name
account_name = if Current.account && Current.session&.identity&.memberships&.many?
Current.account&.name
end
tag.title [ @page_title, account_name, "Fizzy" ].compact.join(" | ")
end
+2 -2
View File
@@ -7,8 +7,8 @@ class ApplicationMailer < ActionMailer::Base
private
def default_url_options
if ApplicationRecord.current_tenant
super.merge(script_name: Account.sole.slug)
if Current.account
super.merge(script_name: Current.account.slug)
else
super
end
+1 -1
View File
@@ -7,7 +7,7 @@ module Board::Entropic
end
def entropy
super || Account.sole.entropy
super || Current.account.entropy
end
def auto_postpone_period=(new_value)
+3 -3
View File
@@ -6,14 +6,14 @@ module Card::Entropic
active
.left_outer_joins(board: :entropy)
.where("last_active_at <= DATETIME('now', '-' || COALESCE(entropies.auto_postpone_period, ?) || ' seconds')",
Account.sole.entropy.auto_postpone_period)
Current.account.entropy.auto_postpone_period)
end
scope :postponing_soon, -> do
active
.left_outer_joins(board: :entropy)
.where("last_active_at > DATETIME('now', '-' || COALESCE(entropies.auto_postpone_period, ?) || ' seconds')", Account.sole.entropy.auto_postpone_period)
.where("last_active_at <= DATETIME('now', '-' || CAST(COALESCE(entropies.auto_postpone_period, ?) * 0.75 AS INTEGER) || ' seconds')", Account.sole.entropy.auto_postpone_period)
.where("last_active_at > DATETIME('now', '-' || COALESCE(entropies.auto_postpone_period, ?) || ' seconds')", Current.account.entropy.auto_postpone_period)
.where("last_active_at <= DATETIME('now', '-' || CAST(COALESCE(entropies.auto_postpone_period, ?) * 0.75 AS INTEGER) || ' seconds')", Current.account.entropy.auto_postpone_period)
end
delegate :auto_postpone_period, to: :board
+1 -1
View File
@@ -24,7 +24,7 @@ module Card::Promptable
* Board id: #{board_id}
* Board name: #{board.name}
* Number of comments: #{comments.count}
* Path: #{card_path(self, script_name: Account.sole.slug)}
* Path: #{card_path(self, script_name: Current.account.slug)}
END OF CARD #{id}
PROMPT
+1 -1
View File
@@ -20,7 +20,7 @@ module Comment::Promptable
* Card title: #{card.title}
* Created by: #{creator.name}}
* Created at: #{created_at}}
* Path: #{card_path(card, anchor: ActionView::RecordIdentifier.dom_id(self), script_name: Account.sole.slug)}
* Path: #{card_path(card, anchor: ActionView::RecordIdentifier.dom_id(self), script_name: Current.account.slug)}
END OF COMMENT #{id}
PROMPT
end
+5 -4
View File
@@ -1,5 +1,5 @@
class Current < ActiveSupport::CurrentAttributes
attribute :session, :membership
attribute :session, :membership, :account
attribute :http_method, :request_id, :user_agent, :ip_address, :referrer
delegate :identity, to: :session, allow_nil: true
@@ -8,8 +8,9 @@ class Current < ActiveSupport::CurrentAttributes
def session=(value)
super(value)
unless value.nil?
self.membership = identity.memberships.find_by(tenant: ApplicationRecord.current_tenant)
end
# # TODO:PLANB: not sure how to patch this up right now
# unless value.nil?
# self.membership = identity.memberships.find_by(tenant: ApplicationRecord.current_tenant)
# end
end
end
+3 -6
View File
@@ -16,14 +16,11 @@ class Membership < ApplicationRecord
end
def account_name
ApplicationRecord.with_tenant(tenant) { Account.sole.name }
rescue ActiveRecord::Tenanted::TenantDoesNotExistError, ActiveRecord::RecordNotFound
nil
Current.account.name
end
def user
ApplicationRecord.with_tenant(tenant) { User.find_by(membership_id: id) }
rescue ActiveRecord::Tenanted::TenantDoesNotExistError
nil
# TODO:PLANB: should this find should be scoped by account?
User.find_by(membership_id: id)
end
end
+3 -3
View File
@@ -88,7 +88,7 @@ class NotificationPusher
{
title: "New notification",
body: "You have a new notification",
path: "#{Account.sole.slug}#{notifications_path}"
path: "#{Current.account.slug}#{notifications_path}"
}
end
@@ -110,10 +110,10 @@ class NotificationPusher
end
def card_path(card)
"#{Account.sole.slug}#{Rails.application.routes.url_helpers.card_path(card)}"
"#{Current.account.slug}#{Rails.application.routes.url_helpers.card_path(card)}"
end
def card_path_with_comment_anchor(comment)
"#{Account.sole.slug}#{Rails.application.routes.url_helpers.card_path(comment.card, anchor: ActionView::RecordIdentifier.dom_id(comment))}"
"#{Current.account.slug}#{Rails.application.routes.url_helpers.card_path(comment.card, anchor: ActionView::RecordIdentifier.dom_id(comment))}"
end
end
+1 -1
View File
@@ -23,7 +23,7 @@
<%= yield :head %>
<% if ApplicationRecord.current_tenant %>
<% if Current.account %>
<link rel="manifest" href="<%= pwa_manifest_path(format: :json) %>">
<% end %>
<link rel="icon" href="/favicon.png" type="image/png">
+1 -1
View File
@@ -61,7 +61,7 @@
<% end %>
<% end %>
<%= turbo_frame_tag Current.identity, :account_menu, src: session_menu_url(script_name: nil, menu_section: true, without: ApplicationRecord.current_tenant) %>
<%= turbo_frame_tag Current.identity, :account_menu, src: session_menu_url(script_name: nil, menu_section: true, without: Current.account.id) %>
<% end %>
<footer class="nav__footer">
+2 -2
View File
@@ -1,12 +1,12 @@
<% @page_title = @board.name %>
<% content_for :head do %>
<%= tag.meta property: "og:title", content: "#{@board.name} | #{Account.sole.name}" %>
<%= tag.meta property: "og:title", content: "#{@board.name} | #{Current.account.name}" %>
<%= tag.meta property: "og:description", content: format_excerpt(@board&.public_description, length: 200) %>
<%= tag.meta property: "og:image", content: "#{request.base_url}/app-icon.png" %>
<%= tag.meta property: "og:url", content: published_board_url(@board) %>
<%= tag.meta property: "twitter:title", content: "#{@board.name} | #{Account.sole.name}" %>
<%= tag.meta property: "twitter:title", content: "#{@board.name} | #{Current.account.name}" %>
<%= tag.meta property: "twitter:description", content: format_excerpt(@board&.public_description, length: 200) %>
<%= tag.meta property: "twitter:image", content: "#{request.base_url}/app-icon.png" %>
<%= tag.meta property: "twitter:card", content: "summary_large_image" %>
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": <%= [ "Fizzy", Account.sole.name, Rails.env.production? ? nil : Rails.env ].compact.join(" - ").to_json.html_safe %>,
"name": <%= [ "Fizzy", Current.account.name, Rails.env.production? ? nil : Rails.env ].compact.join(" - ").to_json.html_safe %>,
"icons": [
{
"src": "/app-icon-192.png",
+1 -1
View File
@@ -6,7 +6,7 @@
<h1 class="txt-x-large font-weight-black margin-none">
<%= @page_title %>
</h1>
<p class="margin-none">Just a sec while we sign you in with <%= Account.sole.name %>.</p>
<p class="margin-none">Just a sec while we sign you in with <%= Current.account.name %>.</p>
</header>
<%= form_with url: session_start_path, method: :post, data: { controller: "form auto-submit" } do |form| %>
+2 -2
View File
@@ -42,8 +42,8 @@ needs_seeding() {
return 1
fi
has_tenant=$(bin/rails runner "pp ApplicationRecord.tenants.any? { ApplicationRecord.with_tenant(_1) { Account.sole } }" 2>/dev/null)
if [ "$has_tenant" = "true" ]; then
has_data=$(bin/rails runner "pp Account.all.any?" 2>/dev/null)
if [ "$has_data" = "true" ] ; then
return 1
else
return 0
+1 -2
View File
@@ -27,9 +27,8 @@ def create_tenant(signal_account_name)
membership: membership
}
)
Current.account = account
end
ApplicationRecord.current_tenant = tenant_id
end
def find_or_create_user(full_name, email_address)
+5 -4
View File
@@ -1,11 +1,12 @@
namespace :seed do
desc "Seed customer data for a specific account (e.g: rails seed:customer ARTENANT=1234)"
task :customer, [ :tenant_id ] => "db:tenant" do |t, args|
raise "Please provide a tenant ID: rails seed:customer ARTENANT=1234" unless ApplicationRecord.current_tenant
raise "TODO:PLANB: Need to re-implement this task for untenanted context"
# raise "Please provide a tenant ID: rails seed:customer ARTENANT=1234" unless ApplicationRecord.current_tenant
account = Account.sole
Account::Seeder.new(account, User.active.first).seed!
# account = Account.sole
# Account::Seeder.new(account, User.active.first).seed!
puts "✓ Seeded account #{account.name} (tenant: #{account.id})"
# puts "✓ Seeded account #{account.name} (tenant: #{account.id})"
end
end
+1 -1
View File
@@ -83,7 +83,7 @@ Current.set(
# Step 3: Get or create join code
ApplicationRecord.with_tenant(tenant_id) do
account = Account.sole
account = Current.account
join_code = account.join_code
puts "✓ Join code ready"
+2 -2
View File
@@ -35,7 +35,7 @@ require_relative "../config/environment"
tenant = ActiveRecord::FixtureSet.identify(identifier)
ApplicationRecord.with_tenant(tenant) do |tenant|
Account.sole.destroy!
Current.account.destroy!
Account.create_with_admin_user \
account: { name: "Company #{identifier}" },
@@ -48,6 +48,6 @@ ApplicationRecord.with_tenant(tenant) do |tenant|
board.accesses.grant_to(user)
end
url = Rails.application.routes.url_helpers.root_url(Rails.application.config.action_controller.default_url_options.merge(script_name: Account.sole.slug))
url = Rails.application.routes.url_helpers.root_url(Rails.application.config.action_controller.default_url_options.merge(script_name: Current.account.slug))
puts "\n\nLogin to #{url} as #{user.email_address} / secret123456"
end
@@ -15,13 +15,13 @@ ApplicationRecord.with_each_tenant do |tenant|
# Create a new Identity
Identity.transaction do
identity = Identity.create!
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Account.sole.name)
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Current.account.name)
puts "Created identity #{identity.id} for user #{user.id} (#{user.email_address})"
end
else
# Merge this User's Membership into the existing Identity
identity = memberships.first.identity
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Account.sole.name)
user.membership = identity.memberships.create!(user_id: user.id, user_tenant: user.tenant, email_address: user.email_address, account_name: Current.account.name)
puts "Merged membership for user #{user.id} (#{user.email_address}) into identity #{identity.id}"
end
end
@@ -25,7 +25,7 @@ def fix_attachments(rich_text)
end
ApplicationRecord.with_each_tenant do |tenant|
account_id = Account.sole.queenbee_id
account_id = Current.account.queenbee_id
unless account_id
puts "Skipping URL fixup for tenant: #{tenant}"
@@ -37,7 +37,7 @@ ApplicationRecord.with_each_tenant do |tenant|
domain = domains[Rails.env] || domains["production"]
regex = %r{://\w+\.#{domain}/}
pp [ Account.sole.name, account_id, domain, regex ]
pp [ Current.account.name, account_id, domain, regex ]
puts
Card.find_each do |card|
@@ -12,7 +12,7 @@ class Account::SettingsControllerTest < ActionDispatch::IntegrationTest
test "update" do
put account_settings_path, params: { account: { name: "New Account Name" } }
assert_equal "New Account Name", Account.sole.name
assert_equal "New Account Name", Current.account.name
assert_redirected_to account_settings_path
end
@@ -1,17 +1,18 @@
class IdentityMailerPreview < ActionMailer::Preview
def email_change_confirmation
ApplicationRecord.current_tenant = "897362094"
flunk "TODO:PLANB: need to figure out how to test this without setting a tenant"
# ApplicationRecord.current_tenant = "897362094"
identity = Identity.find_by(email_address: "david@37signals.com")
membership = identity&.memberships&.find_by(tenant: ApplicationRecord.current_tenant)
# identity = Identity.find_by(email_address: "david@37signals.com")
# membership = identity&.memberships&.find_by(tenant: ApplicationRecord.current_tenant)
new_email_address = "david.new@example.com"
token = membership.send(:generate_email_address_change_token, to: new_email_address)
# new_email_address = "david.new@example.com"
# token = membership.send(:generate_email_address_change_token, to: new_email_address)
IdentityMailer.email_change_confirmation(
email_address: new_email_address,
token: token,
membership: membership
)
# IdentityMailer.email_change_confirmation(
# email_address: new_email_address,
# token: token,
# membership: membership
# )
end
end
+1 -1
View File
@@ -2,7 +2,7 @@ require "test_helper"
class Account::SedeableTest < ActiveSupport::TestCase
setup do
@account = Account.sole
@account = Current.account
end
test "setup_customer_template adds boards, cards, and comments" do
+1 -1
View File
@@ -8,7 +8,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "slug" do
account = Account.sole
account = Current.account
assert_equal "/#{ApplicationRecord.current_tenant}", account.slug
end
+1 -1
View File
@@ -8,7 +8,7 @@ class Entropy::Test < ActiveSupport::TestCase
end
test "touch cards when entropy changes for account container" do
account = Account.sole
account = Current.account
assert_changes -> { account.cards.first.updated_at } do
boards(:writebook).entropy.update!(auto_postpone_period: 15.days)