Sha256: 0520a6b9ef4747cc4ea6773de3e7c4e400c8de9f72da8aff88c6e977008e13e8

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 KB

Contents

require 'digest/sha1'

module DataCatalog
  
  class User
  
    include MongoMapper::Document

    # == Attributes

    key :name,     String
    key :email,    String
    key :role,     String
    key :_api_key, String
    timestamps!

    # == Indices

    ensure_index :email

    # == Validations
    
    validates_presence_of :name
    validates_presence_of :role
    validate :validate_role

    VALID_ROLES = %w(basic curator admin)

    def validate_role
      unless VALID_ROLES.include?(role)
        errors.add(:role, "must be in #{VALID_ROLES.inspect}")
      end
    end
    
    # == Callbacks
    
    before_create :assign_api_key
    def assign_api_key
      unless _api_key
        self._api_key = generate_api_key
      end
    end

    def generate_api_key
      salt = Config.environment_config["api_key_salt"]
      s = "#{Time.now.to_f}#{salt}#{rand(100_000_000)}#{name}#{email}"
      Digest::SHA1.hexdigest(s)
    end
    
    # == Various Instance Methods
    
    def token
      Digest::SHA1.hexdigest("#{id}/#{email}/123456789")
    end

  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
sinatra_resource-0.4.21 examples/datacatalog/models/user.rb
sinatra_resource-0.4.20 examples/datacatalog/models/user.rb
sinatra_resource-0.4.19 examples/datacatalog/models/user.rb
sinatra_resource-0.4.18 examples/datacatalog/models/user.rb
sinatra_resource-0.4.17 examples/datacatalog/models/user.rb
sinatra_resource-0.4.16 examples/datacatalog/models/user.rb
sinatra_resource-0.4.15 examples/datacatalog/models/user.rb
sinatra_resource-0.4.14 examples/datacatalog/models/user.rb
sinatra_resource-0.4.13 examples/datacatalog/models/user.rb
sinatra_resource-0.4.12 examples/datacatalog/models/user.rb
sinatra_resource-0.4.11 examples/datacatalog/models/user.rb
sinatra_resource-0.4.10 examples/datacatalog/models/user.rb
sinatra_resource-0.4.9 examples/datacatalog/models/user.rb