Skip to content

Commit

Permalink
Added an interface for password changing endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
eriq-augustine committed Nov 24, 2023
1 parent 27cf376 commit f27f174
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
12 changes: 10 additions & 2 deletions autograder/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_api_config(config, params,
"""
Given a tiered config and api parameters,
return a dict that can be directly sierialized and sent to the autograder.
Any hashed params will be hashed.
Any hashed params that are not empty will be hashed.
If |exit_on_error| is true sys.exit() will be called on an error,
otherwise an error will be raised on an error.
Any keys in |additional_*_keys| will be returned in a second dict.
Expand Down Expand Up @@ -73,7 +73,7 @@ def _parse_api_config(config, params, additional_required_keys, additional_optio
continue

value = config[param.config_key]
if (param.hash):
if (param.hash and (value != '')):
value = autograder.util.hash.sha256_hex(value)

data[param.key] = value
Expand Down Expand Up @@ -205,6 +205,10 @@ def get_argument_parser(
required = False,
parser_options = {'action': 'store_true', 'default': False})

PARAM_NEW_PASS = APIParam('new-pass',
'The new password to set for the user that is the target of this request.',
required = False, hash = True)

PARAM_SKIP_EMAILS = APIParam('skip-emails',
'Skip sending any emails. Be aware that this may result in inaccessible information.',
required = False,
Expand All @@ -219,6 +223,10 @@ def get_argument_parser(
'The email of the user that is the target of this request.',
required = True)

PARAM_TARGET_EMAIL_OR_SELF = APIParam('target-email',
'The email of the user that is the target of this request (context user if unspecified).',
required = False)

PARAM_TARGET_PASS = APIParam('target-pass',
'The password of the user that is the target of this request.',
required = True, hash = True)
Expand Down
24 changes: 24 additions & 0 deletions autograder/api/user/changepass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import autograder.api.common
import autograder.api.config

API_ENDPOINT = 'user/change/pass'
API_PARAMS = [
autograder.api.config.PARAM_COURSE_ID,
autograder.api.config.PARAM_USER_EMAIL,
autograder.api.config.PARAM_USER_PASS,

autograder.api.config.PARAM_TARGET_EMAIL_OR_SELF,
autograder.api.config.PARAM_NEW_PASS,
]

DESCRIPTION = 'Change a user\' password.'

def send(arguments, **kwargs):
return autograder.api.common.handle_api_request(arguments, API_PARAMS, API_ENDPOINT, **kwargs)

def _get_parser():
parser = autograder.api.config.get_argument_parser(
description = DESCRIPTION,
params = API_PARAMS)

return parser
24 changes: 24 additions & 0 deletions autograder/cli/user/change-pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys

import autograder.api.user.change
import autograder.cli.common

def run(arguments):
result = autograder.api.user.change.send(arguments, exit_on_error = True)

if (not result['found-user']):
print("User not found.")
return 0

print("Password changed.")
return 0

def main():
return run(_get_parser().parse_args())

def _get_parser():
parser = autograder.api.user.change._get_parser()
return parser

if (__name__ == '__main__'):
sys.exit(main())
10 changes: 10 additions & 0 deletions tests/api/data/test_user_change_pass_gen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"endpoint": "user/change/pass",
"arguments": {
"target-email": "[email protected]",
"new-pass": ""
},
"output": {
"found-user": true
}
}
10 changes: 10 additions & 0 deletions tests/api/data/test_user_change_pass_other.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"endpoint": "user/change/pass",
"arguments": {
"target-email": "[email protected]",
"new-pass": "new-pass"
},
"output": {
"found-user": true
}
}
10 changes: 10 additions & 0 deletions tests/api/data/test_user_change_pass_self.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"endpoint": "user/change/pass",
"arguments": {
"target-email": "",
"new-pass": "new-pass"
},
"output": {
"found-user": true
}
}

0 comments on commit f27f174

Please sign in to comment.