Skip to content

Commit

Permalink
Explicitly add organization for super admins (#3282)
Browse files Browse the repository at this point in the history
Co-authored-by: KaylaGallatin <[email protected]>
  • Loading branch information
KaylaGallatin and KaylaGallatin authored Dec 14, 2022
1 parent d799030 commit 6894b54
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
16 changes: 12 additions & 4 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def promote_to_org_admin
user = User.find(params[:user_id])
raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization)
user.add_role(Role::ORG_ADMIN, current_organization)
redirect_to organization_path, notice: "User has been promoted!"
redirect_to user_update_redirect_path, notice: "User has been promoted!"
end

def demote_to_user
Expand All @@ -53,21 +53,21 @@ def demote_to_user
notice = "Admin has been changed to User!"
end

redirect_to organization_path, notice: notice
redirect_to user_update_redirect_path, notice: notice
end

def deactivate_user
user = User.with_discarded.find_by!(id: params[:user_id])
raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization)
user.discard!
redirect_to organization_path, notice: "User has been deactivated."
redirect_to user_update_redirect_path, notice: "User has been deactivated."
end

def reactivate_user
user = User.with_discarded.find_by!(id: params[:user_id])
raise ActiveRecord::RecordNotFound unless user.has_role?(Role::ORG_USER, current_organization)
user.undiscard!
redirect_to organization_path, notice: "User has been reactivated."
redirect_to user_update_redirect_path, notice: "User has been reactivated."
end

private
Expand All @@ -89,4 +89,12 @@ def organization_params
partner_form_fields: []
)
end

def user_update_redirect_path
if current_user.has_role?(Role::SUPER_ADMIN)
admin_organization_path(current_organization.id)
else
organization_path
end
end
end
8 changes: 4 additions & 4 deletions app/views/users/_organization_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@
<li>
<%=
edit_button_to(
promote_to_org_admin_organization_path(user_id: user.id),
promote_to_org_admin_organization_path(current_organization, user_id: user.id),
{text: 'Make admin'},
{method: :post, rel: "nofollow", data: {confirm: 'This will promote the user to admin status. Are you sure that you want to submit this?', size: 'xs'}}
)
%>
</li>

<li>
<%= deactivate_button_to deactivate_user_organization_path(user_id: user.id),
<%= deactivate_button_to deactivate_user_organization_path(current_organization, user_id: user.id),
{id: dom_id(user), method: :post, class: 'deactivate', rel: "nofollow", data: {confirm: 'This will deactivate the user. Are you sure that you want to submit this?', size: 'xs'}}
%>
</li>
<% else %>
<%= reactivate_button_to reactivate_user_organization_path(user_id: user.id),
<%= reactivate_button_to reactivate_user_organization_path(current_organization, user_id: user.id),
{id: dom_id(user), method: :post, class: 'reactivate', rel: "nofollow", data: {confirm: 'This will reactivate the user. Are you sure that you want to submit this?', size: 'xs'}}
%>
<% end %>
</ul>
</div>
<% end %>
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) && user.has_role?(Role::ORG_ADMIN, current_organization) %>
<%= edit_button_to demote_to_user_organization_path(user_id: user.id),
<%= edit_button_to demote_to_user_organization_path(current_organization, user_id: user.id),
{text: 'Make User'},
{method: :post, rel: "nofollow", data: {confirm: 'This will demote the admin to user status. Are you sure that you want to submit this?', size: 'xs'}} unless user.id == current_user.id %>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/users/_organization_users_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<tbody>
<%= render partial: "/users/organization_user",
collection: @organization.users.with_discarded.alphabetized,
as: :user %>
as: :user,
locals: { current_organization: current_organization || @organization } %>
</tbody>
</table>
</div>
Expand Down
59 changes: 59 additions & 0 deletions spec/requests/organization_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,63 @@
end
end
end

context 'When signed in as a super admin' do
before do
sign_in(@super_admin)
end

describe "POST #promote_to_org_admin" do
subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: @user.id)) }

it "redirect after update" do
subject
expect(response).to redirect_to(admin_organization_path(@organization.id, default_params))
end
it "change user" do
expect { subject }.to change { @user.reload.kind }.to "admin"
end
end

describe "POST #demote_to_user" do
let(:admin_user) do
create(:user, organization: @organization, name: "ADMIN USER")
end
subject { post demote_to_user_organization_path(default_params.merge(user_id: admin_user.id)) }

it "redirect after update" do
subject
expect(response).to redirect_to(admin_organization_path(@organization.id, default_params))
end
it "demotes the user to user" do
subject
expect(admin_user.reload.has_role?(Role::ORG_ADMIN, admin_user.organization)).to be_falsey
end
end

describe "PUT #deactivate_user" do
subject { put deactivate_user_organization_path(default_params.merge(user_id: @user.id)) }

it "redirect after update" do
subject
expect(response).to redirect_to(admin_organization_path(@organization.id, default_params))
end
it "deactivates the user" do
expect { subject }.to change { @user.reload.discarded_at }.to be_present
end
end

describe "PUT #reactivate_user" do
subject { put reactivate_user_organization_path(default_params.merge(user_id: @user.id)) }
before { @user.discard! }

it "redirect after update" do
subject
expect(response).to redirect_to(admin_organization_path(@organization.id, default_params))
end
it "reactivates the user" do
expect { subject }.to change { @user.reload.discarded_at }.to be_nil
end
end
end
end

0 comments on commit 6894b54

Please sign in to comment.