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

1034 replace staffaccount #1039

Merged
merged 31 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
10899f8
Remove staff account relation with organization
mononoken Oct 5, 2024
8dc6ac2
Copy deactivated_at logic from StaffAccount to User
mononoken Oct 6, 2024
6b3c1f7
Create UserPolicy in lieu of StaffAccountPolicy
mononoken Oct 6, 2024
bff2b13
Refactor verify_active_staff to remove staff_account logic
mononoken Oct 6, 2024
ca2dedd
Create UserPolicy test
mononoken Oct 6, 2024
3291265
Refactor the staff index route action logic
mononoken Oct 6, 2024
ad1df91
Remove unused routes deactivation routes
mononoken Oct 6, 2024
3f00023
Change http request type from post to put for accuracy
mononoken Oct 6, 2024
97af368
Remove useless staff_account conditional
mononoken Oct 6, 2024
42ba895
Refactor deactivation logic
mononoken Oct 6, 2024
d456d65
Refactor staff_account conditionals
mononoken Oct 6, 2024
6e635f2
Refactor organization_staff class method for role logic
mononoken Oct 6, 2024
30c6c4c
Remove staff_account logic from service
mononoken Oct 6, 2024
81bd008
Add organization to role assignment in service
mononoken Oct 6, 2024
15ca1bb
Remove more staff_account references
mononoken Oct 7, 2024
d30cb77
Drop staff_accounts
mononoken Oct 7, 2024
fcc157c
Refactor the conditionals on the adoptable pets page
mononoken Oct 7, 2024
6bdadd0
Fix localization ref
mononoken Oct 7, 2024
7685d1d
Remove optional Person from User
mononoken Oct 7, 2024
f3cf17d
Fail loudly on activation changes
mononoken Oct 15, 2024
1484d50
Fix this test implementation to actually test OrganizationPolicy
mononoken Oct 19, 2024
eccba8b
Fix incorrect usage of tenancy in test
mononoken Oct 19, 2024
06026a2
Remove unused instance variables
mononoken Oct 19, 2024
365e8a5
Remove unnecessary organization method as AdopterApp has org association
mononoken Oct 19, 2024
0a68b7f
Define organization for verify_active_staff check
mononoken Oct 19, 2024
5c4c1e3
Move staff? check to user model
mononoken Oct 19, 2024
5b7c412
Move role method to authorizable concern
mononoken Oct 19, 2024
9758b38
Add flash message for updating activation
mononoken Oct 19, 2024
b86774b
Remove old references to staff_account
mononoken Oct 19, 2024
35d0728
Loudly fail activation updates
mononoken Oct 19, 2024
f9488c3
Replace staff_account activation logic
mononoken Oct 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create
notice: t(".success", message: MessagesHelper.affirmations.sample)

# mailer
@org_staff = User.organization_staff(@pet.organization_id)
@org_staff = User.staff
mononoken marked this conversation as resolved.
Show resolved Hide resolved
StaffApplicationNotificationMailer.with(pet: @pet,
organization_staff: @org_staff)
.new_adoption_application.deliver_now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def create
user_params.merge(password: SecureRandom.hex(8)).except(:roles)
)
@user.add_role(user_params[:roles], Current.organization)
@user.build_staff_account

if @user.save
@user.invite!(current_user)
Expand Down
40 changes: 16 additions & 24 deletions app/controllers/organizations/staff/staff_controller.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
class Organizations::Staff::StaffController < Organizations::BaseController
before_action :set_staff_account, only: [:deactivate, :activate, :update_activation]
before_action :set_staff, only: [:update_activation]

layout "dashboard"

def index
authorize! StaffAccount, context: {organization: Current.organization}
authorize! User, context: {organization: Current.organization}

@staff_accounts = authorized_scope(StaffAccount.all)
@staff = authorized_scope(User.staff)
end

def deactivate
@staff_account.deactivate
respond_to do |format|
format.html { redirect_to staff_staff_index_path, notice: t(".success") }
format.turbo_stream { render "organizations/staff/staff/update" }
def update_activation
if @staff.deactivated_at
@staff.activate
kasugaijin marked this conversation as resolved.
Show resolved Hide resolved
else
@staff.deactivate
end
end

def activate
@staff_account.activate
respond_to do |format|
format.html { redirect_to staff_staff_index_path, notice: t(".success") }
format.turbo_stream { render "organizations/staff/staff/update" }
end
end

def update_activation
if @staff_account.deactivated_at
activate
else
deactivate
success = @staff.deactivated_at.nil? ?
t(".activated", staff: @staff.full_name) :
t(".deactivated", staff: @staff.full_name)
format.html { redirect_to staff_staff_index_path, notice: success }
format.turbo_stream { flash.now[:notice] = success }
end
end

private

def set_staff_account
@staff_account = StaffAccount.find(params[:staff_id])
def set_staff
@staff = User.find(params[:staff_id])

authorize! @staff_account
authorize! @staff
end
end
2 changes: 1 addition & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create
private

def set_layout
if current_user&.staff_account
if allowed_to?(:index?, with: Organizations::DashboardPolicy, context: {organization: Current.organization})
"dashboard"
elsif allowed_to?(:index?, with: Organizations::AdopterFosterDashboardPolicy, context: {organization: Current.organization})
"adopter_foster_dashboard"
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def permission?(name)
permissions.include?(name)
end

