Skip to content

Allow ExperienceCS admins to create public projects v2 #551

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

Closed

Conversation

floehopper
Copy link
Contributor

No description provided.

floehopper added 12 commits May 25, 2025 09:51
I'm planning to introduce another role-related predicate method in a
subsequent commit. Extracting this method first will make that change
easier.

I'm pretty convinced the call to `#to_s` was made redundant when the
safe navigation operator was introduced in this commit [1]. However,
I suppose it's theoretically possible for the parsed JSON returned from
`HydraPublicApiClient.fetch_oauth_user` via `User.from_token` to contain
non-String values, so I'm going to leave it in place for now.

[1]: 9a1fdb1
I'm planning to make use of this to grant access to
`Api::PublicProjectsController#create` so that admin users on the
`experience-cs` website can create public Scratch projects via the
`editor-api` API.
I plan to use this to restrict creating public projects to experience-cs
admin users.
* Remove redundant `create_project_with_content`. I suspect this was
  introduced due to a misunderstanding of how `subject` & `let` blocks
  are evaluated. The relevant difference is achieved through a variation
  of the `project_hash` `let` block in the two `context` blocks.
* Move `subject`, `let` & `before` blocks into the `describe` or
  `context` blocks where they make the most sense.
* Consistently use `Hash` vs `ActionController::Parameters`. Even though
  the latter is more realistic, it seems like an unnecessary
  complication.
* Remove unneccessary call to
  `ActionController::Parameters.permit_all_parameters = true`. This was
  introduced in this commit [1] without explanation.
I'm planning to make some changes to these methods and these changes
will make that easier.
The `saved_project` wasn't really being used and it's sufficient to
assert the final state of `Project#identifier` rather than aserting that
it's changed.
When creating a public project we want the admin user to be able to
specify the identifier and apply validation rules against it. However,
prior to this change, if the supplied identifier was `nil`, the
`before_validation` callback, `Project#generate_identifier`, would kick
in and generate an identifier. Thus the presence validation on
`Project#identifier` never failed.

The new `Project#skip_identifier_generation` attribute gives us the
ability in specific contexts to prevent the identifier being generated
and thus allow the presence validation to fail.
This is more idiomatic and easier to follow.
I'm about to make a change to how this works and I want to be sure the
change won't break anything.
There's no need to explicitly set `Project#identifier` here as well as
in the `generate_identifier` `before_validation` in `Project`. While I'm
not very convinced about the use of the `before_validation` callback,
removing that would be a bigger and riskier piece of work. So instead
I'm removing it here safe and relying on the `before_validation` to do
its thing.

Note that I added feature spec coverage to check the project is assigned
a generated identifier in a previous commit.
I want to make some new behaviour conditional on the type of user. This
change will make that easier.

Note that I've used the `Identifiable#current_user` method (included
into `ApiController`) to access the user unlike _some_ places in the
`Api::ProjectsController` which use the `@current_user` instance
variable directly. The latter feels dangerous, because the instance
variable is only defined after the first time in a request that
`Identifiable#current_user` is called and the value is memoized. Thus
the places that are using the instance variable directly are relying on
an earlier call to `Identifiable#current_user` (probably in
`load_and_authorize_resource`) which feels risky and confusing. This
seems like a fairly widespread problem across the API controllers, so
I'm not going to attempt to fix it here.
@floehopper
Copy link
Contributor Author

Superseded by #552. Closing.

@floehopper floehopper closed this May 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant