-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | A library simplifying user management for web applications
--
-- Scrap the boilerplate for managing user accounts in web applications
--
-- Features:
--
--
-- - Easy to understand API
-- - CRUD for Users
-- - Session Management
-- - Password reset functionality
-- - Activation functionality
--
--
-- Current Backends:
--
--
@package users
@version 0.4.0.0
module Web.Users.Types
-- | An abstract backend for managing users. A backend library should
-- implement the interface and an end user should build applications on
-- top of this interface.
class (Show (UserId b), Eq (UserId b), ToJSON (UserId b), FromJSON (UserId b), Typeable (UserId b), PathPiece (UserId b)) => UserStorageBackend b where type UserId b :: * updateUserDetails backend userId f = do { _ <- updateUser backend userId $ \ user -> user {u_more = f (u_more user)}; return () } where {
type family UserId b :: *;
}
-- | Initialise the backend. Call once on application launch to for example
-- create missing database tables
initUserBackend :: UserStorageBackend b => b -> IO ()
-- | Destory the backend. WARNING: This is only for testing! It deletes all
-- tables and data.
destroyUserBackend :: UserStorageBackend b => b -> IO ()
-- | This cleans up invalid sessions and other tokens. Call periodically as
-- needed.
housekeepBackend :: UserStorageBackend b => b -> IO ()
-- | Retrieve a user id from the database
getUserIdByName :: UserStorageBackend b => b -> Text -> IO (Maybe (UserId b))
-- | Retrieve a user from the database
getUserById :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> UserId b -> IO (Maybe (User a))
-- | List all users (unlimited, or limited)
listUsers :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> Maybe (Int64, Int64) -> IO [(UserId b, User a)]
-- | Count all users
countUsers :: UserStorageBackend b => b -> IO Int64
-- | Create a user
createUser :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> User a -> IO (Either CreateUserError (UserId b))
-- | Modify a user
updateUser :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> UserId b -> (User a -> User a) -> IO (Either UpdateUserError ())
-- | Modify details of a user
updateUserDetails :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> UserId b -> (a -> a) -> IO ()
-- | Delete a user
deleteUser :: UserStorageBackend b => b -> UserId b -> IO ()
-- | Authentificate a user using username/email and password. The
-- NominalDiffTime describes the session duration
authUser :: UserStorageBackend b => b -> Text -> PasswordPlain -> NominalDiffTime -> IO (Maybe SessionId)
-- | Authentificate a user and execute a single action.
withAuthUser :: (UserStorageBackend b, FromJSON a) => b -> Text -> (User a -> Bool) -> (UserId b -> IO r) -> IO (Maybe r)
-- | Verify a SessionId. The session duration can be extended by
-- NominalDiffTime
verifySession :: UserStorageBackend b => b -> SessionId -> NominalDiffTime -> IO (Maybe (UserId b))
-- | Force create a session for a user. This is useful for support/admin
-- login. If the user does not exist, this will fail.
createSession :: UserStorageBackend b => b -> UserId b -> NominalDiffTime -> IO (Maybe SessionId)
-- | Destroy a session
destroySession :: UserStorageBackend b => b -> SessionId -> IO ()
-- | Request a PasswordResetToken for a given user, valid for
-- NominalDiffTime
requestPasswordReset :: UserStorageBackend b => b -> UserId b -> NominalDiffTime -> IO PasswordResetToken
-- | Check if a PasswordResetToken is still valid and retrieve the
-- owner of it
verifyPasswordResetToken :: (UserStorageBackend b, FromJSON a, ToJSON a) => b -> PasswordResetToken -> IO (Maybe (User a))
-- | Apply a new password to the owner of PasswordResetToken iff the
-- token is still valid
applyNewPassword :: UserStorageBackend b => b -> PasswordResetToken -> Password -> IO (Either TokenError ())
-- | Request an ActivationToken for a given user, valid for
-- NominalDiffTime
requestActivationToken :: UserStorageBackend b => b -> UserId b -> NominalDiffTime -> IO ActivationToken
-- | Activate the owner of ActivationToken iff the token is still
-- valid
activateUser :: UserStorageBackend b => b -> ActivationToken -> IO (Either TokenError ())
-- | Core user datatype. Store custom information in the u_more
-- field
data User a
User :: !Text -> !Text -> !Password -> !Bool -> !a -> User a
[u_name] :: User a -> !Text
[u_email] :: User a -> !Text
[u_password] :: User a -> !Password
[u_active] :: User a -> !Bool
[u_more] :: User a -> !a
-- | Password representation. When updating or creating a user, use
-- makePassword to create one. The implementation details of this
-- type are ONLY for use in backend implementations.
data Password
PasswordHash :: !Text -> Password
PasswordHidden :: Password
-- | Construct a password from plaintext by hashing it
makePassword :: PasswordPlain -> Password
-- | Strip the password from the user type.
hidePassword :: User a -> User a
-- | Plaintext passsword. Used for authentification.
newtype PasswordPlain
PasswordPlain :: Text -> PasswordPlain
[unPasswordPlain] :: PasswordPlain -> Text
-- | Check a plaintext password against a password
verifyPassword :: PasswordPlain -> Password -> Bool
-- | A password reset token to send out to users via email or sms
newtype PasswordResetToken
PasswordResetToken :: Text -> PasswordResetToken
[unPasswordResetToken] :: PasswordResetToken -> Text
-- | An activation token to send out to users via email or sms
newtype ActivationToken
ActivationToken :: Text -> ActivationToken
[unActivationToken] :: ActivationToken -> Text
-- | A session id for identifying user sessions
newtype SessionId
SessionId :: Text -> SessionId
[unSessionId] :: SessionId -> Text
-- | Errors that happen on storage level during user creation
data CreateUserError
-- | Deprecated: Please use the more specific error constructors.
UsernameOrEmailAlreadyTaken :: CreateUserError
InvalidPassword :: CreateUserError
UsernameAlreadyTaken :: CreateUserError
EmailAlreadyTaken :: CreateUserError
UsernameAndEmailAlreadyTaken :: CreateUserError
-- | Errors that happen on storage level during user updating
data UpdateUserError
-- | Deprecated: Please use the more descriptive constructors
-- instead.
UsernameOrEmailAlreadyExists :: UpdateUserError
UsernameAlreadyExists :: UpdateUserError
EmailAlreadyExists :: UpdateUserError
-- | Deprecated: Please use UserDoesntExist instead.
UserDoesntExit :: UpdateUserError
UserDoesntExist :: UpdateUserError
-- | Errors that happen on storage level during token actions
data TokenError
TokenInvalid :: TokenError
instance GHC.Classes.Eq a => GHC.Classes.Eq (Web.Users.Types.User a)
instance GHC.Show.Show a => GHC.Show.Show (Web.Users.Types.User a)
instance GHC.Classes.Eq Web.Users.Types.Password
instance GHC.Show.Show Web.Users.Types.Password
instance Data.String.IsString Web.Users.Types.PasswordPlain
instance GHC.Classes.Eq Web.Users.Types.PasswordPlain
instance GHC.Show.Show Web.Users.Types.PasswordPlain
instance Web.PathPieces.PathPiece Web.Users.Types.SessionId
instance Data.Aeson.Types.FromJSON.FromJSON Web.Users.Types.SessionId
instance Data.Aeson.Types.ToJSON.ToJSON Web.Users.Types.SessionId
instance GHC.Classes.Eq Web.Users.Types.SessionId
instance GHC.Show.Show Web.Users.Types.SessionId
instance Web.PathPieces.PathPiece Web.Users.Types.ActivationToken
instance Data.Aeson.Types.FromJSON.FromJSON Web.Users.Types.ActivationToken
instance Data.Aeson.Types.ToJSON.ToJSON Web.Users.Types.ActivationToken
instance GHC.Classes.Eq Web.Users.Types.ActivationToken
instance GHC.Show.Show Web.Users.Types.ActivationToken
instance Web.PathPieces.PathPiece Web.Users.Types.PasswordResetToken
instance Data.Aeson.Types.FromJSON.FromJSON Web.Users.Types.PasswordResetToken
instance Data.Aeson.Types.ToJSON.ToJSON Web.Users.Types.PasswordResetToken
instance GHC.Classes.Eq Web.Users.Types.PasswordResetToken
instance GHC.Show.Show Web.Users.Types.PasswordResetToken
instance GHC.Classes.Eq Web.Users.Types.TokenError
instance GHC.Show.Show Web.Users.Types.TokenError
instance GHC.Classes.Eq Web.Users.Types.UpdateUserError
instance GHC.Show.Show Web.Users.Types.UpdateUserError
instance GHC.Classes.Eq Web.Users.Types.CreateUserError
instance GHC.Show.Show Web.Users.Types.CreateUserError
instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Web.Users.Types.User a)
instance Data.Aeson.Types.FromJSON.FromJSON a => Data.Aeson.Types.FromJSON.FromJSON (Web.Users.Types.User a)