aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/api
diff options
context:
space:
mode:
authorMarc Florisson2012-12-13 16:03:53 +0100
committerMarc Florisson2012-12-13 16:03:53 +0100
commit3a4ee94cd91de749521ac190656c65266861270f (patch)
tree33cbf422cd46bc5c7095d9a466f940f62f83e90b /app/models/api
parent845a400042115db5c3f7c1b77a685c5ece177d83 (diff)
downloadchouette-core-3a4ee94cd91de749521ac190656c65266861270f.tar.bz2
make api_key persistent
Diffstat (limited to 'app/models/api')
-rw-r--r--app/models/api/v1/api_key.rb32
1 files changed, 12 insertions, 20 deletions
diff --git a/app/models/api/v1/api_key.rb b/app/models/api/v1/api_key.rb
index af029e5f7..0b045e58f 100644
--- a/app/models/api/v1/api_key.rb
+++ b/app/models/api/v1/api_key.rb
@@ -1,28 +1,20 @@
module Api
module V1
- class ApiKey
- def initialize(token)
- @organisation_id, @referential_id = token.split('-')
- end
- def self.create( organisation, referential)
- ApiKey.new( "#{organisation.id}-#{referential.id}")
- end
- def token
- "#{@organisation_id}-#{@referential_id}"
- end
- def exists?
- organisation && referential
- end
- def referential_slug
- referential.slug
- end
- def referential
- @referential ||= organisation.referentials.find_by_id @referential_id
- end
+ class ApiKey < ::ActiveRecord::Base
+ before_create :generate_access_token
+ belongs_to :referential, :class_name => '::Referential'
+
def eql?(other)
- other.token == self.token
+ other.token == self.token && other.referential_id == self.referential_id
end
+
private
+ def generate_access_token
+ begin
+ self.token = SecureRandom.hex
+ puts "self.token=#{self.token}"
+ end while self.class.exists?(:token => self.token)
+ end
def organisation
@organisation ||= Organisation.find_by_id @organisation_id
end