Class: OpenProject::Token
- Inherits:
-
Object
- Object
- OpenProject::Token
- 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
-
.extractor ⇒ Object
readonly
Returns the value of attribute extractor.
-
.key ⇒ Object
Returns the value of attribute key.
Instance Attribute Summary collapse
-
#block_changes_at ⇒ Object
Returns the value of attribute block_changes_at.
-
#company ⇒ Object
Returns the value of attribute company.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#features ⇒ Object
Returns the value of attribute features.
-
#issued_at ⇒ Object
Returns the value of attribute issued_at.
-
#mail ⇒ Object
Returns the value of attribute mail.
-
#notify_admins_at ⇒ Object
Returns the value of attribute notify_admins_at.
-
#notify_users_at ⇒ Object
Returns the value of attribute notify_users_at.
-
#reprieve_days ⇒ Object
Returns the value of attribute reprieve_days.
-
#restrictions ⇒ Object
Returns the value of attribute restrictions.
-
#starts_at ⇒ Object
Returns the value of attribute starts_at.
-
#subscriber ⇒ Object
Returns the value of attribute subscriber.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #block_changes? ⇒ Boolean
-
#expired?(reprieve: true) ⇒ Boolean
Indicates whether or not the token has expired.
- #from_json(json) ⇒ Object
- #has_feature?(name) ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Token
constructor
A new instance of Token.
- #notify_admins? ⇒ Boolean
- #notify_users? ⇒ Boolean
- #parsed_domain ⇒ Object
-
#reprieve_days_left ⇒ Object
Returns the number of days of reprieve left after the token has expired.
- #restricted?(key = nil) ⇒ Boolean
- #to_json ⇒ Object
- #valid_domain?(input) ⇒ Boolean
-
#validate_domain? ⇒ Boolean
tokens with no version or a version lower than 2.0 don’t have the attributes company or domain.
- #will_block_changes? ⇒ Boolean
- #will_expire? ⇒ Boolean
- #will_notify_admins? ⇒ Boolean
- #will_notify_users? ⇒ Boolean
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
.extractor ⇒ Object (readonly)
Returns the value of attribute extractor.
20 21 22 |
# File 'lib/open_project/token.rb', line 20 def extractor @extractor end |
.key ⇒ Object
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_at ⇒ Object
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 |
#company ⇒ Object
Returns the value of attribute company.
52 53 54 |
# File 'lib/open_project/token.rb', line 52 def company @company end |
#domain ⇒ Object
Returns the value of attribute domain.
52 53 54 |
# File 'lib/open_project/token.rb', line 52 def domain @domain end |
#expires_at ⇒ Object
Returns the value of attribute expires_at.
53 54 55 |
# File 'lib/open_project/token.rb', line 53 def expires_at @expires_at end |
#features ⇒ Object
Returns the value of attribute features.
57 58 59 |
# File 'lib/open_project/token.rb', line 57 def features @features end |
#issued_at ⇒ Object
Returns the value of attribute issued_at.
53 54 55 |
# File 'lib/open_project/token.rb', line 53 def issued_at @issued_at end |
#mail ⇒ Object
Returns the value of attribute mail.
52 53 54 |
# File 'lib/open_project/token.rb', line 52 def mail @mail end |
#notify_admins_at ⇒ Object
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_at ⇒ Object
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_days ⇒ Object
Returns the value of attribute reprieve_days.
54 55 56 |
# File 'lib/open_project/token.rb', line 54 def reprieve_days @reprieve_days end |
#restrictions ⇒ Object
Returns the value of attribute restrictions.
56 57 58 |
# File 'lib/open_project/token.rb', line 56 def restrictions @restrictions end |
#starts_at ⇒ Object
Returns the value of attribute starts_at.
53 54 55 |
# File 'lib/open_project/token.rb', line 53 def starts_at @starts_at end |
#subscriber ⇒ Object
Returns the value of attribute subscriber.
52 53 54 |
# File 'lib/open_project/token.rb', line 52 def subscriber @subscriber end |
#version ⇒ Object (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
#attributes ⇒ Object
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
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.
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
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
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
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_domain ⇒ Object
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_left ⇒ Object
Returns the number of days of reprieve left after the token has expired.
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
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_json ⇒ Object
181 182 183 |
# File 'lib/open_project/token.rb', line 181 def to_json JSON.dump(self.attributes) end |
#valid_domain?(input) ⇒ 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
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
91 92 93 |
# File 'lib/open_project/token.rb', line 91 def will_block_changes? self.block_changes_at end |
#will_expire? ⇒ Boolean
79 80 81 |
# File 'lib/open_project/token.rb', line 79 def will_expire? self.expires_at end |
#will_notify_admins? ⇒ 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
87 88 89 |
# File 'lib/open_project/token.rb', line 87 def will_notify_users? self.notify_users_at end |