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
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
|
# File 'lib/webhookdb/api.rb', line 13
def self.inherited(subclass)
super
subclass.instance_eval do
version "v1", using: :path
format :json
require "webhookdb/service/helpers"
helpers Webhookdb::Service::Helpers
require "webhookdb/api/helpers"
helpers Webhookdb::API::Helpers
require "webhookdb/api/connstr_auth"
helpers do
def verified_customer!
c = current_customer
forbidden! unless c.phone_verified?
return c
end
def lookup_org!(identifier=nil, customer: nil, allow_connstr_auth: false)
identifier ||= params[:org_identifier]
if identifier == "-"
identifier = params[:org]
merror!(400, "must supply 'org_identifier' or 'org' param", code: "missing_org") unless identifier
end
customer ||= allow_connstr_auth ? current_customer? : current_customer
orgs = Webhookdb::Organization.with_identifier(identifier).all
merror!(403, "There is no organization with that identifier.") if orgs.empty?
if customer
memberships = customer.verified_memberships_dataset.where(organization: orgs).limit(2).all
permission_error!("You don't have permissions with that organization.") if memberships.empty?
merror!(500, "ambiguous", alert: true) if memberships.size > 1 org = memberships.first.organization
set_request_tags(organization: org.key)
return org
end
raise "something went wrong" unless allow_connstr_auth
org = Webhookdb::API::ConnstrAuth.find_authed(orgs, request)
unauthenticated! if org.nil?
set_request_tags(organization: org.key)
return org
end
def has_admin?(org=nil, customer: nil)
customer ||= current_customer
org ||= lookup_org!
has_no_admin = org.verified_memberships_dataset.
where(customer:, membership_role: Webhookdb::Role.admin_role).
empty?
return !has_no_admin
end
def ensure_admin!(org=nil, customer: nil)
org ||= lookup_org!
admin = has_admin?(org, customer:)
permission_error!("You don't have admin privileges with #{org.name}.") unless admin
end
end
before do
Sentry.configure_scope do |scope|
scope.set_tags(application: "public-api")
end
end
before_validation do
rgx = /\e\[[^\x40-\x7E]*[\x40-\x7E]/
self.params.each do |k, v|
params[k] = v.gsub(rgx, "") if v.is_a?(String)
end
end
end
end
|