2017-06-02 76 views
-1

我正在創建數據庫網站。爲了進行任何編輯,用戶需要登錄該網站。用戶也可以註冊。目前,您可以登錄/註銷。如果你想註冊,用戶必須註銷,然後才能註冊。Rails:特定身份驗證用戶

我想更改此身份驗證,爲了訪問註冊頁面,某個登錄的用戶可以註冊一個用戶。換句話說,除非管理員登錄並且管理員爲新用戶創建帳戶,否則註冊選項不會顯示。我將如何更改我的代碼來執行此操作?

new.html.erb /確認

<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 /登記

<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> 

創建用戶遷移

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 

用戶模型

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

edit.html.erb /登記

<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 /會話

<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> 

的代碼示例片,其中用戶只能編輯如果在

... 

<% 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 %> 
簽署

...

回答

0

執行此認證的地方在控制中呃。有兩個新的控制器動作,並創建登記檢查

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

只有做驗證的前端可能讓你一個「聰明」的人只是通過直接鏈接訪問該網頁,或者直接提交創建行動服務器,未經許可。