From e6002bdaffc819ea3b743955315cf50eb804dbdb Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Fri, 1 Feb 2013 19:04:11 +0200
Subject: [PATCH] Ability to manage and remove group as owner outside of admin
 area

---
 app/controllers/groups_controller.rb | 36 +++++++++++++++++++-
 app/views/groups/edit.html.haml      | 50 ++++++++++++++++++++++++++++
 app/views/layouts/group.html.haml    |  6 ++++
 app/views/projects/_form.html.haml   |  2 +-
 app/views/teams/edit.html.haml       |  8 ++---
 config/routes.rb                     |  2 +-
 features/group/group.feature         |  6 ++++
 features/steps/group/group.rb        | 11 ++++++
 features/steps/shared/paths.rb       |  4 +++
 9 files changed, 117 insertions(+), 8 deletions(-)
 create mode 100644 app/views/groups/edit.html.haml

diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index 72df170f1fd..7b8649a6bdf 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -6,6 +6,7 @@ class GroupsController < ApplicationController
 
   # Authorize
   before_filter :authorize_read_group!, except: [:new, :create]
+  before_filter :authorize_admin_group!, only: [:edit, :update, :destroy]
   before_filter :authorize_create_group!, only: [:new, :create]
 
   # Load group projects
@@ -84,6 +85,31 @@ class GroupsController < ApplicationController
     redirect_to people_group_path(@group), notice: 'Users was successfully added.'
   end
 
+  def edit
+  end
+
+  def update
+    group_params = params[:group].dup
+    owner_id =group_params.delete(:owner_id)
+
+    if owner_id
+      @group.owner = User.find(owner_id)
+    end
+
+    if @group.update_attributes(group_params)
+      redirect_to @group, notice: 'Group was successfully updated.'
+    else
+      render action: "edit"
+    end
+  end
+
+  def destroy
+    @group.truncate_teams
+    @group.destroy
+
+    redirect_to root_path, notice: 'Group was removed.'
+  end
+
   protected
 
   def group
@@ -106,6 +132,14 @@ class GroupsController < ApplicationController
   end
 
   def authorize_create_group!
-    can?(current_user, :create_group, nil)
+    unless can?(current_user, :create_group, nil)
+      return render_404
+    end
+  end
+
+  def authorize_admin_group!
+    unless can?(current_user, :manage_group, group)
+      return render_404
+    end
   end
 end
diff --git a/app/views/groups/edit.html.haml b/app/views/groups/edit.html.haml
new file mode 100644
index 00000000000..7202ef26c70
--- /dev/null
+++ b/app/views/groups/edit.html.haml
@@ -0,0 +1,50 @@
+%h3.page_title Edit Group
+%hr
+= form_for @group do |f|
+  - if @group.errors.any?
+    .alert.alert-error
+      %span= @group.errors.full_messages.first
+  .clearfix
+    = f.label :name do
+      Group name is
+    .input
+      = f.text_field :name, placeholder: "Ex. OpenSource", class: "xxlarge left"
+      &nbsp;
+      = f.submit 'Save group', class: "btn btn-save"
+%hr
+
+
+.row
+  .span7
+    .ui-box
+      %h5.title Projects
+      %ul.well-list
+        - @group.projects.each do |project|
+          %li
+            - if project.public
+              %i.icon-share
+            - else
+              %i.icon-lock.cgreen
+            = link_to project.name_with_namespace, project
+            .pull-right
+              = link_to 'Team', project_team_index_path(project), id: "edit_#{dom_id(project)}", class: "btn btn-small"
+              = link_to 'Edit', edit_project_path(project), id: "edit_#{dom_id(project)}", class: "btn btn-small"
+              = link_to 'Remove', project, confirm: "REMOVE #{project.name}? Are you sure?", method: :delete, class: "btn btn-small btn-remove"
+
+  .span5
+    .ui-box
+      %h5.title Transfer group
+      .padded
+        %p
+          Transferring group will cause loss of admin control over group and all child projects
+        = form_for @group do |f|
+          = f.select :owner_id, User.all.map { |user| [user.name, user.id] }, {}, {class: 'chosen'}
+          = f.submit 'Transfer group', class: "btn btn-small"
+    .ui-box
+      %h5.title Remove group
+      .padded.bgred
+        %p
+          Remove of group will cause removing all child projects and resources
+          %br
+          Removed group can not be restored!
+        = link_to 'Remove Group', @group, confirm: 'Removed group can not be restored! Are you sure?', method: :delete, class: "btn btn-remove btn-small"
diff --git a/app/views/layouts/group.html.haml b/app/views/layouts/group.html.haml
index 4395e408649..9057ad50ce6 100644
--- a/app/views/layouts/group.html.haml
+++ b/app/views/layouts/group.html.haml
@@ -22,4 +22,10 @@
         = nav_link(path: 'groups#people') do
           = link_to "People", people_group_path(@group)
 
