]> git.openstreetmap.org Git - rails.git/commitdiff
Merge pull request #4125 from tomhughes/oauth-scopes
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 2 Aug 2023 14:52:41 +0000 (15:52 +0100)
committerGitHub <noreply@github.com>
Wed, 2 Aug 2023 14:52:41 +0000 (15:52 +0100)
Only show granted permissions in the authorized application list

app/models/oauth2_application.rb
app/views/oauth2_authorized_applications/_application.html.erb
test/controllers/oauth2_authorized_applications_controller_test.rb

index 16576152026328a3c750c43b62eb172ee358d7d6..73a02417da5223802459d4e75c50f1eb2206f92c 100644 (file)
@@ -3,6 +3,10 @@ class Oauth2Application < Doorkeeper::Application
 
   validate :allowed_scopes
 
+  def authorized_scopes_for(user)
+    authorized_tokens.where(:resource_owner_id => user).sum(Doorkeeper::OAuth::Scopes.new, &:scopes)
+  end
+
   private
 
   def allowed_scopes
index 7cb03de2f13022922cbd79f663370d49dc742c48..8abbb26ed8bb46e83e7830d7d2c97f3f979a96b8 100644 (file)
@@ -4,7 +4,7 @@
   </td>
   <td class="align-middle">
     <ul class="list-unstyled mb-0">
-      <% application.scopes.each do |scope| -%>
+      <% application.authorized_scopes_for(current_user).each do |scope| -%>
         <li><%= t "oauth.scopes.#{scope}" %></li>
       <% end -%>
     </ul>
index 347d3e40e4a27049c00ed9e2f49a1e212f1a4bda..c01f7d6f325c55e8a2bbd33d09ce29cb03163911 100644 (file)
@@ -36,6 +36,32 @@ class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTe
     assert_select "tbody tr", 2
   end
 
+  def test_index_scopes
+    user = create(:user)
+    application1 = create(:oauth_application, :scopes => %w[read_prefs write_prefs write_diary read_gpx write_gpx])
+    create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_prefs])
+    create(:oauth_access_token, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_prefs])
+    create(:oauth_access_grant, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_diary])
+    create(:oauth_access_token, :resource_owner_id => user.id, :application => application1, :scopes => %w[read_prefs write_diary])
+
+    get oauth_authorized_applications_path
+    assert_response :redirect
+    assert_redirected_to login_path(:referer => oauth_authorized_applications_path)
+
+    session_for(user)
+
+    get oauth_authorized_applications_path
+    assert_response :success
+    assert_template "oauth2_authorized_applications/index"
+    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, comments and make friends"
+    end
+  end
+
   def test_destroy
     user = create(:user)
     application1 = create(:oauth_application)