diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-09-12 10:51:34 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 22:49:41 +0200 |
commit | 5998157618eafb69f6980f41639a7b485fe2467f (patch) | |
tree | 1c3235903eb02e89a41f61e4695c19e3cf712de1 | |
parent | 6388b8feec28b0f0ca2f9e6d9c5c7b4e404fed2f (diff) | |
download | gitlab-ce-5998157618eafb69f6980f41639a7b485fe2467f.tar.gz |
Include `can_leave` for a group
-rw-r--r-- | app/serializers/group_child_entity.rb | 10 | ||||
-rw-r--r-- | spec/serializers/group_child_entity_spec.rb | 6 |
2 files changed, 15 insertions, 1 deletions
diff --git a/app/serializers/group_child_entity.rb b/app/serializers/group_child_entity.rb index ab0d8a32312..1940bc07eab 100644 --- a/app/serializers/group_child_entity.rb +++ b/app/serializers/group_child_entity.rb @@ -43,7 +43,7 @@ class GroupChildEntity < Grape::Entity # Group only attributes expose :children_count, :leave_path, :parent_id, :number_projects_with_delimiter, - :number_users_with_delimiter, :project_count, :subgroup_count, + :number_users_with_delimiter, :project_count, :subgroup_count, :can_leave, unless: lambda { |_instance, _options| project? } def children_finder @@ -66,6 +66,14 @@ class GroupChildEntity < Grape::Entity leave_group_group_members_path(object) end + def can_leave + if membership = object.members_and_requesters.find_by(user: request.current_user) + can?(request.current_user, :destroy_group_member, membership) + else + false + end + end + def number_projects_with_delimiter number_with_delimiter(project_count) end diff --git a/spec/serializers/group_child_entity_spec.rb b/spec/serializers/group_child_entity_spec.rb index 3a7d2205f53..9ddaf8f6469 100644 --- a/spec/serializers/group_child_entity_spec.rb +++ b/spec/serializers/group_child_entity_spec.rb @@ -83,6 +83,12 @@ describe GroupChildEntity do end end + it 'allows an owner to leave when there is another one' do + object.add_owner(create(:user)) + + expect(json[:can_leave]).to be_truthy + end + it_behaves_like 'group child json' end end |