2017-06-02 3 views
-1

Ich erstelle eine Datenbank-Website. Um jede Bearbeitung vornehmen zu können, muss ein Benutzer auf der Site angemeldet sein. Ein Benutzer kann auch angemeldet werden. Momentan können Sie sich an- und abmelden. Wenn Sie sich anmelden möchten, müssen Sie sich abmelden und sich anmelden.Rails: Spezifische authentifizierte Benutzer

Ich möchte diese Authentifizierung ändern, wo ein bestimmter Benutzer, der angemeldet ist, einen Benutzer anmelden kann, um auf die Anmeldeseite zuzugreifen. Mit anderen Worten, die Anmeldeoption wird nur angezeigt, wenn ein Administrator angemeldet ist und der Administrator ein Konto für einen neuen Benutzer erstellt. Wie würde ich meinen Code ändern, um das zu tun?

new.html.erb/Bestätigung

<h2>Resend confirmation instructions</h2> 

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Resend confirmation instructions" %> 
    </div> 
<% end %> 

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

new.html.erb/registration

<article class = "sign" > 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :password %> 
    <% if @minimum_password_length %> 
    <em>(<%= @minimum_password_length %> characters minimum)</em> 
    <% end %><br /> 
    <%= f.password_field :password, autocomplete: "off" %> 
    </div> 

    <div class="field"> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation, autocomplete: "off" %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 
<%= render "devise/shared/links" %> 
</article> 

Benutzermigration

class DeviseCreateUsers < ActiveRecord::Migration[5.1] 
    def change 
    create_table :users do |t| 
     ## Database authenticatable 
     t.string :email,    null: false, default: "" 
     t.string :encrypted_password, null: false, default: "" 

     ## Recoverable 
     t.string :reset_password_token 
     t.datetime :reset_password_sent_at 

     ## Rememberable 
     t.datetime :remember_created_at 

     ## Trackable 
     t.integer :sign_in_count, default: 0, null: false 
     t.datetime :current_sign_in_at 
     t.datetime :last_sign_in_at 
     t.string :current_sign_in_ip 
     t.string :last_sign_in_ip 

     ## Confirmable 
     # t.string :confirmation_token 
     # t.datetime :confirmed_at 
     # t.datetime :confirmation_sent_at 
     # t.string :unconfirmed_email # Only if using reconfirmable 

     ## Lockable 
     # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 
     # t.string :unlock_token # Only if unlock strategy is :email or :both 
     # t.datetime :locked_at 


     t.timestamps null: false 
    end 

    add_index :users, :email,    unique: true 
    add_index :users, :reset_password_token, unique: true 
    # add_index :users, :confirmation_token, unique: true 
    # add_index :users, :unlock_token,   unique: true 
    end 
end 

Benutzermodell

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 
erstellen

edit.html.erb/registration

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %>  </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password, autocomplete: "off" %> 
    <% if @minimum_password_length %> 
     <br /> 
     <em><%= @minimum_password_length %> characters minimum</em> 
    <% end %> 
    </div> 

    <div class="field"> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation, autocomplete: "off" %> 
    </div> 

    <div class="field"> 
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password, autocomplete: "off" %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Update" %> 
    </div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p> 

<%= link_to "Back", :back %> 

new.html.erb/sessions

<body> 
    <div class = "head"> 
     <h1>Log In</h1> 

     <div class = "image1" > 
      <img src= "http://dx.deucex.com/i/logo.png" > 
     </div> 
    </div> 

</body> 

<article class = "sign"> 

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password, autocomplete: "off" %> 
    </div> 

    <% if devise_mapping.rememberable? -%> 
    <div class="field"> 
     <%= f.check_box :remember_me %> 
     <%= f.label :remember_me %> 
    </div> 
    <% end -%> 

    <div class="actions"> 
    <%= f.submit "Log in" %> 
    </div> 
<% end %> 

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

</article> 

Beispiel Stück Code, wo ein Benutzer kann nur dann, wenn in

... 

<% if user_signed_in? %> 
        <%= button_to "New Vendor", new_vendor_path, :method => "get" %> 
      <% end %> 
      <%= button_to "Inventory", inventories_path, :method => "get" %> 

      <%= form_tag vendors_path, :method => "get" do %> 
      <%= text_field_tag :search, params[:search] %> 
      <%= submit_tag "Search", :contact_name => nil %> 
     <% end %> 
unterzeichnet bearbeiten

...

Antwort

0

Der Ort, um diese Authentifizierung zu tun ist in der Kontrolle er. Haben Sie die Controller-Aktion für die neuen und erstellen Registrierungsüberprüfungs

if user_signed_in? 
    #render page or do action 
else 
    #render 403 not permitted, or redirect to a default landing page. 
end 

Nur die Validierung auf dem Frontend tun öffnet man auf eine „clevere“ person nur die Seite über eine direkte URL zugreifen oder bei Einreichung einer erstellen Aktion direkt an der Server, ohne Erlaubnis.

Verwandte Themen