From c662df0d115dee325c755bd8bf6e9fca2256d9ab Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 10 Apr 2017 13:32:16 +0200 Subject: [PATCH] Fixed issue #903 - member_ids missing in organization list API endpoint. --- app/controllers/organizations_controller.rb | 9 ++++++--- app/controllers/users_controller.rb | 2 +- app/models/organization.rb | 8 -------- app/models/organization/assets.rb | 14 +++----------- .../user_organization_controller_test.rb | 14 ++++++++++++-- 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 9ad221646..cef3e43c0 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -91,8 +91,11 @@ curl http://localhost/api/v1/organizations -v -u #{login}:#{password} }, status: :ok return end - - render json: organizations + list = [] + organizations.each { |organization| + list.push organization.attributes_with_association_ids + } + render json: list end =begin @@ -293,7 +296,7 @@ curl http://localhost/api/v1/organization/{id} -v -u #{login}:#{password} -H "Co list = [] organization_all.each { |organization| - list.push organization.attributes + list.push organization.attributes_with_association_ids } render json: list, status: :ok end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a0d00a002..ee63a51a8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -428,7 +428,7 @@ class UsersController < ApplicationController list = [] user_all.each { |user| - list.push user.attributes + list.push user.attributes_with_association_ids } render json: list, status: :ok end diff --git a/app/models/organization.rb b/app/models/organization.rb index a9a5c8747..e3fa9869b 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -15,7 +15,6 @@ class Organization < ApplicationModel load 'organization/search_index.rb' include Organization::SearchIndex - has_and_belongs_to_many :users has_many :members, class_name: 'User' validates :name, presence: true @@ -34,11 +33,4 @@ class Organization < ApplicationModel domain.downcase! end - def cache_delete - super - - # delete asset caches - key = "Organization::member_ids::#{id}" - Cache.delete(key) - end end diff --git a/app/models/organization/assets.rb b/app/models/organization/assets.rb index 385834951..86840cf20 100644 --- a/app/models/organization/assets.rb +++ b/app/models/organization/assets.rb @@ -33,22 +33,14 @@ returns data[ app_model_user ] = {} end if !data[ app_model_organization ][ id ] - local_attributes = attributes + local_attributes = attributes_with_association_ids # set temp. current attributes to assets pool to prevent # loops, will be updated with lookup attributes later data[ app_model_organization ][ id ] = local_attributes - # get organizations - key = "Organization::member_ids::#{id}" - local_member_ids = Cache.get(key) - if !local_member_ids - local_member_ids = member_ids - Cache.write(key, local_member_ids) - end - local_attributes['member_ids'] = local_member_ids - if local_member_ids - local_member_ids.each { |local_user_id| + if local_attributes['member_ids'] + local_attributes['member_ids'].each { |local_user_id| next if data[ app_model_user ][ local_user_id ] user = User.lookup(id: local_user_id) next if !user diff --git a/test/controllers/user_organization_controller_test.rb b/test/controllers/user_organization_controller_test.rb index 5ef21ed42..71159adc6 100644 --- a/test/controllers/user_organization_controller_test.rb +++ b/test/controllers/user_organization_controller_test.rb @@ -419,7 +419,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest assert_equal(result_user1['id'], result[0]['id']) assert_equal("Customer#{firstname}", result[0]['firstname']) assert_equal('Customer Last', result[0]['lastname']) - assert_not(result[0]['role_ids']) + assert(result[0]['role_ids']) assert_not(result[0]['roles']) get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", {}, @headers.merge('Authorization' => credentials) @@ -538,6 +538,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest assert_response(200) result = JSON.parse(@response.body) assert_equal(result.class, Array) + assert_equal(result[0]['member_ids'].class, Array) assert(result.length >= 3) get '/api/v1/organizations?limit=40&page=1&per_page=2', {}, @headers.merge('Authorization' => credentials) @@ -546,7 +547,9 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest assert_equal(Array, result.class) organizations = Organization.order(:id).limit(2) assert_equal(organizations[0].id, result[0]['id']) + assert_equal(organizations[0].member_ids, result[0]['member_ids']) assert_equal(organizations[1].id, result[1]['id']) + assert_equal(organizations[1].member_ids, result[1]['member_ids']) assert_equal(2, result.count) get '/api/v1/organizations?limit=40&page=2&per_page=2', {}, @headers.merge('Authorization' => credentials) @@ -555,7 +558,10 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest assert_equal(Array, result.class) organizations = Organization.order(:id).limit(4) assert_equal(organizations[2].id, result[0]['id']) + assert_equal(organizations[2].member_ids, result[0]['member_ids']) assert_equal(organizations[3].id, result[1]['id']) + assert_equal(organizations[3].member_ids, result[1]['member_ids']) + assert_equal(2, result.count) # show/:id @@ -563,12 +569,16 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest assert_response(200) result = JSON.parse(@response.body) assert_equal(result.class, Hash) + assert_equal(result['member_ids'].class, Array) + assert_not(result['members']) assert_equal(result['name'], 'Rest Org') get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials) assert_response(200) result = JSON.parse(@response.body) assert_equal(result.class, Hash) + assert_equal(result['member_ids'].class, Array) + assert_not(result['members']) assert_equal(result['name'], 'Rest Org #2') # search as agent @@ -578,7 +588,7 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest result = JSON.parse(@response.body) assert_equal(Array, result.class) assert_equal('Zammad Foundation', result[0]['name']) - assert_not(result[0]['member_ids']) + assert(result[0]['member_ids']) assert_not(result[0]['members']) get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", {}, @headers.merge('Authorization' => credentials)