Class: OpenProject::Token

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/open_project/token.rb,
lib/open_project/token/armor.rb,
lib/open_project/token/version.rb,
lib/open_project/token/extractor.rb

Defined Under Namespace

Modules: Armor Classes: Error, Extractor, ImportError, ParseError, ValidationError

Constant Summary collapse

VERSION =
"4.0.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Token

Returns a new instance of Token.



75
76
77
# File 'lib/open_project/token.rb', line 75

def initialize(attributes = {})
  load_attributes(attributes)
end

Class Attribute Details

.extractorObject (readonly)

Returns the value of attribute extractor.



20
21
22
# File 'lib/open_project/token.rb', line 20

def extractor
  @extractor
end

.keyObject

Returns the value of attribute key.



20
21
22
# File 'lib/open_project/token.rb', line 20

def key
  @key
end

Instance Attribute Details

#block_changes_atObject

Returns the value of attribute block_changes_at.



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

def block_changes_at
  @block_changes_at
end

#companyObject

Returns the value of attribute company.



52
53
54
# File 'lib/open_project/token.rb', line 52

def company
  @company
end

#domainObject

Returns the value of attribute domain.



52
53
54
# File 'lib/open_project/token.rb', line 52

def domain
  @domain
end

#expires_atObject

Returns the value of attribute expires_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def expires_at
  @expires_at
end

#featuresObject

Returns the value of attribute features.



57
58
59
# File 'lib/open_project/token.rb', line 57

def features
  @features
end

#issued_atObject

Returns the value of attribute issued_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def issued_at
  @issued_at
end

#mailObject

Returns the value of attribute mail.



52
53
54
# File 'lib/open_project/token.rb', line 52

def mail
  @mail
end

#notify_admins_atObject

Returns the value of attribute notify_admins_at.



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

def notify_admins_at
  @notify_admins_at
end

#notify_users_atObject

Returns the value of attribute notify_users_at.



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

def notify_users_at
  @notify_users_at
end

#reprieve_daysObject

Returns the value of attribute reprieve_days.



54
55
56
# File 'lib/open_project/token.rb', line 54

def reprieve_days
  @reprieve_days
end

#restrictionsObject

Returns the value of attribute restrictions.



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

def restrictions
  @restrictions
end

#starts_atObject

Returns the value of attribute starts_at.



53
54
55
# File 'lib/open_project/token.rb', line 53

def starts_at
  @starts_at
end

#subscriberObject

Returns the value of attribute subscriber.



52
53
54
# File 'lib/open_project/token.rb', line 52

def subscriber
  @subscriber
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.import(data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/open_project/token.rb', line 31

def import(data)
  raise ImportError, "Missing key." if key.nil?
  raise ImportError, "No token data." if data.nil?

  data = Armor.decode(data)
  json = extractor.read(data)
  attributes = JSON.parse(json)

  new(attributes)
rescue Extractor::Error
  raise ImportError, "Token value could not be read."
rescue JSON::ParserError
  raise ImportError, "Token value is invalid JSON."
rescue Armor::ParseError
  raise ImportError, "Token value could not be parsed."
end

Instance Method Details

#attributesObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/open_project/token.rb', line 157

def attributes
  hash = {}

  hash["version"]          = self.version
  hash["subscriber"]       = self.subscriber
  hash["mail"]             = self.mail
  hash["company"]          = self.company
  hash["domain"]           = self.domain

  hash["issued_at"]        = self.issued_at
  hash["starts_at"]        = self.starts_at
  hash["expires_at"]       = self.expires_at       if self.will_expire?
  hash["reprieve_days"]    = self.reprieve_days    if self.will_expire?

  hash["notify_admins_at"] = self.notify_admins_at if self.will_notify_admins?
  hash["notify_users_at"]  = self.notify_users_at  if self.will_notify_users?
  hash["block_changes_at"] = self.block_changes_at if self.will_block_changes?

  hash["restrictions"]     = self.restrictions     if self.restricted?
  hash["features"]         = self.features

  hash
end

#block_changes?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/open_project/token.rb', line 132

def block_changes?
  will_block_changes? && Date.today >= self.block_changes_at
end

#expired?(reprieve: true) ⇒ Boolean

Indicates whether or not the token has expired.

This does include a reprieve (grace period) if configured. I.e. this will return false even if ‘expires_at` has passed if `reprieve_days` is configured, as long as the current date is still within those days after the actual expiration.

Parameters:

  • reprieve (Boolean) (defaults to: true)

    Allow for reprieve (default true)

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/open_project/token.rb', line 108

def expired?(reprieve: true)
  offset = reprieve ? reprieve_days.to_i : 0

  will_expire? && Date.today >= self.expires_at.next_day(offset)
end

#from_json(json) ⇒ Object



185
186
187
188
189
# File 'lib/open_project/token.rb', line 185

def from_json(json)
  load_attributes(JSON.parse(json))
rescue => e
  raise ParseError, "Failed to load from json: #{e}"
end

#has_feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/open_project/token.rb', line 95

def has_feature?(name)
  features && features.include?(name.to_sym)
end

#notify_admins?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/open_project/token.rb', line 124

def notify_admins?
  will_notify_admins? && Date.today >= self.notify_admins_at
end

#notify_users?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/open_project/token.rb', line 128

def notify_users?
  will_notify_users? && Date.today >= self.notify_users_at
end

#parsed_domainObject



191
192
193
194
195
# File 'lib/open_project/token.rb', line 191

def parsed_domain
  @parsed_domain = read_domain!(domain) unless defined?(@parsed_domain)

  @parsed_domain
end

#reprieve_days_leftObject

Returns the number of days of reprieve left after the token has expired.

Returns:

  • Returns ‘nil` if the token hasn’t expired yet or if no reprieve was given.



118
119
120
121
122
# File 'lib/open_project/token.rb', line 118

def reprieve_days_left
  return nil unless reprieve_days.to_i > 0 && expired?(reprieve: false)

  (self.expires_at.next_day(reprieve_days.to_i) - Date.today).to_i
end

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
# File 'lib/open_project/token.rb', line 149

def restricted?(key = nil)
  if key
    restricted? && restrictions.has_key?(key)
  else
    restrictions && restrictions.length >= 1
  end
end

#to_jsonObject



181
182
183
# File 'lib/open_project/token.rb', line 181

def to_json
  JSON.dump(self.attributes)
end

#valid_domain?(input) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
# File 'lib/open_project/token.rb', line 141

def valid_domain?(input)
  if parsed_domain.is_a?(Regexp)
    parsed_domain.match?(input)
  else
    domain == input
  end
end

#validate_domain?Boolean

tokens with no version or a version lower than 2.0 don’t have the attributes company or domain

Returns:

  • (Boolean)


137
138
139
# File 'lib/open_project/token.rb', line 137

def validate_domain?
  version && Gem::Version.new(version) >= domain_required_from_version
end

#will_block_changes?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/open_project/token.rb', line 91

def will_block_changes?
  self.block_changes_at
end

#will_expire?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/open_project/token.rb', line 79

def will_expire?
  self.expires_at
end

#will_notify_admins?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/open_project/token.rb', line 83

def will_notify_admins?
  self.notify_admins_at
end

#will_notify_users?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/open_project/token.rb', line 87

def will_notify_users?
  self.notify_users_at
end