Permalink
Browse files

(#11046) Add support for user expiry in pw user provider

Add support for setting an expiry date for a user in the pw user
provider. FreeBSD uses the format DD-MM-YYYY rather than Puppet's
YYYY-MM-DD. Tests added to confirm the value is correctly swapped
around.

Also added custom accessor method to take the unix timestamp given
by the operating system to a Puppet-style YYYY-MM-DD. This stops
Puppet from repeatedly trying to set the expiry date if it's already
correct.
  • Loading branch information...
1 parent 9b8829d commit 032043e325c750e70fa33d7aea09e8e076135e96 @tdb tdb committed with Nov 19, 2011
Showing with 35 additions and 1 deletion.
  1. +23 −1 lib/puppet/provider/user/pw.rb
  2. +12 −0 spec/unit/provider/user/pw_spec.rb
@@ -5,13 +5,14 @@
desc "User management via `pw` on FreeBSD."
commands :pw => "pw"
- has_features :manages_homedir, :allows_duplicates, :manages_passwords
+ has_features :manages_homedir, :allows_duplicates, :manages_passwords, :manages_expiry
defaultfor :operatingsystem => :freebsd
options :home, :flag => "-d", :method => :dir
options :comment, :method => :gecos
options :groups, :flag => "-G"
+ options :expiry, :method => :expire
verify :gid, "GID must be an integer" do |value|
value.is_a? Integer
@@ -28,6 +29,10 @@ def addcmd
# the value needs to be quoted, mostly because -c might
# have spaces in it
if value = @resource.should(property) and value != ""
+ if property == :expiry
+ # FreeBSD uses DD-MM-YYYY rather than YYYY-MM-DD
+ value = value.split("-").reverse.join("-")
+ end
cmd << flag(property) << value
end
end
@@ -40,6 +45,10 @@ def addcmd
end
def modifycmd(param, value)
+ if param == :expiry
+ # FreeBSD uses DD-MM-YYYY rather than YYYY-MM-DD
+ value = value.split("-").reverse.join("-")
+ end
cmd = super(param, value)
cmd << "-m" if @resource.managehome?
cmd
@@ -69,5 +78,18 @@ def password
Puppet.debug "finished password for user '#{@resource[:name]}' method called : '#{current_password}'"
current_password
end
+
+ # Get expiry from system and convert to Puppet-style date
+ def expiry
+ expiry = self.get(:expiry)
+ expiry = :absent if expiry == 0
+
+ if expiry != :absent
+ t = Time.at(expiry)
+ expiry = "%4d-%02d-%02d" % [t.year, t.month, t.mday]
+ end
+
+ expiry
+ end
end
@@ -33,6 +33,12 @@
provider.create
end
+ it "should use -e with the correct argument when the expiry property is set" do
+ resource[:expiry] = "2010-02-19"
+ provider.expects(:execute).with(all_of(includes("-e"), includes("19-02-2010")))
+ provider.create
+ end
+
it "should use -g with the correct argument when the gid property is set" do
resource[:gid] = 12345
provider.expects(:execute).with(all_of(includes("-g"), includes(12345)))
@@ -119,6 +125,12 @@
provider.comment = "Testuser New Name"
end
+ it "should use -e with the correct argument when the expiry property is changed" do
+ resource[:expiry] = "2010-02-19"
+ provider.expects(:execute).with(all_of(includes("-e"), includes("19-02-2011")))
+ provider.expiry = "2011-02-19"
+ end
+
it "should use -g with the correct argument when the gid property is changed" do
resource[:gid] = 12345
provider.expects(:execute).with(all_of(includes("-g"), includes(54321)))

0 comments on commit 032043e

Please sign in to comment.