相同的應用程序,不同的問題。我正在使用Dan Benjamin「Meet Sinatra」截屏視頻作爲參考。我試圖包含一個自定義身份驗證模塊,它位於一個lib文件夾(lib/authentication.rb)中。我在我的代碼頂部需要該行,但是當我嘗試加載頁面時,它宣稱沒有要加載的文件。試圖包含自定義模塊時加載錯誤
有什麼想法?
這是我的主要西納特拉文件的頂部:
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")
class Entry
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
property :email, String
property :created_at, DateTime
end
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
helpers do
include Sinatra::Authorization
end
和實際的模塊:
module Sinatra
module Authorization
def auth
@auth ||= Rack::Auth::Basic::Request.new(request.env)
end
def unauthorized!(realm="Short URL Generator")
headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
throw :halt, [ 401, 'Authorization Required' ]
end
def bad_request!
throw :halt, [ 400, 'Bad Request' ]
end
def authorized?
request.env['REMOTE_USER']
end
def authorize(username, password)
if (username=='topfunky' && password=='peepcode') then
true
else
false
end
end
def require_admin
return if authorized?
unauthorized! unless auth.provided?
bad_request! unless auth.basic?
unauthorized! unless authorize(*auth.credentials)
request.env['REMOTE_USER'] = auth.username
end
def admin?
authorized?
end
end
end
然後,在任何我想要保護的處理程序,我把「require_admin。」
你可以發佈你正在使用的確切代碼來嘗試和加載該文件嗎? –
在文本的末尾和代碼的開頭之間添加了一個空白行,以便將所有代碼格式化爲代碼。 – 7stud