Class: Gitlab::License

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/license.rb,
lib/gitlab/license/version.rb,
lib/gitlab/license/boundary.rb,
lib/gitlab/license/encryptor.rb

Defined Under Namespace

Modules: Boundary Classes: Encryptor, Error, ImportError, ValidationError

Constant Summary collapse

VERSION =
'1.0.1'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ License

Returns a new instance of License.



60
61
62
# File 'lib/gitlab/license.rb', line 60

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

Class Attribute Details

.encryption_keyObject

Returns the value of attribute encryption_key.



17
18
19
# File 'lib/gitlab/license.rb', line 17

def encryption_key
  @encryption_key
end

Instance Attribute Details

#block_changes_atObject

Returns the value of attribute block_changes_at.



54
55
56
# File 'lib/gitlab/license.rb', line 54

def block_changes_at
  @block_changes_at
end

#expires_atObject

Returns the value of attribute expires_at.



53
54
55
# File 'lib/gitlab/license.rb', line 53

def expires_at
  @expires_at
end

#licenseeObject

Returns the value of attribute licensee.



53
54
55
# File 'lib/gitlab/license.rb', line 53

def licensee
  @licensee
end

#notify_admins_atObject

Returns the value of attribute notify_admins_at.



54
55
56
# File 'lib/gitlab/license.rb', line 54

def notify_admins_at
  @notify_admins_at
end

#notify_users_atObject

Returns the value of attribute notify_users_at.



54
55
56
# File 'lib/gitlab/license.rb', line 54

def notify_users_at
  @notify_users_at
end

#restrictionsObject

Returns the value of attribute restrictions.



55
56
57
# File 'lib/gitlab/license.rb', line 55

def restrictions
  @restrictions
end

#starts_atObject Also known as: issued_at

Returns the value of attribute starts_at.



53
54
55
# File 'lib/gitlab/license.rb', line 53

def starts_at
  @starts_at
end

#versionObject (readonly)

Returns the value of attribute version.



52
53
54
# File 'lib/gitlab/license.rb', line 52

def version
  @version
end

Class Method Details

.encryptorObject



27
28
29
# File 'lib/gitlab/license.rb', line 27

def encryptor
  @encryptor ||= Encryptor.new(encryption_key)
end

.import(data) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/license.rb', line 31

def import(data)
  raise ImportError, 'No license data.' if data.nil?

  data = Boundary.remove_boundary(data)

  begin
    license_json = encryptor.decrypt(data)
  rescue Encryptor::Error
    raise ImportError, 'License data could not be decrypted.'
  end

  begin
    attributes = JSON.parse(license_json)
  rescue JSON::ParseError
    raise ImportError, 'License data is invalid JSON.'
  end

  new(attributes)
end

Instance Method Details

#attributesObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gitlab/license.rb', line 120

def attributes
  hash = {}

  hash['version']          = version
  hash['licensee']         = licensee

  # `issued_at` is the legacy name for starts_at.
  # TODO: Move to starts_at in a next version.
  hash['issued_at']        = starts_at
  hash['expires_at']       = expires_at       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
end

#block_changes?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/gitlab/license.rb', line 108

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

#expired?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/gitlab/license.rb', line 96

def expired?
  will_expire? && Date.today >= expires_at
end

#export(boundary: nil) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/gitlab/license.rb', line 144

def export(boundary: nil)
  validate!

  data = self.class.encryptor.encrypt(to_json)

  data = Boundary.add_boundary(data, boundary) if boundary

  data
end

#notify_admins?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/gitlab/license.rb', line 100

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

#notify_users?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/gitlab/license.rb', line 104

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

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/gitlab/license.rb', line 112

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

#to_json(*_args) ⇒ Object



140
141
142
# File 'lib/gitlab/license.rb', line 140

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

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/license.rb', line 64

def valid?
  return false if !licensee         || !licensee.is_a?(Hash) || licensee.empty?
  return false if !starts_at        || !starts_at.is_a?(Date)
  return false if expires_at        && !expires_at.is_a?(Date)
  return false if notify_admins_at  && !notify_admins_at.is_a?(Date)
  return false if notify_users_at   && !notify_users_at.is_a?(Date)
  return false if block_changes_at  && !block_changes_at.is_a?(Date)
  return false if restrictions      && !restrictions.is_a?(Hash)

  true
end

#validate!Object

Raises:



76
77
78
# File 'lib/gitlab/license.rb', line 76

def validate!
  raise ValidationError, 'License is invalid' unless valid?
end

#will_block_changes?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/gitlab/license.rb', line 92

def will_block_changes?
  block_changes_at
end

#will_expire?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/gitlab/license.rb', line 80

def will_expire?
  expires_at
end

#will_notify_admins?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/gitlab/license.rb', line 84

def will_notify_admins?
  notify_admins_at
end

#will_notify_users?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/gitlab/license.rb', line 88

def will_notify_users?
  notify_users_at
end