Question

J'ai une table utilisateur dans ma base de données et ce que je souhaite faire, c'est avoir un mot de passe aléatoire généré pour chaque utilisateur sur Créer qui est ensuite envoyé à leur adresse e-mail.Je me demandais comment j'attribuerais le hasard Mot de passe.

J'ai ce qui suit à mon avis:

<p>
    <div id="p1"><%= t('.username')%></div>
    <%= f.text_field :username %>
</p>
<p>
    <div id="p1"><%= t('.email')%></div>
    <%= f.text_field :email %>
</p>
<p class="button"><%= f.submit 'Create Account' %></p>

Les éléments suivants dans mon contrôleur:

def create
 @user = User.new(params[:user])
 respond_to do |format|
   if @user.save
    Notifier.user_created(@user).deliver
       format.html { redirect_to @user, notice: 'User was successfully created.' }
       format.json { render json: @user, status: :created, location: @user }
    else
       format.html { render action: "new" }
       format.json { render json: @user.errors, status::unprocessable_entity }
    end
  end
end

Et j'ai ce qui suit dans mon modèle utilisateur:

attr_accessor :password
before_save :encrypt_password

def encrypt_password
    if password.present?
    self.password_salt = BCrypt::Engine.generate_salt
    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
   end
end

def self.random_string(len)
    #generate a random password consisting of strings and digits
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a password = ""
    1.upto(len) { |i| password << chars[rand(chars.size-1)]}
    return password
end

def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
    user
 else
    nil
 end
end

Je vais devoir supprimer mon mot de passe IF.Présente?Ligne parce que cela ne sera pas présent mais j'ai le code de chaîne aléatoire, j'ai juste besoin de l'assigner au hachage / sel.

Était-ce utile?

La solution

def create
 @user = User.new(params[:user])
 @user.password = User.random_string(10) #set it with the size of the password you want
 respond_to do |format|
   if @user.save
    Notifier.user_created(@user).deliver
       format.html { redirect_to @user, notice: 'User was successfully created.' }
       format.json { render json: @user, status: :created, location: @user }
    else
       format.html { render action: "new" }
       format.json { render json: @user.errors, status::unprocessable_entity }
    end
  end
end

amusez-vous!

Autres conseils

Je suppose que vous pouvez simplement modifier votre chiffrement_password;)

before_save :encrypt_password

def encrypt_password
    self.password = User.random_string(X) unless password.present?
    self.password_salt = BCrypt::Engine.generate_salt
    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end

EDIT: Si vous souhaitez chiffrer le mot de passe uniquement sur Créer, vous pouvez utiliser "avant_create" au lieu de "avant_save"

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top