diff options
| author | Marc Florisson | 2012-12-13 16:03:53 +0100 | 
|---|---|---|
| committer | Marc Florisson | 2012-12-13 16:03:53 +0100 | 
| commit | 3a4ee94cd91de749521ac190656c65266861270f (patch) | |
| tree | 33cbf422cd46bc5c7095d9a466f940f62f83e90b /app/models/api | |
| parent | 845a400042115db5c3f7c1b77a685c5ece177d83 (diff) | |
| download | chouette-core-3a4ee94cd91de749521ac190656c65266861270f.tar.bz2 | |
make api_key persistent
Diffstat (limited to 'app/models/api')
| -rw-r--r-- | app/models/api/v1/api_key.rb | 32 | 
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   | 
