Skip to content

Commit 84797fe

Browse files
author
Dylan Ratcliffe
committed
Added normalization for users in roles
This means that people can specify a list of user names that should be in a role, they will be converted to UUIDs before being enforced
1 parent 2fb7ca8 commit 84797fe

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/puppet/provider/rbac_role/ruby.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create
4343
'description' => resource[:description],
4444
'display_name' => resource[:name],
4545
'permissions' => resource[:permissions],
46-
'user_ids' => resource[:user_ids],
46+
'user_ids' => normalize_users(resource[:user_ids]),
4747
'group_ids' => resource[:group_ids],
4848
}
4949
Puppet::Provider::Rbac_api::post_response('/roles', role)
@@ -64,6 +64,22 @@ def destroy
6464
fail "The id parameter is read-only."
6565
end
6666

67+
def normalize_users(list)
68+
users = nil
69+
list.collect! do |item|
70+
next item if item.to_i != 0
71+
72+
# lazy load the available users. Avoid the API call unless needed
73+
users ||= Puppet::Provider::Rbac_api::get_response('/users')
74+
75+
begin
76+
users.find {|r| r['display_name'].downcase == item.downcase }['id']
77+
rescue NoMethodError => e
78+
fail "User #{item} does not exist"
79+
end
80+
end
81+
end
82+
6783
def flush
6884
# so, flush gets called, even on create() and delete()
6985
return if @property_hash[:id].nil?
@@ -74,7 +90,7 @@ def flush
7490
'description' => @property_hash[:description],
7591
'display_name' => @property_hash[:name],
7692
'permissions' => @property_hash[:permissions],
77-
'user_ids' => @property_hash[:user_ids],
93+
'user_ids' => normalize_users(@property_hash[:user_ids]),
7894
'group_ids' => @property_hash[:group_ids],
7995
}
8096

lib/puppet/type/rbac_role.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def insync?(is)
3838
# The current value may be nil and we don't
3939
# want to call sort on it so make sure we have arrays
4040
if is.is_a?(Array) and @should.is_a?(Array)
41-
is.sort == @should.sort
41+
is.sort == provider.normalize_users(@should).sort
4242
else
4343
is == @should
4444
end

0 commit comments

Comments
 (0)