2011-04-26 57 views

回答

2

這樣做的最好方法是使用activescaffold插件提供的安全方法模板之一(取決於您的需要)。

模型方法:

從activescaffold維基粘貼限制任何 否則

在你的模型對象,你可以定義 方法在任何的四種格式(其中沒有接受任何 參數),根據您對 的粒度的需求而定,您可以使用 。

的格式爲:

* #{column_name}_authorized_for_#{crud_type}? 

例如,如果你有一個activescaffold基於控制器的被叫用戶:

class Admin::UsersController < ApplicationController 
    active_scaffold do |config| 
    config.columns = [:username, :name, :email] 
    end 
end 

而你只希望讓用戶能夠更新用戶名如果​​他們是管理員,那麼你可以做這樣的事情:

用戶型號:

class User < ActiveRecord::Base 

    # ActiveScaffold security template: #{column_name}_authorized_for_#{crud_type}? 
    def username_authorized_for_update? 
    # As soon as this method will return false 
    # the username field will not be available on the update form 
    return true # Write logic to decide if username field should be visible 
    end 
end 

活動腳手架維基鏈接:https://github.com/activescaffold/active_scaffold/wiki/Security

+0

這是相當不錯的。不過,我想按照記錄來做,而不是每個用戶。我不認爲這個解決方案提供了記錄級別的安全性,是嗎? – nroose 2011-05-02 16:45:07

0

whizcreed的答案是正確的,而這些ActiveScaffold安全模型方法其實每個記錄計算,所以你可以做這樣的事情在模型:

def username_authorized_for_update? 
    return true unless existing_record_check? 
    return false if userrights != 'admin' 
    return true 
end 

其中userrights是此記錄上的字符串字段(公認是不好的示例) - 但將此條件替換爲您要檢查該現有模型對象的任何內容。

2

如果您只是想隱藏更新視圖中的某些列,那麼在控制器中配置它非常容易。

要麼你可以指定你想看到的列:

class DocumentsController < ApplicationController 
    active_scaffold :document do |config| 
    config.columns = [ :id, :product, :title, :document_type, :author, :organization, :document_approver, :document_location ] 
    config.list.columns = [ :id, :product, :title, :document_type, :author ] 
    config.show.columns = [ :product, :title, :document_type, :author, :organization, :document_approver, :document_location ] 
    config.create.columns = [ :product, :title, :document_type, :document_approver, :document_location ] 
    config.update.columns = [ :product, :title, :document_type, :organization, :document_approver, :document_location ] 
    end 
end 

或者你可以排除你想隱藏的:

class DocumentsController < ApplicationController 
    active_scaffold :document do |config| 
    config.columns = [ :id, :product, :title, :document_type, :author, :organization, :document_approver, :document_location ] 
    config.list.columns.exclude :organization, :document_approver, :document_location 
    config.show.columns.exclude :id 
    config.create.columns.exclude :id, :author, :organization 
    config.update.columns.exclude :id, :author 
    end 
end 

注意「config.columns」用於定義控制器的總列數,如果沒有特別定義'list','show','create'或'update',則默認使用'config.columns'。

這也意味着,如果你想爲除「更新」的所有視圖中顯示相同的列,那麼你可以將它定義成:

class DocumentsController < ApplicationController 
    active_scaffold :document do |config| 
    config.columns = [ :id, :product, :title, :document_type, :author, :organization, :document_approver, :document_location ] 
    config.update.columns = [ :product, :title, :document_type, :organization, :document_approver, :document_location ] 
    end 
end 

或者:

class DocumentsController < ApplicationController 
    active_scaffold :document do |config| 
    config.columns = [ :id, :product, :title, :document_type, :author, :organization, :document_approver, :document_location ] 
    config.update.columns.exclude :id, :author 
    end 
end 
相關問題