直接貼Code好不好? 噗!
Controller:
def uploading
files = []
for i in 1..session[:files].to_i
next if params[:gallery]["file_#{i}".to_sym].to_s.blank?
files << Gallery.create(:file => params[:gallery]["file_#{i}".to_sym]).id.to_s
end
redirect_to :action => "images", :files => files.join("_")
end
def new_upload_field
session[:files]+=1
render :update do |page|
page.insert_html :after, "file_fields", file_column_field("gallery", "file_#{session[:files]}") + "<br />"
end
end
View:
<%= javascript_include_tag :defaults %>
<% form_for(Gallery.new, :url => {:action => "uploading"}, :html => {:multipart => true}) do |f| -%>
<div id="file_fields">
<%= file_column_field("gallery", "file_#{session[:files]}")%><%= link_to_remote("Add new", :url => {:action => "new_upload_field"}) %>
</div>
<%= f.submit("Upload") %>
<% end %>
解說:
用RJS產生上傳欄位,然後每次新增一個都更新一次檔案數目
接著跑迴圈儲存物件就好了
hechian 發表在 痞客邦 留言(2) 人氣()
Rails中有個安全性漏洞,請參考
* http://manuals.rubyonrails.com/read/chapter/47
* http://www.javaeye.com/topic/58686
假設我們有個users table,表格欄位如下:
* username # 很明顯就是帳號
* password # 這就是密碼
* role # 權限名稱
而我們提供給使用者註冊的頁面只會有username跟password欄位
<%= form_tag :action => "signup" %>
<%= text_field "user", "username" %>
<%= password_field "user", "password" %>
</form>
會產生以下HTML↓
<form action="http://www.xxx.com/user/signup">
<input type="text" name="user[username]" />
<input type="password" name="user[password]" />
</form>
這時,一個使用者如果自己寫出一個表單:
<form action="http://www.xxx.com/user/signup">
<input type="text" name="user[username]" />
<input type="password" name="user[password]" />
<input type="hidden" name="user[role]" value="admin" />
</form>
然後你的後端如果是這樣:
User.create(params[:user])
哦.. 這就真的好玩了..
使用者在註冊時直接提權..
那這要怎樣處理呢?
我們可以在
app/model/user.rb
內新增這行:
attr_protected :role
這樣一來,該欄位就會確定被忽略掉而不會被新增..
不過你得做一下這道手續:
user = User.new(params[:user])
user.role = sanitize_properly(params[:user][:role])
===== 分 - 隔 - 線 =====
另外,我們可以使用
attr_accessible :username, :password
這有點類似白名單的方式,可以過濾掉沒出現的欄位...
hechian 發表在 痞客邦 留言(0) 人氣()