2020-05-10 06:35:32 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2022-04-15 13:35:53 +00:00
|
|
|
{-# LANGUAGE Arrows #-}
|
2020-05-10 06:35:32 +00:00
|
|
|
module Model.Role where
|
2020-05-16 08:02:26 +00:00
|
|
|
|
|
|
|
import qualified Database.PostgreSQL.Simple as PGS
|
|
|
|
|
2020-08-24 16:52:17 +00:00
|
|
|
import Data.Profunctor.Product (p2, p12)
|
2020-05-16 08:02:26 +00:00
|
|
|
|
2020-07-19 10:27:28 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
2020-07-24 05:47:35 +00:00
|
|
|
import Data.Int (Int64)
|
|
|
|
|
2020-05-16 08:02:26 +00:00
|
|
|
import Opaleye as O hiding (null, not)
|
|
|
|
|
2022-04-15 13:35:53 +00:00
|
|
|
import Control.Arrow
|
2020-07-19 10:27:28 +00:00
|
|
|
|
2020-07-19 04:24:22 +00:00
|
|
|
import Control.Monad
|
2020-05-16 14:12:49 +00:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
|
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Types
|
2020-08-24 16:52:17 +00:00
|
|
|
import Classes
|
2020-05-16 14:12:49 +00:00
|
|
|
|
2020-05-16 08:02:26 +00:00
|
|
|
initRole :: PGS.Query
|
|
|
|
initRole = mconcat
|
|
|
|
[ "CREATE TABLE IF NOT EXISTS \"role\" ("
|
|
|
|
, "role_id SERIAL NOT NULL,"
|
|
|
|
, "role_name TEXT NOT NULL,"
|
|
|
|
, "role_can_refill_stock BOOLEAN NOT NULL,"
|
2021-01-31 01:23:57 +00:00
|
|
|
, "role_can_view_journal BOOLEAN NOT NULL,"
|
2020-05-16 08:02:26 +00:00
|
|
|
, "role_can_pay_invoice BOOLEAN NOT NULL,"
|
|
|
|
, "role_can_pay_out BOOLEAN NOT NULL,"
|
|
|
|
, "role_can_manage_products BOOLEAN NOT NULL,"
|
|
|
|
, "role_can_manage_journal BOOLEAN NOT NULL,"
|
|
|
|
, "role_can_manage_roles BOOLEAN NOT NULL,"
|
|
|
|
, "role_can_manage_suppliers BOOLEAN NOT NULL,"
|
2020-08-24 16:52:17 +00:00
|
|
|
, "role_can_manage_avatars BOOLEAN NOT NULL,"
|
2020-05-16 08:02:26 +00:00
|
|
|
, "role_can_manage_settings BOOLEAN NOT NULL,"
|
2020-07-23 20:41:22 +00:00
|
|
|
, "PRIMARY KEY (role_id),"
|
2020-07-19 04:31:35 +00:00
|
|
|
, "UNIQUE (role_name)"
|
2020-05-16 08:02:26 +00:00
|
|
|
, ")"
|
|
|
|
]
|
|
|
|
|
|
|
|
initUserToRole :: PGS.Query
|
|
|
|
initUserToRole = mconcat
|
|
|
|
[ "CREATE TABLE IF NOT EXISTS \"user_to_role\" ("
|
2020-07-19 10:58:25 +00:00
|
|
|
, "user_id INTEGER NOT NULL REFERENCES \"user\"(\"user_id\"),"
|
|
|
|
, "role_id INTEGER NOT NULL REFERENCES \"role\"(\"role_id\"),"
|
2020-05-16 08:02:26 +00:00
|
|
|
, "PRIMARY KEY (user_id, role_id)"
|
|
|
|
, ")"
|
|
|
|
]
|
|
|
|
|
|
|
|
roleTable :: Table
|
|
|
|
( Maybe (Field SqlInt4)
|
|
|
|
, Field SqlText
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
2020-08-24 16:52:17 +00:00
|
|
|
, Field SqlBool
|
2020-05-16 08:02:26 +00:00
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
)
|
|
|
|
( Field SqlInt4
|
|
|
|
, Field SqlText
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
2020-08-24 16:52:17 +00:00
|
|
|
, Field SqlBool
|
2020-05-16 08:02:26 +00:00
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
, Field SqlBool
|
|
|
|
)
|
|
|
|
roleTable = table "role" (
|
2020-08-24 16:52:17 +00:00
|
|
|
p12
|
2020-05-16 08:02:26 +00:00
|
|
|
( tableField "role_id"
|
|
|
|
, tableField "role_name"
|
|
|
|
, tableField "role_can_refill_stock"
|
|
|
|
, tableField "role_can_view_journal"
|
|
|
|
, tableField "role_can_pay_invoice"
|
|
|
|
, tableField "role_can_pay_out"
|
|
|
|
, tableField "role_can_manage_products"
|
|
|
|
, tableField "role_can_manage_journal"
|
|
|
|
, tableField "role_can_manage_roles"
|
|
|
|
, tableField "role_can_manage_suppliers"
|
2020-08-24 16:52:17 +00:00
|
|
|
, tableField "role_can_manage_avatars"
|
2020-05-16 08:02:26 +00:00
|
|
|
, tableField "role_can_manage_settings"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
userToRoleTable :: Table
|
|
|
|
( Field SqlInt4
|
|
|
|
, Field SqlInt4
|
|
|
|
)
|
|
|
|
( Field SqlInt4
|
|
|
|
, Field SqlInt4
|
|
|
|
)
|
|
|
|
userToRoleTable = table "user_to_role" (
|
|
|
|
p2
|
|
|
|
( tableField "user_id"
|
|
|
|
, tableField "role_id"
|
|
|
|
)
|
|
|
|
)
|
2020-05-16 13:33:27 +00:00
|
|
|
|
2020-07-19 04:24:22 +00:00
|
|
|
insertInitialRoles :: PGS.Connection -> MateHandler ()
|
|
|
|
insertInitialRoles conn =
|
|
|
|
void $ liftIO $ runInsertInitialRoles conn
|
2020-05-16 13:33:27 +00:00
|
|
|
|
2020-07-19 04:24:22 +00:00
|
|
|
runInsertInitialRoles :: PGS.Connection -> IO [Int]
|
|
|
|
runInsertInitialRoles conn = do
|
|
|
|
a <- runInsert_ conn $ Insert
|
2020-05-16 13:33:27 +00:00
|
|
|
{ iTable = roleTable
|
|
|
|
, iRows =
|
|
|
|
[
|
2021-07-14 01:08:58 +00:00
|
|
|
( toFields (Nothing :: Maybe Int)
|
|
|
|
, toFields ("Administrator" :: String)
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
|
|
|
, toFields True
|
2020-05-16 13:33:27 +00:00
|
|
|
)
|
|
|
|
]
|
2020-08-24 16:52:17 +00:00
|
|
|
, iReturning = rReturning (\(id_, _, _, _, _, _, _, _, _, _, _, _) -> id_ )
|
2020-05-16 13:33:27 +00:00
|
|
|
, iOnConflict = Nothing
|
|
|
|
}
|
2020-07-19 04:24:22 +00:00
|
|
|
b <- runInsert_ conn $ Insert
|
|
|
|
{ iTable = roleTable
|
|
|
|
, iRows =
|
|
|
|
[
|
2021-07-14 01:08:58 +00:00
|
|
|
( toFields (Nothing :: Maybe Int)
|
|
|
|
, toFields ("User" :: String)
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
|
|
|
, toFields False
|
2020-07-19 04:24:22 +00:00
|
|
|
)
|
|
|
|
]
|
2020-08-24 16:52:17 +00:00
|
|
|
, iReturning = rReturning (\(id_, _, _, _, _, _, _, _, _, _, _, _) -> id_ )
|
2020-07-19 04:24:22 +00:00
|
|
|
, iOnConflict = Nothing
|
|
|
|
}
|
|
|
|
return $ a ++ b
|
2020-07-19 05:01:53 +00:00
|
|
|
|
2020-07-19 06:27:15 +00:00
|
|
|
selectAllRoles
|
|
|
|
:: PGS.Connection
|
|
|
|
-> MateHandler [Role]
|
|
|
|
selectAllRoles conn = do
|
2020-08-24 16:52:17 +00:00
|
|
|
liftIO $ map fromDatabase <$> runSelect conn (
|
2021-07-14 01:08:58 +00:00
|
|
|
selectTable roleTable
|
2020-08-24 16:52:17 +00:00
|
|
|
) :: MateHandler [Role]
|
2020-07-19 06:27:15 +00:00
|
|
|
|
2020-07-24 05:47:35 +00:00
|
|
|
selectRoleList
|
|
|
|
:: [Int]
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler [Role]
|
|
|
|
selectRoleList ids conn = do
|
2022-04-15 13:35:53 +00:00
|
|
|
liftIO $ map fromDatabase <$> runSelect conn ( proc () -> do
|
|
|
|
stuff@(id_, _, _, _, _, _, _, _, _, _, _, _) <- selectTable roleTable -< ()
|
|
|
|
restrict -< in_ (map toFields ids) id_
|
|
|
|
returnA -< stuff
|
2020-08-24 16:52:17 +00:00
|
|
|
) :: MateHandler [Role]
|
2020-07-24 05:47:35 +00:00
|
|
|
|
2020-07-19 18:48:58 +00:00
|
|
|
insertRole
|
|
|
|
:: T.Text
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
2020-08-24 16:52:17 +00:00
|
|
|
-> Bool
|
2020-07-19 18:48:58 +00:00
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> Bool
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler Int
|
2020-08-24 16:52:17 +00:00
|
|
|
insertRole name c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 conn = do
|
2020-07-19 18:48:58 +00:00
|
|
|
head <$> liftIO (runInsert_ conn $ Insert
|
|
|
|
{ iTable = roleTable
|
|
|
|
, iRows =
|
|
|
|
[
|
2021-07-14 01:08:58 +00:00
|
|
|
( toFields (Nothing :: Maybe Int)
|
|
|
|
, toFields name
|
|
|
|
, toFields c1
|
|
|
|
, toFields c2
|
|
|
|
, toFields c3
|
|
|
|
, toFields c4
|
|
|
|
, toFields c5
|
|
|
|
, toFields c6
|
|
|
|
, toFields c7
|
|
|
|
, toFields c8
|
|
|
|
, toFields c9
|
|
|
|
, toFields c10
|
2020-07-19 18:48:58 +00:00
|
|
|
)
|
|
|
|
]
|
2020-08-24 16:52:17 +00:00
|
|
|
, iReturning = rReturning (\(id_, _, _, _, _, _, _, _, _, _, _, _) -> id_ )
|
2020-07-19 18:48:58 +00:00
|
|
|
, iOnConflict = Nothing
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-07-19 05:01:53 +00:00
|
|
|
queryRoleIdByName
|
|
|
|
:: T.Text
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler Int
|
2020-07-19 10:27:28 +00:00
|
|
|
queryRoleIdByName name conn = do
|
2022-04-15 13:35:53 +00:00
|
|
|
liftIO $ roleID . fromDatabase . head <$> runSelect conn ( proc () -> do
|
|
|
|
stuff@(_, rname, _, _, _, _, _, _, _, _, _, _) <- selectTable roleTable -< ()
|
|
|
|
restrict -< toFields name .== rname
|
|
|
|
returnA -< stuff
|
2020-08-24 16:52:17 +00:00
|
|
|
) :: MateHandler Int
|
2020-07-19 05:01:53 +00:00
|
|
|
|
|
|
|
queryRoleIdByCapabilities
|
2020-08-24 16:52:17 +00:00
|
|
|
:: (Bool, Bool, Bool, Bool, Bool, Bool, Bool, Bool, Bool, Bool)
|
2020-07-19 05:01:53 +00:00
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler Int
|
2021-07-14 01:08:58 +00:00
|
|
|
queryRoleIdByCapabilities (pa1, pa2, pa3, pa4, pa5, pa6, pa7, pa8, pa9, pa10) conn =
|
2020-07-19 10:27:28 +00:00
|
|
|
do
|
2022-04-15 13:35:53 +00:00
|
|
|
liftIO $ roleID . fromDatabase . head <$> runSelect conn ( proc () -> do
|
|
|
|
stuff@(_, _, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) <- selectTable roleTable -< ()
|
|
|
|
restrict -<
|
2021-07-14 01:08:58 +00:00
|
|
|
toFields pa1 .== c1 .&&
|
|
|
|
toFields pa2 .== c2 .&&
|
|
|
|
toFields pa3 .== c3 .&&
|
|
|
|
toFields pa4 .== c4 .&&
|
|
|
|
toFields pa5 .== c5 .&&
|
|
|
|
toFields pa6 .== c6 .&&
|
|
|
|
toFields pa7 .== c7 .&&
|
|
|
|
toFields pa8 .== c8 .&&
|
|
|
|
toFields pa9 .== c9 .&&
|
|
|
|
toFields pa10 .== c10
|
2022-04-15 13:35:53 +00:00
|
|
|
returnA -< stuff
|
2020-08-24 16:52:17 +00:00
|
|
|
) :: MateHandler Int
|
2020-07-19 05:01:53 +00:00
|
|
|
|
2020-07-19 10:58:25 +00:00
|
|
|
|
|
|
|
selectAllRoleAssociations
|
|
|
|
:: PGS.Connection
|
|
|
|
-> MateHandler [RoleAssociation]
|
|
|
|
selectAllRoleAssociations conn = do
|
|
|
|
rawRoleAssocs <- liftIO $ runSelect conn (
|
2021-07-14 01:08:58 +00:00
|
|
|
selectTable userToRoleTable
|
2020-07-19 10:58:25 +00:00
|
|
|
) :: MateHandler
|
|
|
|
[
|
|
|
|
( Int
|
|
|
|
, Int
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return $ map
|
2020-08-24 07:43:22 +00:00
|
|
|
(uncurry RoleAssociation)
|
2020-07-19 10:58:25 +00:00
|
|
|
rawRoleAssocs
|
|
|
|
|
|
|
|
|
2020-07-24 05:47:35 +00:00
|
|
|
selectUserAssociations
|
|
|
|
:: Int
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler [RoleAssociation]
|
|
|
|
selectUserAssociations uid conn = do
|
2022-04-15 13:35:53 +00:00
|
|
|
rawAssocs <- liftIO $ runSelect conn( proc () -> do
|
|
|
|
stuff@(auid, _) <- selectTable userToRoleTable -< ()
|
|
|
|
restrict -< auid .== toFields uid
|
|
|
|
returnA -< stuff
|
2020-07-24 05:47:35 +00:00
|
|
|
) :: MateHandler
|
|
|
|
[
|
|
|
|
( Int
|
|
|
|
, Int
|
|
|
|
)
|
|
|
|
]
|
2020-08-24 07:43:22 +00:00
|
|
|
return $ map (uncurry RoleAssociation) rawAssocs
|
2020-07-24 05:47:35 +00:00
|
|
|
|
|
|
|
|
2020-07-19 05:01:53 +00:00
|
|
|
associateUserToRole
|
|
|
|
:: Int -- ^ User id
|
2020-07-19 09:10:00 +00:00
|
|
|
-> Int -- ^ Role id
|
|
|
|
-> PGS.Connection
|
2020-07-23 20:41:22 +00:00
|
|
|
-> MateHandler ()
|
2020-07-19 10:27:28 +00:00
|
|
|
associateUserToRole uid rid conn =
|
|
|
|
head <$> liftIO (runInsert_ conn $ Insert
|
2020-07-19 05:01:53 +00:00
|
|
|
{ iTable = userToRoleTable
|
|
|
|
, iRows =
|
|
|
|
[
|
2021-07-14 01:08:58 +00:00
|
|
|
( toFields uid
|
|
|
|
, toFields rid
|
2020-07-19 05:01:53 +00:00
|
|
|
)
|
|
|
|
]
|
2020-07-19 10:27:28 +00:00
|
|
|
, iReturning = rReturning (const ())
|
2020-07-19 05:01:53 +00:00
|
|
|
, iOnConflict = Nothing
|
2020-07-19 10:27:28 +00:00
|
|
|
})
|
2020-07-23 20:41:22 +00:00
|
|
|
|
|
|
|
|
2020-07-31 20:31:27 +00:00
|
|
|
deleteAssociation
|
|
|
|
:: Int -- ^ User id
|
|
|
|
-> Int -- ^ Role id
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler Int64
|
|
|
|
deleteAssociation uid rid conn =
|
|
|
|
liftIO $ runDelete_ conn $ Delete
|
|
|
|
{ dTable = userToRoleTable
|
|
|
|
, dWhere =
|
2021-07-14 01:08:58 +00:00
|
|
|
\(auid, arid) -> auid .== toFields uid .&& arid .== toFields rid
|
2020-07-31 20:31:27 +00:00
|
|
|
, dReturning = rCount
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-23 20:41:22 +00:00
|
|
|
updateRole
|
2020-07-24 05:47:35 +00:00
|
|
|
:: Int -- ID of the updated role
|
|
|
|
-> RoleSubmit -- The role with already updated info
|
2020-07-23 20:41:22 +00:00
|
|
|
-> PGS.Connection
|
2020-07-24 05:47:35 +00:00
|
|
|
-> MateHandler Int64
|
2021-07-14 01:08:58 +00:00
|
|
|
updateRole rid (RoleSubmit name c1 c2 c3 c4 c5 c6 c7 c8 c9 c10) conn =
|
2020-07-23 20:41:22 +00:00
|
|
|
liftIO $ runUpdate_ conn $ Update
|
|
|
|
{ uTable = roleTable
|
2020-08-24 16:52:17 +00:00
|
|
|
, uUpdateWith = updateEasy (\(id_, _, _, _, _, _, _, _, _, _, _, _) ->
|
2020-07-23 20:41:22 +00:00
|
|
|
( id_
|
2021-07-14 01:08:58 +00:00
|
|
|
, toFields name
|
|
|
|
, toFields c1
|
|
|
|
, toFields c2
|
|
|
|
, toFields c3
|
|
|
|
, toFields c4
|
|
|
|
, toFields c5
|
|
|
|
, toFields c6
|
|
|
|
, toFields c7
|
|
|
|
, toFields c8
|
|
|
|
, toFields c9
|
|
|
|
, toFields c10
|
2020-07-23 20:41:22 +00:00
|
|
|
)
|
|
|
|
)
|
2020-08-24 16:52:17 +00:00
|
|
|
, uWhere = \(id_, _, _, _, _, _, _, _, _, _, _, _) ->
|
2021-07-14 01:08:58 +00:00
|
|
|
id_ .== toFields rid
|
2020-07-23 20:41:22 +00:00
|
|
|
, uReturning = rCount
|
|
|
|
}
|
2020-07-31 20:31:27 +00:00
|
|
|
|
|
|
|
deleteRole
|
|
|
|
:: Int
|
|
|
|
-> PGS.Connection
|
|
|
|
-> MateHandler Int64
|
|
|
|
deleteRole rid conn =
|
|
|
|
liftIO $ runDelete_ conn $ Delete
|
|
|
|
{ dTable = roleTable
|
|
|
|
, dWhere =
|
2021-07-14 01:08:58 +00:00
|
|
|
\(id_, _, _, _, _, _, _, _, _, _, _, _) -> id_ .== toFields rid
|
2020-07-31 20:31:27 +00:00
|
|
|
, dReturning = rCount
|
|
|
|
}
|