|
| 1 | +# |
| 2 | +# Author:: Doug MacEachern (<dougm@vmware.com>) |
| 3 | +# Copyright:: Copyright (c) 2010 VMware, Inc. |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require 'chef/provider/user' |
| 20 | +if RUBY_PLATFORM =~ /mswin|mingw32|windows/ |
| 21 | + require 'chef/util/windows/net_user' |
| 22 | +end |
| 23 | + |
| 24 | +class Chef |
| 25 | + class Provider |
| 26 | + class User |
| 27 | + class Windows < Chef::Provider::User |
| 28 | + |
| 29 | + def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil) |
| 30 | + super(node, new_resource, collection, definitions, cookbook_loader) |
| 31 | + @net_user = Chef::Util::Windows::NetUser.new(@new_resource.name) |
| 32 | + end |
| 33 | + |
| 34 | + def load_current_resource |
| 35 | + @current_resource = Chef::Resource::User.new(@new_resource.name) |
| 36 | + @current_resource.username(@new_resource.username) |
| 37 | + user_info = nil |
| 38 | + begin |
| 39 | + user_info = @net_user.get_info |
| 40 | + rescue |
| 41 | + @user_exists = false |
| 42 | + Chef::Log.debug("User #{@new_resource.username} does not exist") |
| 43 | + end |
| 44 | + |
| 45 | + if user_info |
| 46 | + @current_resource.uid(user_info[:user_id]) |
| 47 | + @current_resource.gid(user_info[:primary_group_id]) |
| 48 | + @current_resource.comment(user_info[:full_name]) |
| 49 | + @current_resource.home(user_info[:home_dir]) |
| 50 | + @current_resource.shell(user_info[:script_path]) |
| 51 | + end |
| 52 | + |
| 53 | + @current_resource |
| 54 | + end |
| 55 | + |
| 56 | + # Check to see if the user needs any changes |
| 57 | + # |
| 58 | + # === Returns |
| 59 | + # <true>:: If a change is required |
| 60 | + # <false>:: If the users are identical |
| 61 | + def compare_user |
| 62 | + unless @net_user.validate_credentials(@new_resource.password) |
| 63 | + Chef::Log.debug("User #{@new_resource.username} password has changed") |
| 64 | + return true |
| 65 | + end |
| 66 | + [ :uid, :gid, :comment, :home, :shell ].any? do |user_attrib| |
| 67 | + !@new_resource.send(user_attrib).nil? && @new_resource.send(user_attrib) != @current_resource.send(user_attrib) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def create_user |
| 72 | + @net_user.add(set_options) |
| 73 | + end |
| 74 | + |
| 75 | + def manage_user |
| 76 | + @net_user.update(set_options) |
| 77 | + end |
| 78 | + |
| 79 | + def remove_user |
| 80 | + @net_user.delete |
| 81 | + end |
| 82 | + |
| 83 | + def check_lock |
| 84 | + @net_user.check_enabled |
| 85 | + end |
| 86 | + |
| 87 | + def lock_user |
| 88 | + @net_user.disable_account |
| 89 | + end |
| 90 | + |
| 91 | + def unlock_user |
| 92 | + @net_user.enable_account |
| 93 | + end |
| 94 | + |
| 95 | + def set_options |
| 96 | + opts = {:name => @new_resource.username} |
| 97 | + |
| 98 | + field_list = { |
| 99 | + 'comment' => 'full_name', |
| 100 | + 'home' => 'home_dir', |
| 101 | + 'gid' => 'primary_group_id', |
| 102 | + 'uid' => 'user_id', |
| 103 | + 'shell' => 'script_path', |
| 104 | + 'password' => 'password' |
| 105 | + } |
| 106 | + |
| 107 | + field_list.sort{ |a,b| a[0] <=> b[0] }.each do |field, option| |
| 108 | + field_symbol = field.to_sym |
| 109 | + if @current_resource.send(field_symbol) != @new_resource.send(field_symbol) |
| 110 | + if @new_resource.send(field_symbol) |
| 111 | + unless field_symbol == :password |
| 112 | + Chef::Log.debug("Setting #{@new_resource} #{field} to #{@new_resource.send(field_symbol)}") |
| 113 | + end |
| 114 | + opts[option.to_sym] = @new_resource.send(field_symbol) |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + opts |
| 119 | + end |
| 120 | + |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | +end |
0 commit comments