use of com.tremolosecurity.unison.openstack.model.KSRoleAssignment in project OpenUnison by TremoloSecurity.
the class KeystoneProvisioningTarget method lookupUser.
public UserAndID lookupUser(String userID, Set<String> attributes, Map<String, Object> request, KSToken token, HttpCon con) throws Exception {
KSUser fromKS = null;
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("domain_id", this.usersDomain));
qparams.add(new BasicNameValuePair("name", userID));
StringBuffer b = new StringBuffer();
b.append(this.url).append("/users?").append(URLEncodedUtils.format(qparams, "UTF-8"));
String fullURL = b.toString();
String json = this.callWS(token.getAuthToken(), con, fullURL);
Gson gson = new Gson();
UserLookupResponse resp = gson.fromJson(json, UserLookupResponse.class);
if (resp.getUsers().isEmpty()) {
return null;
} else {
fromKS = resp.getUsers().get(0);
User user = new User(fromKS.getName());
if (attributes.contains("name")) {
user.getAttribs().put("name", new Attribute("name", fromKS.getName()));
}
if (attributes.contains("id")) {
user.getAttribs().put("id", new Attribute("id", fromKS.getId()));
}
if (attributes.contains("email") && fromKS.getEmail() != null) {
user.getAttribs().put("email", new Attribute("email", fromKS.getEmail()));
}
if (attributes.contains("description") && fromKS.getDescription() != null) {
user.getAttribs().put("description", new Attribute("description", fromKS.getEmail()));
}
if (attributes.contains("enabled")) {
user.getAttribs().put("enabled", new Attribute("enabled", Boolean.toString(fromKS.getEnabled())));
}
if (!rolesOnly) {
b.setLength(0);
b.append(this.url).append("/users/").append(fromKS.getId()).append("/groups");
json = this.callWS(token.getAuthToken(), con, b.toString());
GroupLookupResponse gresp = gson.fromJson(json, GroupLookupResponse.class);
for (KSGroup group : gresp.getGroups()) {
user.getGroups().add(group.getName());
}
}
if (attributes.contains("roles")) {
b.setLength(0);
b.append(this.url).append("/role_assignments?user.id=").append(fromKS.getId()).append("&include_names=true");
json = this.callWS(token.getAuthToken(), con, b.toString());
RoleAssignmentResponse rar = gson.fromJson(json, RoleAssignmentResponse.class);
Attribute attr = new Attribute("roles");
for (KSRoleAssignment role : rar.getRole_assignments()) {
if (role.getScope().getProject() != null) {
attr.getValues().add(gson.toJson(new Role(role.getRole().getName(), "project", role.getScope().getProject().getDomain().getName(), role.getScope().getProject().getName())));
} else {
attr.getValues().add(gson.toJson(new Role(role.getRole().getName(), "domain", role.getScope().getDomain().getName())));
}
}
if (!attr.getValues().isEmpty()) {
user.getAttribs().put("roles", attr);
}
}
UserAndID userAndId = new UserAndID();
userAndId.setUser(user);
userAndId.setId(fromKS.getId());
return userAndId;
}
}
Aggregations