Refactored Channel classes' authorization #8054
Conversation
| private | ||
|
|
||
| def authorize_channel | ||
| @authorization_checked = true |
There was a problem hiding this comment.
Seems like you should put this after the authorize! call
There was a problem hiding this comment.
From the documentation, it seems that verify_authorized only checks that authorization occurs (Link). In the context of the controllers, it seems that it is there to make sure each child controller calls before_action {authorize!}
But since we already 'automatically' call authorization from the parent, the children don't really need to do anything. Is it safe to remove verify_authorized entirely?
There was a problem hiding this comment.
Hmm okay, you can go ahead and remove the @authorization_checked variable and verify_authorized entirely.
|
|
||
| def manage? | ||
| check?(:manage_submissions?, role) | ||
| !role.nil? && check?(:manage_submissions?, role) |
There was a problem hiding this comment.
I'm a bit surprised by this check. I thought ApplicationPolicy already has a pre_check :role_exists? that will handle this?
There was a problem hiding this comment.
I am deleting this from all policies. This is because it seems to be necessary to have this 'logic' inside the authorize_channel function, as some test cases in test_runs_channel_spec.rb test whether giving a course_id value of nil (which causes role to be nil) will stop the channel from being subscribed to.
This means I would have to go through all (relevant) policies to add this 'nil checking' so I ended up keeping the 'role nil checking' inside the authorize_channel function.
There was a problem hiding this comment.
UPDATE: However, I think I will keep the logic to check if 'assignment' is nil in the file assignment_policy.rb.
This is because if I were to put this logic inside the parent channel.rb file, not all channel actions require the assignment to not be nil, which could cause some connections to be rejected when not intended to be rejected.
|
|
||
| def manage? | ||
| check?(:manage_assessments?, role) | ||
| (!role.nil? || !record.nil?) && check?(:manage_assessments?, role) |
There was a problem hiding this comment.
See my comment below about role.nil?.
For record.nil?, can you dig deeper into how recordis defined here? There's one definition insession_handler.rb` (again, for controllers); it seems like maybe we'd need to define this in the relevant channels as well?
There was a problem hiding this comment.
For this, I ended up removing record and replacing it with assignment instead and updated the policy to declare that they need :assignment
My initial thought about using record was because originally, we checked if assignment is nil. Since we end up passing it into our authorize! function, record is representing assignment (link)
|
Hi David, took a look at some of the feedback. I wrote down some comments on some of the feedback comments. I will let you know when I update my PR with the new changes. |
… refactor/channel-classes
|
Hi David. I pushed all of my changes and addressed all of your comments. I tried my best to thoroughly explain all of my changes, but let me know if there's something in particular that needs to be addressed. Note: I will change the changelog description to be more descriptive. One question: I ended up changing the definition of I wanted to know if this is okay. My understanding of the differentiation between current/real users is to handle impersonation logins. The channel tests don't handle this kind of case. |
david-yz-liu
left a comment
There was a problem hiding this comment.
Hi @mrafie1, good progress, and I left a few more comments. The changes to the tests are fine.
| private | ||
|
|
||
| def authorize_channel | ||
| @authorization_checked = true |
There was a problem hiding this comment.
Hmm okay, you can go ahead and remove the @authorization_checked variable and verify_authorized entirely.
… refactor/channel-classes
Coverage Report for CI Build 29784276163Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage remained the same at 90.411%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
david-yz-liu
left a comment
There was a problem hiding this comment.
Nice work, @mrafie1!
Proposed Changes
(Describe your changes here. Also describe the motivation for your changes: what problem do they solve, or how do they improve the application or codebase? If this pull request fixes an open issue, use a keyword to link this pull request to the issue.)
before_subscribe :authorize_channelbefore_subscribe :verify_authorizedand customverify_authorizedmethodUPDATE JUL 16
Assignmentmethod to classes that only required itauthorize:...context to classes that only required itrecordfromassignment_policy.rband replaced it withassignmentthat is passed in as contextauthorize!app/policies/assignment_policy.rbexplicitly accepts:assignmentchannel.rb'sauthorize_channelmethod to reject if current_role is nil. See response to third comment for more information as I found this change to be a little complicatedchannel.rbdefinition ofreal_userto useconnection.real_userinstead ofconnection.current_userconnection.rbto fit the definitions insession_handler.rbreal_userwhose values are the same ascurrent_userScreenshots of your changes (if applicable)
Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)
About your comment regarding
verify_authorized:verify_authorizedIt would be wiser to implement our own
verify_authorizedmethod, which only checks if authorization occurs, and not perform authorization itself. (In the context of controllers,before_action { authorize! }automatically authorizes, andverify_authorizedchecks to see if that function is called`Questions:
channel.rbfile, what are your opinions on the methods defined such ascurrent_role,real_user, etc? I feel like there must be a better way to define variables without using many 'one-line methods'