def staff?(organization)
has_role?("super_admin", organization) || has_role?("admin", organization)
end

ADOPTER_PERMISSIONS = %i[
view_adopter_foster_dashboard
create_adopter_applications
Expand Down
3 changes: 1 addition & 2 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class Organization < ApplicationRecord
# Rolify resource
resourcify

has_many :staff_accounts
has_many :users, through: :staff_accounts
has_many :users
has_many :pets
has_many :default_pet_tasks
has_many :forms, class_name: "CustomForm::Form", dependent: :destroy
Expand Down
41 changes: 0 additions & 41 deletions app/models/staff_account.rb

This file was deleted.

31 changes: 19 additions & 12 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: users
#
# id :bigint not null, primary key
# deactivated_at :datetime
mononoken marked this conversation as resolved.
Show resolved Hide resolved
# email :string default(""), not null
# encrypted_password :string default(""), not null
# first_name :string not null
Expand All @@ -22,7 +23,7 @@
# updated_at :datetime not null
# invited_by_id :bigint
# organization_id :bigint
# person_id :bigint
# person_id :bigint not null
#
# Indexes
#
Expand Down Expand Up @@ -66,22 +67,16 @@ class User < ApplicationRecord
# validates :tos_agreement, acceptance: {message: "Please accept the Terms and Conditions"},
# allow_nil: false, on: :create

has_one :staff_account, dependent: :destroy

# Once we've migrated the existing data to connect a user to a person,
# we should remove the optional: true part
belongs_to :person, optional: true
belongs_to :person

before_validation :ensure_person_exists, on: :create

before_save :downcase_email

delegate :latest_form_submission, to: :person

# get user accounts for staff in a given organization
def self.organization_staff(org_id)
User.includes(:staff_account)
.where(staff_account: {organization_id: org_id})
def self.staff
joins(:roles).where(roles: {name: %i[admin super_admin]})
end

def self.ransackable_attributes(auth_object = nil)
Expand All @@ -98,11 +93,11 @@ def custom_messages(attribute)
end

def active_for_authentication?
super && !staff_account&.deactivated_at
super && !deactivated?
end

def inactive_message
staff_account.deactivated_at ? :deactivated : super
deactivated? ? :deactivated : super
end

def ensure_person_exists
Expand Down Expand Up @@ -132,6 +127,18 @@ def name_initials
full_name.split.map { |part| part[0] }.join.upcase
end

def deactivate
mononoken marked this conversation as resolved.
Show resolved Hide resolved
update!(deactivated_at: Time.now) unless deactivated_at
end

def activate
update!(deactivated_at: nil) if deactivated_at
end

def deactivated?
!!deactivated_at
end

private

def downcase_email
Expand Down
4 changes: 2 additions & 2 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def verify_organization!
end

def verify_active_staff!
deny! unless user.staff_account
deny! if user.staff_account.deactivated?
deny! unless user.staff?(organization)
deny! if user.deactivated?
end

def permission?(name)
Expand Down
6 changes: 0 additions & 6 deletions app/policies/organizations/adopter_application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,4 @@ class Organizations::AdopterApplicationPolicy < ApplicationPolicy
def manage?
permission?(:review_adopter_applications)
end

private

def organization
@organization || record.pet.organization
end
end
6 changes: 6 additions & 0 deletions app/policies/organizations/organization_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ class OrganizationPolicy < ApplicationPolicy
def manage?
permission?(:manage_organization)
end

private

def organization
record
end
end
end
14 changes: 0 additions & 14 deletions app/policies/organizations/staff_account_policy.rb

This file was deleted.

12 changes: 12 additions & 0 deletions app/policies/organizations/user_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Organizations::UserPolicy < ApplicationPolicy
pre_check :verify_organization!
pre_check :verify_active_staff!

def index?
permission?(:manage_staff)
end

def update_activation?
permission?(:activate_staff) && record.id != user.id
end
end
18 changes: 4 additions & 14 deletions app/services/organizations/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def signal(args)
args[:user][:first_name],
args[:user][:last_name]
)
create_staff_account
add_super_admin_role_to_staff_account
add_super_admin_role_to_user
send_email
create_custom_page
end
Expand Down Expand Up @@ -63,19 +62,10 @@ def create_user(email, first_name, last_name)
end
end

def create_staff_account
ActsAsTenant.with_tenant(@organization) do
@staff_account = StaffAccount.create!(
organization_id: @organization.id,
user_id: @user.id
)
end
end

def add_super_admin_role_to_staff_account
@user.add_role(:super_admin)
def add_super_admin_role_to_user
@user.add_role(:super_admin, @organization)

if [email protected]_role?(:super_admin)
if [email protected]_role?(:super_admin, @organization)
raise StandardError, "Failed to add super admin role"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/dashboard/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<i class="nav-icon fe fe-users me-2"></i>Adopters
<% end %>
</li>
<% if allowed_to?(:index?, StaffAccount, namespace: Organizations,context: {organization: Current.organization})%>
<% if allowed_to?(:index?, User, namespace: Organizations, context: {organization: Current.organization})%>
<li class="nav-item">
<%= active_link_to staff_staff_index_path, class: "nav-link" do %>
<i class="nav-icon fe fe-users me-2"></i>Staff
Expand Down
Loading
Loading