Class: Organizations::MembershipsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Organizations::MembershipsController
- Defined in:
- app/controllers/organizations/memberships_controller.rb
Overview
Controller for managing organization memberships. Provides listing members, changing roles, and removing members.
Instance Method Summary collapse
-
#destroy ⇒ Object
DELETE /memberships/:id Remove a member from the current organization.
-
#index ⇒ Object
GET /memberships List all members of the current organization.
-
#leave ⇒ Object
DELETE /memberships/leave Leave the current organization (current user removes themselves) Note: No permission guard needed — users can only leave themselves, and require_organization! ensures valid org context.
-
#transfer_ownership ⇒ Object
POST /memberships/:id/transfer_ownership Transfer organization ownership to another member.
-
#update ⇒ Object
PATCH/PUT /memberships/:id Change a member's role.
Methods included from ControllerHelpers
#accept_pending_organization_invitation!, #clear_pending_organization_invitation!, #create_organization_and_switch!, #current_membership, #current_organization, #current_organization=, #handle_pending_invitation_acceptance_for, #no_organization_redirect_path, #organization_signed_in?, #pending_invitation_acceptance_redirect_path_for, #pending_organization_invitation, #pending_organization_invitation?, #pending_organization_invitation_email, #pending_organization_invitation_token, #redirect_path_after_invitation_accepted, #redirect_path_after_organization_switched, #redirect_path_when_invitation_requires_authentication, #redirect_path_when_no_organization, #redirect_to_no_organization!, #require_organization!, #require_organization_admin!, #require_organization_owner!, #require_organization_permission_to!, #require_organization_role!, #switch_to_organization!
Instance Method Details
#destroy ⇒ Object
DELETE /memberships/:id Remove a member from the current organization
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/organizations/memberships_controller.rb', line 66 def destroy # Validate the requester can make this change validate_removal! current_organization.remove_member!(@membership.user, removed_by: current_user) respond_to do |format| format.html { redirect_back fallback_location: memberships_path, notice: "Member removed successfully." } format.json { head :no_content } end rescue Organizations::Organization::CannotRemoveOwner, Organizations::Error => e respond_to do |format| format.html { redirect_back fallback_location: memberships_path, alert: e. } format.json { render json: { error: e. }, status: :unprocessable_entity } end end |
#index ⇒ Object
GET /memberships List all members of the current organization
17 18 19 20 21 22 23 24 |
# File 'app/controllers/organizations/memberships_controller.rb', line 17 def index @memberships = current_organization.memberships.includes(:user).by_role_hierarchy respond_to do |format| format.html format.json { render json: memberships_json(@memberships) } end end |
#leave ⇒ Object
DELETE /memberships/leave Leave the current organization (current user removes themselves) Note: No permission guard needed — users can only leave themselves, and require_organization! ensures valid org context. Domain rules (can't leave as last owner, etc.) are enforced by leave_organization!.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/controllers/organizations/memberships_controller.rb', line 87 def leave org_name = current_organization.name begin current_user.leave_organization!(current_organization) respond_to do |format| format.html { redirect_to organizations_path, notice: "You have left #{org_name}." } format.json { head :no_content } end rescue Models::Concerns::HasOrganizations::CannotLeaveLastOrganization, Models::Concerns::HasOrganizations::CannotLeaveAsLastOwner, Error => e respond_to do |format| format.html { redirect_back fallback_location: organizations_path, alert: e. } format.json { render json: { error: e. }, status: :unprocessable_entity } end end end |
#transfer_ownership ⇒ Object
POST /memberships/:id/transfer_ownership Transfer organization ownership to another member
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/controllers/organizations/memberships_controller.rb', line 109 def transfer_ownership new_owner = @membership.user begin current_organization.transfer_ownership_to!(new_owner) respond_to do |format| format.html { redirect_back fallback_location: memberships_path, notice: "Ownership transferred to #{new_owner.email}." } format.json { render json: { success: true, new_owner: new_owner.email } } end rescue Organizations::Organization::CannotTransferToNonMember, Organizations::Organization::CannotTransferToNonAdmin, Organizations::Error => e respond_to do |format| format.html { redirect_back fallback_location: memberships_path, alert: e. } format.json { render json: { error: e. }, status: :unprocessable_entity } end end end |
#update ⇒ Object
PATCH/PUT /memberships/:id Change a member's role
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/organizations/memberships_controller.rb', line 28 def update # Validate the requester can make this change validate_role_change! new_role = membership_params[:role].to_sym begin # Use the invariant-safe domain method that enforces owner rules current_organization.change_role_of!( @membership.user, to: new_role, changed_by: current_user ) # Reload to get updated role @membership.reload respond_to do |format| format.html { redirect_back fallback_location: memberships_path, notice: "Role updated successfully." } format.json { render json: membership_json(@membership) } end rescue Organizations::Organization::CannotHaveMultipleOwners, Organizations::Organization::CannotDemoteOwner, Organizations::Error => e respond_to do |format| format.html { redirect_back fallback_location: memberships_path, alert: e. } format.json { render json: { error: e. }, status: :unprocessable_entity } end rescue ActiveRecord::RecordInvalid => e respond_to do |format| format.html { redirect_back fallback_location: memberships_path, alert: e.record.errors..join(", ") } format.json { render json: { errors: e.record.errors }, status: :unprocessable_entity } end end end |