Skip to content

Commit

Permalink
Adapt reader to return both system and target ones
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 4, 2021
1 parent 8674213 commit c6523dd
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 33 deletions.
82 changes: 63 additions & 19 deletions src/lib/y2users/users_module/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ module Y2Users
module UsersModule
# Class for reading the configuration from Yast::Users module
class Reader
# Generates a new config with the information from YaST::Users module
# Generates two new configs with the information from YaST::Users module
#
# @return [Config]
# @return [Array<Config>] returns pair of configs. system one and modified one
def read
Config.new.tap do |config|
read_elements(config)
read_autologin(config)
read_useradd_defaults(config)
end
system_config = Config.new
target_config = Config.new
read_elements(system_config, target_config)
read_autologin(target_config)
read_useradd_defaults(target_config)

[system_config, target_config]
end

private
Expand Down Expand Up @@ -75,34 +77,73 @@ def read_useradd_defaults(config)
# @return [LoginConfig]
def read_autologin(config)
res = LoginConfig.new
res.autologin_user = Yast::Autologin.user.empty? ? nil : Yast::Autologin.user
res.autologin_user = if Yast::Autologin.user.empty?
nil
else
config.users.by_name(Yast::Autologin.user)
end
res.passwordless = Yast::Autologin.pw_less

config.login = res
end

# Reads the users and groups
#
# @param config [Config]
def read_elements(config)
config.attach(groups)
config.attach(users(config))
# @param system_config [Config]
# @param target_config [Config]
def read_elements(system_config, target_config)
read_groups(system_config, target_config)
read_users(system_config, target_config)
end

# Returns the list of users
#
# @param config [Config]
# @return [Array<User>] the collection of users
def users(config)
# @param system_config [Config]
# @param target_config [Config]
def read_users(system_config, target_config)
all_local = Yast::Users.GetUsers("uid", "local").values +
Yast::Users.GetUsers("uid", "system").values
all_local.map { |u| user(u, config) }
all_local.each do |user_hash|
if user_hash["what"] == "add_user" # new user
target_config.attach(user(user_hash, target_config))
else # everything else is edit of different kinds
# at first create original user
system_user = user(user_hash.merge(user_hash["org_user"] || {}), system_config)
system_config.attach(system_user)
target_user = user(user_hash, target_config)
target_user.assign_internal_id(system_user.id)
target_config.attach(target_user)
end
end
removed_users = Yast::Users.RemovedUsers
["local", "system"].each do |type|
next unless removed_users[type]

removed_users[type].each { |u| system_config.attach(user(u, system_config)) }
end
end

def groups
def read_groups(system_config, target_config)
all_local = Yast::Users.GetGroups("cn", "local").values +
Yast::Users.GetGroups("cn", "system").values
all_local.map { |u| group(u) }
all_local.each do |group_hash|
if group_hash["what"] == "add_group" # new group
target_config.attach(group(group_hash))
else # everything else is edit of different kinds
# at first create original group
system_group = group(group_hash.merge(group_hash["org_group"] || {}))
system_config.attach(system_group)
target_group = group(group_hash)
target_group.assign_internal_id(system_group.id)
target_config.attach(target_group)
end
end
removed_groups = Yast::Users.RemovedGroups
["local", "system"].each do |type|
next unless removed_groups[type]

removed_groups[type].each { |u| system_config.attach(group(u)) }
end
end

# Creates a {User} object based on the data structure of a Users user
Expand All @@ -116,7 +157,10 @@ def user(user_attrs, config)
user.gid = user_gid(user_attrs, config)
user.home = home(user_attrs)
user.shell = attr_value(:loginShell, user_attrs)
user.gecos = [attr_value(:cn, user_attrs), *user_attrs["addit_data"].split(",")].compact
user.gecos = [
attr_value(:cn, user_attrs),
*(user_attrs["addit_data"] || "").split(",")
].compact
user.system = true if !user.uid && user_attrs["type"] == "system"
user.receive_system_mail = Yast::Users.GetRootAliases[user.name] == 1

Expand Down
10 changes: 10 additions & 0 deletions src/modules/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ sub Modified {
return $ret;
}

BEGIN { $TYPEINFO{RemovedUsers} = ["function", ["map", "string", "any"]]; }
sub RemovedUsers {
return \%removed_users;
}

BEGIN { $TYPEINFO{RemovedGroups} = ["function", ["map", "string", "any"]]; }
sub RemovedGroups {
return \%removed_groups;
}

