如何在拼写错误申请中成功登录?

时间:2017-07-27 23:19:11

标签: ruby-on-rails ruby

我可以通过修复Gemfile中的错误

来查看heroku中的输出

Errors in Gemfile

此外,我可以以管理员身份登录...但只有一次...然后我的密码无法识别并登录 不成功...... 我无法在管理页面上保存我的密码(错误:密码没有匹配确认)并且无法登录 使用提供的管理员密码(错误:登录失败)

我尝试手动创建管理员和密码,但却出错了。

如何使用管理员凭据登录?

感谢

错误和相关文件如下

//错误

typo-1 (master) $ rails console
Loading development environment (Rails 3.0.17)
1.9.3-p551 :001 > User.create(:username => 'admin', 
1.9.3-p551 :002 >       :password => 'abc123', 
1.9.3-p551 :003 >       :password_confirmation => 'abc123') 
ActiveRecord::UnknownAttributeError: unknown attribute: username
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1565:in `block in attributes='
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `each'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `attributes='
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1412:in `initialize'
        from /home/ubuntu/workspace/typo-1/app/models/user.rb:53:in `initialize'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `new'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `create'
        from (irb):1
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:44:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:8:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
1.9.3-p551 :004 > User.create(:username => 'admin',                                                                
1.9.3-p551 :005 >       :password_confirmation => 'abc123')                                                        
ActiveRecord::UnknownAttributeError: unknown attribute: username
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1565:in `block in attributes='
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `each'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1561:in `attributes='
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:1412:in `initialize'
        from /home/ubuntu/workspace/typo-1/app/models/user.rb:53:in `initialize'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `new'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/activerecord-3.0.17/lib/active_record/base.rb:502:in `create'
        from (irb):4
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:44:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands/console.rb:8:in `start'
        from /usr/local/rvm/gems/ruby-1.9.3-p551/gems/railties-3.0.17/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
1.9.3-p551 :006 > 

//控制器/管理/ users_controller.rb

    class Admin::UsersController < Admin::BaseController
      cache_sweeper :blog_sweeper

      def index
        @users = User.order('login asc').page(params[:page]).per(this_blog.admin_display_elements)
      end

      def new
        @user = User.new
        @user.attributes = params[:user]
        @user.text_filter = TextFilter.find_by_name(this_blog.text_filter)
        setup_profiles
        @user.name = @user.login
        if request.post? and @user.save
          flash[:notice] = _('User was successfully created.')
          redirect_to :action => 'index'
        end
      end



    def edit
        @user = params[:id] ? User.find_by_id(params[:id]) : current_user

        setup_profiles
        @user.attributes = params[:user]
        if request.post? and @user.save
          if @user.id = current_user.id
            current_user = @user
          end


        flash[:notice] = _('User was successfully updated.')
              redirect_to :action => 'index'
        end
      end

      def destroy
        @record = User.find(params[:id])
        return(render 'admin/shared/destroy') unless request.post?

        @record.destroy if User.count > 1
        redirect_to :action => 'index'
      end

      private

      def setup_profiles
        @profiles = Profile.find(:all, :order => 'id')
      end
    end

//模型/ user.rb

    require 'digest/sha1'

    # Typo user.
    class User < ActiveRecord::Base
      include ConfigManager

      belongs_to :profile
      belongs_to :text_filter



     delegate :name, :to => :text_filter, :prefix => true
      delegate :label, :to => :profile, :prefix => true

      has_many :notifications, :foreign_key => 'notify_user_id'
      has_many :notify_contents, :through => :notifications,
        :source => 'notify_content',
        :uniq => true

      has_many :articles, :order => 'created_at DESC'



    serialize :settings, Hash

      # Settings
      setting :notify_watch_my_articles,   :boolean, true
      setting :editor,                     :string, 'visual'
      setting :firstname,                  :string, ''
      setting :lastname,                   :string, ''
      setting :nickname,                   :string, ''



    setting :description,                :string, ''
      setting :url,                        :string, ''
      setting :msn,                        :string, ''
      setting :aim,                        :string, ''
      setting :yahoo,                      :string, ''
      setting :twitter,                    :string, ''
      setting :jabber,                     :string, ''
      setting :show_url,                   :boolean, false
      setting :show_msn,                   :boolean, false
      setting :show_aim,                   :boolean, false
      setting :show_yahoo,                 :boolean, false



    setting :show_twitter,               :boolean, false
      setting :show_jabber,                :boolean, false
      setting :admin_theme,                :string,  'blue'

      # echo "typo" | sha1sum -
      class_attribute :salt

      def self.salt
        '20ac4d290c2293702c64b3b287ae5ea79b26a5c1'
      end



    attr_accessor :last_venue

      def initialize(*args)
        super
        self.settings ||= {}
      end


      def self.authenticate(login, pass)
        find(:first,


        :conditions => ["login = ? AND password = ? AND state = ?", login, password_hash(pass), 'active'])
      end

      def update_connection_time
        self.last_venue = last_connection
        self.last_connection = Time.now
        self.save
      end



    # These create and unset the fields required for remembering users between browser closes
      def remember_me
        remember_me_for 2.weeks
      end

      def remember_me_for(time)
        remember_me_until time.from_now.utc
      end

      def remember_me_until(time)
        self.remember_token_expires_at = time
        self.remember_token            = Digest::SHA1.hexdigest("#{email}--#{remember_token_expires_at}")
        save(:validate => false)
      end

      def forget_me
        self.remember_token_expires_at = nil
        self.remember_token            = nil
        save(:validate => false)
      end

      def permalink_url(anchor=nil, only_path=false)
        blog = Blog.default # remove me...

        blog.url_for(
          :controller => 'authors',


         :action => 'show',
          :id => login,
          :only_path => only_path
        )
      end

      def self.authenticate?(login, pass)
        user = self.authenticate(login, pass)
        return false if user.nil?
        return true if user.login == login



     false
      end

      def self.find_by_permalink(permalink)
        self.find_by_login(permalink).tap do |user|
          raise ActiveRecord::RecordNotFound unless user
        end
      end

      def project_modules
        profile.project_modules
      end

      # Generate Methods takes from AccessControl rules
      # Example:
      #
      #   def publisher?
      #     profile.label == :publisher
      #   end



    AccessControl.roles.each do |role|
        define_method "#{role.to_s.downcase}?" do
          profile.label.to_s.downcase == role.to_s.downcase
        end
      end



    def self.to_prefix
        'author'
      end

      def simple_editor?
        editor == 'simple'



    end

      def password=(newpass)
        @password = newpass
      end



    def password(cleartext = nil)
        if cleartext
          @password.to_s
        else
          @password || read_attribute("password")


     end
      end

      def article_counter
        articles.size

  end



