我正在尝试用Yesod (博客应用)做一个非常简单的应用程序,并跟随https://www.youtube.com/watch?v=SadfV-qbVg8 (我使用了脚手架网站)
我想添加一个简单的身份验证,以确保文章创建的访问权限。
在http://www.yesodweb.com/book/authentication-and-authorization之后,我添加了:
-- Routes not requiring authentication.
isAuthorized (AuthR _) _ = return Authorized
isAuthorized FaviconR _ = return Authorized
isAuthorized RobotsR _ = return Authorized
isAuthorized PublishArticleR _ = isAdmin
-- Default to Authorized for now.
isAuthorized _ _ = return Authorized我的新路线是PublishArticleR。isAdmin函数与书中的相同:
isAdmin = do
mu <- maybeAuthId
return $ case mu of
Nothing -> AuthenticationRequired
Just "admin" -> Authorized
Just _ -> Unauthorized "You must be an admin"而且它不编译:
Foundation.hs:76:38:
No instance for (IsString UserId) arising from a use of ‘isAdmin’
In the expression: isAdmin
In an equation for ‘isAuthorized’:
isAuthorized PublishArticleR _ = isAdmin
In the instance declaration for ‘Yesod App’我不明白我做错了什么,…
谢谢,
编辑:
关于我的AuthId的更多信息,定义如下:
type AuthId App = UserId我的模型是:
User
ident Text
password Text Maybe
UniqueUser ident
deriving Typeable我想检查ident属性是否等于授权发布新文章的东西(例如我的电子邮件地址)。
发布于 2015-06-22 07:59:44
如果用户经过身份验证,maybeAuthId将返回一个AuthId对象。在yesod书中的示例中,AuthId只是Text的同义词:它只是一个用户名。Text对象(以及具有IsString实例的其他类型)可以从字符串文本构建,这就是为什么示例代码工作的原因: Haskell知道如何转换。
“管理”
转换为Text对象。
您正在使用更复杂的类型来表示登录用户,因此您需要为用户提供一个IsString实例(比方说,它将生成一个没有密码的用户):
instance IsString User where
fromString s = User (pack s) ""或者,也许更容易修改isAdmin函数,以获得User对象的ident部分,如下所示:
isAdmin = do
mu <- maybeAuthId
return $ case mu of
Nothing -> AuthenticationRequired
Just (User ident _) -> case ident of
"admin" -> Authorized
_ -> Unauthorized "You must be an admin"编辑:我误解了你对AuthId的定义,我以为它是
type AuthId App = User实际上,您拥有的是UserID,它是数据库中用户对象的ID。所以你可以做两件事:预先计算拥有管理特权的用户的id列表,看看用户ID maybeAuthId给你的是其中之一,或者从给定的ID读取数据库中的用户,看看他是否有权限.
https://stackoverflow.com/questions/30938041
复制相似问题