use of org.gitlab4j.api.models.Group in project catma by forTEXT.
the class GitlabManagerRestricted method createRepository.
@Override
public CreateRepositoryResponse createRepository(String name, String path, String groupPath) throws IOException {
GroupApi groupApi = restrictedGitLabApi.getGroupApi();
ProjectApi projectApi = restrictedGitLabApi.getProjectApi();
try {
Group group = groupApi.getGroup(groupPath);
Namespace namespace = new Namespace();
namespace.setId(group.getId());
Project project = new Project();
project.setName(name);
project.setNamespace(namespace);
if (StringUtils.isNotEmpty(path)) {
project.setPath(path);
}
project = projectApi.createProject(project);
return new CreateRepositoryResponse(groupPath, project.getId(), GitlabUtils.rewriteGitLabServerUrl(project.getHttpUrlToRepo()));
} catch (GitLabApiException e) {
throw new IOException("Failed to create remote Git repository", e);
}
}
use of org.gitlab4j.api.models.Group in project catma by forTEXT.
the class GitlabManagerCommon method getRoleOnProject.
@Override
public final RBACRole getRoleOnProject(RBACSubject subject, String projectId) throws IOException {
try {
Group group = getGitLabApi().getGroupApi().getGroup(projectId);
if (group == null) {
throw new IOException("Project unkown " + projectId);
}
Member member = getGitLabApi().getGroupApi().getMember(group.getId(), subject.getUserId());
if (member == null) {
throw new IOException("member not found " + subject);
}
return RBACRole.forValue(member.getAccessLevel().value);
} catch (GitLabApiException e) {
throw new IOException("error getting role on project #" + projectId, e);
}
}
use of org.gitlab4j.api.models.Group in project catma by forTEXT.
the class GitlabManagerCommon method unassignFromProject.
@Override
public final void unassignFromProject(RBACSubject subject, String projectId) throws IOException {
try {
Group group = getGitLabApi().getGroupApi().getGroup(projectId);
if (group == null) {
throw new IOException(String.format("CATMA-Project/git-Group unknown %1$s", projectId));
}
getGitLabApi().getGroupApi().removeMember(group.getId(), subject.getUserId());
} catch (GitLabApiException e) {
throw new IOException(String.format("Error accessing CATMA-Project/git-Group %1$s or user %2$s", projectId, subject == null ? "null" : subject.toString()), e);
}
}
use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.
the class GitlabUserProvider method syncUser.
@Override
public void syncUser(User user, boolean addOnly, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
List<GitlabFedIdentity> ids = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
User fromGitlab = this.findUser(user.getUserID(), attributes, request);
if (fromGitlab == null) {
this.createUser(user, attributes, request);
return;
}
List<GitlabFedIdentity> idsFromGitlab = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
HashMap<String, String> toSet = new HashMap<String, String>();
HashSet<String> toDelete = new HashSet<String>();
for (String attrName : attributes) {
Attribute attrFromGitlab = fromGitlab.getAttribs().get(attrName);
Attribute attrIn = user.getAttribs().get(attrName);
if ((attrIn != null && attrFromGitlab == null) || (attrIn != null && attrFromGitlab != null && !attrIn.getValues().get(0).equals(attrFromGitlab.getValues().get(0)))) {
toSet.put(attrName, attrIn.getValues().get(0));
} else if (!addOnly) {
if (attrIn == null && attrFromGitlab != null) {
toDelete.add(attrName);
}
}
}
org.gitlab4j.api.models.User toSave = this.findUserByName(user.getUserID());
for (String attrName : toSet.keySet()) {
try {
this.beanUtils.setProperty(toSave, attrName, toSet.get(attrName));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new ProvisioningException("Could not update user " + user.getUserID(), e);
}
}
for (String attrName : toDelete) {
try {
this.beanUtils.setProperty(toSave, attrName, "");
} catch (IllegalAccessException | InvocationTargetException e) {
throw new ProvisioningException("Could not update user " + user.getUserID(), e);
}
}
if (ids != null) {
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 {
for (GitlabFedIdentity id : ids) {
boolean found = false;
for (GitlabFedIdentity idfromgl : idsFromGitlab) {
if (id.getExternalUid().equals(idfromgl.getExternalUid()) && id.getProvider().equals(idfromgl.getProvider())) {
found = true;
break;
}
}
if (!found) {
HttpPut getmembers = new HttpPut(new StringBuilder().append(this.url).append("/api/v4/users/").append(toSave.getId()).append("?provider=").append(id.getProvider()).append("&extern_uid=").append(URLEncoder.encode(user.getUserID(), "UTF-8")).toString());
CloseableHttpResponse resp = http.execute(getmembers);
if (resp.getStatusLine().getStatusCode() != 200) {
throw new IOException("Invalid response " + resp.getStatusLine().getStatusCode());
}
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-provider", id.getProvider());
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-externid", id.getExternalUid());
}
}
} catch (IOException e) {
throw new ProvisioningException("Could not set identity", e);
} finally {
try {
http.close();
} catch (IOException e) {
}
bhcm.close();
}
}
try {
this.userApi.updateUser(toSave, null);
} catch (GitLabApiException e) {
throw new ProvisioningException("Could not save user " + user.getUserID(), e);
}
for (String attrName : toSet.keySet()) {
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Replace, approvalID, workflow, attrName, toSet.get(attrName));
}
for (String attrName : toDelete) {
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Replace, approvalID, workflow, attrName, "");
}
HashMap<String, Integer> groupmap = (HashMap<String, Integer>) request.get(GitlabUserProvider.GITLAB_GROUP_ENTITLEMENTS);
if (groupmap == null) {
groupmap = new HashMap<String, Integer>();
}
for (String inGroup : user.getGroups()) {
if (!fromGitlab.getGroups().contains(inGroup)) {
try {
Group groupObj = this.findGroupByName(inGroup);
if (groupObj == null) {
logger.warn("Group " + inGroup + " does not exist");
} else {
int accessLevel = AccessLevel.DEVELOPER.ordinal();
if (groupmap.containsKey(inGroup)) {
accessLevel = groupmap.get(inGroup);
}
this.groupApi.addMember(groupObj.getId(), toSave.getId(), accessLevel);
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", inGroup);
}
} catch (GitLabApiException e) {
if (e.getMessage().equalsIgnoreCase("Member already exists")) {
continue;
} else {
throw new ProvisioningException("Could not find group " + inGroup, e);
}
}
}
}
if (!addOnly) {
for (String groupFromGitlab : fromGitlab.getGroups()) {
if (!user.getGroups().contains(groupFromGitlab)) {
try {
Group groupObj = this.findGroupByName(groupFromGitlab);
if (groupObj == null) {
logger.warn("Group " + groupFromGitlab + " does not exist");
} else {
this.groupApi.removeMember(groupObj.getId(), toSave.getId());
this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Delete, approvalID, workflow, "group", groupFromGitlab);
}
} catch (GitLabApiException e) {
throw new ProvisioningException("Could not find group " + groupFromGitlab);
}
}
}
}
}
use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.
the class AddGroupToProject method doTask.
@Override
public boolean doTask(User user, Map<String, Object> request) throws ProvisioningException {
if (request.get("newProjectJSON") == null) {
logger.warn("Project not created, skipping");
return true;
}
String localGroupName = task.renderTemplate(this.groupName, request);
int approvalID = 0;
if (request.containsKey("APPROVAL_ID")) {
approvalID = (Integer) request.get("APPROVAL_ID");
}
Workflow workflow = (Workflow) request.get("WORKFLOW");
GitlabUserProvider gitlab = (GitlabUserProvider) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
GitLabApi api = gitlab.getApi();
ObjectMapper mapper = new ObjectMapper();
Project newProject = null;
if (this.projectName == null) {
try {
newProject = (Project) mapper.readValue((String) request.get("newProjectJSON"), Project.class);
} catch (JsonProcessingException e) {
throw new ProvisioningException("Could not parse", e);
}
} else {
String localProjectName = task.renderTemplate(this.projectName, request);
String localNamespace = task.renderTemplate(this.namespace, request);
try {
newProject = api.getProjectApi().getProject(localNamespace, localProjectName);
} catch (GitLabApiException e) {
throw new ProvisioningException("Could not find " + localNamespace + "/" + localProjectName, e);
}
}
Group groupToAdd;
try {
groupToAdd = gitlab.findGroupByName(localGroupName);
if (groupToAdd == null) {
throw new ProvisioningException("Group " + localGroupName + " does not exist");
}
api.getProjectApi().shareProject(newProject, groupToAdd.getId(), AccessLevel.valueOf(accessLevel), null);
} catch (GitLabApiException e) {
throw new ProvisioningException("Could not add group " + localGroupName + " to project " + newProject.getNameWithNamespace(), e);
}
GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().logAction(gitlab.getName(), false, ActionType.Add, approvalID, workflow, "gitlab-project-" + newProject.getNameWithNamespace() + "-group", localGroupName);
return true;
}
Aggregations