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/plans.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

LEGACY_ENTERPRISE_PLAN_FEATURES =
i[
  baseline_comparison
  board_view
  conditional_highlighting
  custom_actions
  custom_field_hierarchies
  customize_life_cycle
  date_alerts
  define_custom_style
  edit_attribute_groups
  gantt_pdf_export
  grid_widget_wp_graph
  ldap_groups
  one_drive_sharepoint_file_storage
  placeholder_users
  project_list_sharing
  readonly_work_packages
  sso_auth_providers
  team_planner_view
  two_factor_authentication
  virus_scanning
  work_package_query_relation_columns
  work_package_sharing
  work_package_subject_generation
].freeze
FEATURES_PER_PLAN =
{
  legacy_enterprise: LEGACY_ENTERPRISE_PLAN_FEATURES
}.freeze
AVAILABLE_PLAN_NAMES =
FEATURES_PER_PLAN.keys.sort.freeze
DEFAULT_PLAN =

default plan that is assigned to a token if no plan is given (especially legacy tokens)

:legacy_enterprise
VERSION =
"5.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.



23
24
25
# File 'lib/open_project/token.rb', line 23

def extractor
  @extractor
end

.keyObject

Returns the value of attribute key.



23
24
25
# File 'lib/open_project/token.rb', line 23

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.



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

def company
  @company
end

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

#expires_atObject

Returns the value of attribute expires_at.



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

def expires_at
  @expires_at
end

#featuresObject

Returns the value of attribute features.



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

def features
  @features
end

#issued_atObject

Returns the value of attribute issued_at.



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

def issued_at
  @issued_at
end

#mailObject

Returns the value of attribute mail.



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

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

#planObject

Returns the value of attribute plan.



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

def plan
  @plan
end

#reprieve_daysObject

Returns the value of attribute reprieve_days.



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

def reprieve_days
  @reprieve_days
end

#restrictionsObject

Returns the value of attribute restrictions.



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

def restrictions
  @restrictions
end

#starts_atObject

Returns the value of attribute starts_at.



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

def starts_at
  @starts_at
end

#subscriberObject

Returns the value of attribute subscriber.



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

def subscriber
  @subscriber
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.import(data) ⇒ Object



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

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/open_project/token.rb', line 170

def attributes
  hash = {}

  hash["version"]          = version
  hash["subscriber"]       = subscriber
  hash["mail"]             = mail
  hash["company"]          = company
  hash["domain"]           = domain
  hash["plan"]             = plan

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

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

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

  hash
end

#available_featuresObject



103
104
105
106
107
108
109
110
# File 'lib/open_project/token.rb', line 103

def available_features
  return @available_features if defined?(@available_features)

  relevant_features = OpenProject::Token::FEATURES_PER_PLAN[plan] || []
  additional_features = features || []

  @available_features = (relevant_features + additional_features).uniq
end

#block_changes?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/open_project/token.rb', line 145

def block_changes?
  will_block_changes? && Date.today >= 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)


121
122
123
124
125
# File 'lib/open_project/token.rb', line 121

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

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

#from_json(json) ⇒ Object



199
200
201
202
203
# File 'lib/open_project/token.rb', line 199

def from_json(json)
  load_attributes(JSON.parse(json))
rescue StandardError => 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)
  available_features.include?(name.to_sym)
end

#notify_admins?Boolean

Returns:

  • (Boolean)


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

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

#notify_users?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/open_project/token.rb', line 141

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

#parsed_domainObject



205
206
207
208
209
# File 'lib/open_project/token.rb', line 205

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.



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

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

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

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'lib/open_project/token.rb', line 162

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

#to_json(*_args) ⇒ Object



195
196
197
# File 'lib/open_project/token.rb', line 195

def to_json(*_args)
  JSON.dump(attributes)
end

#valid_domain?(input) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'lib/open_project/token.rb', line 154

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)


150
151
152
# File 'lib/open_project/token.rb', line 150

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?
  block_changes_at
end

#will_expire?Boolean

Returns:

  • (Boolean)


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

def will_expire?
  expires_at
end

#will_notify_admins?Boolean

Returns:

  • (Boolean)


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

def will_notify_admins?
  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?
  notify_users_at
end