diff --git a/src/API.hs b/src/API.hs index 7d9c271..6c454fe 100644 --- a/src/API.hs +++ b/src/API.hs @@ -81,9 +81,9 @@ type MateAPI = "v1" :> ( :> Get '[JSON] [RoleAssociation] :<|> "role" :> "association" :> AuthProtect "header-auth" :> ReqBody '[JSON] RoleAssociationSubmit - :> Post '[JSON] Int + :> Post '[JSON] () :<|> "role" :> "association" :> AuthProtect "header-auth" - :> ReqBody '[JSON] Int + :> ReqBody '[JSON] RoleAssociation :> Delete '[JSON] () :<|> "meta" :> Get '[JSON] MetaInformation diff --git a/src/Control/Role.hs b/src/Control/Role.hs index 479d452..275dfa2 100644 --- a/src/Control/Role.hs +++ b/src/Control/Role.hs @@ -59,16 +59,64 @@ roleUpdate Nothing _ _ = { errBody = "No Authentication present." } -roleDelete _ _ = notImplemented +roleDelete + :: Maybe (Int, AuthMethod) + -> Int + -> MateHandler () +roleDelete (Just (uid, auth)) id_ = do + isRoleManager <- checkCapability uid roleCanManageRoles + if (auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager) + then + void $ deleteRole id_ =<< asks rsConnection + else + throwError $ err401 + { errBody = "You are not authorized for this action." + } +roleDelete Nothing _ = + throwError $ err401 + { errBody = "No Authentication present." + } roleAssociationList :: MateHandler [RoleAssociation] roleAssociationList = selectAllRoleAssociations =<< asks rsConnection -roleAssociationSubmit _ _ = notImplemented +roleAssociationSubmit + :: Maybe (Int, AuthMethod) + -> RoleAssociationSubmit + -> MateHandler () +roleAssociationSubmit (Just (uid, auth)) (RoleAssociationSubmit auid arid) = do + isRoleManager <- checkCapability uid roleCanManageRoles + if (auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager) + then + associateUserToRole auid arid =<< asks rsConnection + else + throwError $ err401 + { errBody = "You are not authorized for this action." + } +roleAssociationSubmit Nothing _ = + throwError $ err401 + { errBody = "No Authentication present." + } -roleAssociationDelete _ _ = notImplemented +roleAssociationDelete + :: Maybe (Int, AuthMethod) + -> RoleAssociation + -> MateHandler () +roleAssociationDelete (Just (uid, auth)) (RoleAssociation auid arid) = do + isRoleManager <- checkCapability uid roleCanManageRoles + if (auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager) + then + void $ deleteAssociation auid arid =<< asks rsConnection + else + throwError $ err401 + { errBody = "You are not authorized for this action." + } +roleAssociationDelete Nothing _ = + throwError $ err401 + { errBody = "No Authentication present." + } -- | This is the internal function to check a users authorization to do certain -- actions diff --git a/src/Model/Role.hs b/src/Model/Role.hs index 050ed0d..c753c07 100644 --- a/src/Model/Role.hs +++ b/src/Model/Role.hs @@ -389,6 +389,20 @@ associateUserToRole uid rid conn = }) +deleteAssociation + :: Int -- ^ User id + -> Int -- ^ Role id + -> PGS.Connection + -> MateHandler Int64 +deleteAssociation uid rid conn = + liftIO $ runDelete_ conn $ Delete + { dTable = userToRoleTable + , dWhere = + (\(auid, arid) -> auid .== C.constant uid .&& arid .== C.constant rid) + , dReturning = rCount + } + + updateRole :: Int -- ID of the updated role -> RoleSubmit -- The role with already updated info @@ -417,3 +431,15 @@ updateRole rid role@(RoleSubmit name c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11) conn = id_ .== C.constant rid , uReturning = rCount } + +deleteRole + :: Int + -> PGS.Connection + -> MateHandler Int64 +deleteRole rid conn = + liftIO $ runDelete_ conn $ Delete + { dTable = roleTable + , dWhere = + (\(id_, _, _, _, _, _, _, _, _, _, _, _, _) -> id_ .== C.constant rid) + , dReturning = rCount + }