# if root password value should be explicitely written
BEGIN { $TYPEINFO{SaveRootPassword} = ["function", "void", "boolean"]; }
sub SaveRootPassword {
Expand Down
203 changes: 189 additions & 14 deletions test/lib/y2users/users_module/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,94 @@
"userPassword" => "$6$CIrJOmyF8WBnHsAn$Sh.pjryO9CD.Dfm9KzDdVYYXblxiTw05b9b0GVpMbckbU" \
"gK/fFvn7nM.ipqooa3Ks5fGgzV.6gPBGG1l8hs7L.",
"what" => "add_user"
},
{
"addit_data" => "",
"authorized_keys" => [],
"btrfs_subvolume" => 0,
"chown_home" => true,
"cn" => "testing guy",
"create_home" => true,
"enabled" => false,
"encrypted" => true,
"gidNumber" => 100,
"givenName" => "",
"grouplist" => {
"video" => 1
},
"groupname" => "users",
"homeDirectory" => "/home/test",
"home_mode" => "755",
"loginShell" => "/bin/bash",
"modified" => "edited",
"no_skeleton" => false,
"org_homeDirectory" => "/home/test",
"org_uid" => "test",
"org_uidNumber" => 1001,
"org_user" => {
"addit_data" => "",
"authorized_keys" => [],
"chown_home" => true,
"cn" => "test",
"create_home" => true,
"enabled" => false,
"encrypted" => true,
"gidNumber" => "100",
"grouplist" => {
"video" => 1,
"wheel" => 1
},
"groupname" => "users",
"homeDirectory" => "/home/test",
"loginShell" => "/bin/bash",
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "",
"shadowLastChange" => "18894",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"type" => "local",
"uid" => "test",
"uidNumber" => 1001,
"userPassword" => "!$6$7CgeIaVsqcVd2OXq$T9ObPbjPCOm7E3U730S8ZLJ82GBBi9XXYJM4iUNadk" \
"gfpZ3CU/cXe.hdaGhdutqhixtFuZ2hrhEIZvlTcKgSc."
},
"plugins" => [],
"removed_grouplist" => {
"wheel" => 1
},
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "",
"shadowLastChange" => "18894",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"sn" => "",
"type" => "local",
"uid" => "test",
"uidNumber" => 1001,
"userPassword" => "!$6$7CgeIaVsqcVd2OXq$T9ObPbjPCOm7E3U730S8ZLJ82GBBi9XXYJM4iUNadkg" \
"fpZ3CU/cXe.hdaGhdutqhixtFuZ2hrhEIZvlTcKgSc.",
"what" => "edit_user"
}
]
end

let(:sys_groups) do
[
# real data with data dumper from perl after modifications in UI
{
"cn" => "users",
"gidNumber" => 100,
"more_users" => {},
"org_cn" => "users",
"org_gidNumber" => 100,
"type" => "local",
"userPassword" => "x",
"userlist" => {}
},
{
"cn" => "wheel",
"gidNumber" => 497,
Expand All @@ -92,14 +173,19 @@
let(:local_groups) do
[
{
"cn" => "users",
"gidNumber" => 100,
"cn" => "testing",
"gidNumber" => 1000,
"modified" => "added",
"more_users" => {},
"org_cn" => "users",
"org_gidNumber" => 100,
"org_cn" => "testing",
"org_gidNumber" => 1000,
"plugins" => [],
"type" => "local",
"userPassword" => "x",
"userlist" => {}
"userPassword" => nil,
"userlist" => {
"test2" => 1
},
"what" => "add_group"
}
]
end
Expand All @@ -117,26 +203,101 @@
}
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" => "test7",
"delete_home" => true,
"gidNumber" => "100",
"grouplist" => {},
"groupname" => "users",
"homeDirectory" => "/home/test7",
"loginShell" => "/bin/bash",
"modified" => "deleted",
"org_user" => {},
"plugins" => [],
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "",
"shadowLastChange" => "18899",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"type" => "local",
"uid" => "test7",
"uidNumber" => 1002,
"userPassword" => "!$6$yRZunFQ0DSZghYQ4$7K2cLQ/XrhucUZr4btKmUbfMuUmbDmRX7msfs6VQGKEf" \
"b2nkrbNn0c2d3mNmG.MGfFgmYyv.540Yaq2GtpVaK1",
"what" => "delete_user"
}
] }
end

let(:removed_groups) do
{
"local" => [{
"cn" => "testing",
"gidNumber" => 1000,
"modified" => "deleted",
"more_users" => {},
"type" => "local",
"userPassword" => "x",
"userlist" => {},
"what" => "delete_group"
}]
}
end

before do
allow(Yast::Users).to receive(:GetLoginDefaults).and_return(login_config)
mapped_users = Hash[users.map { |u| [u["uid"], u] }]
allow(Yast::Users).to receive(:GetUsers).and_return({}, mapped_users)
mapped_sys_groups = Hash[sys_groups.map { |g| [g["cn"], g] }]
mapped_local_groups = Hash[local_groups.map { |g| [g["cn"], g] }]
allow(Yast::Users).to receive(:GetGroups).and_return(mapped_sys_groups, mapped_local_groups)
allow(Yast::Users).to receive(:RemovedUsers).and_return(removed_users)
allow(Yast::Users).to receive(:RemovedGroups).and_return(removed_groups)
allow(Yast::Users).to receive(:GetRootAliases).and_return("test5" => 1)
end

describe "#read" do
it "generates a config with read data" do
config = subject.read
it "generates the system and target config" do
system_config, target_config = subject.read

expect(config).to be_a(Y2Users::Config)
expect(target_config).to be_a(Y2Users::Config)

expect(config.users.size).to eq 1
expect(config.groups.size).to eq 2
expect(target_config.users.size).to eq 2
expect(target_config.groups.size).to eq 3

test_user = config.users.by_name("test5")
test_user = target_config.users.by_name("test5")
expect(test_user.uid).to eq "1002"
expect(test_user.home).to be_a(Y2Users::Home)
expect(test_user.home.path).to eq "/home/test5"
Expand All @@ -150,15 +311,29 @@
expect(test_user.password.aging.content).to eq("18887")
expect(test_user.password.account_expiration.content).to eq("")

test_group = config.groups.by_name("wheel")
test_group = target_config.groups.by_name("wheel")
expect(test_group.gid).to eq "497"
expect(test_group.users.map(&:name)).to eq(["test5"])

useradd = config.useradd
useradd = target_config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.umask).to eq "022"

expect(system_config).to be_a(Y2Users::Config)

expect(system_config.users.size).to eq 3
expect(system_config.groups.size).to eq 3

added_user = system_config.users.by_name("test5")
expect(added_user).to eq nil

removed_user = system_config.users.by_name("test6")
expect(removed_user).to be_a(Y2Users::User)

removed_user = system_config.groups.by_name("testing")
expect(removed_user).to be_a(Y2Users::Group)
end
end
end

0 comments on commit c6523dd

Please sign in to comment.