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

(dev/core#4462) Form Builder (etal) - Define generic permissions to assist with message-tokens #31386

Merged
merged 8 commits into from
Nov 5, 2024

Conversation

totten
Copy link
Member

@totten totten commented Oct 29, 2024

Overview

This is a follow-up to #30585 (5.79.alpha) to address some of remaining issues (for 5.79.beta). It is part of https://lab.civicrm.org/dev/core/-/issues/4462. It's one off-shoot from last week's discussion with @eileenmcnaughton @ufundo @seamuslee001 about about how to improve configuration of permissions.

Suppose you are creating a custom form. You wish to link to it from mailings/messages. You must define the permissions for reading the form. But which permissions should you use? You need to think about the roles/authorizations of every person in the email list.

Before

You can theoretically use permissions like access CiviEvent or access CiviCRM, but these aren't very useful for a list of email recipients.

When mailing links to regular constituents, the only useful option is *always allow*:

Screenshot from 2024-10-29 15-41-16

That's OK sometimes, but it can also be too broad -- it means that anonymous users (who never received the email) can access it.

After

You have two more generic options:

Screenshot from 2024-10-29 15-36-00

Compare:

Permission Description
Generic: Anyone with secret link You must access form through a signed/authenticated hyperlink (as might be generated in a mail-blast). This specifically refers to Afform page-tokens.
Generic: Allow any authenticated contact You can access the form as you are somehow authenticated (e.g. login-session).
Generic: Allow all users (including anonymous) OK if you really want the form to be public

Technical Details

The patch-set looks a bit bigger than it should. This is mostly because the afform/authx test-suites needed some reorganization to develop tests for these things.

This is similar to the `*always allow*` and `*always deny*` in that it's a
generic permission based on special logic.

For example, suppose you're define a new form "About Me". It should not
be available to the anonymous public, but it should be available
to any logged-in users and/or anyone with authenticated hyperlink.
Then set the required permission to `*authenticated*`.
This shouldn't change functionality. It's just switching the short-circuit
so that we can add more branches/options in the same fcuntion.
Copy link

civibot bot commented Oct 29, 2024

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the 5.79 label Oct 29, 2024
…tion

Before:

* In `Standalone::permissionDenied()`, it prefers to do one of two things, either:
    * Show a login page, or
    * Do a status-bounce (put messages and redirect to another page, like the dashboard)
* But what if you have a user with stateless authentication? (As in some of the edge-cases from `MockPublicFormTest::testSpecialPermissions`?)
    * The login page is wrong because they're already authenticated.
    * The status-bounce is wrong because they won't get session messages.

After:

* In `Standalone::permissionDenied()`, it has an extra check -- for stateless auth, a full "Access denied" page
@totten totten added the run-extended-tests Civibot should run comprehensive test suites with versions of UF/PHP/MySQL label Oct 30, 2024
@totten
Copy link
Member Author

totten commented Oct 30, 2024

Since this touches on users/permissions, I ran the extended tests. The failures regarding wp-clean,phpunit-core-exts are pre-existing.

if (!str_starts_with($permission, '@afform:') || strlen($permission) < 9) {
// Micro-optimization - this function may get hit a lot.
return;
// Micro-optimization - this function may get hit a lot.
Copy link
Contributor

Choose a reason for hiding this comment

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

the micro optimization has gone now right? so this comment should too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I suppose that could be interpreted either way. (Does "micro-optimization" mean "returns early" or does it mean "uses peculiar string manipulation"?) I'll just trim the comment a bit.

use GuzzleHttp\Psr7\Utils;

/**
* (TEST HELPER/EXPERIMENTAL)
Copy link
Contributor

Choose a reason for hiding this comment

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

why doesn't it live in the tests directory?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's a test helper, but the helper is used by tests in two extensions (authx and afform). Classloading gets weirder if you try share code from the tests/ folder. (Slightly different, but similar idea - note how newer helpers tend to go in civicrm-core:Civi/Test/ rather than civicrm-core:tests/phpunit/CiviTest/. It's because the class-loading is clearer.)

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@ufundo
Copy link
Contributor

ufundo commented Nov 4, 2024

Have been trying to r-run this on Standalone, but it's not working for me as expected. I'm getting access denied on my form. Need to take a break now but will investigate further later.

@ufundo
Copy link
Contributor

ufundo commented Nov 4, 2024

Ah, my bad. I had AND on my form permissions:

image

I think you flagged that before as an easy mistake to make.

Having fixed that, it r-runs as expected for me.

@ufundo ufundo added the merge ready PR will be merged after a few days if there are no objections label Nov 4, 2024
@ufundo
Copy link
Contributor

ufundo commented Nov 4, 2024

Code looks good (minor question and a trailing comment).

I think it's mergeable.

@totten
Copy link
Member Author

totten commented Nov 5, 2024

Thanks @ufundo. Updated those bits.

Yes, agree about how the "Permission" UI is easy to misread (and that's a pre-existing problem).

Other test failures are pre-existing/unrelated.

@totten totten merged commit e9668c6 into civicrm:5.79 Nov 5, 2024
1 of 2 checks passed
@totten totten deleted the 5.79-authenticated-permission branch November 7, 2024 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.79 merge ready PR will be merged after a few days if there are no objections run-extended-tests Civibot should run comprehensive test suites with versions of UF/PHP/MySQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants