From b9fe3eaf96a8902a722982b40226a9696444954e Mon Sep 17 00:00:00 2001 From: Marwin Hochfelsner <50826859+hlfan@users.noreply.github.com> Date: Wed, 19 Aug 2026 17:07:55 +0000 Subject: [PATCH] Unify OAuth scope rendering Co-authored-by: Diya Maheshwari --- app/helpers/authorization_helper.rb | 10 ++++++++++ app/views/oauth2_applications/_application.html.erb | 6 +----- app/views/oauth2_applications/_form.html.erb | 2 +- app/views/oauth2_applications/show.html.erb | 6 +----- app/views/oauth2_authorizations/new.html.erb | 6 +----- .../_application.html.erb | 6 +----- lib/oauth.rb | 4 ---- .../controllers/oauth2_applications_controller_test.rb | 1 + .../oauth2_authorized_applications_controller_test.rb | 9 ++++++--- 9 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/helpers/authorization_helper.rb b/app/helpers/authorization_helper.rb index 16b78074f..17ac1204c 100644 --- a/app/helpers/authorization_helper.rb +++ b/app/helpers/authorization_helper.rb @@ -10,9 +10,19 @@ module AuthorizationHelper html << " " html << role_icon(:classes => "bi-star-fill role-moderator", :title => t("oauth.for_roles.moderator")) end + html << " " + html << tag.code("(#{scope})", :class => "text-body-secondary") safe_join(html) end + def scopes_list(scopes) + tag.ul(:class => "list-unstyled mb-0") do + safe_join(scopes.map do |scope| + tag.li authorization_scope(scope) + end) + end + end + def role_icon(classes: "", title: "") safe_join([ tag.i(:class => ["bi fs-5 align-middle", classes], :title => title, :aria => { :hidden => "true" }), diff --git a/app/views/oauth2_applications/_application.html.erb b/app/views/oauth2_applications/_application.html.erb index 85d9f395f..42dabe1eb 100644 --- a/app/views/oauth2_applications/_application.html.erb +++ b/app/views/oauth2_applications/_application.html.erb @@ -10,11 +10,7 @@ - + <%= scopes_list(application.scopes) %> <%= link_to t(".edit"), edit_oauth_application_path(application), :class => "btn btn-outline-primary" %> diff --git a/app/views/oauth2_applications/_form.html.erb b/app/views/oauth2_applications/_form.html.erb index b612ca05d..aebbbe5f1 100644 --- a/app/views/oauth2_applications/_form.html.erb +++ b/app/views/oauth2_applications/_form.html.erb @@ -5,5 +5,5 @@ <%= f.form_group :confidential do %> <%= f.check_box :confidential %> <% end %> -<%= f.collection_check_boxes :scopes, Oauth.scopes(:privileged => current_user.administrator?), :name, :description %> +<%= f.collection_check_boxes :scopes, Oauth.scopes(:privileged => current_user.administrator?), :name, ->(scope) { authorization_scope(scope.name) } %> <%= f.primary %> diff --git a/app/views/oauth2_applications/show.html.erb b/app/views/oauth2_applications/show.html.erb index d0f6aa4e6..74cf7fba3 100644 --- a/app/views/oauth2_applications/show.html.erb +++ b/app/views/oauth2_applications/show.html.erb @@ -26,11 +26,7 @@ <%= t ".permissions" %> - + <%= scopes_list(@application.scopes) %> diff --git a/app/views/oauth2_authorizations/new.html.erb b/app/views/oauth2_authorizations/new.html.erb index 8720bf05a..0553acc34 100644 --- a/app/views/oauth2_authorizations/new.html.erb +++ b/app/views/oauth2_authorizations/new.html.erb @@ -4,11 +4,7 @@

<%= t ".introduction", :application => @pre_auth.client.name %>

- +<%= scopes_list(@pre_auth.scopes) %>
diff --git a/app/views/oauth2_authorized_applications/_application.html.erb b/app/views/oauth2_authorized_applications/_application.html.erb index cbee1b916..73d96790b 100644 --- a/app/views/oauth2_authorized_applications/_application.html.erb +++ b/app/views/oauth2_authorized_applications/_application.html.erb @@ -5,11 +5,7 @@ <%= application.name %> -
    - <% application.authorized_scopes_for(current_user).each do |scope| -%> -
  • <%= authorization_scope(scope) %>
  • - <% end -%> -
+ <%= scopes_list(application.authorized_scopes_for(current_user)) %> <%= friendly_date_ago(application.authorized_tokens.where(:resource_owner_id => current_user).maximum(:created_at)) %> diff --git a/lib/oauth.rb b/lib/oauth.rb index 28ebf7109..bb06f0450 100644 --- a/lib/oauth.rb +++ b/lib/oauth.rb @@ -15,10 +15,6 @@ module Oauth def initialize(name) @name = name end - - def description - I18n.t("oauth.scopes.#{name}") - end end def self.scopes(privileged: false) diff --git a/test/controllers/oauth2_applications_controller_test.rb b/test/controllers/oauth2_applications_controller_test.rb index 3caf50481..337e41003 100644 --- a/test/controllers/oauth2_applications_controller_test.rb +++ b/test/controllers/oauth2_applications_controller_test.rb @@ -191,6 +191,7 @@ class Oauth2ApplicationsControllerTest < ActionDispatch::IntegrationTest assert_select "input#oauth2_application_confidential", 1 Oauth.scopes.each do |scope| assert_select "input#oauth2_application_scopes_#{scope.name}", 1 + assert_select "label[for='oauth2_application_scopes_#{scope.name}'] code", :text => "(#{scope.name})" end end end diff --git a/test/controllers/oauth2_authorized_applications_controller_test.rb b/test/controllers/oauth2_authorized_applications_controller_test.rb index 6f11f851b..f8b8d58da 100644 --- a/test/controllers/oauth2_authorized_applications_controller_test.rb +++ b/test/controllers/oauth2_authorized_applications_controller_test.rb @@ -56,9 +56,12 @@ class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTe assert_select "tbody tr", 1 assert_select "tbody tr td ul" do assert_select "li", :count => 3 - assert_select "li", :text => "Read user preferences" - assert_select "li", :text => "Modify user preferences" - assert_select "li", :text => "Create diary entries and comments" + assert_select "li", :text => /^Read user preferences/ + assert_select "li", :text => /^Modify user preferences/ + assert_select "li", :text => /^Create diary entries and comments/ + assert_select "li code", :text => "(read_prefs)" + assert_select "li code", :text => "(write_prefs)" + assert_select "li code", :text => "(write_diary)" end end -- 2.47.3