diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index a1d8344..125d98f 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -33,4 +33,14 @@ class MembershipsController < MyProfileController end end + def destroy_community + @community = Community.find(params[:id]) + if request.post? + if @community.destroy + flash[:notice] = _('%s was destroyed!') % @community.display_name + redirect_to :action => 'index' + end + end + end + end diff --git a/app/views/memberships/destroy_community.rhtml b/app/views/memberships/destroy_community.rhtml new file mode 100644 index 0000000..689a5a5 --- /dev/null +++ b/app/views/memberships/destroy_community.rhtml @@ -0,0 +1,9 @@ +

<%= _('Destroy %s') % @community.display_name %>

+ +

<%= _('Are you sure you want to destroy %s?') % @community.display_name %>

+ +<% form_tag do %> + <%= hidden_field_tag(:confirmation, 1) %> + <%= submit_button(:ok, _("Yes, I want to destroy.") % @community.display_name) %> + <%= button(:cancel, _("No, I don't want."), :action => 'index') %> +<% end %> diff --git a/app/views/memberships/index.rhtml b/app/views/memberships/index.rhtml index 02eed7a..b3b7a76 100644 --- a/app/views/memberships/index.rhtml +++ b/app/views/memberships/index.rhtml @@ -12,13 +12,13 @@ <%= membership.display_name %>
<%= _('Role: %s') % rolename_for(profile, membership) %>
<%= _('Type: %s') % _(membership.class.name) %>
- <%= _('Description: %s') % membership.description + '
' if membership.kind_of?(Community) %> + <%= _('Description: %s') % membership.description + '
' if membership.community? %> <%= _('Members: %s') % membership.members.size.to_s %>
<%= _('Created at: %s') % show_date(membership.created_at) %>
<%= [ link_to(_('Manage'), membership.admin_url), - link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership.id }), - link_to(_('Destroy'), { :profile => profile.identifier, :controller => 'memberships', :action => 'destroy', :id => membership.id }) - ].join(', ') + link_to(_('Leave'), { :profile => profile.identifier, :controller => 'memberships', :action => 'leave', :id => membership }), + (membership.community? && user.has_permission?(:destroy_profile, membership) ? link_to(_('Destroy'), { :action => 'destroy_community', :id => membership }) : nil) + ].compact.join(', ') %> diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index ff2bb45..e752727 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -36,6 +36,7 @@ profile_admin: permissions: - edit_profile_design - moderate_comments + - destroy_profile profile_member: id: 6 key: 'profile_member' diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 0ccb9ab..9a4b76b 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -171,4 +171,48 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_tag :tag => 'a', :content => 'Register a new Enterprise' end + should 'render destroy_community template' do + community = Community.create!(:name => 'A community to destroy') + get :destroy_community, :profile => 'testuser', :id => community.id + assert_template 'destroy_community' + end + + should 'display destroy link only to communities' do + community = Community.create!(:name => 'A community to destroy') + enterprise = Enterprise.create!(:name => 'A enterprise test', :identifier => 'enterprise-test') + + person = Person['testuser'] + community.add_admin(person) + enterprise.add_admin(person) + + get :index, :profile => 'testuser' + + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" } + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{enterprise.id}" } + end + + should 'be able to destroy communities' do + community = Community.create!(:name => 'A community to destroy') + + person = Person['testuser'] + community.add_admin(person) + + assert_difference Community, :count, -1 do + post :destroy_community, :profile => 'testuser', :id => community.id + end + end + + should 'not display destroy link to normal members' do + community = Community.create!(:name => 'A community to destroy') + + person = Person['testuser'] + community.add_member(person) + + login_as('testuser') + get :index, :profile => 'testuser' + + assert_template 'index' + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testuser/memberships/destroy_community/#{community.id}" } + end + end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 5afb567..69ed3f2 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -64,4 +64,23 @@ class CommunityTest < Test::Unit::TestCase assert_not_includes c.members, p end + should 'clear relationships after destroy' do + c = Community.create!(:name => 'my test profile', :identifier => 'mytestprofile') + member = create_user('memberuser').person + admin = create_user('adminuser').person + moderator = create_user('moderatoruser').person + + c.add_member(member) + c.add_admin(admin) + c.add_moderator(moderator) + + relationships = c.role_assignments + assert_not_nil relationships + + c.destroy + relationships.each do |i| + assert !RoleAssignment.exists?(i.id) + end + end + end -- libgit2 0.21.2