def display_name
    name
  end

  def permalink
    login
  end

  def to_param
    permalink
  end

  def admin?
    profile.label == Profile::ADMIN
  end

  protected

  # Apply SHA1 encryption to the supplied password.
  # We will additionally surround the password with a salt
  # for additional security.
  def self.password_hash(pass)
    Digest::SHA1.hexdigest("#{salt}--#{pass}--")
  end

  def password_hash(pass)
    self.class.password_hash(pass)
  end

  before_create :crypt_password

  # Before saving the record to database we will crypt the password
  # using SHA1.
  # We never store the actual password in the DB.
  # But before the encryption, we send an email to user for he can remind his
  # password
  def crypt_password
    send_create_notification
    write_attribute "password", password_hash(password(true))
    @password = nil
  end

  before_update :crypt_unless_empty

  # If the record is updated we will check if the password is empty.
  # If its empty we assume that the user didn't want to change his
  # password and just reset it to the old value.
  def crypt_unless_empty
    if password(true).empty?
      user = self.class.find(self.id)
      write_attribute "password", user.password
    else
      crypt_password
    end
  end

  before_validation :set_default_profile

  def set_default_profile
    if User.count.zero?
      self.profile ||= Profile.find_by_label('admin')
    else
      self.profile ||= Profile.find_by_label('contributor')
    end
  end

  validates_uniqueness_of :login, :on => :create
  validates_uniqueness_of :email, :on => :create
  validates_length_of :password, :within => 5..40, :if => Proc.new { |user|
    user.read_attribute('password').nil? or user.password.to_s.length > 0
  }

  validates_presence_of :login
  validates_presence_of :email

  validates_confirmation_of :password
  validates_length_of :login, :within => 3..40


  private

  # Send a mail of creation user to the user create
  def send_create_notification
    begin
      email_notification = NotificationMailer.notif_user(self)
      EmailNotify.send_message(self, email_notification)
    rescue => err
      logger.error "Unable to send notification of create user email: #{err.inspect}"
    end
  end
end

//编辑:将user_id替换为登录

$ rails console
Loading development environment (Rails 3.0.17)
1.9.3-p551 :001 > User.create(login: 'admin', password: 'password123', password_confirmation: 'password123')
 => #<User id: nil, login: "admin", password: nil, email: nil, name: nil, notify_via_email: nil, notify_on_new_articles: nil, notify_on_comments: nil, profile_id: 3, remember_token: nil, remember_token_expires_at: nil, text_filter_id: "1", state: "active", last_connection: nil, settings: {}> 
1.9.3-p551 :002 > exit

2 个答案:

答案 0 :(得分:0)

根据您的错误消息,您的username模型中没有User字段。

ActiveRecord::UnknownAttributeError: unknown attribute: username

我认为您在模型中使用属性login。尝试将username替换为login,看看是否有效。

User.create(login: 'admin', password: 'password123', password_confirmation: 'password123')

或者您可以发布用户模型的架构,我们可以看看。

答案 1 :(得分:0)

Typo是一款古老的传统不安全应用程序,没有人特别支持。

此版本首先不支持电子邮件和登录凭据。

这就是为什么你只能使用初始凭据登录并且如果你想续订这个凭证就会出错。

最新版的拼写错误(现称为publify):

https://github.com/publify/publify

这个版本应该支持电子邮件并且应该维护。