From b7470440ffbc9cb9f58f9de4b3064760670a20a4 Mon Sep 17 00:00:00 2001
From: Andrey Kumanyaev <me@zzet.org>
Date: Mon, 21 Jan 2013 01:03:29 +0400
Subject: [PATCH] Move team project management to own controller

---
 .../admin/teams/projects_controller.rb        | 42 +++++++++++++++++++
 app/helpers/admin/teams/projects_helper.rb    |  5 +++
 app/models/user_team.rb                       |  4 ++
 .../admin/teams/projects/_form.html.haml      | 16 +++++++
 app/views/admin/teams/projects/edit.html.haml | 16 +++++++
 app/views/admin/teams/projects/new.html.haml  | 23 ++++++++++
 config/routes.rb                              |  1 +
 lib/gitlab/user_team_manager.rb               | 14 +++++++
 8 files changed, 121 insertions(+)
 create mode 100644 app/controllers/admin/teams/projects_controller.rb
 create mode 100644 app/helpers/admin/teams/projects_helper.rb
 create mode 100644 app/views/admin/teams/projects/_form.html.haml
 create mode 100644 app/views/admin/teams/projects/edit.html.haml
 create mode 100644 app/views/admin/teams/projects/new.html.haml

diff --git a/app/controllers/admin/teams/projects_controller.rb b/app/controllers/admin/teams/projects_controller.rb
new file mode 100644
index 00000000000..74e566191b1
--- /dev/null
+++ b/app/controllers/admin/teams/projects_controller.rb
@@ -0,0 +1,42 @@
+class Admin::Teams::ProjectsController < Admin::Teams::ApplicationController
+  before_filter :team_project, only: [:edit, :destroy, :update]
+
+  def new
+    @projects = Project.scoped
+    @projects = @projects.without_team(@team) if @team.projects.any?
+    #@projects.reject!(&:empty_repo?)
+  end
+
+  def create
+    unless params[:project_ids].blank?
+      project_ids = params[:project_ids]
+      access = params[:greatest_project_access]
+      @team.assign_to_projects(project_ids, access)
+    end
+
+    redirect_to admin_team_path(@team), notice: 'Projects was successfully added.'
+  end
+
+  def edit
+  end
+
+  def update
+    if @team.update_project_access(@project, params[:greatest_project_access])
+      redirect_to admin_team_path(@team), notice: 'Membership was successfully updated.'
+    else
+      render :edit
+    end
+  end
+
+  def destroy
+    @team.resign_from_project(@project)
+    redirect_to admin_team_path(@team), notice: 'Project was successfully removed.'
+  end
+
+  private
+
+  def team_project
+    @project = @team.projects.find_by_path(params[:id])
+  end
+
+end
diff --git a/app/helpers/admin/teams/projects_helper.rb b/app/helpers/admin/teams/projects_helper.rb
new file mode 100644
index 00000000000..b97cc403337
--- /dev/null
+++ b/app/helpers/admin/teams/projects_helper.rb
@@ -0,0 +1,5 @@
+module Admin::Teams::ProjectsHelper
+  def assigned_since(team, project)
+    team.user_team_project_relationships.find_by_project_id(project).created_at
+  end
+end
diff --git a/app/models/user_team.rb b/app/models/user_team.rb
index c9dfd671efc..2e2f75060c3 100644
--- a/app/models/user_team.rb
+++ b/app/models/user_team.rb
@@ -68,6 +68,10 @@ class UserTeam < ActiveRecord::Base
     Gitlab::UserTeamManager.update_team_user_membership(self, user, options)
   end
 
+  def update_project_access(project, permission)
+    Gitlab::UserTeamManager.update_project_greates_access(self, project, permission)
+  end
+
   def max_project_access(project)
     user_team_project_relationships.find_by_project_id(project).greatest_access
   end
