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 =
"3.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.
-
#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
-
#initialize(attributes = {}) ⇒ Token
constructor
A new instance of Token.
- #notify_admins? ⇒ Boolean
- #notify_users? ⇒ Boolean
-
#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.
74 75 76 |
# File 'lib/open_project/token.rb', line 74 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 |
#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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/open_project/token.rb', line 152 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 end |
#block_changes? ⇒ Boolean
127 128 129 |
# File 'lib/open_project/token.rb', line 127 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.
103 104 105 106 107 |
# File 'lib/open_project/token.rb', line 103 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
179 180 181 182 183 |
# File 'lib/open_project/token.rb', line 179 def from_json(json) load_attributes(JSON.parse(json)) rescue => e raise ParseError, "Failed to load from json: #{e}" end |
#notify_admins? ⇒ Boolean
119 120 121 |
# File 'lib/open_project/token.rb', line 119 def notify_admins? will_notify_admins? && Date.today >= self.notify_admins_at end |
#notify_users? ⇒ Boolean
123 124 125 |
# File 'lib/open_project/token.rb', line 123 def notify_users? will_notify_users? && Date.today >= self.notify_users_at end |
#reprieve_days_left ⇒ Object
Returns the number of days of reprieve left after the token has expired.
113 114 115 116 117 |
# File 'lib/open_project/token.rb', line 113 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
144 145 146 147 148 149 150 |
# File 'lib/open_project/token.rb', line 144 def restricted?(key = nil) if key restricted? && restrictions.has_key?(key) else restrictions && restrictions.length >= 1 end end |
#to_json ⇒ Object
175 176 177 |
# File 'lib/open_project/token.rb', line 175 def to_json JSON.dump(self.attributes) end |
#valid_domain?(input) ⇒ Boolean
136 137 138 139 140 141 142 |
# File 'lib/open_project/token.rb', line 136 def valid_domain?(input) if domain.is_a?(Regexp) 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
132 133 134 |
# File 'lib/open_project/token.rb', line 132 def validate_domain? version && Gem::Version.new(version) >= domain_required_from_version end |
#will_block_changes? ⇒ Boolean
90 91 92 |
# File 'lib/open_project/token.rb', line 90 def will_block_changes? self.block_changes_at end |
#will_expire? ⇒ Boolean
78 79 80 |
# File 'lib/open_project/token.rb', line 78 def will_expire? self.expires_at end |
#will_notify_admins? ⇒ Boolean
82 83 84 |
# File 'lib/open_project/token.rb', line 82 def will_notify_admins? self.notify_admins_at end |
#will_notify_users? ⇒ Boolean
86 87 88 |
# File 'lib/open_project/token.rb', line 86 def will_notify_users? self.notify_users_at end |