Class: Checkpoint::Resource::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/checkpoint/resource/token.rb

Overview

A Resource::Token is an identifier object for a Resource. It includes a type and an identifier. A Grant can be created for a Token. Concrete entities are resolved into a number of resources, and those resources’ tokens will be checked for matching grants.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id) ⇒ Token

Create a new Resource representing a domain entity or concept that would be acted upon.

Parameters:

  • type (String)

    the application-determined type of this resource. This might correspond to a model class or other type of named concept in the application. The type is always coerced to String with ‘#to_s` in case something else is supplied.

  • id (String)

    the application-resolvable identifier for this resource. For example, this might be the ID of a model object, the name of a section. The id is always coerced to String with ‘#to_s` in case something else is supplied.



26
27
28
29
# File 'lib/checkpoint/resource/token.rb', line 26

def initialize(type, id)
  @type = type.to_s
  @id = id.to_s
end

Instance Attribute Details

#idObject (readonly) Also known as: resource_id

Returns the value of attribute id.



12
13
14
# File 'lib/checkpoint/resource/token.rb', line 12

def id
  @id
end

#typeObject (readonly) Also known as: resource_type

Returns the value of attribute type.



12
13
14
# File 'lib/checkpoint/resource/token.rb', line 12

def type
  @type
end

Class Method Details

.allResource::Token

Get the special “all” Resource Token. This is a singleton that represents all resources of all types. It is used to grant permissions or roles within a zone, but not specific to a particular resource.

Returns:



36
37
38
# File 'lib/checkpoint/resource/token.rb', line 36

def self.all
  @all ||= new(Resource::ALL, Resource::ALL).freeze
end

Instance Method Details

#all?Boolean

Test whether this token is for the special “all” Resource.

Returns:

  • (Boolean)

    true if this token represents any/all resources



43
44
45
# File 'lib/checkpoint/resource/token.rb', line 43

def all?
  type == Resource::ALL && id == Resource::ALL
end

#all_of_type?Boolean

Test whether this token is a wildcard for some specific type.

Returns:

  • (Boolean)

    true if this token has a specific type, but represents any/all resources of that type



51
52
53
# File 'lib/checkpoint/resource/token.rb', line 51

def all_of_type?
  type != Resource::ALL && id == Resource::ALL
end

#eql?(other) ⇒ Boolean Also known as: ==

Compare with another Resource for equality. Consider them to represent the same resource if ‘other` is a Resource, has the same type, and same id.

Returns:

  • (Boolean)


72
73
74
# File 'lib/checkpoint/resource/token.rb', line 72

def eql?(other)
  other.is_a?(Resource::Token) && type == other.type && id == other.id
end

#hashInteger

Returns hash code based on to_s.

Returns:

  • (Integer)

    hash code based on to_s



77
78
79
# File 'lib/checkpoint/resource/token.rb', line 77

def hash
  to_s.hash
end

#to_sString

Returns a token suitable for granting or matching this resource.

Returns:

  • (String)

    a token suitable for granting or matching this resource



66
67
68
# File 'lib/checkpoint/resource/token.rb', line 66

def to_s
  "#{type}:#{id}"
end

#tokenToken

Returns self; for convenience of taking a Resource or token.

Returns:

  • (Token)

    self; for convenience of taking a Resource or token



56
57
58
# File 'lib/checkpoint/resource/token.rb', line 56

def token
  self
end

#uriString Also known as: inspect

Returns a URI for this resource, including its type and id.

Returns:

  • (String)

    a URI for this resource, including its type and id



61
62
63
# File 'lib/checkpoint/resource/token.rb', line 61

def uri
  "resource://#{type}/#{id}"
end