2014-01-20 57 views
1

我有一個索引頁面,通過使用布爾值來隱藏/顯示不同的作者。我遇到的問題是登錄的用戶仍然可以通過URL訪問隱藏的作者和他們的書籍。阻止用戶瀏覽隱藏的作者

如何防止當前用戶通過URL導航到隱藏的作者及其相應書籍?如果作者隱藏,是否有辦法將他們重定向回作者頁面?

目前,我使用控制器&布爾值來幫助隱藏/顯示作者或書籍從登錄用戶。有人可以請我指出正確的方向。這是我的代碼。

MODELS

class Author < ActiveRecord::Base 
    attr_accessible :name, :photo 

    has_many :books 

end 

class Book < ActiveRecord::Base 
    attr_accessible :author_id, :title, :photo 

    belongs_to :author 

end 

CONTROLLERS

class AuthorsController < ApplicationController 
    before_filter :signed_in_user, only: [:index] 
    before_filter :admin_user, only: [:edit, :update, :destroy, :new, :show] 

    respond_to :html, :js 

    ###Only displays unhidden authors to non admin users. 

    def index 
    if current_user.admin? 
     @authors = Author.all(:order => "created_at") 
    else 
     @authors = Author.where(:display => true).all(:order => "created_at") 
    end 
    end 

    private 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to (root_path), notice: "Please sign in." 
     end 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 
end 

class BooksController < ApplicationController 
    before_filter :signed_in_user, only: [:index] 
    before_filter :admin_user, only: [:edit, :update, :destroy, :new, :show] 
    before_filter :get_author 

    respond_to :html, :js 

    def get_author 
    @author = Author.find(params[:author_id]) 
    end 

    def index 
    @books = @author.books 
    end 

    private 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to (root_path), notice: "Please sign in." 
     end 
    end 

    def admin_user 
     redirect_to(root_path) unless current_user.admin? 
    end 
end 

VIEWS

Authors index.html.erb 

<% @authors.each do |author| %> 

    <%= link_to (image_tag author.photo(:medium)), 
    url_for(author_books_path(author)), 
    class: "img-rounded" %> 

<% end %> 

### How Can I prevent Users from accessing Hidden Author's Books (Index Page) 
Books index.html.erb 

    <% @books.each do |book| %> 
    <%= image_tag book.photo(:medium) %> 
    <%= book.name %> 
    <% end %> 

個ROUTES

resources :authors do 
    resources :books 
end 

SCHEMA

create_table "authors", :force => true do |t| 
    t.string "name" 
    t.boolean "display",    :default => false 
    t.datetime "created_at",        :null => false 
    t.datetime "updated_at",        :null => false 
    t.string "photo_file_name" 
    t.string "photo_content_type" 
    t.integer "photo_file_size" 
    t.datetime "photo_updated_at" 
end 

create_table "books", :force => true do |t| 
    t.integer "author_id" 
    t.string "title" 
    t.datetime "created_at",   :null => false 
    t.datetime "updated_at",   :null => false 
    t.string "photo_file_name" 
    t.string "photo_content_type" 
    t.integer "photo_file_size" 
    t.datetime "photo_updated_at" 
end 

回答

1

顯示控制器,比如,你可以寫 -

Authors#Show 
def show 
@author = Author.find(params[:id]) 
redirect_to root_url unless @author.display 
end 

在這種情況下,當用戶訪問任何作者的網址,它會檢查,看看是否是作者的顯示屬性爲真或不,並相應地重定向。

2

使用適當的授權模式,如CanCan

的關鍵部分(康康舞)將是基於用戶角色授權:

if user.role == admin 
    can :manage, :all 
else 
    can :read, Author, display: true 
end 

有一個有用的RailsCast通過使用慘慘授權步驟你。

還有其他選項,例如Declarative Authorization,或者您可以自己推出。

在作者#
2
Use a proper authorization model, such as CanCan. 

就個人而言,我不知道我同意這一點,至少鑑於該問題,你似乎是試圖解決的範圍。但是,您在/app/views/books/index.html.erb中的意見似乎表明您想在查看文件中放置一些邏輯。做不是這樣做。遵循適當的MVC體系結構,您試圖執行的操作屬於業務邏輯範疇。因此,控制這個的代碼應該在你的控制器中。

在您的/app/controllers/book_controller.rb文件中,根據作者的屬性將作者的書籍頁面的操作更改爲重定向回作者。像這樣的東西:(不知道確切的路徑是什麼):

def index 
    # Redirect if author is set to hidden 
    if [email protected] 
    redirect_to author_path 
    else 
    @books = @author.books 
    end 
end