Module: EPC::Command::Role::InstanceMethods

Defined in:
lib/epc/modules/role.rb

Instance Method Summary collapse

Instance Method Details

#add_grant(role_id, grant) ⇒ Object

Raises:

  • (FatalError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/epc/modules/role.rb', line 82

def add_grant(role_id, grant)
  secured_type, action, secured_id = 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")
  else
    say("Request failed: [#{response[:message]}]")
  end
  return status
end

#add_group(role_id, group) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/epc/modules/role.rb', line 55

def add_group(role_id, group)
  group_id = retrieve_identifier_for("UserGroup", 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})
  if status.successful?
    say("Group [#{group}] added to role") 
  else
    say("Failed to add [#{group}]: [#{response[:message]}]")
  end

  return status
end

#add_user(role_id, user) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/epc/modules/role.rb', line 27

def add_user(role_id, user)
  user_id = retrieve_identifier_for("User", 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})
  if status.successful?
    say("User [#{user}] added to role") 
  else
    say("Failed to add [#{user}]: [#{response[:message]}]")
  end
  return status
end

#batch_add(role_id, data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/epc/modules/role.rb', line 9

def batch_add(role_id, data)
  users = data["users"] rescue []
  users.each do |user|
    add_user(role_id, user["id"])
  end

  groups = data["user_groups"] rescue []
  groups.each do |group|
    add_group(role_id, group["id"])
  end

  grants = data["grants"] rescue []
  grants.each do |grant|
    add_grant(role_id, [grant["secured_type"], grant["action"], grant["secured_id"]].join(":"))
  end

end

#remove_grant(role_id, grant) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/epc/modules/role.rb', line 103

def remove_grant(role_id, grant)
  secured_type, action, secured_id = 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")
    else
      say("Request failed: [#{response[:message]}]")
    end
  end
  return status
end

#remove_group(role_id, group) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/epc/modules/role.rb', line 70

def remove_group(role_id, group)
  group_id = retrieve_identifier_for("UserGroup", group)
  status, response, headers = client.delete(EPC::Config::ROLES_PATH + "/#{role_id}/remove_member", {:member_id => group_id, :member_type => "UserGroup"})
  if status.successful?
    say("Group [#{group}] removed from role") 
  else
    say("Failed to remove [#{group}]: [#{response[:message]}]")
  end

  return status
end

#remove_user(role_id, user) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/epc/modules/role.rb', line 42

def remove_user(role_id, user)
  user_id = retrieve_identifier_for("User", user)
  status, response, headers = client.delete(EPC::Config::ROLES_PATH + "/#{role_id}/remove_member", {:member_id => @options[:remove_user], :member_type => "User"})

  if status.successful?
    say("User [#{user}] removed from role")
  else
    say("Failed to remove [#{user}]: [#{response[:message]}]")
  end

  return status
end