Sha256: 2c86fe151ced91f8bf40957ec55fa5edf9c98cdf598b6d843cea770222d7e941

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 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
    
    def before_create
      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

14 entries across 14 versions & 1 rubygems

Version Path
sinatra_resource-0.4.8 examples/datacatalog/models/user.rb
sinatra_resource-0.4.7 examples/datacatalog/models/user.rb
sinatra_resource-0.4.6 examples/datacatalog/models/user.rb
sinatra_resource-0.4.5 examples/datacatalog/models/user.rb
sinatra_resource-0.4.4 examples/datacatalog/models/user.rb
sinatra_resource-0.4.3 examples/datacatalog/models/user.rb
sinatra_resource-0.4.2 examples/datacatalog/models/user.rb
sinatra_resource-0.4.1 examples/datacatalog/models/user.rb
sinatra_resource-0.4.0 examples/datacatalog/models/user.rb
sinatra_resource-0.3.6 examples/datacatalog/models/user.rb
sinatra_resource-0.3.5 examples/datacatalog/models/user.rb
sinatra_resource-0.3.4 examples/datacatalog/models/user.rb
sinatra_resource-0.3.3 examples/datacatalog/models/user.rb
sinatra_resource-0.3.2 examples/datacatalog/models/user.rb