Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
CUNY Florian
FooGood
Commits
46dce337
Commit
46dce337
authored
Dec 13, 2021
by
poslovitch
Browse files
Added Delete User route
parent
a5481993
Changes
1
Hide whitespace changes
Inline
Side-by-side
app/apiroutes/users.php
View file @
46dce337
...
...
@@ -371,6 +371,54 @@ $app->put('/api/users/{username}', function( Request $request, Response $respons
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
)
->
withStatus
(
200
);
});
/**
* Delete User : admin only (see documentation)
*/
$app
->
delete
(
'/api/users/{username}'
,
function
(
Request
$request
,
Response
$response
){
$username
=
$request
->
getAttribute
(
'username'
);
$token
=
get_token_infos
(
$request
);
if
(
!
$token
->
admin
)
{
// response : 403 : denied
$response
->
getBody
()
->
write
(
'{"error": {"msg": "Access denied."}}'
);
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
)
->
withStatus
(
403
);
}
$dbconn
=
new
DB\DBConnection
();
// Check user exists in database
try
{
$db
=
$dbconn
->
connect
();
// query
$sql
=
"SELECT * FROM users WHERE (username='"
.
$username
.
"')"
;
$stmt
=
$db
->
query
(
$sql
);
$users
=
$stmt
->
fetchAll
(
PDO
::
FETCH_OBJ
);
$db
=
null
;
// clear db object
// Check if the user does not exist
if
(
sizeof
(
$users
)
!=
1
)
{
// response : 404 : not Found
$response
->
getBody
()
->
write
(
'{"error": {"msg": "Could not find user."}}'
);
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
)
->
withStatus
(
404
);
}
$user
=
$users
[
0
];
$sql
=
"DELETE FROM users WHERE (id='"
.
$user
->
id
.
"');"
;
$db
=
$dbconn
->
connect
();
$db
->
query
(
$sql
);
$db
=
null
;
// clear the db object
}
catch
(
PDOException
$e
)
{
echo
$e
;
// response : 500 : PDO Error (DB)
$response
->
getBody
()
->
write
(
'{"error": {"msg": "'
.
$e
->
getMessage
()
.
'"}}'
);
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
)
->
withStatus
(
500
);
}
return
$response
->
withHeader
(
'Content-Type'
,
'application/json'
)
->
withStatus
(
200
);
});
/**
* Function which parse token, decode user infos from this token and Throws
* UnauthenticatedException if Authentication Issue.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment