on master help sign in
Merge pull request #133 from ilyakooo0/develop
2afcbc5iko
denis/src/Data/Query.hs+8−1
@@ -59,7 +59,8 @@ module Data.Query (
59 searchUsers,
59 searchUsers,
60 searchTags,
60 searchTags,
61 updateUser,
61 updateUser,
62− UserUpdateRow(..)
62+ UserUpdateRow(..),
63+ killAllTokens
63 ) where
64 ) where
64
65
65import Squeal.PostgreSQL
66import Squeal.PostgreSQL
@@ -737,6 +738,10 @@ getVerifiedTokenQ = selectStar (from (table #tokens) &
737 isNull #tokenVerificationCode .&&
738 isNull #tokenVerificationCode .&&
738 currentTimestamp .< #tokenExpiryDate))
739 currentTimestamp .< #tokenExpiryDate))
739
740
741+killAllTokensQ :: Manipulation Schema (TuplePG (Only UserId)) '[]
742+killAllTokensQ = deleteFrom_ #tokens
743+ (#tokenUserId .== param @1)
744+
740-- MARK: FrontEnd Data Structures
745-- MARK: FrontEnd Data Structures
741
746
742data UserCreation = UserCreation {
747data UserCreation = UserCreation {
@@ -1031,6 +1036,8 @@ searchTags query = map fromOnly <$>
1031createToken :: Token -> StaticPQ ()
1036createToken :: Token -> StaticPQ ()
1032createToken token = void $ manipulateParams createTokenQ token
1037createToken token = void $ manipulateParams createTokenQ token
1033
1038
1039+killAllTokens :: UserId -> StaticPQ ()
1040+killAllTokens = void . manipulateParams killAllTokensQ . Only
1034
1041
1035-- NOTE: Not transactional
1042-- NOTE: Not transactional
1036validateToken :: Token -> StaticPQ ()
1043validateToken :: Token -> StaticPQ ()
denis/src/Server/API.hs+9−1
@@ -46,6 +46,8 @@ type UserSearchDescription = Description "Searches for users with the given stri
46
46
47type UpdateUserDescription = Description "Updates the current user.\n\nThrow 401 if you try to change email or something like that."
47type UpdateUserDescription = Description "Updates the current user.\n\nThrow 401 if you try to change email or something like that."
48
48
49+type DeactivateAllDescription = Description "Deactivates all tokens for the logged in user including the one used to authenticate this request."
50+
49-- MARK: Implementation
51-- MARK: Implementation
50
52
51type Authentication = AuthProtect "basicAuth"
53type Authentication = AuthProtect "basicAuth"
@@ -55,6 +57,8 @@ type API =
55 "authentication" :> AuthenticationHandler :<|>
57 "authentication" :> AuthenticationHandler :<|>
56 Authentication :> ComplexQuery :> (
58 Authentication :> ComplexQuery :> (
57 "authentication" :> "me" :> Post '[JSON] UserId :<|>
59 "authentication" :> "me" :> Post '[JSON] UserId :<|>
60+ "deactivateAll" :> DeactivateAllDescription
61+ :> DeleteNoContent '[JSON, PlainText, FormUrlEncoded] NoContent :<|>
58 "users" :> ReqBody '[JSON] [UserId] :> Post '[JSON] [User Faculty] :<|>
62 "users" :> ReqBody '[JSON] [UserId] :> Post '[JSON] [User Faculty] :<|>
59 "users" :> "all" :> Post '[JSON] [User Faculty] :<|>
63 "users" :> "all" :> Post '[JSON] [User Faculty] :<|>
60 "users" :> "search" :> UserSearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [User Faculty] :<|>
64 "users" :> "search" :> UserSearchDescription :> ReqBody '[JSON, PlainText] Text :> Post '[JSON] [User Faculty] :<|>
@@ -74,6 +78,7 @@ mkServerAPI l =
74 l :<|>
78 l :<|>
75 authenticationAPI :<|> (\uId ->
79 authenticationAPI :<|> (\uId ->
76 return uId :<|>
80 return uId :<|>
81+ deactivateApi uId :<|>
77 maybeNotFound . runQnotFound . getUsers :<|>
82 maybeNotFound . runQnotFound . getUsers :<|>
78 runQerror getAllUsers :<|>
83 runQerror getAllUsers :<|>
79 runQerror . searchUsers :<|>
84 runQerror . searchUsers :<|>
@@ -96,4 +101,7 @@ updateUserApi uId UserUpdate{..} = do
96 }
101 }
97 if isJust res
102 if isJust res
98 then return NoContent
103 then return NoContent
99− else throwError err401
104+ else throwError err401
105+
106+deactivateApi :: UserId -> App NoContent
107+deactivateApi uId = runQerror (killAllTokens uId) >> return NoContent