Ruby on Rails设计密码覆盖

时间:2017-01-18 21:26:11

标签: ruby-on-rails ruby devise

我现有的应用程序使用Devise进行用户身份验证,这对我的用户注册/确认过程很有效。用户只需输入电子邮件地址,密码和password_confirmation即可。他们收到确认电子邮件,一切都很好!

现在,我想允许account_admins能够创建属于它们的其他用户。

我已将控制器和表单连接起来并正在创建用户。但是,我发现我需要允许account_admin绕过passwordpassword_confirmation字段。 否则,account_admin用户需要使用他们为他们创建的密码向每个用户发送单独的电子邮件,这是我不喜欢的。

相反,让account_admin填写必填字段first_namelast_namephone_numberemail更有意义来创建用户和Devise创造&通过电子邮件向用户发送密码。

我已经看了很多Devise resourcesother things ppl已经完成了,但是大多数人都让我完全重新连接确认过程,我不想这样做。< / p>

所以,这是我的表单视图views/users/new.html.erb

<%= form_for @user, url: users_admin_index_path(@user) do |f| %>
   <%= f.label :first_name, "First Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.text_field :first_name, class: "form-control", :required => true, required: "" %>

   <%= f.label :last_name, "Last Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.text_field :last_name, class: "form-control", :required => true, required: "" %>

   <%= f.label :email, "Email", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.email_field :email, class: "form-control", :required => true, required: "" %>

   <%= f.label :phone_number, "Phone Number", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.text_field :phone_number, class: "form-control", :required => true, required: "" %>

   <%= f.label :password, "Password", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.password_field :password, class: "form-control", :required => true, required: "" %>

   <%= f.label :password_confirmation, "Password Confirmation", class: "control-label col-md-2 col-sm-2 col-xs-12" %>
   <%= f.password_field :password_confirmation, class: "form-control", :required => true, required: "" %>

   <%= link_to "Cancel", "", class: "btn btn-danger" %>
   <%= f.submit "Submit", class: "btn btn-success" %>
<% end %>

关联的控制器方法controllers/users_controller.rb

class UsersController < ApplicationController
  before_action :get_company_and_locations

  def new
    if current_user.is_account_owner
      @user = User.new
    else
      flash[:danger] = "You do not have permission to do this action!"
    end
  end

  def create
    @user = User.new(user_params)
    @user.company_id = current_user.company.id
    if @user.save
      flash[:success] = "User succesfully created!"
      redirect_to :back
    else
      render :new
    end
  end



  private

  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
  end
end

由于我还在为User模型使用设计,我需要将以下内容添加到routes.rb

resources :users_admin, :controller => 'users'

1 个答案:

答案 0 :(得分:0)

您是否考虑过使用devise_invitable?

https://github.com/scambra/devise_invitable

这将允许您使用表单创建额外的用户数据,然后向新用户发送邀请 - gem本身有一个已删除的电子邮件,其中只有一个链接,可以通过令牌返回该站点,他们设置密码

这不是一个复杂的过程。

1将gem添加到Gemfile并运行bundle install

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
    NSLog(@"Received auth challenge");
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURL *baseURL = [NSURL URLWithString:[defaults objectForKey:@"lastSession"]];
        if([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
            NSLog(@"Trusting connection to host %@", challenge.protectionSpace.host);
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
            NSLog(@"Trusted webpage successfully");
        }
    }
}

2运行初始化程序的安装生成器

gem 'devise_invitable'

3运行模型

的生成器
rails generate devise_invitable:install

4为您的模型添加邀请

rails generate devise_invitable User

5创建迁移以添加邀请字段

  # your list of mods might be different, it will depend on what you're using
  devise  :database_authenticatable, :registerable, :omniauthable,  :recoverable, :rememberable, :trackable , :validatable , :confirmable, :invitable

打开迁移并将其添加到其中

rails g migration add_invitable_to_user 

6生成视图,以便您可以编辑邀请页

def change
    add_column :users, :invitation_token, :string
    add_column :users, :invitation_created_at, :datetime
    add_column :users, :invitation_sent_at, :datetime
    add_column :users, :invitation_accepted_at, :datetime
    add_column :users, :invitation_limit, :integer
    add_column :users, :invited_by_id, :integer
    add_column :users, :invited_by_type, :string
    add_index :users, :invitation_token, :unique => true

    # Allow null encrypted_password
    change_column_null :users, :encrypted_password, :string, true
    # Allow null password_salt (add it if you are using Devise's encryptable module)
    change_column_null :users, :password_salt, :string, true
end

7添加设计邀请的路线

rails generate devise_invitable:views

8添加邀请控制器

# you might have other controllers or put them in a different directory, but it will be similar to this
devise_for :users, :controllers => { :invitations => 'users/invitations' }

9将表单更改为邀请

class Users::InvitationsController < Devise::InvitationsController
  protected

  def invite_params
    params.permit(user: [:email, :first_name, :last_name, :phone_number, :invitation_token, :provider, :skip_invitation])
  end

  def accept_invitation_params
    params.permit(:password, :password_confirmation, :invitation_token, :first_name, :last_name, :phone_number )
  end

end