From 28063cfffef6325fa96d387c4ceef830ed735dda Mon Sep 17 00:00:00 2001 From: nek0 Date: Sun, 19 Jul 2020 12:58:25 +0200 Subject: [PATCH] make associations --- src/Control/Role.hs | 5 ++++- src/Model/Role.hs | 22 ++++++++++++++++++++-- src/Types/Role.hs | 3 +-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Control/Role.hs b/src/Control/Role.hs index 51e893b..7f12985 100644 --- a/src/Control/Role.hs +++ b/src/Control/Role.hs @@ -27,7 +27,10 @@ roleUpdate _ _ = notImplemented roleDelete _ _ = notImplemented -roleAssociationList = notImplemented +roleAssociationList + :: MateHandler [RoleAssociation] +roleAssociationList = + selectAllRoleAssociations =<< asks rsConnection roleAssociationSubmit _ _ = notImplemented diff --git a/src/Model/Role.hs b/src/Model/Role.hs index ac802b1..796f1fb 100644 --- a/src/Model/Role.hs +++ b/src/Model/Role.hs @@ -46,8 +46,8 @@ initRole = mconcat initUserToRole :: PGS.Query initUserToRole = mconcat [ "CREATE TABLE IF NOT EXISTS \"user_to_role\" (" - , "user_id INTEGER NOT NULL," - , "role_id INTEGER NOT NULL," + , "user_id INTEGER NOT NULL REFERENCES \"user\"(\"user_id\")," + , "role_id INTEGER NOT NULL REFERENCES \"role\"(\"role_id\")," , "PRIMARY KEY (user_id, role_id)" , ")" ] @@ -261,6 +261,24 @@ queryRoleIdByCapabilities (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) conn = ] return $ (\(rid, _, _, _, _, _, _, _, _, _, _, _, _) -> rid) (head roles) + +selectAllRoleAssociations + :: PGS.Connection + -> MateHandler [RoleAssociation] +selectAllRoleAssociations conn = do + rawRoleAssocs <- liftIO $ runSelect conn ( + queryTable userToRoleTable + ) :: MateHandler + [ + ( Int + , Int + ) + ] + return $ map + (\(uid, rid) -> RoleAssociation uid rid) + rawRoleAssocs + + associateUserToRole :: Int -- ^ User id -> Int -- ^ Role id diff --git a/src/Types/Role.hs b/src/Types/Role.hs index 52c1af4..b0e339b 100644 --- a/src/Types/Role.hs +++ b/src/Types/Role.hs @@ -50,8 +50,7 @@ instance ToJSON RoleSubmit where instance FromJSON RoleSubmit data RoleAssociation = RoleAssociation - { roleAssociationId :: Int - , roleAssociationUser :: Int + { roleAssociationUser :: Int , roleAssociationRole :: Int } deriving (Generic, Show)