This commit is contained in:
Yann Esposito (Yogsototh) 2019-04-01 23:31:41 +02:00
parent c16e1f6962
commit ec626d5a08
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
3 changed files with 11 additions and 13 deletions

View file

@ -34,11 +34,11 @@ where
import Protolude
import Aggreact.User (User,NewUser(..),Role(..))
import Aggreact.User (NewUser (..), Role (..), User)
import Database.Store (Entity (..))
import Servant (Handler)
import Servant.Errors (unauthorized)
import Database.Store (Entity(..))
data AuthorizationStrategy =
Anybody
@ -52,7 +52,7 @@ newtype AuthorizationHandler =
}
data Scope = Scope { resource :: Text
, access :: Access }
, access :: Access }
data Access = Read | Write | AdminAccess
newAuthorizationHandler :: AuthorizationStrategy -> IO AuthorizationHandler
@ -67,7 +67,7 @@ _checkAccess Anybody (Scope _ Write) _ = pure ()
_checkAccess Anybody (Scope _ AdminAccess) (Just (Entity _ u _)) =
case role u of
Admin -> pure ()
_ -> unauthorized "Need admin privilege"
_ -> unauthorized "Need admin privilege"
_checkAccess Anybody (Scope _ AdminAccess) _ =
unauthorized "Need admin privilege"
@ -78,7 +78,7 @@ _checkAccess LoggedInOnly (Scope _ Write) (Just _) = pure ()
_checkAccess LoggedInOnly (Scope _ AdminAccess) (Just (Entity _ u _)) =
case role u of
Admin -> pure ()
_ -> unauthorized "Only for admins"
_ -> unauthorized "Only for admins"
_checkAccess ProgressiveTrust (Scope _ Read) Nothing = pure ()
_checkAccess ProgressiveTrust (Scope _ _) Nothing = unauthorized "You must log in"
@ -87,4 +87,4 @@ _checkAccess ProgressiveTrust (Scope _ Write) (Just _) = pure ()
_checkAccess ProgressiveTrust (Scope _ AdminAccess) (Just (Entity _ u _)) =
case role u of
Admin -> pure ()
_ -> unauthorized "Only for admins"
_ -> unauthorized "Only for admins"

View file

@ -48,7 +48,7 @@ import Aggreact.Html (boilerplate, cvt, urlEncode)
import qualified Crypto.Scrypt as Crypt
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Data (Data (..))
import qualified Data.UUID as UUID
import qualified Data.UUID as UUID
import Database.SQLite.Simple (NamedParam (..),
SQLData (..))
import Database.SQLite.Simple.FromField (FromField (..),
@ -156,7 +156,6 @@ instance ToField Role where toField = toField . (show :: Role -> Text)
instance (ToSQLiteFieldTypeList rest) => ToSQLiteFieldTypeList (Role ': rest) where
toSqliteTypes _ = "TEXT":toSqliteTypes (Proxy :: Proxy rest)
-- For HTTP
deriving anyclass instance ToJSON NewUser
deriving anyclass instance FromJSON NewUser
@ -282,9 +281,9 @@ getUserByNick muser userHandler userNick = do
SQL.SR (SQL.Paginated mu _ _) <- liftIO $
searchUsers userHandler (Filter {params = ["nick" := userNick]})
case mu of
[] -> Err.notFound "user not found"
[u] -> pure (UserPage u muser)
_ -> Err.internalServerError "Incoherent DB"
[] -> Err.notFound "user not found"
[u] -> pure (UserPage u muser)
_ -> Err.internalServerError "Incoherent DB"
getUserById :: Maybe User -> UserHandler -> Id -> Handler UserPage
getUserById muser userHandler userId = do
@ -295,7 +294,7 @@ getUserById muser userHandler userId = do
data UserPage =
UserPage
{ user :: User
{ user :: User
, muser :: Maybe User
}

View file

@ -68,7 +68,6 @@ import qualified Data.UUID as UUID
import qualified Generics.SOP as SOP
import Servant.Auth.Server (FromJWT, ToJWT)
import qualified Web.HttpApiData as FormI
import qualified Service.Service as Serv
-- | This is the ID type, it is like @Text@.
instance StringConv UUID [Char] where strConv l = strConv l . UUID.toString