Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle deactivated users gracefully from a bank #3631

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ class Role < ApplicationRecord
ORG_ADMIN = :org_admin
SUPER_ADMIN = :super_admin
PARTNER = :partner
DEACTIVATED = :deactivated
end
8 changes: 5 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ class User < ApplicationRecord
# :invitable is from the devise_invitable gem
# If you change any of these options, adjust ConsolidatedLoginsController::DeviseMappingShunt accordingly
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:timeoutable
:recoverable, :rememberable, :trackable, :validatable,
:timeoutable
devise :omniauthable, omniauth_providers: [:google_oauth2]

validates :name, presence: true
before_validation
validates :email, presence: true, uniqueness: {case_sensitive: false},
format: {with: URI::MailTo::EMAIL_REGEXP, on: :create}
format: {with: URI::MailTo::EMAIL_REGEXP, on: :create}

validate :password_complexity

Expand Down Expand Up @@ -99,6 +100,7 @@ def kind
return "admin" if has_role?(Role::ORG_ADMIN, organization)
return "normal" if has_role?(Role::ORG_USER, organization)
return "partner" if has_role?(Role::PARTNER, partner)
return "deactived" if has_role?(Role::DEACTIVATED)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like the right way to model the user. We shouldn't be deliberately giving them a deactivated role. If the user has no roles, we should consider that user deactivated and treat them accordingly.

Copy link
Collaborator

@cielf cielf Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. @dorner -- Here's the thing, though. The bank can deactivate a user and reactivate them. However, there is also a case where someone may go to work for a partner after having worked for a bank (or vice versa), with possible overlap. (pls check the issue ). As things are now (deactivating the user as a user), when the bank deactivates a user, it precludes them working for a partner.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may well be a way of allowing this situation without using a deactivated role, and if we were to use a deactivated role (which, I admit, is how I thought this would be done, given that we allow reactivation, and that someone might (also) be working with a partner) , it would need to be deactivated in the context of an organization, not just deactivated, imo.

Copy link
Collaborator

@dorner dorner Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - I wasn't aware that the bank needed to be able to reactivate users. If that's the case, then yes an org-level deactivated role probably makes the most sense. However, there might be code lurking around that assumes that if you have any role with an org, you have the most basic one (which is ORG_USER) - so you might need to tweak that assumption. E.g. ensure that if that's the role the user would be given when logging in, the user is instead kicked out.

I'm just a bit concerned about the data - what if someone somehow ends up with both an ORG_USER and a DEACTIVATED role?

Maybe a better approach would be to add a "deactivated" column to the user_roles table and use that. Again, we'd have to ensure the login and permission logic would specifically exclude deactivated roles in 99% of cases. But there's less chance of weird data issues since you are activating and deactivating a specific role. We also get the benefit of (if we need to) in the future allowing any role to be activated and deactivated - e.g. partner users.

Copy link
Collaborator

@dorner dorner Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See RolifyCommunity/rolify#551 - similar idea but here we'd use deactivated instead of deleted. We'd also have to use unscoped when on the page that shows deactivated users.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I have a gut "that sounds right" reaction to the idea of adding the "deactivated" column to the user_roles table.


"normal"
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/users_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UsersRole < ApplicationRecord
# @param user [User]
# @return [Role,nil]
def self.current_role_for(user)
role_order = [Role::SUPER_ADMIN, Role::ORG_ADMIN, Role::ORG_USER, Role::PARTNER]
role_order = [Role::SUPER_ADMIN, Role::ORG_ADMIN, Role::ORG_USER, Role::PARTNER, Role::DEACTIVATED]
role_order.each do |role|
found_role = user&.roles&.find { |r| r.name.to_sym == role }
return found_role if found_role
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20230529045228_deactive_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DeactiveUsers < ActiveRecord::Migration[7.0]
def change
Users.discarded.each do |user|
user.add_role(:deactivated)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - here we'd want to just remove all roles rather than add a new one.

end
end
end
31 changes: 21 additions & 10 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
expect(build(:user)).to be_valid
end

context "discarded user from organization should be able to create a partner profile" do
let(:discarded_at) { Time.zone.now }
let(:email) { "[email protected]" }
let!(:discarded_user) { build(:user, name: "Larry", email: email, discarded_at: discarded_at) }
let!(:partner) { create(:partner, email: email) }

it "allows a previously discarded user to create a partner account" do
expect(partner).not_to be_valid
end
end

context "Validations >" do
it "requires a name" do
expect(build(:user, name: nil)).not_to be_valid
Expand All @@ -55,8 +66,8 @@

describe "Scopes >" do
describe "->alphabetized" do
let!(:z_name_user) { create(:user, name: 'Zachary') }
let!(:a_name_user) { create(:user, name: 'Amanda') }
let!(:z_name_user) { create(:user, name: "Zachary") }
let!(:a_name_user) { create(:user, name: "Amanda") }

it "retrieves users in the correct order" do
alphabetized_list = described_class.alphabetized
Expand All @@ -71,10 +82,10 @@
describe "->alphabetized" do
let(:discarded_at) { Time.zone.now }

let!(:z_name_user) { create(:user, name: 'Zachary') }
let!(:a_name_user) { create(:user, name: 'Amanda') }
let!(:deactivated_a_name_user) { create(:user, name: 'Alice', discarded_at: discarded_at) }
let!(:deactivated_z_name_user) { create(:user, name: 'Zeke', discarded_at: discarded_at) }
let!(:z_name_user) { create(:user, name: "Zachary") }
let!(:a_name_user) { create(:user, name: "Amanda") }
let!(:deactivated_a_name_user) { create(:user, name: "Alice", discarded_at: discarded_at) }
let!(:deactivated_z_name_user) { create(:user, name: "Zeke", discarded_at: discarded_at) }

it "retrieves users in the correct order" do
alphabetized_list = described_class.org_users.with_discarded.alphabetized
Expand Down Expand Up @@ -118,12 +129,12 @@
end
end

describe 'omniauth' do
it 'retrieves the user from an omniauth context' do
describe "omniauth" do
it "retrieves the user from an omniauth context" do
# can't use instance_double since AuthHash uses Hashie for dynamically created methods
token = double(OmniAuth::AuthHash, info: {'email' => '[email protected]'})
token = double(OmniAuth::AuthHash, info: {"email" => "[email protected]"})
expect(described_class.from_omniauth(token)).to eq(nil)
user = FactoryBot.create(:user, email: '[email protected]')
user = FactoryBot.create(:user, email: "[email protected]")
expect(described_class.from_omniauth(token)).to eq(user)
end
end
Expand Down