Commit a424d173 authored by Felipe Artur's avatar Felipe Artur

Polish reusing of controllers for project boards

parent d1356dc9
...@@ -4,8 +4,8 @@ import Vue from 'vue'; ...@@ -4,8 +4,8 @@ import Vue from 'vue';
class BoardService { class BoardService {
constructor (root, listsEndpoint, bulkUpdatePath, boardId) { constructor (root, listsEndpoint, bulkUpdatePath, boardId) {
alert(listsEndpoint) // alert(listsEndpoint)
alert(root) // alert(root)
this.boards = Vue.resource(`${root}{/id}.json`, {}, { this.boards = Vue.resource(`${root}{/id}.json`, {}, {
issues: { issues: {
method: 'GET', method: 'GET',
...@@ -18,8 +18,8 @@ class BoardService { ...@@ -18,8 +18,8 @@ class BoardService {
url: `${listsEndpoint}/generate.json` url: `${listsEndpoint}/generate.json`
} }
}); });
this.issue = Vue.resource(`${root}/${boardId}/issues{/id}`, {}); this.issue = Vue.resource(`/boards/${boardId}/issues{/id}`, {});
this.issues = Vue.resource(`${root}/${boardId}/lists{/id}/issues`, {}, { this.issues = Vue.resource(`${listsEndpoint}{/id}/issues`, {}, {
bulkUpdate: { bulkUpdate: {
method: 'POST', method: 'POST',
url: bulkUpdatePath, url: bulkUpdatePath,
......
module Boards module Boards
class ApplicationController < ::ApplicationController class ApplicationController < ::ApplicationController
respond_to :json respond_to :json
...@@ -7,6 +6,14 @@ module Boards ...@@ -7,6 +6,14 @@ module Boards
private private
def board
@board ||= Board.find(params[:board_id])
end
def board_parent
@board_parent ||= board.parent
end
def record_not_found(exception) def record_not_found(exception)
render json: { error: exception.message }, status: :not_found render json: { error: exception.message }, status: :not_found
end end
......
module Boards module Boards
class IssuesController < Boards::ApplicationController class IssuesController < Boards::ApplicationController
include BoardsAuthorizations
before_action :authorize_read_issue!, only: [:index] before_action :authorize_read_issue!, only: [:index]
before_action :authorize_create_issue!, only: [:create] before_action :authorize_create_issue!, only: [:create]
before_action :authorize_update_issue!, only: [:update] before_action :authorize_update_issue!, only: [:update]
def index def index
issues = ::Boards::Issues::ListService.new(project, current_user, filter_params).execute issues = Boards::Issues::ListService.new(board_parent, current_user, filter_params).execute
issues = issues.page(params[:page]).per(params[:per] || 20) issues = issues.page(params[:page]).per(params[:per] || 20)
make_sure_position_is_set(issues) unless Gitlab::Geo.secondary? make_sure_position_is_set(issues) unless Gitlab::Geo.secondary?
...@@ -16,7 +18,7 @@ module Boards ...@@ -16,7 +18,7 @@ module Boards
end end
def create def create
service = ::Boards::Issues::CreateService.new(project, current_user, issue_params) service = Boards::Issues::CreateService.new(board_parent, current_user, issue_params)
issue = service.execute issue = service.execute
if issue.valid? if issue.valid?
...@@ -27,7 +29,7 @@ module Boards ...@@ -27,7 +29,7 @@ module Boards
end end
def update def update
service = ::Boards::Issues::MoveService.new(project, current_user, move_params) service = Boards::Issues::MoveService.new(board_parent, current_user, move_params)
if service.execute(issue) if service.execute(issue)
head :ok head :ok
...@@ -46,24 +48,12 @@ module Boards ...@@ -46,24 +48,12 @@ module Boards
def issue def issue
@issue ||= @issue ||=
IssuesFinder.new(current_user, project_id: project.id) IssuesFinder.new(current_user, project_id: board_parent.id)
.execute .execute
.where(iid: params[:id]) .where(iid: params[:id])
.first! .first!
end end
def authorize_read_issue!
return render_403 unless can?(current_user, :read_issue, project)
end
def authorize_create_issue!
return render_403 unless can?(current_user, :admin_issue, project)
end
def authorize_update_issue!
return render_403 unless can?(current_user, :update_issue, issue)
end
def filter_params def filter_params
params.merge(board_id: params[:board_id], id: params[:list_id]).compact params.merge(board_id: params[:board_id], id: params[:list_id]).compact
end end
......
module Boards module Boards
class ListsController < Boards::ApplicationController class ListsController < Boards::ApplicationController
include BoardsAuthorizations
#before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate] #before_action :authorize_admin_list!, only: [:create, :update, :destroy, :generate]
#before_action :authorize_read_list!, only: [:index] before_action :authorize_read_list!, only: [:index]
def index def index
lists = ::Boards::Lists::ListService.new(board.parent, current_user).execute(board) lists = Boards::Lists::ListService.new(board.parent, current_user).execute(board)
render json: serialize_as_json(lists) render json: serialize_as_json(lists)
end end
...@@ -21,7 +23,7 @@ module Boards ...@@ -21,7 +23,7 @@ module Boards
def update def update
list = board.lists.movable.find(params[:id]) list = board.lists.movable.find(params[:id])
service = ::Boards::Lists::MoveService.new(project, current_user, move_params) service = Boards::Lists::MoveService.new(project, current_user, move_params)
if service.execute(list) if service.execute(list)
head :ok head :ok
...@@ -32,7 +34,7 @@ module Boards ...@@ -32,7 +34,7 @@ module Boards
def destroy def destroy
list = board.lists.destroyable.find(params[:id]) list = board.lists.destroyable.find(params[:id])
service = ::Boards::Lists::DestroyService.new(project, current_user) service = Boards::Lists::DestroyService.new(project, current_user)
if service.execute(list) if service.execute(list)
head :ok head :ok
...@@ -42,7 +44,7 @@ module Boards ...@@ -42,7 +44,7 @@ module Boards
end end
def generate def generate
service = ::Boards::Lists::GenerateService.new(project, current_user) service = Boards::Lists::GenerateService.new(board_parent, current_user)
if service.execute(board) if service.execute(board)
render json: serialize_as_json(board.lists.movable) render json: serialize_as_json(board.lists.movable)
...@@ -57,14 +59,6 @@ module Boards ...@@ -57,14 +59,6 @@ module Boards
return render_403 unless can?(current_user, :admin_list, project) return render_403 unless can?(current_user, :admin_list, project)
end end
def authorize_read_list!
return render_403 unless can?(current_user, :read_list, project)
end
def board
@board ||= Board.find(params[:board_id])
end
def list_params def list_params
params.require(:list).permit(:label_id) params.require(:list).permit(:label_id)
end end
......
module BoardsAuthorizations
# Shared authorizations between projects and groups which
# have different policies.
def authorize_read_list!
ability = board.is_group_board? ? :read_group : :read_list
return render_403 unless action_allowed_for?(board.parent, ability)
end
def authorize_read_issue!
ability = board.is_group_board? ? :read_group : :read_issue
return render_403 unless action_allowed_for?(board.parent, ability)
end
def authorize_update_issue!
return render_403 unless action_allowed_for?(issue, :admin_issue)
end
def authorize_create_issue!
return render_403 unless action_allowed_for?(board.parent, :admin_issue)
end
def action_allowed_for?(resource, ability)
can?(current_user, ability, resource)
end
end
...@@ -25,6 +25,10 @@ module EE ...@@ -25,6 +25,10 @@ module EE
group || project group || project
end end
def is_group_board?
group_id.present?
end
def as_json(options = {}) def as_json(options = {})
milestone_attrs = options.fetch(:include, {}) milestone_attrs = options.fetch(:include, {})
.extract!(:milestone) .extract!(:milestone)
......
...@@ -82,6 +82,19 @@ Rails.application.routes.draw do ...@@ -82,6 +82,19 @@ Rails.application.routes.draw do
# Notification settings # Notification settings
resources :notification_settings, only: [:create, :update] resources :notification_settings, only: [:create, :update]
# Boards resources shared between group and projects
resources :boards do
resources :lists, module: :boards, only: [:index, :create, :update, :destroy] do
collection do
post :generate
end
resources :issues, only: [:index, :create, :update]
end
resources :issues, module: :boards, only: [:index, :update]
end
draw :import draw :import
draw :uploads draw :uploads
draw :explore draw :explore
...@@ -96,15 +109,5 @@ Rails.application.routes.draw do ...@@ -96,15 +109,5 @@ Rails.application.routes.draw do
draw :test if Rails.env.test? draw :test if Rails.env.test?
resources :boards do
resources :lists, module: :boards, only: [:index, :create, :update, :destroy] do
collection do
post :generate
end
resources :issues, only: [:index, :create, :update]
end
end
get '*unmatched_route', to: 'application#route_not_found' get '*unmatched_route', to: 'application#route_not_found'
end end
...@@ -379,11 +379,7 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -379,11 +379,7 @@ constraints(ProjectUrlConstrainer.new) do
get 'noteable/:target_type/:target_id/notes' => 'notes#index', as: 'noteable_notes' get 'noteable/:target_type/:target_id/notes' => 'notes#index', as: 'noteable_notes'
resources :boards, only: [:index, :show, :create, :update, :destroy] do resources :boards, only: [:index, :show, :create, :update, :destroy]
scope module: :boards do
resources :issues, only: [:index, :update]
end
end
resources :todos, only: [:create] resources :todos, only: [:create]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment