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

Preselect default supported permissions when creating new users #2442

Merged

Conversation

chrislo
Copy link
Contributor

@chrislo chrislo commented Oct 17, 2023

Trello: https://trello.com/c/5tHKKbWq

This PR ensures that any SupportedPermission with default set to true will be preselected in the user invitation and batch user upload permissions pages. With this change we no longer grant these permissions by default automatically, allowing the inviting user to deselect some of them if they are not appropriate for the specific user.

I haven't included a migration to make the permissions listed in the trello card default. I figured we can easily do this through the UI once this change has landed (and double-check that we want the same permissions by default on both integration and production).

User Invitation

user_invitation_before

Before

After

users_invitation_after

Batch Invitation Permissions

Before

batch_permissions_before

After

batch_permissions_after

@chrislo chrislo force-pushed the preselect-default-supported-permissions-when-creating-new-users branch 2 times, most recently from 8024736 to 683ce89 Compare October 18, 2023 09:06
@chrislo chrislo marked this pull request as ready for review October 18, 2023 09:18
@chrisroos chrisroos self-assigned this Oct 18, 2023
Copy link
Contributor

@chrisroos chrisroos left a comment

Choose a reason for hiding this comment

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

This all looks good apart from the implementation of User#grant_permission which I think should work consistently irrespective of whether the user is persisted.

I've suggested another couple of changes but I'll leave it to you to decide whether to make those.

application_permissions.where(supported_permission_id: supported_permission.id).first_or_create!
else
supported_permissions << supported_permission
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there's a potential problem here because it's possible to add the same supported permission multiple times to an unsaved user. Saving that user will then fail with ActiveRecord::RecordInvalid: Validation failed: Supported permission has already been taken. I wonder if it's better to use something like:

supported_permissions << supported_permission unless supported_permissions.include?(supported_permission)

Which works in both the persisted and unpersisted case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @chrisroos - I think I've addressed this in 513e5a0. I wasn't able to simplify the implementation to just the single line you suggest as another method relies on the return value of this method in the persisted case. But hopefully this is ok?

@@ -122,6 +122,16 @@ def self.with_2sv_statuses(scope_names)
relation
end

def self.with_default_permissions
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method can be simplified as new(supported_permissions: SupportedPermission.default).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in cbf2077

user.grant_permission(supported_permission)

assert user.has_permission?(supported_permission)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be worth adding another test here, to exercise the branch of code where the user already has the supported permission (i.e. the first bit of first_or_create).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expanded the test in 62b4a86 to test this branch.

@chrislo chrislo force-pushed the preselect-default-supported-permissions-when-creating-new-users branch from 683ce89 to cbf2077 Compare October 18, 2023 11:10
Copy link
Contributor

@chrisroos chrisroos left a comment

Choose a reason for hiding this comment

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

Looks good to me, @chrislo 👍

@chrislo
Copy link
Contributor Author

chrislo commented Oct 18, 2023

Thanks @chrisroos - I'll rebase this and get it merged.

This method was previously untested and I'd like to change its
behaviour so I'm adding a test first.
We want to change the user invitation page so that some permissions
are "preselected". Since the User is not persisted before they are
invited we'll need to be able to add permissions to non-persisted
users.
This commit ensures that the default permissions defined by the
SupportedPermission.default scope are checked when invititing a new
user. These permissions are currently automatically added when
inviting a new user but that mechanism is not very clear. By
preselecting them we give the inviting user the option to configure
them or add new ones via the application permissions UI. In a
subsequent commit I'll remove the code that adds these automatically
so everything is consistent.

I've inlined the implementation of Devise::InvitationsController#new
into InvitationsController#new so that I can set `self.resource` with
`User.with_preselected_permissions` instead of `User.new`.
In the previous commit we exposed the default permissions via the user
invitation form's checkboxes. To allow the inviting user to deselect
these default permissions this commit stops granting them
automatically on user create.
This commit ensures that the default permissions defined by the
SupportedPermission.default scope are checked when adding a batch of
new users. These permissions are currently automatically added when
inviting batches of users but that mechanism is not very clear. By
preselecting them we give the inviting user the option to configure
them or add new ones via the application permissions UI. In a
subsequent commit I'll remove the code that adds these automatically
so everything is consistent.
In the previous commit we exposed the default permissions via the
batch invitation form's checkboxes. To allow the inviting user to deselect
these default permissions this commit stops granting them
automatically.
@chrislo chrislo force-pushed the preselect-default-supported-permissions-when-creating-new-users branch from cbf2077 to e330339 Compare October 18, 2023 11:36
@chrislo chrislo merged commit b425f9b into main Oct 18, 2023
6 checks passed
@chrislo chrislo deleted the preselect-default-supported-permissions-when-creating-new-users branch October 18, 2023 11:47
chrislo added a commit that referenced this pull request Oct 18, 2023
I noticed the existence of this rake task while working on #2442. As
far as I can tell from the history (6333730, 3432e0c) this task was
added when there was no other mechanism for adding users. We now have
the /users/invitation/new and /batch_invitations/new pages to create
individual and batches of users respectively. I've asked in the
`#govuk-2ndline-tech` slack channel and no-one can remember using this
task recently to add users.

I think it's safe to remove it, the UserCreator class that contains
its logic, the related tests and documentation.
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.

2 participants