Class: CFA::LoginDefs

Inherits:
BaseModel
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/general/src/lib/cfa/login_defs.rb

Overview

Model to handle login.defs configuration files

Examples:

Reading a value

file = LoginDefs.new(file_path: "/etc/login.defs")
file.load
file.encrypt_method #=> "SHA512"

Writing a value

file = LoginDefs.new(file_path: "/etc/login.defs.d/70-yast.defs")
file.encrypt_method = "DES"
file.save

Loading shortcut

file = LoginDefs.load(file_path: "/etc/login.defs.d/70-yast.defs")
file.encrypt_method #=> "SHA512"

Constant Summary collapse

KNOWN_ATTRIBUTES =

Returns List of known login.defs attributes.

Returns:

  • (Array<Symbol>)

    List of known login.defs attributes

[
  :character_class,
  :create_home,
  :encrypt_method,
  :fail_delay,
  :gid_max,
  :gid_min,
  :groupadd_cmd,
  :home_mode,
  :pass_max_days,
  :pass_min_days,
  :pass_warn_age,
  :sub_gid_min,
  :sub_gid_max,
  :sub_gid_count,
  :sub_uid_min,
  :sub_uid_max,
  :sub_uid_count,
  :sys_gid_max,
  :sys_gid_min,
  :sys_uid_max,
  :sys_uid_min,
  :uid_max,
  :uid_min,
  :umask,
  :useradd_cmd,
  :userdel_postcmd,
  :userdel_precmd,
  :usergroups_enab
].freeze
DEFAULT_PATH =
"/etc/login.defs".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path: DEFAULT_PATH, file_handler: Yast::TargetFile) ⇒ LoginDefs

Constructor

Parameters:

  • file_handler (#read, #write) (defaults to: Yast::TargetFile)

    something able to read/write a string (like File)

  • file_path (String) (defaults to: DEFAULT_PATH)

    File path

See Also:

  • BaseModel#initialize


113
114
115
# File 'library/general/src/lib/cfa/login_defs.rb', line 113

def initialize(file_path: DEFAULT_PATH, file_handler: Yast::TargetFile)
  super(AugeasParser.new("login_defs.lns"), file_path, file_handler: file_handler)
end

Instance Attribute Details

#file_pathString (readonly)

Returns File path.

Returns:

  • (String)

    File path



105
106
107
# File 'library/general/src/lib/cfa/login_defs.rb', line 105

def file_path
  @file_path
end

Class Method Details

.known_attributesArray<Symbol>

Returns the list of known attributes

Returns:

  • (Array<Symbol>)


84
85
86
# File 'library/general/src/lib/cfa/login_defs.rb', line 84

def known_attributes
  KNOWN_ATTRIBUTES
end

.load(file_path: DEFAULT_PATH, file_handler: Yast::TargetFile) ⇒ LoginDefs

Instantiates and loads a file

This method is basically a shortcut to instantiate and load the content in just one call.

Parameters:

  • file_handler (#read, #write) (defaults to: Yast::TargetFile)

    something able to read/write a string (like File)

  • file_path (String) (defaults to: DEFAULT_PATH)

    File path

Returns:

  • (LoginDefs)

    File with the already loaded content



95
96
97
# File 'library/general/src/lib/cfa/login_defs.rb', line 95

def load(file_path: DEFAULT_PATH, file_handler: Yast::TargetFile)
  new(file_path: file_path, file_handler: file_handler).tap(&:load)
end

Instance Method Details

#conflicts(other) ⇒ Array<Symbol>

Determines the list of conflicting attributes for two files

Two attributes are conflicting when both of them are defined with different values.

Parameters:

  • other (BaseModel)

    The file to compare with

Returns:

  • (Array<Symbol>)

    List of conflicting attributes



139
140
141
142
# File 'library/general/src/lib/cfa/login_defs.rb', line 139

def conflicts(other)
  conflicting_attrs = present_attributes & other.present_attributes
  conflicting_attrs.reject { |a| public_send(a) == other.public_send(a) }
end

#loadObject

Loads the file content

If the file does not exist, consider the file as empty.

See Also:

  • BaseModel#load


149
150
151
152
153
154
# File 'library/general/src/lib/cfa/login_defs.rb', line 149

def load
  super
rescue Errno::ENOENT # PATH does not exist yet
  self.data = @parser.empty
  @loaded = true
end

#present?(attr) ⇒ Boolean

Determines whether an attribute has a value

Returns:

  • (Boolean)

    +true+ if it is defined; +false+ otherwise.



120
121
122
# File 'library/general/src/lib/cfa/login_defs.rb', line 120

def present?(attr)
  !public_send(attr).nil?
end

#present_attributesArray<Symbol>

Returns the list of attributes with a value

Returns:

  • (Array<Symbol>)

    List of attribute names

See Also:



128
129
130
# File 'library/general/src/lib/cfa/login_defs.rb', line 128

def present_attributes
  self.class.known_attributes.select { |a| present?(a) }
end

#saveObject

Saves the file content

It creates the directory if it does not exist.

See Also:

  • BaseModel#save


161
162
163
164
165
# File 'library/general/src/lib/cfa/login_defs.rb', line 161

def save
  directory = File.dirname(file_path)
  Yast::Execute.on_target("/usr/bin/mkdir", "--parents", directory) if !Yast::FileUtils.IsDirectory(directory)
  super
end