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,
  :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,
  :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


109
110
111
# File 'library/general/src/lib/cfa/login_defs.rb', line 109

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



101
102
103
# File 'library/general/src/lib/cfa/login_defs.rb', line 101

def file_path
  @file_path
end

Class Method Details

.known_attributesArray<Symbol>

Returns the list of known attributes

Returns:

  • (Array<Symbol>)


80
81
82
# File 'library/general/src/lib/cfa/login_defs.rb', line 80

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



91
92
93
# File 'library/general/src/lib/cfa/login_defs.rb', line 91

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



135
136
137
138
# File 'library/general/src/lib/cfa/login_defs.rb', line 135

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


145
146
147
148
149
150
# File 'library/general/src/lib/cfa/login_defs.rb', line 145

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.



116
117
118
# File 'library/general/src/lib/cfa/login_defs.rb', line 116

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:



124
125
126
# File 'library/general/src/lib/cfa/login_defs.rb', line 124

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


157
158
159
160
161
# File 'library/general/src/lib/cfa/login_defs.rb', line 157

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