diff options
| author | Xinhui | 2017-08-17 15:47:55 +0200 |
|---|---|---|
| committer | Xinhui | 2017-08-17 16:03:13 +0200 |
| commit | 24862b2b1baa94c80fd7206bfe2244019604dbf5 (patch) | |
| tree | 5f6905f8b76287f061f7ba98258d0ad43fa11bf9 /app | |
| parent | 27d0fc5da357c6ba7b7b3288399c7934c30265f2 (diff) | |
| download | chouette-core-24862b2b1baa94c80fd7206bfe2244019604dbf5.tar.bz2 | |
Model ApiKey add belongs_to organisation
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/api/v1/api_key.rb | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/app/models/api/v1/api_key.rb b/app/models/api/v1/api_key.rb index e1a7ab5a4..e1cc2cc3b 100644 --- a/app/models/api/v1/api_key.rb +++ b/app/models/api/v1/api_key.rb @@ -3,19 +3,27 @@ module Api class ApiKey < ::ActiveRecord::Base before_create :generate_access_token belongs_to :referential, :class_name => '::Referential' - validates_presence_of :referential + belongs_to :organisation, :class_name => '::Organisation' + + validates_presence_of :organisation class << self def from(referential, name:) find_or_create_by!(name: name, referential: referential) end - def model_name - ActiveModel::Name.new Api::V1, self.name.demodulize - end + def referential_from_token(token) array = token.split('-') - return nil unless array.size==2 - ::Referential.find( array.first) + if !array.first.empty? && array.size > 1 + ::Referential.find array.first + end + end + + def organisation_from_token(token) + array = token.split('-') + if !array[1].empty? && array.size > 1 + ::Organisation.find array[1] + end end end @@ -28,7 +36,7 @@ module Api private def generate_access_token begin - self.token = "#{referential_id}-#{SecureRandom.hex}" + self.token = "#{referential_id}-#{organisation_id}-#{SecureRandom.hex}" end while self.class.exists?(:token => self.token) end end |