diff --git a/app/views/admin/teams/projects/_form.html.haml b/app/views/admin/teams/projects/_form.html.haml
new file mode 100644
index 00000000000..db4fe85b000
--- /dev/null
+++ b/app/views/admin/teams/projects/_form.html.haml
@@ -0,0 +1,16 @@
+= form_tag admin_team_project_path(@team, @project), method: :put do
+  -if @project.errors.any?
+    .alert-message.block-message.error
+      %ul
+        - @project.errors.full_messages.each do |msg|
+          %li= msg
+
+  .clearfix
+    %label Max access for Team members:
+    .input
+      = select_tag :greatest_project_access, options_for_select(UserTeam.access_roles, @team.max_project_access(@project)), class: "project-access-select chosen span3"
+
+  %br
+  .actions
+    = submit_tag 'Save', class: "btn primary"
+    = link_to 'Cancel', :back, class: "btn"
diff --git a/app/views/admin/teams/projects/edit.html.haml b/app/views/admin/teams/projects/edit.html.haml
new file mode 100644
index 00000000000..b91a4982b81
--- /dev/null
+++ b/app/views/admin/teams/projects/edit.html.haml
@@ -0,0 +1,16 @@
+%h3
+  Edit max access in #{@project.name} for #{@team.name} team
+
+%hr
+%table.zebra-striped
+  %tr
+    %td Project:
+    %td= @project.name
+  %tr
+    %td Team:
+    %td= @team.name
+  %tr
+    %td Since:
+    %td= assigned_since(@team, @project).stamp("Nov 11, 2010")
+
+= render 'form'
diff --git a/app/views/admin/teams/projects/new.html.haml b/app/views/admin/teams/projects/new.html.haml
new file mode 100644
index 00000000000..8a0a18a48c0
--- /dev/null
+++ b/app/views/admin/teams/projects/new.html.haml
@@ -0,0 +1,23 @@
+%h3.page_title
+  Team: #{@team.name}
+
+%fieldset
+  %legend Projects (#{@team.projects.count})
+  = form_tag admin_team_projects_path(@team), id: "assign_projects", class: "bulk_import", method: :post  do
+    %table#projects_list
+      %thead
+        %tr
+          %th Project name
+          %th Max access
+          %th
+      - @team.projects.each do |project|
+        %tr.project
+          %td
+            = link_to project.name_with_namespace, [:admin, project]
+          %td
+            %span= @team.human_max_project_access(project)
+          %td
+      %tr
+        %td= select_tag :project_ids, options_from_collection_for_select(@projects , :id, :name_with_namespace), multiple: true, data: {placeholder: 'Select projects'}, class: 'chosen span5'
+        %td= select_tag :greatest_project_access, options_for_select(Project.access_options), {class: "project-access-select chosen span3" }
+        %td= submit_tag 'Add', class: "btn primary", id: :assign_projects_to_team
diff --git a/config/routes.rb b/config/routes.rb
index a31d0e7797d..3132c310c99 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -78,6 +78,7 @@ Gitlab::Application.routes.draw do
       end
       scope module: :teams do
         resources :members, only: [:edit, :update, :destroy, :new, :create]
+        resources :projects, only: [:edit, :update, :destroy, :new, :create]
       end
     end
     resources :team_members, only: [:edit, :update, :destroy]
diff --git a/lib/gitlab/user_team_manager.rb b/lib/gitlab/user_team_manager.rb
index 753081ea718..7d9a9bdf3f5 100644
--- a/lib/gitlab/user_team_manager.rb
+++ b/lib/gitlab/user_team_manager.rb
@@ -48,6 +48,20 @@ module Gitlab
         end
       end
 
+      def update_project_greates_access(team, project, permission)
+        project_relation = team.user_team_project_relationships.find_by_project_id(project)
+        if permission != team.max_project_access(project)
+          if project_relation.update_attributes(greatest_access: permission)
+            update_team_users_access_in_project(team, project)
+            true
+          else
+            false
+          end
+        else
+          true
+        end
+      end
+
       def rebuild_project_permissions_to_member(team, member)
         team.projects.each do |project|
           update_team_user_access_in_project(team, member, project)
-- 
GitLab