aboutsummaryrefslogtreecommitdiffstats
path: root/app
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
parent845a400042115db5c3f7c1b77a685c5ece177d83 (diff)
downloadchouette-core-3a4ee94cd91de749521ac190656c65266861270f.tar.bz2
make api_key persistent
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/chouette_controller.rb12
-rw-r--r--app/controllers/api/v1/lines_controller.rb1
-rw-r--r--app/controllers/api/v1/networks_controller.rb4
-rw-r--r--app/models/api/v1/api_key.rb32
-rw-r--r--app/models/referential.rb1
5 files changed, 24 insertions, 26 deletions
diff --git a/app/controllers/api/v1/chouette_controller.rb b/app/controllers/api/v1/chouette_controller.rb
index 1264e3484..98f15414b 100644
--- a/app/controllers/api/v1/chouette_controller.rb
+++ b/app/controllers/api/v1/chouette_controller.rb
@@ -1,18 +1,20 @@
module Api
module V1
class ChouetteController < ActionController::Base
+ inherit_resources
respond_to :json, :xml
layout false
before_filter :authenticate
- def referential
- @referential ||= @api_key.referential
- end
+ belongs_to :referential
+
private
+ alias_method :referential, :parent
+
def authenticate
authenticate_or_request_with_http_token do |token, options|
- @api_key = ApiKey.new(token)
- switch_referential if @api_key.exists?
+ @api_key = referential.api_keys.find_by_token(token)
+ switch_referential if @api_key
end
end
def switch_referential
diff --git a/app/controllers/api/v1/lines_controller.rb b/app/controllers/api/v1/lines_controller.rb
index 1df8a6618..9e58ea46b 100644
--- a/app/controllers/api/v1/lines_controller.rb
+++ b/app/controllers/api/v1/lines_controller.rb
@@ -1,7 +1,6 @@
module Api
module V1
class LinesController < ChouetteController
- inherit_resources
defaults :resource_class => Chouette::Line, :finder => :find_by_objectid!
diff --git a/app/controllers/api/v1/networks_controller.rb b/app/controllers/api/v1/networks_controller.rb
index 0b014e27b..c69c46433 100644
--- a/app/controllers/api/v1/networks_controller.rb
+++ b/app/controllers/api/v1/networks_controller.rb
@@ -4,9 +4,13 @@ module Api
inherit_resources
defaults :resource_class => Chouette::Network, :finder => :find_by_objectid!
+ belongs_to :referential, :parent_class => ::Referential
protected
+ def parent
+ @referential ||= Referential.find(params[:referential_id])
+ end
def collection
@networks ||= referential.networks.search(params[:q]).result(:distinct => true)
end
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
diff --git a/app/models/referential.rb b/app/models/referential.rb
index 70035a32b..2e49e844b 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -20,6 +20,7 @@ class Referential < ActiveRecord::Base
has_many :imports, :dependent => :destroy
has_many :exports, :dependent => :destroy
+ has_many :api_keys, :class_name => 'Api::V1::ApiKey', :dependent => :destroy
belongs_to :organisation
validates_presence_of :organisation