diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb index 27c5ee548..cb6370d8e 100644 --- a/app/channels/application_cable/connection.rb +++ b/app/channels/application_cable/connection.rb @@ -9,8 +9,9 @@ module ApplicationCable private def set_current_user if session = find_session_by_cookie - membership = session.identity.memberships.find_by!(tenant: request.env["fizzy.external_account_id"]) - self.current_user = membership.user if membership.user.active? + account = Account.find(request.env["fizzy.external_account_id"]) + user = session.identity.user.find_by!(account: account) + self.current_user = user if user.active? end end diff --git a/app/controllers/concerns/authorization.rb b/app/controllers/concerns/authorization.rb index aa64155a1..9a3a8279a 100644 --- a/app/controllers/concerns/authorization.rb +++ b/app/controllers/concerns/authorization.rb @@ -26,7 +26,7 @@ module Authorization end def ensure_can_access_account - redirect_to session_menu_url(script_name: nil) if Current.membership.blank? + redirect_to session_menu_url(script_name: nil) if Current.user.blank? || !Current.user.active? end def redirect_existing_user diff --git a/app/controllers/memberships/email_addresses/confirmations_controller.rb b/app/controllers/users/email_addresses/confirmations_controller.rb similarity index 87% rename from app/controllers/memberships/email_addresses/confirmations_controller.rb rename to app/controllers/users/email_addresses/confirmations_controller.rb index 2bee5499a..5e9deb703 100644 --- a/app/controllers/memberships/email_addresses/confirmations_controller.rb +++ b/app/controllers/users/email_addresses/confirmations_controller.rb @@ -1,4 +1,4 @@ -class Memberships::EmailAddresses::ConfirmationsController < ApplicationController +class Users::EmailAddresses::ConfirmationsController < ApplicationController disallow_account_scope allow_unauthenticated_access diff --git a/app/controllers/memberships/email_addresses_controller.rb b/app/controllers/users/email_addresses_controller.rb similarity index 91% rename from app/controllers/memberships/email_addresses_controller.rb rename to app/controllers/users/email_addresses_controller.rb index 268e4ec58..f90c838b5 100644 --- a/app/controllers/memberships/email_addresses_controller.rb +++ b/app/controllers/users/email_addresses_controller.rb @@ -1,4 +1,4 @@ -class Memberships::EmailAddressesController < ApplicationController +class Users::EmailAddressesController < ApplicationController disallow_account_scope layout "public" diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0839f1ae3..cefd6fd37 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,6 @@ module ApplicationHelper def page_title_tag - account_name = if Current.account && Current.session&.identity&.memberships&.many? + account_name = if Current.account && Current.session&.identity&.users&.many? Current.account&.name end tag.title [ @page_title, account_name, "Fizzy" ].compact.join(" | ") diff --git a/app/models/current.rb b/app/models/current.rb index 9a9189d96..26f4ef189 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,15 +1,14 @@ class Current < ActiveSupport::CurrentAttributes - attribute :session, :membership, :account + attribute :session, :user, :account attribute :http_method, :request_id, :user_agent, :ip_address, :referrer delegate :identity, to: :session, allow_nil: true - delegate :user, to: :membership, allow_nil: true def session=(value) super(value) if value.present? && Current.account.present? - self.membership = identity.memberships.find_by(tenant: Current.account.external_account_id) + self.user = identity.users.find_by(account: Current.account) end end end diff --git a/app/models/identity.rb b/app/models/identity.rb index 9c00e999c..e0bb7ec0f 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -1,10 +1,12 @@ class Identity < ApplicationRecord include Joinable, Transferable - has_many :memberships, dependent: :destroy + has_many :users, dependent: :nullify has_many :magic_links, dependent: :destroy has_many :sessions, dependent: :destroy + before_destroy :deactivate_users + normalizes :email_address, with: ->(value) { value.strip.downcase.presence } def send_magic_link @@ -16,4 +18,9 @@ class Identity < ApplicationRecord def staff? email_address.ends_with?("@37signals.com") || email_address.ends_with?("@basecamp.com") end + + private + def deactivate_users + users.find_each(&:deactivate) + end end diff --git a/app/models/identity/joinable.rb b/app/models/identity/joinable.rb index 6286485f6..4a6eabcd4 100644 --- a/app/models/identity/joinable.rb +++ b/app/models/identity/joinable.rb @@ -1,14 +1,15 @@ module Identity::Joinable extend ActiveSupport::Concern - def join(account) + def join(account, **attributes) + attributes[:name] ||= email_address + transaction do - membership = memberships.create!(tenant: account.external_account_id) - account.users.create!(membership: membership, name: email_address) + account.users.create!(**attributes, identity: self) end end def member_of?(account) - memberships.exists?(tenant: account.external_account_id.to_s) + account.users.exists?(identity: self) end end diff --git a/app/models/membership.rb b/app/models/membership.rb deleted file mode 100644 index cecfa1777..000000000 --- a/app/models/membership.rb +++ /dev/null @@ -1,30 +0,0 @@ -class Membership < ApplicationRecord - include EmailAddressChangeable - - belongs_to :identity, touch: true - - class << self - def change_email_address(from:, to:, tenant:) - identity = Identity.find_by(email_address: from) - membership = find_by(tenant: tenant, identity: identity) - - if membership - new_identity = Identity.find_or_create_by!(email_address: to) - membership.update!(identity: new_identity) - end - end - end - - def account - Account.find_by_external_account_id(tenant) - end - - def account_name - account&.name - end - - def user - # TODO:PLANB: should this find should be scoped by account? - User.find_by(membership_id: id) - end -end diff --git a/app/models/user.rb b/app/models/user.rb index b940b67a0..3b541f1ac 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,9 +6,7 @@ class User < ApplicationRecord has_one_attached :avatar belongs_to :account, default: -> { Current.account } - belongs_to :membership, optional: true - - has_one :identity, through: :membership, disable_joins: true + belongs_to :identity, optional: true has_many :comments, inverse_of: :creator, dependent: :destroy @@ -22,7 +20,6 @@ class User < ApplicationRecord def deactivate transaction do accesses.destroy_all - membership.destroy! update! active: false end end diff --git a/app/models/membership/email_address_changeable.rb b/app/models/user/email_address_changeable.rb similarity index 97% rename from app/models/membership/email_address_changeable.rb rename to app/models/user/email_address_changeable.rb index 5ec2b554a..3dc5b6992 100644 --- a/app/models/membership/email_address_changeable.rb +++ b/app/models/user/email_address_changeable.rb @@ -1,4 +1,4 @@ -module Membership::EmailAddressChangeable +module User::EmailAddressChangeable EMAIL_CHANGE_TOKEN_PURPOSE = "change_email_address" EMAIL_CHANGE_TOKEN_EXPIRATION = 30.minutes diff --git a/app/views/mailers/identity_mailer/email_change_confirmation.html.erb b/app/views/mailers/identity_mailer/email_change_confirmation.html.erb index e07fdedf1..f16c866ab 100644 --- a/app/views/mailers/identity_mailer/email_change_confirmation.html.erb +++ b/app/views/mailers/identity_mailer/email_change_confirmation.html.erb @@ -1,6 +1,6 @@

