Skip to content

Commit

Permalink
add reading of remove home
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 5, 2021
1 parent 9ccadc4 commit 334820c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/lib/y2users/commit_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Y2Users
# Writers can receive a collection of objects of this class (see {CommitConfigCollection}) in
# order to decide what actions to perform over the user. For example, a writer can use the commit
# config to check whether the content of the home directory should be moved or not.
# TODO: It is confusing to have config for different commit actions, so for future it makes sense
# to split it
class CommitConfig
# Name of the user this commit config applies to
#
Expand All @@ -45,6 +47,11 @@ class CommitConfig
# @return [Boolean]
attr_writer :adapt_home_ownership

# Whether to remove user home when removing it.
#
# @return [Boolean]
attr_writer :remove_home

# @return [Boolean]
def home_without_skel?
!!@home_without_skel
Expand All @@ -59,5 +66,10 @@ def move_home?
def adapt_home_ownership?
!!@adapt_home_ownership
end

# @return [Boolean]
def remove_home?
!!@remove_home
end
end
end
24 changes: 24 additions & 0 deletions src/lib/y2users/users_module/commit_config_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def read
users.each do |user|
collection.add(commit_config(user))
end
removed_users.each do |user|
update_user(user, collection)
end
end
end

Expand All @@ -50,6 +53,27 @@ def users
Yast::Users.GetUsers("uid", "system").values
end

# Removed local users from the Yast::Users module
#
# @return [Array<Hash>]
def removed_users
users = Yast::Users.RemovedUsers
(users["local"] || []) + (users["system"] || [])
end

# Updates existing commit config or creates a new one
def update_user(user, collection)
name = user["uid"]
config = collection.by_username(name)
if !config
config = CommitConfig.new
config.username = name
collection.add(config)
end

config.remove_home = user["delete_home"]
end

# Generates a commit config from the given user
#
# @param user [Hash] a user representation in the format used by Yast::Users
Expand Down
66 changes: 65 additions & 1 deletion test/lib/y2users/users_module/commit_config_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,76 @@
]
end

let(:removed_users) do
{ "local" => [
{
"addit_data" => "",
"authorized_keys" => [],
"cn" => "test6",
"delete_home" => false,
"gidNumber" => "100",
"grouplist" => {},
"groupname" => "users",
"homeDirectory" => "/home/test6",
"loginShell" => "/bin/bash",
"modified" => "deleted",
"plugins" => [],
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "",
"shadowLastChange" => "18899",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"type" => "local",
"uid" => "test6",
"uidNumber" => 1001,
"userPassword" => "$6$jap/4cvK4.veohli$0JPqLC3sheKRTv79PoiW1fBtbudBad04hWKrUdfOMyzAt" \
"VoGCUZ1KZivJqq1bIFUlJUJPXIbwFOqxNU1wrpZ8/",
"what" => "delete_user"
},
{
"addit_data" => "",
"authorized_keys" => [],
"cn" => "test2",
"delete_home" => true,
"gidNumber" => "100",
"grouplist" => {},
"groupname" => "users",
"homeDirectory" => "/home/test2",
"loginShell" => "/bin/bash",
"modified" => "deleted",
"org_user" => {},
"plugins" => [],
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "",
"shadowLastChange" => "18899",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"type" => "local",
"uid" => "test2",
"uidNumber" => 1002,
"userPassword" => "!$6$yRZunFQ0DSZghYQ4$7K2cLQ/XrhucUZr4btKmUbfMuUmbDmRX7msfs6VQGKEf" \
"b2nkrbNn0c2d3mNmG.MGfFgmYyv.540Yaq2GtpVaK1",
"what" => "delete_user"
}
] }
end

before do
mapped_users = Hash[users.map { |u| [u["uid"], u] }]
allow(Yast::Users).to receive(:GetUsers).and_return(mapped_users, {})
allow(Yast::Users).to receive(:RemovedUsers).and_return(removed_users)
end

it "generates a commit config collection with the read data" do
commit_configs = subject.read

expect(commit_configs).to be_a(Y2Users::CommitConfigCollection)

expect(commit_configs.size).to eq(2)
expect(commit_configs.size).to eq(3)

commit_config1 = commit_configs.by_username("test1")
expect(commit_config1.username).to eq("test1")
Expand All @@ -74,6 +133,11 @@
expect(commit_config2.home_without_skel?).to eq(false)
expect(commit_config2.move_home?).to eq(false)
expect(commit_config2.adapt_home_ownership?).to eq(false)
expect(commit_config2.remove_home?).to eq(true)

commit_config3 = commit_configs.by_username("test6")
expect(commit_config3.username).to eq("test6")
expect(commit_config3.remove_home?).to eq(false)
end
end
end

0 comments on commit 334820c

Please sign in to comment.