From 73b69adf6cc91b9530d0556dd39288f64f0ed68c Mon Sep 17 00:00:00 2001 From: cirun Date: Wed, 22 Nov 2023 14:34:27 +0100 Subject: [PATCH 1/4] add option to create apitoken when creating a user via api --- ckan/logic/action/create.py | 13 +++++++++- ckan/tests/logic/action/test_create.py | 35 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index 76acee506a4..d9f8031d861 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -965,7 +965,9 @@ def user_create(context: Context, } } :type plugin_extras: dict - + :param with_apitoken: whether to create an API token for the user. + (Optional) + :type with_apitoken: bool :returns: the newly created user :rtype: dictionary @@ -974,6 +976,7 @@ def user_create(context: Context, model = context['model'] schema = context.get('schema') or ckan.logic.schema.default_user_schema() session = context['session'] + with_apitoken = data_dict.pop("with_apitoken", False) _check_access('user_create', context, data_dict) @@ -1032,10 +1035,18 @@ def user_create(context: Context, # Create dashboard for user. dashboard = model.Dashboard(user.id) + session.add(dashboard) if not context.get('defer_commit'): model.repo.commit() + if with_apitoken: + # Create apitoken for user. + api_token = _get_action("api_token_create")( + context, {"user": user.name, "name": "test"} + ) + user_dict["token"] = api_token["token"] + log.debug('Created user {name}'.format(name=user.name)) return user_dict diff --git a/ckan/tests/logic/action/test_create.py b/ckan/tests/logic/action/test_create.py index df84ed207a0..4a3290d384a 100644 --- a/ckan/tests/logic/action/test_create.py +++ b/ckan/tests/logic/action/test_create.py @@ -1270,6 +1270,41 @@ def test_user_create_defer_commit(self): with pytest.raises(logic.NotFound): helpers.call_action("user_show", id=user_dict["name"]) + def test_create_user_with_apitoken(self): + stub = factories.User.stub() + context = {"ignore_auth": True} + user_dict = { + "name": stub.name, + "email": stub.email, + "password": "test1234", + "with_apitoken": True + } + user = helpers.call_action("user_create", context={}, **user_dict) + assert user["token"] + + user_dict = {"user_id": user["name"]} + token = helpers.call_action( + "api_token_list", context=context, **user_dict + ) + assert len(token) == 1 + + def test_create_user_with_apitoken_missing_flag(self): + stub = factories.User.stub() + context = {"ignore_auth": True} + user_dict = { + "name": stub.name, + "email": stub.email, + "password": "test1234", + } + user = helpers.call_action("user_create", context={}, **user_dict) + assert "token" not in user + + user_dict = {"user_id": user["name"]} + token = helpers.call_action( + "api_token_list", context=context, **user_dict + ) + assert not token + @pytest.mark.usefixtures("clean_db") @pytest.mark.ckan_config("ckan.auth.create_user_via_web", True) From 3c2fe712bcd0ba17ce087520fbf8db562513c5df Mon Sep 17 00:00:00 2001 From: cirun Date: Wed, 22 Nov 2023 14:35:11 +0100 Subject: [PATCH 2/4] change token name to default --- ckan/logic/action/create.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index d9f8031d861..f4354951525 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -1043,7 +1043,7 @@ def user_create(context: Context, if with_apitoken: # Create apitoken for user. api_token = _get_action("api_token_create")( - context, {"user": user.name, "name": "test"} + context, {"user": user.name, "name": "default"} ) user_dict["token"] = api_token["token"] From 8a920db455219818486cf0e2226618dfc78cb453 Mon Sep 17 00:00:00 2001 From: cirun Date: Thu, 23 Nov 2023 21:27:16 +0100 Subject: [PATCH 3/4] add changelog --- changes/7932.feature | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changes/7932.feature diff --git a/changes/7932.feature b/changes/7932.feature new file mode 100644 index 00000000000..2826ac9bcce --- /dev/null +++ b/changes/7932.feature @@ -0,0 +1,2 @@ +Introducing a new parameter to the user_create action - with_apitoken. +When set, this parameter triggers the creation of an API token for the user. From 74f8bb02ad069c7bf1fd334080e94d7022d9a657 Mon Sep 17 00:00:00 2001 From: cirun Date: Wed, 10 Jan 2024 00:14:36 +0100 Subject: [PATCH 4/4] add username to the context --- ckan/logic/action/create.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index f4354951525..25ad1cfc1aa 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -1041,6 +1041,9 @@ def user_create(context: Context, model.repo.commit() if with_apitoken: + if not context['user']: + context["user"] = user.name + # Create apitoken for user. api_token = _get_action("api_token_create")( context, {"user": user.name, "name": "default"}