Class: Permit

Inherits:
Object
  • Object
show all
Defined in:
lib/permit.rb

Constant Summary collapse

DEFAULT_API_URL =
'https://api.permit.io'.freeze
DEFAULT_PDP_URL =
'http://localhost:7766'.freeze
DEFAULT_TENANT =
"default"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, api_url = DEFAULT_API_URL, pdp_url = DEFAULT_PDP_URL, debug = false, logger = Logger.new(STDOUT)) ⇒ Permit

Returns a new instance of Permit.



12
13
14
15
16
17
# File 'lib/permit.rb', line 12

def initialize(token, api_url = DEFAULT_API_URL, pdp_url = DEFAULT_PDP_URL, debug = false, logger = Logger.new(STDOUT))
  @config = PermitConfig.new(token, api_url, pdp_url, nil, debug, logger)
  @api = PermitApiClient.new(@config, logger)
  # @elements = PermitElements.new
  # @enforcement = PermitEnforcer.new
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



7
8
9
# File 'lib/permit.rb', line 7

def api
  @api
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/permit.rb', line 7

def config
  @config
end

#elementsObject

Returns the value of attribute elements.



7
8
9
# File 'lib/permit.rb', line 7

def elements
  @elements
end

#enforcementObject

Returns the value of attribute enforcement.



7
8
9
# File 'lib/permit.rb', line 7

def enforcement
  @enforcement
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/permit.rb', line 7

def logger
  @logger
end

Instance Method Details

#check(user, action, resource, context = {}) ⇒ Object



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
# File 'lib/permit.rb', line 19

def check(user, action, resource, context = {})
  normalized_user = if user.is_a?(String)
                      { key: user }
                    else
                      user
                    end
  normalized_resource = if resource.is_a?(String)
                          { type: resource.split(":").first, key: resource.split(":").last, tenant: DEFAULT_TENANT }
                        else
                          resource
                        end

  input = {
    user: normalized_user,
    action: action,
    resource: normalized_resource,
    context: context
  }
  uri = URI("#{self.config.pdp_url}/allowed")
  req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
  req['Authorization'] = "Bearer #{config.token}"
  req.body = input.to_json

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end

  if res.code != '200'
    raise "Permit SDK got unexpected status code: #{res.code}, please check your Permit SDK class init and PDP container are configured correctly. \nRead more about setting up the PDP at https://docs.permit.io/reference/SDKs/Ruby/quickstart_ruby"
  end

  content = JSON.parse(res.body)
  content["allow"]

end

#sync_user(user) ⇒ Object



55
56
57
# File 'lib/permit.rb', line 55

def sync_user(user)
  self.api.users.sync_user(user)
end