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

Add pundit to owners controller #4744

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

martinemde
Copy link
Member

Slightly changes behavior: You are now expected to verify before you will be forbidden. I think this makes sense because you shouldn't be able to find out what an account has access to without having passed authentication.

This is an example of pundit-ification of our controllers. I'd like to hear feedback on how this looks so we can decide on the style.

Copy link

codecov bot commented May 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.06%. Comparing base (5f9740e) to head (f921c72).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4744      +/-   ##
==========================================
+ Coverage   97.00%   97.06%   +0.05%     
==========================================
  Files         397      399       +2     
  Lines        8428     8452      +24     
==========================================
+ Hits         8176     8204      +28     
+ Misses        252      248       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member Author

@martinemde martinemde left a comment

Choose a reason for hiding this comment

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

A few questions...

app/policies/ownership_policy.rb Outdated Show resolved Hide resolved
test/test_helper.rb Show resolved Hide resolved
Base automatically changed from martinemde/reorganize-policies to master May 26, 2024 21:35
@martinemde martinemde added this to the Refactor Authorization milestone May 29, 2024
@martinemde martinemde force-pushed the martinemde/owner-controller-policy branch 3 times, most recently from 4561812 to f921c72 Compare June 13, 2024 04:20
@@ -3,7 +3,7 @@
rubygem
user
confirmed_at { Time.current }
authorizer { user }
authorizer { association :user }
Copy link
Member Author

Choose a reason for hiding this comment

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

This makes it so the user and authorizer are different.

test/policies/ownership_policy_test.rb Outdated Show resolved Hide resolved
class OwnershipPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.confirmed.or(scope.where(rubygem: user.rubygems)).or(scope.where(user: user))
Copy link
Member

Choose a reason for hiding this comment

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

This could easily be potentially impactful on performance if someone owns a lot of gems. I'm wondering what benefit applying a scope here really gives us?

Copy link
Member

Choose a reason for hiding this comment

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

@martinemde can you post what query this resolves to? I think it should collapse to a single left join on an index, so that should be OK

Copy link
Member Author

Choose a reason for hiding this comment

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

For the curious, even though I removed it:

SELECT "ownerships".* FROM "ownerships"
WHERE ("ownerships"."confirmed_at" IS NOT NULL OR "ownerships"."rubygem_id" IN (
  SELECT "rubygems"."id"
  FROM "rubygems"
  INNER JOIN "ownerships" ON "rubygems"."id" = "ownerships"."rubygem_id"
  WHERE "ownerships"."user_id" = $1 AND "ownerships"."confirmed_at" IS NOT NULL
) OR "ownerships"."user_id" = $2)

If we use scopes, we'll probably need to be more careful. That could be optimized at the very least to use rubygem_id from the ownerships table instead of loading the association.

class OwnershipPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.confirmed.or(scope.where(rubygem: user.rubygems)).or(scope.where(user: user))
Copy link
Member

Choose a reason for hiding this comment

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

@martinemde can you post what query this resolves to? I think it should collapse to a single left join on an index, so that should be OK

class RubygemPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.with_versions
Copy link
Member

Choose a reason for hiding this comment

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

See e.g. https://rubygems.org/gems/hpriocot. We do allow showing gems without versions, so I am not sure this is correct.

@martinemde
Copy link
Member Author

@segiddins @colby-swandale I removed the scope tests/features because scopes are unused right now. Better to just write it correctly when we do need it.

@martinemde martinemde force-pushed the martinemde/owner-controller-policy branch 2 times, most recently from 528f081 to d276a3e Compare June 13, 2024 15:52
@martinemde martinemde force-pushed the martinemde/owner-controller-policy branch from d276a3e to a37264b Compare June 13, 2024 15:52
@martinemde
Copy link
Member Author

Merging since I addressed all the comments. We can follow up if there's something else that needs a look.

@martinemde martinemde enabled auto-merge (squash) June 13, 2024 15:52
@martinemde martinemde merged commit 7b16ec2 into master Jun 13, 2024
15 checks passed
@martinemde martinemde deleted the martinemde/owner-controller-policy branch June 13, 2024 15:59
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