Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#197] Make SendGrid permissions management easy #278

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'rspec/core/rake_task'

Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/sendgrid/test*.rb', 'test/sendgrid/helpers/mail/test*.rb']
t.test_files = FileList['test/sendgrid/test*.rb', 'test/sendgrid/helpers/mail/test*.rb', 'test/sendgrid/helpers/permissions/test*.rb']
t.verbose = true
end

Expand Down
52 changes: 49 additions & 3 deletions examples/scopes/scopes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require 'sendgrid-ruby'

require_relative '../../lib/sendgrid-ruby.rb'

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])


##################################################
# Retrieve a list of scopes for which this user has access. #
# GET /scopes #
Expand All @@ -13,3 +11,51 @@
puts response.body
puts response.headers

##################################################
# Update the name & scopes of an API Key #
# PUT /api_keys/{api_key_id} #


scopes = [
"user.profile.read",
"user.profile.update"
]

data = {
"name": "A New Hope",
"scopes": scopes
}
api_key_id = "test_url_param"
response = sg.client.api_keys._(api_key_id).put(request_body: data)
puts response.status_code
puts response.body
puts response.headers

# The above method shows how to update the scopes
# To get various scopes that each of the endpoint has, use the following

# To get all admin permissions
scopes = SendGrid::Scope.admin_permissions

# To get all read only permissions
scopes = SendGrid::Scope.read_only_permissions

# There are two methods for each endpoints, namely
# {endpoint}_read_only_permissions and {endpoint}_full_access_permissions

# These are the endpoints :
# alerts, api_keys, asm_groups, billing, categories, credentials, stats, ips, mail_settings, mail,
# marketing_campaigns, partner_settings, scheduled_sends, subusers, suppression, teammates,
# templates, tracking_settings, user_settings, webhooks, whitelabel, access_settings

# read only permissions for alerts
scopes = SendGrid::Scope.alerts_read_only_permissions

# full access permissions for alerts
scopes = SendGrid::Scope.alerts_full_access_permissions

# read only permissions for billing
scopes = SendGrid::Scope.billing_read_only_permissions

# full access permissions for billing
scopes = SendGrid::Scope.billing_full_access_permissions
1 change: 1 addition & 0 deletions lib/sendgrid-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
require_relative 'sendgrid/helpers/stats/email_stats'
require_relative 'sendgrid/helpers/stats/stats_response'
require_relative 'sendgrid/helpers/stats/metrics'
require_relative 'sendgrid/helpers/permissions/scope'
28 changes: 28 additions & 0 deletions lib/sendgrid/helpers/permissions/scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is used for getting scopes
require 'yaml'

module SendGrid
class Scope
SCOPES = YAML.load_file(File.dirname(__FILE__) + '/scopes.yml').freeze

class << self
def admin_permissions
SCOPES.values.map(&:values).flatten
end

def read_only_permissions
SCOPES.map { |_, v| v[:read] }.flatten
end

SCOPES.each_key do |endpoint|
define_method "#{endpoint}_read_only_permissions" do
SCOPES[endpoint][:read]
end

define_method "#{endpoint}_full_access_permissions" do
SCOPES[endpoint].values.flatten
end
end
end
end
end
Loading