Skip to content
Snippets Groups Projects

Resolve "Ability to leave company"

5 files
+ 74
18
Compare changes
  • Side-by-side
  • Inline

Files

@@ -3,14 +3,13 @@ module Companies
class RolesController < ApplicationController
# Users can only remove their own role, no permission check on role necessary
skip_authorization_check
load_resource
load_and_authorize_resource :company
load_resource :company, class: 'Company', parent: false
# Destroy the current users role
def destroy
remove_current_role
redirect_to offerer_url(@company.acting_as)
redirect_to current_user.role_student? ? dashboard_path : companies_path
end
protected
@@ -18,14 +17,25 @@ module Companies
# Removes the current users employee role, checks whether there are enough employees on the company
# before removing the role.
def remove_current_role
if @company.confirmed_involved_users.count > 1
if current_user.remove_role(:employee, @company)
flash[:success] = 'Successfully left the company'
success = false
if current_user.has_role?(:unconfirmed_employee, @company)
success = current_user.remove_role(:unconfirmed_employee, @company)
end
if current_user.has_role?(:employee, @company)
if @company.confirmed_involved_users.count > 1
success = current_user.remove_role(:employee, @company)
else
flash[:danger] = 'Failed to leave company'
flash[:danger] = 'Cannot leave company. The company must have at least one employee.'
return
end
end
if success
flash[:success] = 'Successfully left the company'
else
flash[:danger] = 'Cannot leave company. The company must have at least one employee.'
flash[:danger] = 'Failed to leave company'
Sentry.capture_message("Unable to remove role for #{current_user.id} for company #{@company.id}", level: :error)
end
end
end
Loading