1# frozen_string_literal: true
2
3# Makes api V4 compatible with old project features permissions methods
4#
5# After migrating issues_enabled merge_requests_enabled builds_enabled snippets_enabled and wiki_enabled
6# fields to a new table "project_features", support for the old fields is still needed in the API.
7require 'gitlab/utils'
8
9module ProjectFeaturesCompatibility
10  extend ActiveSupport::Concern
11
12  # TODO: remove in API v5, replaced by *_access_level
13  def wiki_enabled=(value)
14    write_feature_attribute_boolean(:wiki_access_level, value)
15  end
16
17  # TODO: remove in API v5, replaced by *_access_level
18  def builds_enabled=(value)
19    write_feature_attribute_boolean(:builds_access_level, value)
20  end
21
22  # TODO: remove in API v5, replaced by *_access_level
23  def merge_requests_enabled=(value)
24    write_feature_attribute_boolean(:merge_requests_access_level, value)
25  end
26
27  # TODO: remove in API v5, replaced by *_access_level
28  def issues_enabled=(value)
29    write_feature_attribute_boolean(:issues_access_level, value)
30  end
31
32  # TODO: remove in API v5, replaced by *_access_level
33  def snippets_enabled=(value)
34    write_feature_attribute_boolean(:snippets_access_level, value)
35  end
36
37  def security_and_compliance_enabled=(value)
38    write_feature_attribute_boolean(:security_and_compliance_access_level, value)
39  end
40
41  def repository_access_level=(value)
42    write_feature_attribute_string(:repository_access_level, value)
43  end
44
45  def wiki_access_level=(value)
46    write_feature_attribute_string(:wiki_access_level, value)
47  end
48
49  def builds_access_level=(value)
50    write_feature_attribute_string(:builds_access_level, value)
51  end
52
53  def merge_requests_access_level=(value)
54    write_feature_attribute_string(:merge_requests_access_level, value)
55  end
56
57  def forking_access_level=(value)
58    write_feature_attribute_string(:forking_access_level, value)
59  end
60
61  def issues_access_level=(value)
62    write_feature_attribute_string(:issues_access_level, value)
63  end
64
65  def snippets_access_level=(value)
66    write_feature_attribute_string(:snippets_access_level, value)
67  end
68
69  def pages_access_level=(value)
70    write_feature_attribute_string(:pages_access_level, value)
71  end
72
73  def metrics_dashboard_access_level=(value)
74    write_feature_attribute_string(:metrics_dashboard_access_level, value)
75  end
76
77  def analytics_access_level=(value)
78    write_feature_attribute_string(:analytics_access_level, value)
79  end
80
81  def operations_access_level=(value)
82    write_feature_attribute_string(:operations_access_level, value)
83  end
84
85  def security_and_compliance_access_level=(value)
86    write_feature_attribute_string(:security_and_compliance_access_level, value)
87  end
88
89  def container_registry_access_level=(value)
90    write_feature_attribute_string(:container_registry_access_level, value)
91  end
92
93  # TODO: Remove this method after we drop support for project create/edit APIs to set the
94  # container_registry_enabled attribute. They can instead set the container_registry_access_level
95  # attribute.
96  def container_registry_enabled=(value)
97    write_feature_attribute_boolean(:container_registry_access_level, value)
98  end
99
100  private
101
102  def write_feature_attribute_boolean(field, value)
103    access_level = Gitlab::Utils.to_boolean(value) ? ProjectFeature::ENABLED : ProjectFeature::DISABLED
104    write_feature_attribute_raw(field, access_level)
105  end
106
107  def write_feature_attribute_string(field, value)
108    access_level = ProjectFeature.access_level_from_str(value)
109    write_feature_attribute_raw(field, access_level)
110  end
111
112  def write_feature_attribute_raw(field, value)
113    build_project_feature unless project_feature
114
115    project_feature.__send__(:write_attribute, field, value) # rubocop:disable GitlabSecurity/PublicSend
116  end
117end
118
119ProjectFeaturesCompatibility.prepend_mod_with('ProjectFeaturesCompatibility')
120