Sha256: 2d7c50a3334a55b52eeb1d8878c3a1aebe52d8150d0670abf40b95d2b7c1d33f

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

require 'bcrypt' unless RUBY_PLATFORM == 'opal'

module Volt
  class User < Model
    field :username
    field :email
    field :name
    field :password

    # returns login field name depending on config settings
    def self.login_field
      if Volt.config.try(:public).try(:auth).try(:use_username)
        :username
      else
        :email
      end
    end

    validate login_field, unique: true, length: 8
    validate :email, email: true

    permissions(:read) do
      # Never pass the hashed_password to the client
      deny :hashed_password

      # Deny all if this isn't the owner
      deny if !id == Volt.current_user_id && !new?
    end

    if RUBY_PLATFORM == 'opal'
      validations do
        # Only validate password when it has changed
        if changed?(:password)
          # Don't validate on the server
          validate :password, length: 8
        end
      end
    end

    def password=(val)
      if Volt.server?
        # on the server, we bcrypt the password and store the result
        self._hashed_password = BCrypt::Password.create(val)
      else
        # Assign the attribute
        self._password = val
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.9.4.pre1 app/volt/models/user.rb
volt-0.9.3 app/volt/models/user.rb
volt-0.9.3.pre6 app/volt/models/user.rb
volt-0.9.3.pre5 app/volt/models/user.rb
volt-0.9.3.pre4 app/volt/models/user.rb