From: Tom Clegg Date: Fri, 2 May 2014 20:26:37 +0000 (-0400) Subject: Add writable_by to Group API response. X-Git-Tag: 1.1.0~2596^2~11^2~20 X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/a8fd97bee8b0d194a7013dffa0c8bfb8533e669b Add writable_by to Group API response. --- diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index 4f06f054a3..70bd446ba6 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -59,6 +59,24 @@ class ArvadosModel < ActiveRecord::Base self.columns.select { |col| col.name == attr.to_s }.first end + # Return nil if current user is not allowed to see the list of + # writers. Otherwise, return a list of user_ and group_uuids with + # write permission. (If not returning nil, current_user is always in + # the list because can_manage permission is needed to see the list + # of writers.) + def writable_by + unless (owner_uuid == current_user.uuid or + current_user.is_admin or + current_user.groups_i_can(:manage).index(owner_uuid)) + return nil + end + [owner_uuid, current_user.uuid] + permissions.collect do |p| + if ['can_write', 'can_manage'].index p.name + p.tail_uuid + end + end.compact.uniq + end + # Return a query with read permissions restricted to the union of of the # permissions of the members of users_list, i.e. if something is readable by # any user in users_list, it will be readable in the query returned by this diff --git a/services/api/app/models/group.rb b/services/api/app/models/group.rb index 7391df5dde..4d7f630053 100644 --- a/services/api/app/models/group.rb +++ b/services/api/app/models/group.rb @@ -7,5 +7,6 @@ class Group < ArvadosModel t.add :name t.add :group_class t.add :description + t.add :writable_by end end diff --git a/services/api/test/functional/arvados/v1/groups_controller_test.rb b/services/api/test/functional/arvados/v1/groups_controller_test.rb index e7f03718a4..f8f9eaeb0d 100644 --- a/services/api/test/functional/arvados/v1/groups_controller_test.rb +++ b/services/api/test/functional/arvados/v1/groups_controller_test.rb @@ -206,4 +206,41 @@ class Arvados::V1::GroupsControllerTest < ActionController::TestCase end end + test 'get writable_by list for owned group' do + authorize_with :active + get :show, { + id: groups(:afolder).uuid, + format: :json + } + assert_response :success + assert_not_nil(json_response['writable_by'], + "Should receive uuid list in 'writable_by' field") + assert_includes(json_response['writable_by'], users(:active).uuid, + "owner should be included in writable_by list") + end + + test 'no writable_by list for group with read-only access' do + authorize_with :rominiadmin + get :show, { + id: groups(:testusergroup_admins).uuid, + format: :json + } + assert_response :success + assert_nil(json_response['writable_by'], + "Should not receive uuid list in 'writable_by' field") + end + + test 'get writable_by list by admin user' do + authorize_with :admin + get :show, { + id: groups(:testusergroup_admins).uuid, + format: :json + } + assert_response :success + assert_not_nil(json_response['writable_by'], + "Should receive uuid list in 'writable_by' field") + assert_includes(json_response['writable_by'], + users(:admin).uuid, + "Current user should be included in 'writable_by' field") + end end