Class: CF::UAA::GroupCli

Inherits:
CommonCli show all
Defined in:
lib/uaa/cli/group.rb

Instance Method Summary collapse

Methods inherited from CommonCli

#askd, #auth_header, #clientid, #clientname, #clientsecret, #complain, #debug?, #handle_request, #passcode, #scim_common_list, #scim_get_helper, #scim_get_object, #scim_get_user_object, #scim_request, #trace?, #update_target_info, #username, #userpwd, #verified_pwd

Methods inherited from Topic

#add_command, #ask, #ask_pwd, commands, define_option, desc, #gripe, #help_col_start, #initialize, #opt_help, #opt_strs, option_defs, #opts, #pp, #print_tree, #say, #say_cmd_helper, #say_command_help, #say_commands, #say_definition, #say_help, #terminal_columns, topic

Constructor Details

This class inherits a constructor from CF::UAA::Topic

Instance Method Details

#add_members(scim, name, members) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/uaa/cli/group.rb', line 144

def add_members(scim, name, members)
  group = scim_get_object(scim, :group, gname(name))

  old_members = (group['members'] || [])
  new_members = find_members(scim, members).map do |member|
    member.merge('origin' => 'uaa')
  end

  unless new_members.size == members.size
    raise 'not all users found, none added'
  end

  group['members'] = union(old_members, new_members)

  unless group['members'].size > old_members.size
    raise 'no new users given'
  end

  scim.put(:group, group)
  'success'
end

#delete_members(scim, name, members) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/uaa/cli/group.rb', line 166

def delete_members(scim, name, members)
  group = scim_get_object(scim, :group, gname(name))

  old_members = (group['members'] || [])
  new_members = find_members(scim, members)

  unless new_members.size == members.size
    raise 'not all users found, none deleted'
  end

  group['members'] = difference(old_members, new_members)

  unless group['members'].size < old_members.size
    raise 'no existing users to delete'
  end

  group.delete('members') if group['members'].empty?

  scim.put(:group, group)
  'success'
end

#difference(old_members, new_members) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/uaa/cli/group.rb', line 136

def difference(old_members, new_members)
  new_ids = id_set(new_members)

  old_members.reject do |member|
    new_ids.include?(member['value'])
  end
end

#find_members(scim, members) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/uaa/cli/group.rb', line 109

def find_members(scim, members)
  found_members = []

  scim.ids(:user, *members).each do |member|
    found_members << {
        'type' => 'USER',
        'value' => member['id'],
        'origin' => member['origin']
    }
  end

  found_members
end

#gname(name) ⇒ Object



24
# File 'lib/uaa/cli/group.rb', line 24

def gname(name) name || ask("Group name") end

#id_set(objs) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/uaa/cli/group.rb', line 101

def id_set(objs)
  objs.each_with_object(Set.new) {|o, s|
    id = o.is_a?(String)? o: (o["id"] || o["value"] || o["memberid"])
    raise BadResponse, "no id found in response of current members" unless id
    s << id
  }
end

#union(old_members, new_members) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/uaa/cli/group.rb', line 123

def union(old_members, new_members)
  old_ids = id_set(old_members)
  all_members = old_members.clone

  new_members.each do |member|
    unless old_ids.include?(member['value'])
      all_members << member
    end
  end

  all_members
end