-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2622 from alphagov/refactor-api-users-controller
Refactor ApiUsersController
- Loading branch information
Showing
4 changed files
with
64 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,13 @@ | ||
module UserPermissionsControllerMethods | ||
private | ||
|
||
def visible_applications(user) | ||
if user.api_user? | ||
applications = Doorkeeper::Application.includes(:supported_permissions) | ||
if current_user.superadmin? | ||
api_user_authorised_apps = user.authorisations.not_revoked.pluck(:application_id) | ||
applications.where(id: api_user_authorised_apps) | ||
else | ||
applications.none | ||
end | ||
else | ||
policy_scope(:user_permission_manageable_application) | ||
end | ||
end | ||
|
||
def applications_and_permissions(user) | ||
zip_permissions(visible_applications(user).includes(:supported_permissions), user) | ||
zip_permissions(policy_scope(:user_permission_manageable_application).includes(:supported_permissions), user) | ||
end | ||
|
||
def zip_permissions(applications, user) | ||
applications.map do |application| | ||
[application, user.application_permissions.where(application_id: application.id)] | ||
end | ||
end | ||
|
||
def all_applications_and_permissions_for(user) | ||
user | ||
.supported_permissions | ||
.merge(Doorkeeper::Application.not_api_only) | ||
.includes(:application) | ||
.group_by(&:application) | ||
end | ||
end |