Confirm your email address change

-<%= link_to "Yes use use this email address", email_address_confirmation_url(membership_id: @membership.id, email_address_token: @token), class: "btn" %> +<%= link_to "Yes use use this email address", email_address_confirmation_url(user_id: @user.id, email_address_token: @token), class: "btn" %>

If you didn’t request this change, you can ignore this email. Your email address WILL NOT be changed unless you hit the button.

diff --git a/app/views/mailers/identity_mailer/email_change_confirmation.text.erb b/app/views/mailers/identity_mailer/email_change_confirmation.text.erb index 265c9cbf2..b6ecd33c0 100644 --- a/app/views/mailers/identity_mailer/email_change_confirmation.text.erb +++ b/app/views/mailers/identity_mailer/email_change_confirmation.text.erb @@ -3,6 +3,6 @@ Confirm your email address change Hit the link below to use this email address in Fizzy: -<%= email_address_confirmation_url(membership_id: @membership.id, email_address_token: @token) %> +<%= email_address_confirmation_url(user_id: @user.id, email_address_token: @token) %> -If you didn’t request this change, you can ignore this email. Your email address WILL NOT be changed unless you hit the button. \ No newline at end of file +If you didn’t request this change, you can ignore this email. Your email address WILL NOT be changed unless you hit the button. diff --git a/app/views/mailers/magic_link_mailer/sign_in_instructions.html.erb b/app/views/mailers/magic_link_mailer/sign_in_instructions.html.erb index 8ba613348..68ddeb165 100644 --- a/app/views/mailers/magic_link_mailer/sign_in_instructions.html.erb +++ b/app/views/mailers/magic_link_mailer/sign_in_instructions.html.erb @@ -1,5 +1,5 @@ -<% if @identity.memberships.any? %> +<% if @identity.users.any? %>

Fizzy verification code

Please enter this 6-character verification code on the Fizzy sign-in page:

<% else %> diff --git a/app/views/mailers/magic_link_mailer/sign_in_instructions.text.erb b/app/views/mailers/magic_link_mailer/sign_in_instructions.text.erb index 2de3e5533..2d5ba8f8b 100644 --- a/app/views/mailers/magic_link_mailer/sign_in_instructions.text.erb +++ b/app/views/mailers/magic_link_mailer/sign_in_instructions.text.erb @@ -1,4 +1,4 @@ -<% if @identity.memberships.any? %> +<% if @identity.users.any? %> Please enter this 6-character verification code on the Fizzy sign-in page: <% else %> Please enter this 6-character verification code on the Fizzy sign-up page to create your new account: diff --git a/app/views/my/menus/_jump.html.erb b/app/views/my/menus/_jump.html.erb index 2f505b362..5e627784e 100644 --- a/app/views/my/menus/_jump.html.erb +++ b/app/views/my/menus/_jump.html.erb @@ -1,7 +1,7 @@