Skip to content

Delete a organization + organization policy #368

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

Merged
merged 13 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
- add some missing "dependent: :destroy" in Organization model: fixes #…
…359, removes all organization memberships and activity when destroying an org instance

- introduce OrganizationPolicy to properly define an access control strategy for Organizations
  • Loading branch information
markets committed Jun 7, 2018
commit 1be5c192977fb0e90ce19f239d1710e4d76bdbdd
15 changes: 8 additions & 7 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class OrganizationsController < ApplicationController
before_filter :load_resource, only: [:show, :edit, :update, :destroy]
before_filter :load_resource, only: [:show, :edit, :update]

def new
@organization = Organization.new

authorize @organization
end

def index
@organizations = Organization.all
@organizations = Organization.all.page(params[:page]).per(25)
end

def show
Expand All @@ -21,6 +23,8 @@ def show
def create
@organization = Organization.new(organization_params)

authorize @organization

if @organization.save
redirect_to @organization, status: :created
else
Expand All @@ -36,11 +40,6 @@ def update
end
end

def destroy
@organization.destroy
redirect_to organizations_path, notice: "deleted"
end

def set_current
if current_user
session[:current_organization_id] = @organization.id
Expand All @@ -52,6 +51,8 @@ def set_current

def load_resource
@organization = Organization.find(params[:id])

authorize @organization
end

def organization_params
Expand Down
8 changes: 4 additions & 4 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Organization < ActiveRecord::Base
has_many :members, dependent: :destroy
has_many :users, -> { order "members.created_at DESC" }, through: :members
has_many :all_accounts, class_name: "Account", inverse_of: :organization
has_many :all_accounts, class_name: "Account", inverse_of: :organization, dependent: :destroy
has_many :all_movements, class_name: "Movement", through: :all_accounts, source: :movements
has_many :all_transfers, class_name: "Transfer", through: :all_movements, source: :transfer
has_one :account, as: :accountable
has_one :account, as: :accountable, dependent: :destroy
has_many :member_accounts, through: :members, source: :account
has_many :posts
has_many :posts, dependent: :destroy
has_many :offers
has_many :inquiries
has_many :documents, as: :documentable
has_many :documents, as: :documentable, dependent: :destroy

validates :name, presence: true, uniqueness: true

Expand Down
2 changes: 1 addition & 1 deletion app/models/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Transfer < ActiveRecord::Base

belongs_to :post
belongs_to :operator, class_name: "User"
has_many :movements
has_many :movements, dependent: :destroy

validate :different_source_and_destination

Expand Down
19 changes: 19 additions & 0 deletions app/policies/organization_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class OrganizationPolicy < ApplicationPolicy
alias_method :organization, :record

def index?
true
end

def show?
user&.active?(organization)
end

def create?
user&.superadmin?
end

def update?
user&.admins?(organization)
end
end
10 changes: 3 additions & 7 deletions app/views/organizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@
<td><%= link_to org.name, org %></td>
<td><%= org.users.count %></td>
<td class="hover-actions">
<% if current_user.admins?(org) %>
<% if current_user&.admins?(org) %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the same question here

Copy link
Collaborator Author

@markets markets May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to previous comment, this controller doesn't require authentication at this point, so now you can visit the /organizations page without a crash.

<%= link_to edit_organization_path(org), class: 'action' do %>
<%= glyph :pencil %>
<%= t 'global.edit' %>
<% end %>
<%= link_to organization_path(org),
data: { method: :delete },
class: 'action' do %>
<%= glyph :trash %>
<%= t 'global.borrar' %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= paginate @organizations %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we showing all organizations now??

Copy link
Collaborator Author

@markets markets May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately yes, without pagination 🙊

9 changes: 0 additions & 9 deletions app/views/organizations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@
<% end %>
</li>
<% end %>
<% if superadmin? %>
<li>
<%= link_to organization_path(@organization),
data: { method: :delete, confirm: t("are_you_sure") } do %>
<%= glyph :trash %>
<%= t "global.delete" %>
<% end %>
</li>
<% end %>
<li>
<%= link_to new_transfer_path(id: @organization, destination_account_id: @organization.account.id) do %>
<%= glyph :time %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
get :give_time, on: :member
end

resources :organizations, concerns: :accountable do
resources :organizations, concerns: :accountable, except: :destroy do
member do
post :set_current
end
Expand Down
3 changes: 3 additions & 0 deletions spec/controllers/organizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
describe OrganizationsController do
describe '#show' do
let(:organization) { Fabricate(:organization) }
let(:member) { Fabricate(:member, organization: organization) }

it 'links to new_transfer_path' do
login(member.user)

get 'show', id: organization.id
expect(response.body).to include(
"<a href=\"/transfers/new?destination_account_id=#{organization.account.id}&amp;id=#{organization.id}\">"
Expand Down