<commit_before>#
# Copyright:: Copyright 2016-2018, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../user"

class Chef
  class Provider
    class User
      class Linux < Chef::Provider::User
        provides :linux_user
        provides :user, os: "linux"

        def create_user
          shell_out!("useradd", universal_options, useradd_options, new_resource.username, returns: return_codes)
        end

        def manage_user
          shell_out!("usermod", universal_options, usermod_options, new_resource.username, returns: return_codes)
        end

        def remove_user
          shell_out!("userdel", userdel_options, new_resource.username, returns: return_codes)
        end

        def lock_user
          shell_out!("usermod", "-L", new_resource.username, returns: return_codes)
        end

        def unlock_user
          shell_out!("usermod", "-U", new_resource.username, returns: return_codes)
        end

        # common to usermod and useradd
        def universal_options
          opts = []
          opts << "-c" << new_resource.comment if should_set?(:comment)
          opts << "-g" << new_resource.gid if should_set?(:gid)
          opts << "-p" << new_resource.password if should_set?(:password)
          opts << "-s" << new_resource.shell if should_set?(:shell)
          opts << "-u" << new_resource.uid if should_set?(:uid)
          opts << "-d" << new_resource.home if updating_home?
          opts << "-o" if new_resource.non_unique
          opts
        end

        def return_codes
          ret_codes = [0]
          if updating_home?
            if new_resource.manage_home
              home_dir_not_exist = shell_out("test", "-d", new_resource.home, returns: [1])
              if home_dir_not_exist.error?
                ret_codes << 12
              end
            end
          end
          ret_codes
        end

        def usermod_options
          opts = []
          opts += [ "-u", new_resource.uid ] if new_resource.non_unique
          if updating_home?
            if new_resource.manage_home
              opts << "-m"
            end
          end
          opts
        end

        def useradd_options
          opts = []
          opts << "-r" if new_resource.system
          opts << if new_resource.manage_home
                    "-m"
                  else<commit_msg>Replaced 'test' shellout with `Dir.exist?` Signed-off-by: <NAME> <<EMAIL>><commit_after>#
# Copyright:: Copyright 2016-2018, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../user"

class Chef
  class Provider
    class User
      class Linux < Chef::Provider::User
        provides :linux_user
        provides :user, os: "linux"

        def create_user
          shell_out!("useradd", universal_options, useradd_options, new_resource.username, returns: return_codes)
        end

        def manage_user
          shell_out!("usermod", universal_options, usermod_options, new_resource.username, returns: return_codes)
        end

        def remove_user
          shell_out!("userdel", userdel_options, new_resource.username, returns: return_codes)
        end

        def lock_user
          shell_out!("usermod", "-L", new_resource.username, returns: return_codes)
        end

        def unlock_user
          shell_out!("usermod", "-U", new_resource.username, returns: return_codes)
        end

        # common to usermod and useradd
        def universal_options
          opts = []
          opts << "-c" << new_resource.comment if should_set?(:comment)
          opts << "-g" << new_resource.gid if should_set?(:gid)
          opts << "-p" << new_resource.password if should_set?(:password)
          opts << "-s" << new_resource.shell if should_set?(:shell)
          opts << "-u" << new_resource.uid if should_set?(:uid)
          opts << "-d" << new_resource.home if updating_home?
          opts << "-o" if new_resource.non_unique
          opts
        end

        def return_codes
          ret_codes = [0]
          if updating_home?
            if new_resource.manage_home
              ret_codes << 12 if Dir.exist?(new_resource.home)
            end
          end
          ret_codes
        end

        def usermod_options
          opts = []
          opts += [ "-u", new_resource.uid ] if new_resource.non_unique
          if updating_home?
            if new_resource.manage_home
              opts << "-m"
            end
          end
          opts
        end

        def useradd_options
          opts = []
          opts << "-r" if new_resource.system
          opts << if new_resource.manage_home
                    "-m"
                  else
{'fraction_non_alphanumeric': 0.09150665974744854, 'fraction_numerical': 0.006054315862307559, 'mean_word_length': 2.1254054054054055, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4800034', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1559, 'n_words': 491}