Skip to content

Commit 2a7c753

Browse files
committed
Fix user.present not having capability to persist home directory by adding persist_home flag
1 parent b539903 commit 2a7c753

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

changelog/68322.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed user.present not having capability to persist home directory by adding persist_home flag.

salt/states/user.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def present(
294294
remove_groups=True,
295295
home=None,
296296
createhome=True,
297+
persist_home=False,
297298
password=None,
298299
hash_password=False,
299300
enforce_password=True,
@@ -393,6 +394,10 @@ def present(
393394
Additionally, parent directories will *not* be created. The parent
394395
directory for ``home`` must already exist.
395396
397+
persist_home : False
398+
If set to ``True`` and not on Windows or Darwin, move contents of the
399+
home directory to the new location
400+
396401
nologinit : False
397402
If set to ``True``, it will not add the user to lastlog and faillog
398403
databases.
@@ -716,7 +721,7 @@ def _change_homedir(name, val):
716721
if __grains__["kernel"] in ("Darwin", "Windows"):
717722
__salt__["user.chhome"](name, val)
718723
else:
719-
__salt__["user.chhome"](name, val, persist=False)
724+
__salt__["user.chhome"](name, val, persist=persist_home)
720725

721726
_homedir_changed = False
722727
if "home" in changes:

tests/pytests/functional/states/test_user.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,27 @@ def test_user_present_home_directory_created(modules, states, username, createho
314314
assert pathlib.Path(user_info["home"]).is_dir() is createhome
315315

316316

317+
@pytest.mark.skip_on_windows(reason="windows minion does not support persist_home")
318+
@pytest.mark.skip_on_darwin(reason="Mac minion does not support persist_home")
319+
@pytest.mark.parametrize("persist_home", [True, False])
320+
def test_user_present_home_directory_persisted(
321+
modules, states, existing_account, user_home, persist_home
322+
):
323+
"""
324+
It ensures that the home directory is persisted when home changed.
325+
"""
326+
pathlib.Path(existing_account.info.home, "testfile").touch()
327+
ret = states.user.present(
328+
name=existing_account.username, home=str(user_home), persist_home=persist_home
329+
)
330+
assert ret.result is True
331+
332+
user_info = modules.user.info(existing_account.info.name)
333+
assert user_info
334+
335+
assert pathlib.Path(user_info["home"], "testfile").is_file() is persist_home
336+
337+
317338
@pytest.mark.skip_on_darwin(reason="groups/gid not fully supported")
318339
@pytest.mark.skip_on_windows(reason="groups/gid not fully supported")
319340
def test_user_present_change_gid_but_keep_group(

0 commit comments

Comments
 (0)