Sha256: ef4e4d7343e2a999ac23e552214ce463ce5e0f9774ebb1b0001ad22eb1366c2a

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

require 'wright/provider'
require 'wright/provider/user'

module Wright
  class Provider
    class User
      # Darwin DirectoryService user provider. Used as a provider for
      # {Resource::User} on OS X systems.
      class DarwinDirectoryService < User
        private

        def create_user
          attributes = default_attributes.merge(resource_attributes)
          attributes.each do |k, v|
            args = dscl_args(:create, k, v)
            exec_or_fail('dscl', args, "cannot create user '#{user_name}'")
          end
        end

        def update_user
          resource_attributes.each do |k, v|
            args = dscl_args(:create, k, v)
            exec_or_fail('dscl', args, "cannot create user '#{user_name}'")
          end
        end

        def remove_user
          exec_or_fail('dscl',
                       %W(. -delete /Users/#{user_name}),
                       "cannot remove user '#{user_name}'")
        end

        def dscl_args(cmd, key, value)
          %W(. -#{cmd} /Users/#{user_name} #{key} #{value})
        end

        def default_attributes
          uid_range = system_user? ? 1...500 : 500...1000
          {
            'UniqueID' => Wright::Util::User.next_free_uid(uid_range),
            'UserShell' => '/bin/bash',
            'RealName' => '',
            'NFSHomeDirectory' => "/Users/#{user_name}",
            'PrimaryGroupID' => Wright::Util::User.group_to_gid('staff'),
            'Password' => '*'
          }
        end

        def resource_attributes
          gid = Wright::Util::User.group_to_gid(primary_group)
          {
            'UniqueID' => uid,
            'UserShell' => shell,
            'RealName' => full_name,
            'NFSHomeDirectory' => home,
            'PrimaryGroupID' => gid
          }.reject { |_k, v| v.nil? }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wright-0.5.0 lib/wright/provider/user/darwin_directory_service.rb
wright-0.4.4 lib/wright/provider/user/darwin_directory_service.rb
wright-0.4.3 lib/wright/provider/user/darwin_directory_service.rb
wright-0.4.2 lib/wright/provider/user/darwin_directory_service.rb
wright-0.4.1 lib/wright/provider/user/darwin_directory_service.rb
wright-0.4.0 lib/wright/provider/user/darwin_directory_service.rb