Skip to content

Commit

Permalink
Move UserAction to Action
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Oct 14, 2021
1 parent 33cc970 commit 85bbb76
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

module Y2Users
module Linux
# Abstract base class for actions to perform over a user
# Abstract base class for actions over the system (e.g., create a user, delete a group, etc)
#
# Derived classes must implement #run_action method.
#
# @example
# class ActionTest < UserAction
# class ActionTest < Action
# def run_action
# print("test")
# true
Expand All @@ -39,13 +39,13 @@ module Linux
# result = action.perform
# result.success? #=> true
# result.issues.empty? #=> true
class UserAction
class Action
# Constructor
#
# @param user [User]
# @param action_element [Object] object to perform the action (e.g., a user)
# @param commit_config [CommitConfig, nil] optional configuration for the commit
def initialize(user, commit_config = nil)
@user = user
def initialize(action_element, commit_config = nil)
@action_element = action_element
@commit_config = commit_config
end

Expand All @@ -60,8 +60,8 @@ def perform

private

# @return [User]
attr_reader :user
# @return [Object]
attr_reader :action_element

# @return [CommitConfig]
attr_reader :commit_config
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/create_user_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for creating a new user
class CreateUserAction < UserAction
class CreateUserAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -41,6 +41,8 @@ def initialize(user, commit_config = nil)

private

alias_method :user, :action_element

# Command for creating new users
USERADD = "/usr/sbin/useradd".freeze
private_constant :USERADD
Expand All @@ -50,7 +52,7 @@ def initialize(user, commit_config = nil)
USERADD_E_HOMEDIR = 12
private_constant :USERADD_E_HOMEDIR

# @see UserAction#run_action
# @see Action#run_action
#
# Issues are generated when the user cannot be created.
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/delete_user_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for deleting an existing user
class DeleteUserAction < UserAction
class DeleteUserAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -41,11 +41,13 @@ def initialize(user, commit_config = nil)

private

alias_method :user, :action_element

# Command for deleting a user
USERDEL = "/usr/sbin/userdel".freeze
private_constant :USERDEL

# @see UserAction#run_action
# @see Action#run_action
#
# Issues are generated when the user cannot be deleted.
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/delete_user_password_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for deleting the user password
class DeleteUserPasswordAction < UserAction
class DeleteUserPasswordAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -41,11 +41,13 @@ def initialize(user, commit_config = nil)

private

alias_method :user, :action_element

# Command for editing a password (i.e., used for deleting the password)
PASSWD = "/usr/bin/passwd".freeze
private_constant :PASSWD

# @see UserAction#run_action
# @see Action#run_action
#
# Issues are generated when the password cannot be deleted
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/edit_user_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for editing an existing user
class EditUserAction < UserAction
class EditUserAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(initial_user, target_user, commit_config = nil)
textdomain "users"

Expand All @@ -43,6 +43,8 @@ def initialize(initial_user, target_user, commit_config = nil)

private

alias_method :user, :action_element

# Initial state of the user
#
# It is used to calculate the changes to apply over the user.
Expand All @@ -54,7 +56,7 @@ def initialize(initial_user, target_user, commit_config = nil)
USERMOD = "/usr/sbin/usermod".freeze
private_constant :USERMOD

# @see UserAction#run_action
# @see Action#run_action
#
# Issues are generated when the user cannot be edited.
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/remove_home_content_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
Expand All @@ -30,13 +30,13 @@ module Linux
# Note that this action is not intended to remove the home itself, but only its content.
# Basically, this action exists to supply the use case of creating a home without skel files,
# which is not currently supported by the shadow tools.
class RemoveHomeContentAction < UserAction
class RemoveHomeContentAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -45,11 +45,13 @@ def initialize(user, commit_config = nil)

private

alias_method :user, :action_element

# Command for finding files
FIND = "/usr/bin/find".freeze
private_constant :FIND

# @see UserAction#run_action
# @see Action#run_action
#
# Removes the content of the user home, even the hidden files.
#
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/set_auth_keys_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "y2issues/issue"
require "users/ssh_authorized_keyring"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for setting the authorized keys of a user
class SetAuthKeysAction < UserAction
class SetAuthKeysAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -41,7 +41,9 @@ def initialize(user, commit_config = nil)

private

# @see UserAction#run_action
alias_method :user, :action_element

# @see Action#run_action
#
# Issues are generated when the authorized keys cannot be set.
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/set_home_ownership_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for changing the ownership of the home
#
# This action is needed when reusing an existing home in order to adapt home ownership to the
# user.
class SetHomeOwnershipAction < UserAction
class SetHomeOwnershipAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -44,11 +44,13 @@ def initialize(user, commit_config = nil)

private

alias_method :user, :action_element

# Command for changing ownership
CHOWN = "/usr/bin/chown".freeze
private_constant :CHOWN

# @see UserAction#run_action
# @see Action#run_action
#
# Issues are generated when ownership cannot be changed.
def run_action
Expand Down
10 changes: 6 additions & 4 deletions src/lib/y2users/linux/set_user_password_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
require "yast/i18n"
require "yast2/execute"
require "y2issues/issue"
require "y2users/linux/user_action"
require "y2users/linux/action"

module Y2Users
module Linux
# Action for setting the user password
class SetUserPasswordAction < UserAction
class SetUserPasswordAction < Action
include Yast::I18n
include Yast::Logger

# Constructor
#
# @see UserAction
# @see Action
def initialize(user, commit_config = nil)
textdomain "users"

Expand All @@ -41,7 +41,9 @@ def initialize(user, commit_config = nil)

private

# @see UserAction#run_action
alias_method :user, :action_element

# @see Action#run_action
def run_action
set_password_value && set_password_attributes
end
Expand Down
6 changes: 3 additions & 3 deletions test/lib/y2users/linux/users_writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def issues(messages)

before do
# Prevent to perform real actions into the system
allow_any_instance_of(Y2Users::Linux::UserAction).to receive(:perform).and_return(success)
allow_any_instance_of(Y2Users::Linux::Action).to receive(:perform).and_return(success)
allow(Yast::MailAliases).to receive(:SetRootAlias)
end

Expand Down Expand Up @@ -343,7 +343,7 @@ def issues(messages)
end

it "does not perform more actions" do
expect_any_instance_of(Y2Users::Linux::UserAction).to_not receive(:perform)
expect_any_instance_of(Y2Users::Linux::Action).to_not receive(:perform)

subject.write
end
Expand Down Expand Up @@ -560,7 +560,7 @@ def issues(messages)
end

it "does not perform more actions" do
expect_any_instance_of(Y2Users::Linux::UserAction).to_not receive(:perform)
expect_any_instance_of(Y2Users::Linux::Action).to_not receive(:perform)

subject.write
end
Expand Down

0 comments on commit 85bbb76

Please sign in to comment.