2020-07-19 06:27:15 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Control.Role where
|
|
|
|
|
|
|
|
import Servant
|
|
|
|
|
2022-04-15 18:51:22 +00:00
|
|
|
import Control.Monad (void)
|
|
|
|
|
2020-07-19 06:27:15 +00:00
|
|
|
import Control.Monad.Reader (asks)
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Model
|
2022-04-15 18:52:32 +00:00
|
|
|
import Util
|
2020-07-19 06:27:15 +00:00
|
|
|
|
|
|
|
roleList :: MateHandler [Role]
|
|
|
|
roleList = do
|
|
|
|
conn <- asks rsConnection
|
|
|
|
selectAllRoles conn
|
|
|
|
|
2020-07-19 18:48:58 +00:00
|
|
|
roleNew
|
|
|
|
:: Maybe (Int, AuthMethod)
|
|
|
|
-> RoleSubmit
|
|
|
|
-> MateHandler Int
|
2020-08-24 16:52:17 +00:00
|
|
|
roleNew (Just (uid, auth)) (RoleSubmit name c1 c2 c3 c4 c5 c6 c7 c8 c9 c10) =
|
2020-07-24 05:47:35 +00:00
|
|
|
do
|
|
|
|
isRoleManager <- checkCapability uid roleCanManageRoles
|
2020-08-24 07:43:22 +00:00
|
|
|
if auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager
|
2020-07-24 05:47:35 +00:00
|
|
|
then
|
2020-08-24 16:52:17 +00:00
|
|
|
insertRole name c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 =<< asks rsConnection
|
2020-07-24 05:47:35 +00:00
|
|
|
else
|
|
|
|
throwError $ err401
|
|
|
|
{ errBody = "You are not authorized for this action."
|
|
|
|
}
|
|
|
|
roleNew Nothing _ =
|
2020-07-23 20:41:22 +00:00
|
|
|
throwError $ err401
|
|
|
|
{ errBody = "No Authentication present."
|
|
|
|
}
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-23 20:41:22 +00:00
|
|
|
roleUpdate
|
|
|
|
:: Maybe (Int, AuthMethod)
|
2020-07-24 05:47:35 +00:00
|
|
|
-> Int
|
|
|
|
-> RoleSubmit
|
2020-07-23 20:41:22 +00:00
|
|
|
-> MateHandler ()
|
2020-07-24 05:47:35 +00:00
|
|
|
roleUpdate (Just (uid, auth)) id_ roleSubmit = do
|
|
|
|
isRoleManager <- checkCapability uid roleCanManageRoles
|
2020-08-24 07:43:22 +00:00
|
|
|
if auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager
|
2020-07-24 05:47:35 +00:00
|
|
|
then
|
|
|
|
void $ updateRole id_ roleSubmit =<< asks rsConnection
|
|
|
|
else
|
|
|
|
throwError $ err401
|
|
|
|
{ errBody = "You are not authorized for this action."
|
|
|
|
}
|
|
|
|
roleUpdate Nothing _ _ =
|
2020-07-23 20:41:22 +00:00
|
|
|
throwError $ err401
|
|
|
|
{ errBody = "No Authentication present."
|
|
|
|
}
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-31 20:31:27 +00:00
|
|
|
roleDelete
|
|
|
|
:: Maybe (Int, AuthMethod)
|
|
|
|
-> Int
|
|
|
|
-> MateHandler ()
|
|
|
|
roleDelete (Just (uid, auth)) id_ = do
|
|
|
|
isRoleManager <- checkCapability uid roleCanManageRoles
|
2020-08-24 07:43:22 +00:00
|
|
|
if auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager
|
2020-07-31 20:31:27 +00:00
|
|
|
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."
|
|
|
|
}
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-19 10:58:25 +00:00
|
|
|
roleAssociationList
|
|
|
|
:: MateHandler [RoleAssociation]
|
|
|
|
roleAssociationList =
|
|
|
|
selectAllRoleAssociations =<< asks rsConnection
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-31 20:31:27 +00:00
|
|
|
roleAssociationSubmit
|
|
|
|
:: Maybe (Int, AuthMethod)
|
|
|
|
-> RoleAssociationSubmit
|
|
|
|
-> MateHandler ()
|
|
|
|
roleAssociationSubmit (Just (uid, auth)) (RoleAssociationSubmit auid arid) = do
|
|
|
|
isRoleManager <- checkCapability uid roleCanManageRoles
|
2020-08-24 07:43:22 +00:00
|
|
|
if auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager
|
2020-07-31 20:31:27 +00:00
|
|
|
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."
|
|
|
|
}
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-31 20:31:27 +00:00
|
|
|
roleAssociationDelete
|
|
|
|
:: Maybe (Int, AuthMethod)
|
|
|
|
-> RoleAssociation
|
|
|
|
-> MateHandler ()
|
|
|
|
roleAssociationDelete (Just (uid, auth)) (RoleAssociation auid arid) = do
|
|
|
|
isRoleManager <- checkCapability uid roleCanManageRoles
|
2020-08-24 07:43:22 +00:00
|
|
|
if auth `elem` [PrimaryPass, ChallengeResponse] && isRoleManager
|
2020-07-31 20:31:27 +00:00
|
|
|
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."
|
|
|
|
}
|