completed roles and associations for now

This commit is contained in:
nek0 2020-07-31 22:31:27 +02:00
parent b80fc8786b
commit b3cb5114ae
3 changed files with 79 additions and 5 deletions

View file

@ -81,9 +81,9 @@ type MateAPI = "v1" :> (
:> Get '[JSON] [RoleAssociation] :> Get '[JSON] [RoleAssociation]
:<|> "role" :> "association" :> AuthProtect "header-auth" :<|> "role" :> "association" :> AuthProtect "header-auth"
:> ReqBody '[JSON] RoleAssociationSubmit :> ReqBody '[JSON] RoleAssociationSubmit
:> Post '[JSON] Int :> Post '[JSON] ()
:<|> "role" :> "association" :> AuthProtect "header-auth" :<|> "role" :> "association" :> AuthProtect "header-auth"
:> ReqBody '[JSON] Int :> ReqBody '[JSON] RoleAssociation
:> Delete '[JSON] () :> Delete '[JSON] ()
:<|> "meta" :> Get '[JSON] MetaInformation :<|> "meta" :> Get '[JSON] MetaInformation

View file

@ -59,16 +59,64 @@ roleUpdate Nothing _ _ =
{ errBody = "No Authentication present." { 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 roleAssociationList
:: MateHandler [RoleAssociation] :: MateHandler [RoleAssociation]
roleAssociationList = roleAssociationList =
selectAllRoleAssociations =<< asks rsConnection 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 -- | This is the internal function to check a users authorization to do certain
-- actions -- actions

View file

@ -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 updateRole
:: Int -- ID of the updated role :: Int -- ID of the updated role
-> RoleSubmit -- The role with already updated info -> 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 id_ .== C.constant rid
, uReturning = rCount , 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
}