Class: EPC::Command::UpdateRoleCommand

Inherits:
UpdateCommand show all
Defined in:
lib/epc/command/role/update_role_command.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::GIVEUP_TICKS, BaseCommand::SLEEP_TIME, BaseCommand::TICKER_TICKS

Instance Attribute Summary

Attributes inherited from BaseCommand

#client, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#go, inherited, #initialize, required_options

Methods included from PersistentAttributes

#auth_token, #caller_id, #target_url

Constructor Details

This class inherits a constructor from EPC::Command::BaseCommand

Instance Method Details

#execute(role = nil, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/epc/command/role/update_role_command.rb', line 6

def execute(role = nil, *args)
  role_id = retrieve_identifier_for("Role", role)

  if @options[:add_user].present?
    user_id = retrieve_identifier_for("User", @options[:add_user])
    params = []
    params << {:member_id => user_id.to_i, :member_type => "User"}

    status, response, headers = client.put(EPC::Config::ROLES_PATH + "/#{role_id}/assign_members", {:role_memberships => params})
    say("User added to role") if status.successful?
  end


  if @options[:remove_user].present?
    user_id = retrieve_identifier_for("User", @options[:add_user])
    status, response, headers = client.delete(EPC::Config::ROLES_PATH + "/#{role_id}/remove_member", {:member_id => @options[:remove_user], :member_type => "User"})
    say("User removed from role") if status.successful?
  end

  if @options[:add_group].present?
    group_id = retrieve_identifier_for("UserGroup", @options[:add_group])
    params = []
    params << {:member_id => group_id.to_i, :member_type => "UserGroup"}

    status, response, headers = client.put(EPC::Config::ROLES_PATH + "/#{role_id}/assign_members", {:role_memberships => params})
    say("UserGroup added to role") if status.successful?
  end


  if @options[:remove_group].present?
    group_id = retrieve_identifier_for("UserGroup", @options[:remove_group])
    status, response, headers = client.delete(EPC::Config::ROLES_PATH + "/#{role_id}/remove_member", {:member_id => group_id, :member_type => "UserGroup"})
    say("UserGroup removed from role") if status.successful?
  end



  if @options[:add_grant].present?
    secured_type, action, secured_id = @options[:add_grant].split(":")
    raise FatalError, "Grant incorrectly specified" if secured_type.blank? || action.blank?

    params = {}
    params[:grant_action] = action
    params[:permitted_type] = "Role"
    params[:permitted_id] = role_id.to_i
    params[:secured_type] = secured_type
    params[:secured_id] = secured_id if secured_id.present?


    status, response, headers = client.post(EPC::Config::GRANTS_PATH, params)

    if status.successful?
      say("Role permission updated")
    end
  end


  if @options[:remove_grant].present?
    secured_type, action, secured_id = @options[:remove_grant].split(":")
    status, response, headers = client.get(EPC::Config::ROLES_PATH + "/#{role_id}/grants")

    if status.successful?
      grant_id = response.detect do |g|
        found = (g[:action] == action)
        found = found && (g[:secured_type].to_s == secured_type)
        if secured_id.blank?
          found = found && g[:secured_id].blank?
        else
          found = found && (g[:secured_id].to_s == secured_id)
        end
        found
      end[:id] rescue nil

      raise FatalError, "Permission couldn't be determined" if grant_id.blank?

      status, response, headers = client.delete(EPC::Config::GRANTS_PATH + "/#{grant_id}")

      if status.successful?
        say("Role permission deleted")
      end
    end
  end



  say("Request failed: [#{response[:message]}]") unless status.successful?
  return status
end