+        - if can?(current_user, :manage_group, @group)
+          = nav_link(path: 'groups#edit') do
+            = link_to edit_group_path(@group), class: "tab " do
+              %i.icon-edit
+              Edit Group
+
       .content= yield
diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml
index 8d3b1aded5c..0336654dc69 100644
--- a/app/views/projects/_form.html.haml
+++ b/app/views/projects/_form.html.haml
@@ -42,7 +42,7 @@
         = f.check_box :wiki_enabled
         %span.descr Pages for project documentation
 
-  - if can? current_user, :change_public_mode, @project
+  - if can?(current_user, :change_public_mode, @project)
     %fieldset.features
       %legend
         %i.icon-share
diff --git a/app/views/teams/edit.html.haml b/app/views/teams/edit.html.haml
index a3b1c734414..e0962f2b05b 100644
--- a/app/views/teams/edit.html.haml
+++ b/app/views/teams/edit.html.haml
@@ -15,8 +15,6 @@
       Team path is
     .input
       = f.text_field :path, placeholder: "opensource", class: "xxlarge left"
-  .clearfix
-    .input.span3.center
-      = f.submit 'Save team changes', class: "btn btn-primary"
-    .input.span3.center
-      = link_to 'Delete team', team_path(@team), method: :delete, confirm: "You are shure?", class: "btn btn-remove"
+  .form-actions
+    = f.submit 'Save team changes', class: "btn btn-primary"
+    = link_to 'Delete team', team_path(@team), method: :delete, confirm: "You are shure?", class: "btn btn-remove pull-right"
diff --git a/config/routes.rb b/config/routes.rb
index 1abd37fe45f..d6432b86007 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -129,7 +129,7 @@ Gitlab::Application.routes.draw do
   #
   # Groups Area
   #
-  resources :groups, constraints: { id: /[^\/]+/ }, only: [:show, :new, :create] do
+  resources :groups, constraints: { id: /[^\/]+/ }  do
     member do
       get :issues
       get :merge_requests
diff --git a/features/group/group.feature b/features/group/group.feature
index a4a55a7fc26..a48affe8e02 100644
--- a/features/group/group.feature
+++ b/features/group/group.feature
@@ -24,3 +24,9 @@ Feature: Groups
     When I visit group people page
     And I select user "John" from list with role "Reporter"
     Then I should see user "John" in team list
+
+  Scenario: I should see edit group page
+    When I visit group settings page
+    And I change group name
+    Then I should see new group name
+
diff --git a/features/steps/group/group.rb b/features/steps/group/group.rb
index c6c6b4b5e5b..5cfa4756ac3 100644
--- a/features/steps/group/group.rb
+++ b/features/steps/group/group.rb
@@ -82,6 +82,17 @@ class Groups < Spinach::FeatureSteps
     current_path.should == group_path(Group.last)
   end
 
+  And 'I change group name' do
+    fill_in 'group_name', :with => 'new-name'
+    click_button "Save group"
+  end
+
+  Then 'I should see new group name' do
+    within ".navbar-gitlab" do
+      page.should have_content "group: new-name"
+    end
+  end
+
   protected
 
   def current_group
diff --git a/features/steps/shared/paths.rb b/features/steps/shared/paths.rb
index 97adfd13f30..a8e68012d05 100644
--- a/features/steps/shared/paths.rb
+++ b/features/steps/shared/paths.rb
@@ -25,6 +25,10 @@ module SharedPaths
     visit people_group_path(current_group)
   end
 
+  When 'I visit group settings page' do
+    visit edit_group_path(current_group)
+  end
+
   # ----------------------------------------
   # Dashboard
   # ----------------------------------------
-- 
GitLab