用户无法使用rails中的设备更改密码

时间:2015-03-16 06:45:53

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 devise

我在rails应用程序中使用了devise。我已经为使用rolify gem的用户定义了一些角色。

只有经理可以注册,注册后,可以创建自己的助理和主管。当经理创建主管或助理时,会向他们发送带有凭据的电子邮件通知。

问题是只有管理员才能更改密码,但其他角色则不会,而不会显示错误消息。

如何让任何角色都可以更改密码?

def settings_save
    if params[:user][:assistant] == "0"
      current_user.remove_role "assistant" if current_user.has_role? :assistant
      assistants_ = current_user.company.assistants      
      assistants_.each do |assistant|
        assistant.assistant = false        
        if assistant.save(validate:false)
          assistant.remove_role "assistant"
        end
      end
      params[:assistants_email].each do |m|        
        next if m.blank?          
          if User.where(email: m).first.nil?
            password = Devise.friendly_token.first(8)
            user = User.new(company_id:current_user.company_id, email: m, password: password, assistant:true)
            if user.save(validate:false)
              user.add_role "assistant"
              UserMailer.new_assistant(user,password,current_user).deliver
            end
          else
            user = User.where(email: m).first
            user.assistant = true
            if user.save(validate:false)
              user.add_role "assistant"
            end
            #flash[:notice] = "Email Already Exists" 
            # redirect_to :back 
          end
      end
    else
      current_user.add_role "assistant"
      assistant = current_user.company.assistant.where('email !=?', current_user.email)

      assistants.each do |u|
        #u.email += rand(5..30).to_s
        u.marketer = false
        if u.save(validate: false)
          u.remove_role "assistant"
        end 
      end
    end

这是密码更改视图

  

<div class="col-xs-5 signup_container">
  <h2>Change your password</h2>

  <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
    <div class="form-fields-aligned-error">
      <%= f.error_notification %>
    </div>

    <%= f.input :reset_password_token, as: :hidden %>
    <%= f.full_error :reset_password_token %>

    <div class="form-inputs">
      <%= f.input :password, label: "New password", required: true, autofocus: true %>
      <%= f.input :password_confirmation, label: "Confirm your new password", required: true %>
    </div>

    <div class="form-actions">
      <%= f.button :submit, "Change my password" %>
    </div>
  <% end %>

  <%= render "devise/shared/links" %>
</div>

通过上面的代码,注册管理员可以创建他的助手,它工作正常,用户创建成功,我可以使用这些用户凭据登录。但是,我无法更改此用户的密码。

0 个答案:

没有答案