Class: EPC::Command::UpdatePermissionCommand

Inherits:
UpdateCommand show all
Defined in:
lib/epc/command/update_rolepermissions_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

Raises:



3
4
5
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
# File 'lib/epc/command/update_rolepermissions_command.rb', line 3

def execute(role = nil, *args)
  raise FatalError, "You have to specify a role" if role.blank?


  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.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")
    else
      say("Request failed: [#{response[:message]}]")
    end

    return status
  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}/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")
        return 1
      else
        say("Request failed: [#{response[:message]}]")
        return 0
      end
    else
      say("Request failed: [#{response[:message]}]")
    end

    return status
  end

  if @options[:file].present?
    grants = EPC::Config.read_content_as_json(@options[:file])
    grants.each do |grant|
      raise FatalError, "Incorrectly specified grant.Grants should have at least secured_type and grant_action specified" if grant["secured_type"].blank? || grant["grant_action"].blank?
      grant["permitted_type"] = "Role"
      grant["permitted_id"] = role.to_i
      status, response, headers = client.post(EPC::Config::GRANTS_PATH, grant)
      if status.successful?
        say("Added grant [#{grant["secured_type"]}:#{grant["grant_action"]}:#{grant["secured_id"]}]")
      else
        say("Request failed: [#{response[:message]}]")
      end
    end

  end

end