Skip to content
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

Delete a organization + organization policy #368

Merged
merged 13 commits into from Sep 20, 2018
Merged

Delete a organization + organization policy #368

merged 13 commits into from Sep 20, 2018

Conversation

markets
Copy link
Collaborator

@markets markets commented May 26, 2018

Closes #359
Closes #424

We need a way to remove all Organitzation activity through active admin panel

Changes:

  • add some missing dependent: :destroy to properly delete all organization's activity and traces when destroying an instance
  • ✂️ OrganizationsController#destroy endpoint from the public part, so organizations should be removed from the admin section
  • Introduced OrganizationPolicy (logic partially extracted from current visibility of links in views, but please, let me know what do you think or we should change/tune something 👀):
    • index public
    • show public
    • new/create superadmins
    • edit/update superadmins or organization admin
  • Updated Pundit to latest release (0.3.0 => 1.1.0 => 2.0.0)
  • Paginate organizations list
  • Moar tests!

end

def show?
user&.superadmin? || user&.active?(organization)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just for me to understand, when do we have a user here and when not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pundit uses the following to setup the user variable:

def current_member
@current_member ||= current_user.as_member_of(current_organization) if current_user
end
def pundit_user
current_member
end

Sometimes there is no current_user, so we should check its presence at least in controllers (like OrganizationsController) that don't use before_action :authenticate_user!.

@@ -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.


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

Choose a reason for hiding this comment

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

IMO views specs are better suited for this. We started doing it like this and when we started feeling the pain we switched to views specs.

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.

Totally agree! view specs are a better option to test those kind of things

@markets
Copy link
Collaborator Author

markets commented May 28, 2018

@sauloperez @sseerrggii ready for review 👀

Let me know, especially, what do you think about this organization policy:

  • index is public
  • show superadmins or active members
  • new/create superadmins
  • edit/update superadmins or organization admin
  • destroy ✂️ only allowed from /admin section

@markets markets changed the title Delete a organization (closes #359) Delete a organization + organization policy May 28, 2018
<% 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 🙊

@sseerrggii
Copy link
Contributor

show is public now and it's good because you can find organizations contact information (some day we want to add a map in TimeOverflow homepage)

https://www.timeoverflow.org/organizations/3

Note: in this view when you are a member of this organitzation you can see also the account (balance, transfers) of the organization

@markets
Copy link
Collaborator Author

markets commented May 29, 2018

Ok @sseerrggii I'll change policy for show action. Thanks!

@markets markets added wip and removed wip labels May 29, 2018
@markets
Copy link
Collaborator Author

markets commented May 29, 2018

@sseerrggii @sauloperez changes done and PR description updated.

@enricostano
Copy link
Contributor

@markets testing in https://staging.timeoverflow.org I tryed to delete a org as superadmin, but I got a 500. More details here: https://gist.github.com/enricostano/f0b4701a501bc9542ce0ffaa8ada6e63#file-deleting_org_to-L51

@markets
Copy link
Collaborator Author

markets commented Jun 7, 2018

thanks @enricostano! I'll take a look this night, as per your gist, seems something related to new code in develop, I'll rebase and look and it.

Reporting exception: PG::ForeignKeyViolation: ERROR: update or delete on table "members" violates foreign key constraint "events_member_id_fkey" on table "events"
DETAIL: Key (id)=(15) is still referenced from table "events".

@enricostano
Copy link
Contributor

enricostano commented Jun 7, 2018

@markets yep, it's recent code, but it's already in your branch:
https://github.com/coopdevs/timeoverflow/blob/delete_org/db/schema.rb#L215

Sorry if the issue I detected is not related with your PR, maybe it's already broken in develop 😬

EDIT: I added more details in the issue #359 (comment)

…359, removes all organization memberships and activity when destroying an org instance

- introduce OrganizationPolicy to properly define an access control strategy for Organizations
This policy inherits from the ApplicationPolicy class this behaviour:

  def new?
    create?
  end
@markets
Copy link
Collaborator Author

markets commented Jun 7, 2018

Pushed 9418484 (pending final decision in #359) and rebased from develop.

@@ -3,6 +3,7 @@ class Member < ActiveRecord::Base
belongs_to :organization
has_one :account, as: :accountable
has_many :movements, through: :account
has_many :events, dependent: :destroy
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind to add this kind of change to the model tests also? Thanks 😍

e.g. https://github.com/coopdevs/timeoverflow/blob/develop/spec/models/member_spec.rb#L8

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👉 7f6a087

- added some missing Relations specs
- remove a couple of unused Relations
@markets
Copy link
Collaborator Author

markets commented Jun 10, 2018

@enricostano I pushed one commit with more Relation specs and also the dependent: :destroy in Event->PushNotification. Maybe the point of adding the ON DELETE CASCADE for all dependent: :destroy associations deserves a different PR including a general review about this topic?

@enricostano
Copy link
Contributor

Thanks @markets, we'll test this (and your other PRs 😬 ) ASAP, we're a bit overwhelmed right now :(

Sorry for the long wait.

@enricostano
Copy link
Contributor

@markets could you please rebase from the most recent develop and resolve conflicts please?

Thanks! And sorry again for the long wait...

@markets
Copy link
Collaborator Author

markets commented Aug 8, 2018

No prob @enricostano 👍 rebased from latest develop (and updated Pundit again, they released the v2 some days ago)!

Lot of merges today 👏 👌

@markets markets mentioned this pull request Sep 6, 2018
@enricostano
Copy link
Contributor

Deployed on https://staging.timeoverflow.org

cc/ @sseerrggii

@sseerrggii
Copy link
Contributor

Works fine for me.
Only something weird happens if you delete a organization when you are logged in into this organization. I think current organization it's stored on cookies. In this case we need to log out after organization has been deleted.

@markets
Copy link
Collaborator Author

markets commented Sep 19, 2018

good catch @sseerrggii, I'll send a patch later!

@markets
Copy link
Collaborator Author

markets commented Sep 19, 2018

@sseerrggii @enricostano fixed here 👉 14fe6bc

@sseerrggii
Copy link
Contributor

sseerrggii commented Sep 20, 2018

Hey @markets thanks for looking into it, after delete Organization that is my current organization is doing something diferent but the session still doing strange things... look, this is just after deletion.
peek 20-09-2018 10-08

I need to remove the cookie to fix it
peek 20-09-2018 10-09

@sauloperez
Copy link
Collaborator

So as deleting users and deleting orgs mostly works, I merge this and then we open a new PR to fix that case.

@sauloperez sauloperez merged commit a47597e into develop Sep 20, 2018
@sauloperez sauloperez deleted the delete_org branch September 20, 2018 08:24
@sauloperez sauloperez mentioned this pull request Sep 20, 2018
@markets
Copy link
Collaborator Author

markets commented Sep 20, 2018

@sauloperez @sseerrggii definitive fix (I hope) #432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants