Class: ApiKeyScope

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/api_key_scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_mappingsObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/api_key_scope.rb', line 16

def default_mappings
  return @default_mappings unless @default_mappings.nil?

  mappings = {
    global: {
      read: {
        methods: %i[get],
      },
    },
    topics: {
      write: {
        actions: %w[posts#create],
        params: %i[topic_id],
      },
      update: {
        actions: %w[topics#update topics#status],
        params: %i[topic_id category_id],
      },
      delete: {
        actions: %w[topics#destroy],
      },
      read: {
        actions: %w[topics#show topics#feed topics#posts topics#show_by_external_id],
        params: %i[topic_id external_id],
        aliases: {
          topic_id: :id,
        },
      },
      read_lists: {
        actions: list_actions,
        params: %i[category_id],
        aliases: {
          category_id: :category_slug_path_with_id,
        },
      },
      status: {
        actions: %w[topics#status],
        params: %i[topic_id category_id status enabled],
      },
    },
    posts: {
      edit: {
        actions: %w[posts#update],
        params: %i[id],
      },
      delete: {
        actions: %w[posts#destroy],
      },
      list: {
        actions: %w[posts#latest],
      },
    },
    tags: {
      list: {
        actions: %w[tags#index],
      },
    },
    categories: {
      list: {
        actions: %w[categories#index],
      },
      show: {
        actions: %w[categories#show],
        params: %i[id],
      },
    },
    uploads: {
      create: {
        actions: %w[
          uploads#create
          uploads#generate_presigned_put
          uploads#complete_external_upload
          uploads#create_multipart
          uploads#batch_presign_multipart_parts
          uploads#abort_multipart
          uploads#complete_multipart
        ],
      },
    },
    users: {
      bookmarks: {
        actions: %w[users#bookmarks],
        params: %i[username],
      },
      sync_sso: {
        actions: %w[admin/users#sync_sso],
        params: %i[sso sig],
      },
      show: {
        actions: %w[users#show],
        params: %i[username external_id external_provider],
      },
      check_emails: {
        actions: %w[users#check_emails],
        params: %i[username],
      },
      update: {
        actions: %w[users#update],
        params: %i[username],
      },
      log_out: {
        actions: %w[admin/users#log_out],
      },
      anonymize: {
        actions: %w[admin/users#anonymize],
      },
      suspend: {
        actions: %w[admin/users#suspend],
      },
      delete: {
        actions: %w[admin/users#destroy],
      },
      list: {
        actions: %w[admin/users#index],
      },
    },
    user_status: {
      read: {
        actions: %w[user_status#get],
      },
      update: {
        actions: %w[user_status#set user_status#clear],
      },
    },
    email: {
      receive_emails: {
        actions: %w[admin/email#handle_mail admin/email#smtp_should_reject],
      },
    },
    invites: {
      create: {
        actions: %w[invites#create],
      },
    },
    badges: {
      create: {
        actions: %w[admin/badges#create],
      },
      show: {
        actions: %w[badges#show],
      },
      update: {
        actions: %w[admin/badges#update],
      },
      delete: {
        actions: %w[admin/badges#destroy],
      },
      list_user_badges: {
        actions: %w[user_badges#username],
        params: %i[username],
      },
      assign_badge_to_user: {
        actions: %w[user_badges#create],
        params: %i[username],
      },
      revoke_badge_from_user: {
        actions: %w[user_badges#destroy],
      },
    },
    groups: {
      manage_groups: {
        actions: %w[groups#members groups#add_members groups#remove_member],
        params: %i[id],
      },
      administer_groups: {
        actions: %w[
          admin/groups#create
          admin/groups#destroy
          groups#show
          groups#update
          groups#index
        ],
      },
    },
    search: {
      show: {
        actions: %w[search#show],
        params: %i[q page],
      },
      query: {
        actions: %w[search#query],
        params: %i[term],
      },
    },
    wordpress: {
      publishing: {
        actions: %w[site#site posts#create topics#update topics#status topics#show],
      },
      commenting: {
        actions: %w[topics#wordpress],
      },
      discourse_connect: {
        actions: %w[admin/users#sync_sso admin/users#log_out admin/users#index users#show],
      },
      utilities: {
        actions: %w[users#create groups#index],
      },
    },
  }

  parse_resources!(mappings)
  @default_mappings = mappings
end

.find_urls(actions:, methods:) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'app/models/api_key_scope.rb', line 243

def find_urls(actions:, methods:)
  urls = Set.new

  if actions.present?
    route_sets = [Rails.application.routes]
    Rails::Engine.descendants.each do |engine|
      next if engine == Rails::Application # abstract engine, can't call routes on it
      next if engine == Discourse::Application # equiv. to Rails.application
      route_sets << engine.routes
    end

    route_sets.each do |set|
      engine_mount_path = set.find_script_name({}).presence
      engine_mount_path = nil if engine_mount_path == "/"
      set.routes.each do |route|
        defaults = route.defaults
        action = "#{defaults[:controller].to_s}##{defaults[:action]}"
        path = route.path.spec.to_s.gsub(/\(\.:format\)/, "")
        api_supported_path =
          (
            path.end_with?(".rss") || !route.path.requirements[:format] ||
              route.path.requirements[:format].match?("json")
          )
        excluded_paths = %w[/new-topic /new-message /exception]

        if actions.include?(action) && api_supported_path && !excluded_paths.include?(path)
          urls << "#{engine_mount_path}#{path} (#{route.verb})"
        end
      end
    end
  end

  methods.each { |method| urls << "* (#{method})" } if methods.present?

  urls.to_a
end

.list_actionsObject



8
9
10
11
12
13
14
# File 'app/models/api_key_scope.rb', line 8

def list_actions
  actions = %w[list#category_feed list#category_default list#latest_feed]

  %i[latest unread new top].each { |f| actions.concat(["list#category_#{f}", "list##{f}"]) }

  actions
end

.parse_resources!(mappings) ⇒ Object



232
233
234
235
236
237
238
239
240
241
# File 'app/models/api_key_scope.rb', line 232

def parse_resources!(mappings)
  mappings.each_value do |resource_actions|
    resource_actions.each_value do |action_data|
      action_data[:urls] = find_urls(
        actions: action_data[:actions],
        methods: action_data[:methods],
      )
    end
  end
end

.scope_mappingsObject



220
221
222
223
224
225
226
227
228
229
230
# File 'app/models/api_key_scope.rb', line 220

def scope_mappings
  plugin_mappings = DiscoursePluginRegistry.api_key_scope_mappings
  return default_mappings if plugin_mappings.empty?

  default_mappings.deep_dup.tap do |mappings|
    plugin_mappings.each do |plugin_mapping|
      parse_resources!(plugin_mapping)
      mappings.deep_merge!(plugin_mapping)
    end
  end
end

Instance Method Details

#permits?(env) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
284
285
# File 'app/models/api_key_scope.rb', line 281

def permits?(env)
  RouteMatcher.new(**mapping.except(:urls), allowed_param_values: allowed_parameters).match?(
    env: env,
  )
end