-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organisation ownership transfer (#152)
* migration - AddOwnerToOrganizations * associations * transfer ownership model * fix existing tests * set owner on creating org * basic controller to transfer ownership * authorize_organization_owner! * link to transfer * fix transfer method * lint * prep for adding tests * Update transfer.rb * styling transfer views * try to fix net-pop gem uninstall net-pop bundle update net-pop * Update transfer_test.rb * Update transfers_controller_test.rb * transfer form - frontend validation
- Loading branch information
Showing
18 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,7 @@ GEM | |
date | ||
net-protocol | ||
net-pop (0.1.2) | ||
net-protocol | ||
net-protocol (0.2.2) | ||
timeout | ||
net-scp (4.0.0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Organizations::TransfersController < Organizations::BaseController | ||
before_action :authorize_organization_owner! | ||
|
||
def show | ||
end | ||
|
||
def update | ||
if @organization.transfer_ownership(params[:user_id]) | ||
redirect_to @organization, notice: "Ownership transferred successfully." | ||
else | ||
render :show, status: :unprocessable_entity | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Organization::Transfer | ||
extend ActiveSupport::Concern | ||
|
||
def transfer_ownership(user_id) | ||
# previous_owner = owner | ||
|
||
membership = memberships.find_by(user_id: user_id) | ||
return false unless membership | ||
|
||
new_owner = membership.user | ||
|
||
ApplicationRecord.transaction do | ||
membership.update!(role: Membership.roles[:admin]) | ||
update!(owner: new_owner) | ||
end | ||
rescue => e | ||
Rails.logger.error("Ownership transfer failed: #{e.message}") | ||
false | ||
end | ||
|
||
def owner?(user) | ||
owner_id == user.id | ||
end | ||
|
||
def can_transfer?(user) | ||
owner?(user) && users.size >= 2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<%= form_with url: organization_transfer_path(current_organization), method: :patch, class: "contents space-y-4" do |form| %> | ||
<div class=""> | ||
<%= form.label :user_id %> | ||
<%= form.collection_select :user_id, current_organization.users.where.not(users: { id: current_user.id }), :id, :email, { include_blank: true, required: true }, { class: "form-input" } %> | ||
</div> | ||
|
||
<div class="inline"> | ||
<%#= form.button, data: { turbo_confirm: "Are you sure?" } %> | ||
<%= form.submit class: "btn btn-primary" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render PageComponent.new(title: "Transfer organization", content_container: true, full_width: false) do |component| %> | ||
<%= render "organizations/transfers/form" %> | ||
<%= link_to "Cancel", organization_path(current_organization), class: "btn btn-secondary" %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class AddOwnerToOrganizations < ActiveRecord::Migration[8.0] | ||
def change | ||
add_reference :organizations, :owner, foreign_key: { to_table: :users } | ||
|
||
Organization.all.each do |organization| | ||
user = organization.memberships.admin.first&.user | ||
organization.update(owner: user) | ||
end | ||
|
||
change_column_null :organizations, :owner_id, false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
test/controllers/organizations/transfers_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "test_helper" | ||
|
||
class Organizations::TransfersControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@user = users(:one) | ||
@organization = organizations(:one) | ||
@membership = memberships(:one) | ||
|
||
@user2 = users(:two) | ||
end | ||
|
||
test "#show" do | ||
sign_in @user | ||
|
||
get organization_transfer_path(@organization) | ||
assert_response :success | ||
end | ||
|
||
test "admin can transfer organization" do | ||
@membership2 = @organization.memberships.create!(user: @user2, role: Membership.roles[:member]) | ||
sign_in @user | ||
|
||
patch organization_transfer_path(@organization), params: { user_id: @user2.id } | ||
assert_redirected_to organization_path(@organization) | ||
assert_equal @user2, @organization.reload.owner | ||
end | ||
|
||
test "regular_user cannot transfer organization" do | ||
@membership2 = @organization.memberships.create!(user: @user2, role: Membership.roles[:member]) | ||
sign_in @user2 | ||
|
||
patch organization_transfer_path(@organization), params: { user_id: @user2.id } | ||
assert_redirected_to organization_path(@organization) | ||
assert_equal @user, @organization.reload.owner | ||
end | ||
|
||
test "transfer organization to non-member" do | ||
sign_in @user | ||
|
||
patch organization_transfer_path(@organization), params: { user_id: @user2.id } | ||
assert_response :unprocessable_entity | ||
assert_equal @user, @organization.reload.owner | ||
end | ||
|
||
test "transfer organization that user is not a member of" do | ||
sign_in @user2 | ||
|
||
patch organization_transfer_path(@organization), params: { user_id: @user2.id } | ||
assert_response :not_found | ||
assert_equal @user, @organization.reload.owner | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
one: | ||
name: Organization 1 | ||
owner: one | ||
|
||
two: | ||
name: Organization 2 | ||
owner: two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ one: | |
|
||
two: | ||
email: [email protected] | ||
|
||
unassociated: | ||
email: [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "test_helper" | ||
|
||
class OrganizationTransferTest < ActiveSupport::TestCase | ||
test "transfer_ownership" do | ||
organization = organizations(:one) | ||
user = users(:two) | ||
|
||
assert_not organization.transfer_ownership(user.id) | ||
assert_not organization.owner?(user) | ||
|
||
membership = organization.memberships.create!(user: user, role: Membership.roles[:member]) | ||
assert organization.transfer_ownership(user.id) | ||
assert organization.owner?(user) | ||
assert membership.reload.admin? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters