Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_service.rb « lists « boards « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35ce81d99689d23809c6d073e5ac3a3b54e9c1fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Boards
  module Lists
    class UpdateService < Boards::BaseService
      def execute(list)
        return not_authorized if preferences? && !can_read?(list)
        return not_authorized if list_properties? && !can_admin?(list)

        if execute_by_params(list)
          success(list: list)
        else
          error(list.errors.messages, 422)
        end
      end

      def execute_by_params(list)
        update_preferences(list) || update_position(list)
      end

      def list_properties?
        position?
      end

      def update_preferences(list)
        return unless preferences?

        list.update_preferences_for(current_user, preferences)
      end

      def update_position(list)
        return unless position?

        move_service = Boards::Lists::MoveService.new(parent, current_user, params)

        move_service.execute(list)
      end

      def preferences
        { collapsed: Gitlab::Utils.to_boolean(params[:collapsed]) }
      end

      def not_authorized
        error("Not authorized", 403)
      end

      def preferences?
        params.has_key?(:collapsed)
      end

      def position?
        params.has_key?(:position)
      end

      def can_read?(list)
        Ability.allowed?(current_user, :read_list, parent)
      end

      def can_admin?(list)
        Ability.allowed?(current_user, :admin_list, parent)
      end
    end
  end
end