转授权

This commit is contained in:
LIm1tZz7 committed 2026-10-03 19:35:30 +08:00
1 parent 39480e2f2e
commit a8e85b7ac0
18 files changed
+284 -57

No files matched your search

@@ -341,12 +341,20 @@ dropRoleSql name = do
pure ("DROP ROLE " ++ T.unpack role)
-- | 构造 GRANT 权限 ON 对象 TO 角色
grantPrivilegesSql :: [Text] -> Text -> Text -> Either String String
grantPrivilegesSql privileges object role = do
grantPrivilegesSql :: [Text] -> Text -> Text -> Bool -> Either String String
grantPrivilegesSql privileges object role withOption = do
words' <- privilegeWords privileges
target <- grantObject object
who <- roleName role
pure ("GRANT " ++ T.unpack (T.intercalate ", " words') ++ " ON " ++ T.unpack target ++ " TO " ++ T.unpack who)
pure
( "GRANT " ++ T.unpack (T.intercalate ", " words') ++ " ON " ++ T.unpack target
++ " TO " ++ T.unpack who ++ tailText
)
where
-- | 转授权后缀
tailText
| withOption = " WITH GRANT OPTION"
| otherwise = ""
-- | 构造 REVOKE 权限 ON 对象 FROM 角色
revokePrivilegesSql :: [Text] -> Text -> Text -> Either String String
@@ -26,11 +26,12 @@ import qualified Data.Text as T
-- server 线上协议的编解码:一行一条 JSON(UTF-8)。
-- | 一条角色授权:角色 + 具体权限 + 对象
-- | 一条角色授权:角色 + 具体权限 + 对象,可带转授权
data Grant = Grant
{ grantRole :: Text
, grantPrivilege :: Text
, grantObject :: Text
, grantable :: Bool
}
deriving (Show, Eq, Ord)
@@ -49,12 +50,17 @@ instance ToJSON Grant where
[ "role" .= grantRole grant
, "privilege" .= grantPrivilege grant
, "object" .= grantObject grant
, "grantable" .= grantable grant
]
-- | 从 JSON 读一条授权
instance FromJSON Grant where
parseJSON = withObject "Grant" $ \o ->
Grant <$> o .: "role" <*> o .: "privilege" <*> o .: "object"
Grant
<$> o .: "role"
<*> o .: "privilege"
<*> o .: "object"
<*> o .:? "grantable" .!= False
-- | 一个角色转成 JSON
instance ToJSON RoleView where