Skip to content

Commit

Permalink
Add support for new attrs to UsersModule::Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 24, 2021
1 parent e786aaf commit 6335bb3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
52 changes: 37 additions & 15 deletions src/lib/y2users/users_module/reader.rb
Expand Up @@ -20,6 +20,7 @@
require "yast"
require "y2users/config"
require "y2users/user"
require "y2users/home"
require "y2users/group"
require "y2users/password"
require "y2users/login_config"
Expand Down Expand Up @@ -110,24 +111,45 @@ def groups
# @param config [Config]
# @return [User]
def user(user_attrs, config)
user = User.new(user_attrs["uid"])

user.uid = attr_value(:uidNumber, user_attrs)&.to_s
user.gid = attr_value(:gidNumber, user_attrs)&.to_s
user.gid ||= config.groups.by_name(attr_value["groupname"])&.gid

user.shell = attr_value(:loginShell, user_attrs)
user.home = attr_value(:homeDirectory, user_attrs)
user.gecos = [attr_value(:cn, user_attrs), *user_attrs["addit_data"].split(",")].compact
user.system = true if !user.uid && user_attrs["type"] == "system"
User.new(user_attrs["uid"]).tap do |user|
user.uid = attr_value(:uidNumber, user_attrs)&.to_s
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.system = true if !user.uid && user_attrs["type"] == "system"
user.receive_system_mail = Yast::Users.GetRootAliases[user.name] == 1

# set password only if specified, nil means not touch it
user.password = create_password(user_attrs) if user_attrs["userPassword"]

# set authorized keys only if set in users module
user.authorized_keys = user_attrs["authorized_keys"] if user_attrs["authorized_keys"]
end
end

# set password only if specified, nil means not touch it
user.password = create_password(user_attrs) if user_attrs["userPassword"]
# Obtains the gid for the user
#
# @param user_attrs [Hash] a user representation in the format used by Users
# @param config [Config]
#
# @return [String, nil] nil if the gid is unknown
def user_gid(user_attrs, config)
gid = attr_value(:gidNumber, user_attrs)&.to_s

# set authorized keys only if set in users module
user.authorized_keys = user_attrs["authorized_keys"] if user_attrs["authorized_keys"]
gid || config.groups.by_name(attr_value["groupname"])&.gid
end

user
# Creates a home from the given attributes
#
# @param user_attrs [Hash] a user representation in the format used by Users
# @return [Home]
def home(user_attrs)
Home.new(attr_value(:homeDirectory, user_attrs)).tap do |home|
# permissions is an octal number starting by 0 (e.g., "0755")
home.permissions = attr_value(:home_mode, user_attrs)&.prepend("0")
home.btrfs_subvol = attr_value(:btrfs_subvolume, user_attrs)
end
end

# Creates a {Group} object based on the data structure of a Users group
Expand Down
9 changes: 7 additions & 2 deletions test/lib/y2users/users_module/reader_test.rb
Expand Up @@ -30,7 +30,7 @@
# real data with data dumper from perl after modifications in UI
{
"addit_data" => "",
"btrfs_subvolume" => false,
"btrfs_subvolume" => true,
"chown_home" => true,
"cn" => "test5",
"create_home" => true,
Expand Down Expand Up @@ -124,6 +124,7 @@
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(:GetRootAliases).and_return("test5" => 1)
end

describe "#read" do
Expand All @@ -137,9 +138,13 @@

test_user = config.users.by_name("test5")
expect(test_user.uid).to eq "1002"
expect(test_user.home).to eq "/home/test5"
expect(test_user.home).to be_a(Y2Users::Home)
expect(test_user.home.path).to eq "/home/test5"
expect(test_user.home.btrfs_subvol?).to eq true
expect(test_user.home.permissions).to eq "0755"
expect(test_user.shell).to eq "/bin/bash"
expect(test_user.primary_group.name).to eq "users"
expect(test_user.receive_system_mail?).to eq true
expect(test_user.password.value.encrypted?).to eq true
expect(test_user.password.value.content).to match(/^\$6\$CI/)
expect(test_user.password.aging.content).to eq("18887")
Expand Down

0 comments on commit 6335bb3

Please sign in to comment.