use of org.gitlab4j.api.models.Identity in project catma by forTEXT.
the class GitlabManagerPrivileged method createUser.
// it's more convenient to work with the User class internally, which is why this method exists
private User createUser(String email, String username, String password, String publicname, String provider) throws IOException {
UserApi userApi = privilegedGitLabApi.getUserApi();
if (password == null) {
// generate a random password
password = RandomStringUtils.random(12, 0, GitlabUtils.PWD_CHARS.length - 1, false, false, GitlabUtils.PWD_CHARS, new SecureRandom());
}
User user = new User();
user.setEmail(email);
user.setUsername(username);
user.setName(publicname);
user.setIsAdmin(false);
user.setSkipConfirmation(true);
if (provider != null) {
Identity identity = new Identity();
identity.setExternUid(username);
identity.setProvider(provider);
user.setIdentities(Collections.singletonList(identity));
}
try {
// do not send a pwd reset link
user = userApi.createUser(user, password, false);
return user;
} catch (GitLabApiException e) {
throw new IOException("Failed to create user", e);
}
}
use of org.gitlab4j.api.models.Identity in project OpenUnison by TremoloSecurity.
the class GitlabUserProvider method findUser.
@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
org.gitlab4j.api.models.User fromGitlab = findUserByName(userID);
if (fromGitlab == null) {
return null;
}
User forUnison = new User(userID);
for (String attrName : attributes) {
try {
String val = beanUtils.getProperty(fromGitlab, attrName);
if (val != null) {
Attribute attr = new Attribute(attrName, val);
forUnison.getAttribs().put(attrName, attr);
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new ProvisioningException("Couldn't load attribute " + attrName, e);
}
}
if (fromGitlab.getIdentities() != null) {
ArrayList<GitlabFedIdentity> ids = new ArrayList<GitlabFedIdentity>();
for (Identity fedid : fromGitlab.getIdentities()) {
GitlabFedIdentity id = new GitlabFedIdentity();
id.setExternalUid(fedid.getExternUid());
id.setProvider(fedid.getProvider());
ids.add(id);
}
request.put(GitlabUserProvider.GITLAB_IDENTITIES, ids);
}
ArrayList<Header> defheaders = new ArrayList<Header>();
defheaders.add(new BasicHeader("Private-Token", this.token));
BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
try {
HttpGet getmembers = new HttpGet(new StringBuilder().append(this.url).append("/api/v4/users/").append(fromGitlab.getId()).append("/memberships").toString());
CloseableHttpResponse resp = http.execute(getmembers);
if (resp.getStatusLine().getStatusCode() != 200) {
throw new IOException("Invalid response " + resp.getStatusLine().getStatusCode());
}
String json = EntityUtils.toString(resp.getEntity());
JSONArray members = (JSONArray) new JSONParser().parse(json);
for (Object o : members) {
JSONObject member = (JSONObject) o;
String sourceType = (String) member.get("source_type");
String sourceName = (String) member.get("source_name");
if (sourceType.equalsIgnoreCase("Namespace")) {
forUnison.getGroups().add(sourceName);
}
}
} catch (IOException | ParseException e) {
throw new ProvisioningException("Could not get group memebers", e);
} finally {
try {
http.close();
} catch (IOException e) {
}
bhcm.close();
}
return forUnison;
}
Aggregations