module Model.Authorize.SQL
( authorizationRow
, authorizeRow
, selectAuthorizeParent
, selectAuthorizeChild
, updateAuthorize
, insertAuthorize
, deleteAuthorize
, selectAuthorizeActivity
, makeAuthorize
) where
import qualified Language.Haskell.TH as TH
import Model.Time
import Model.SQL.Select
import Model.Party.SQL (selectParty)
import Model.Audit.SQL
import Model.Party.Types
import Model.Permission.Types
import Model.Permission.SQL
import Model.Authorize.Types
authorizationRow :: Selector
authorizationRow = selectMap (TH.ConE 'Authorization `TH.AppE`) $ accessRow "authorize_view"
makeAuthorize :: Access -> Maybe Timestamp -> Party -> Party -> Authorize
makeAuthorize a e c p = Authorize
{ authorization = Authorization
{ authorizeAccess = a
, authorizeChild = c
, authorizeParent = p
}
, authorizeExpires = e
}
authorizeRow :: Selector
authorizeRow = addSelects 'makeAuthorize
(accessRow "authorize") [SelectColumn "authorize" "expires"]
selectAuthorizeParent :: TH.Name
-> TH.Name
-> Selector
selectAuthorizeParent child ident = selectJoin '($)
[ selectMap (`TH.AppE` TH.VarE child) authorizeRow
, joinOn ("authorize.parent = party.id AND authorize.child = ${partyId $ partyRow " ++ nameRef child ++ "}")
$ selectParty ident
]
selectAuthorizeChild :: TH.Name
-> TH.Name
-> Selector
selectAuthorizeChild parent ident = selectMap (`TH.AppE` TH.VarE parent) $ selectJoin '($)
[ authorizeRow
, joinOn ("authorize.child = party.id AND authorize.parent = ${partyId $ partyRow " ++ nameRef parent ++ "}")
$ selectParty ident
]
authorizeSets :: String
-> [(String, String)]
authorizeSets a = accessSets a ++
[ ("expires", "${authorizeExpires " ++ a ++ "}")
]
authorizeKeys :: String
-> [(String, String)]
authorizeKeys a =
[ ("child", "${partyId $ partyRow $ authorizeChild $ authorization " ++ a ++ "}")
, ("parent", "${partyId $ partyRow $ authorizeParent $ authorization " ++ a ++ "}")
]
updateAuthorize :: TH.Name
-> TH.Name
-> TH.ExpQ
updateAuthorize ident a = auditUpdate ident "authorize"
(authorizeSets as)
(whereEq $ authorizeKeys as)
Nothing
where as = nameRef a
insertAuthorize :: TH.Name
-> TH.Name
-> TH.ExpQ
insertAuthorize ident a = auditInsert ident "authorize"
(authorizeKeys as ++ authorizeSets as)
Nothing
where as = nameRef a
deleteAuthorize :: TH.Name
-> TH.Name
-> TH.ExpQ
deleteAuthorize ident a = auditDelete ident "authorize"
(whereEq $ authorizeKeys as)
Nothing
where as = nameRef a
selectAuthorizeActivity :: TH.Name
-> Selector
selectAuthorizeActivity ident = selectJoin '(,)
[ selectAuditActivity "authorize"
, joinOn "audit.child = party.id"
$ selectParty ident
]