Skip to content

Commit

Permalink
Refactoring package name. Add swagger description.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfi2022 committed Mar 7, 2024
1 parent c64cabe commit 101fc93
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 157 deletions.
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,30 @@
<version>1.0.2-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung;
package de.muenchen.mpdz.zammad.ldap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package de.muenchen.mpdz.zammad.ldap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import de.muenchen.mpdz.zammad.ldap.domain.ZammadRoleDTO;
import de.muenchen.mpdz.zammad.ldap.service.ZammadLdapService;
import de.muenchen.mpdz.zammad.ldap.service.ZammadService;
import de.muenchen.mpdz.zammad.ldap.service.ZammadSyncService;
import de.muenchen.oss.ezldap.core.LdapOuNode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;

/**
*
* Request swagger api documentation : http://[url]:[port]/swagger-ui/index.htm
*
*/

@RestController
@Slf4j
public class SyncController {

@Autowired
public ZammadService zammadService;

@Autowired
public ZammadLdapService ldapService;

@Autowired
public ZammadSyncService syncService;

@Operation(summary="Adds ldap ou to zammad role 'zuweisungsrolle'.", description="Set ldap ou authority in zammad.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "400", description = "BAD REQUEST" ),
}
)
@PutMapping("/assignmentrole")
public ResponseEntity<Object> updateAssignmentRole() {
try {
syncService.syncAssignmentRole();
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
return new ResponseEntity<>(e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
}
}

@Operation(summary="Get assignment role.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ZammadRoleDTO.class)) ),
@ApiResponse(responseCode = "400", description = "BAD REQUEST" ),
}
)
@GetMapping("/assignmentrole")
public ResponseEntity<ZammadRoleDTO> getAssignmentRole(@RequestParam(required = true) String id) {
ZammadRoleDTO zammadRoleDTO = zammadService.getZammadRole(id);
return new ResponseEntity<>(zammadRoleDTO, HttpStatus.OK);
}

@Operation(summary="Synchronize ldap zammad ou and user.", description="Add/update zammad entities.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/text") ),
@ApiResponse(responseCode = "400", description = "BAD REQUEST" ),
}
)
@PutMapping("/syncsubtree")
public ResponseEntity<Object> synchronizeDnSubtree(@Schema(example="o=oubase,dc=example,dc=org") @RequestParam(required = true) String distinguishedName, @Schema(example="20240226083627Z (yyyyMMddHHmmssZ)") @RequestParam(required = false) String timeStamp) {
try {
var treeView = syncService.syncSubtreeByDn(distinguishedName, timeStamp);
return new ResponseEntity<>(treeView, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
return new ResponseEntity<>(e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
}
}

@Operation(summary="Flag user for deletion.", description="Check whether user has left the organization. Zammad automation job delets user.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/text") ),
@ApiResponse(responseCode = "400", description = "BAD REQUEST" ),
}
)
@DeleteMapping("/finduserdelete")
public ResponseEntity<Object> flagUserToDeleteDnSubtree(@Schema(example="o=oubase,dc=example,dc=org") @RequestParam(required = true) String distinguishedName) {
try {
var treeView = syncService.flagZammadUserToDelete(distinguishedName);
return new ResponseEntity<>(treeView, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
return new ResponseEntity<>(e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
}
}

@Operation(summary="Get ldap subtree as json")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LdapOuNode.class)) ),
@ApiResponse(responseCode = "400", description = "BAD REQUEST" ),
}
)
@GetMapping("/subtreeasjson")
public ResponseEntity<Object> subtreeAsJson(@Schema(example="o=oubase,dc=example,dc=org") @RequestParam(required = true) String distinguishedName, @Schema(example="20240226083627Z (yyyyMMddHHmmssZ)") @RequestParam(required = false) String timeStamp) {
try {
var treeView = syncService.subtreeAsJson(distinguishedName, timeStamp);
return new ResponseEntity<>(treeView, HttpStatus.OK);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
return new ResponseEntity<>(e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.domain;
package de.muenchen.mpdz.zammad.ldap.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.domain;
package de.muenchen.mpdz.zammad.ldap.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.domain;
package de.muenchen.mpdz.zammad.ldap.domain;

import java.util.List;
import java.util.Map;
Expand All @@ -19,6 +19,7 @@ public class ZammadUserDTO implements Comparable<ZammadUserDTO>{
private Map<String, List<String>> group_ids;
private String updated_at;
private boolean active;
private String deleteldapsync;


@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.service;
package de.muenchen.mpdz.zammad.ldap.service;

import java.util.Map;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.service;
package de.muenchen.mpdz.zammad.ldap.service;

import de.muenchen.mpdz.zammad.ldapAnbindung.domain.ZammadGroupDTO;
import de.muenchen.mpdz.zammad.ldapAnbindung.domain.ZammadRoleDTO;
import de.muenchen.mpdz.zammad.ldapAnbindung.domain.ZammadUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
Expand All @@ -13,6 +10,10 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import de.muenchen.mpdz.zammad.ldap.domain.ZammadGroupDTO;
import de.muenchen.mpdz.zammad.ldap.domain.ZammadRoleDTO;
import de.muenchen.mpdz.zammad.ldap.domain.ZammadUserDTO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -25,6 +26,7 @@ public class ZammadService {

@Value("${zammad.token}")
private String authorization;

@Value("${zammad.url.base}")
private String zammadBaseURL;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.service;
package de.muenchen.mpdz.zammad.ldap.service;


import de.muenchen.oss.ezldap.core.LdapService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.muenchen.mpdz.zammad.ldapAnbindung.service;
package de.muenchen.mpdz.zammad.ldap.service;

import java.util.HashMap;
import java.util.List;
Expand All @@ -8,8 +8,10 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import de.muenchen.mpdz.zammad.ldapAnbindung.domain.ZammadGroupDTO;
import de.muenchen.mpdz.zammad.ldapAnbindung.domain.ZammadRoleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;

import de.muenchen.mpdz.zammad.ldap.domain.ZammadGroupDTO;
import de.muenchen.mpdz.zammad.ldap.domain.ZammadRoleDTO;
import lombok.extern.slf4j.Slf4j;

@Service
Expand Down Expand Up @@ -64,16 +66,16 @@ public void syncAssignmentRole() {
* @param modifyTimeStamp Optional search attribute for ldap ou und user
* @return ldapTreeView
*/
public String syncSubtreeByDn(String distinguishedName, String modifyTimeStamp, boolean fullsync) {
public String syncSubtreeByDn(String distinguishedName, String modifyTimeStamp) {

var dn = distinguishedName;
log.info("*****************************************");
log.info("START sychronize Zammad groups and users with LDAP DN : " + dn);
log.info("START synchronize Zammad groups and users with LDAP DN : " + dn);

log.debug("Calculate LDAP Subtree with DN ... " + dn);
var shadeDnSubtree = zammadLdapService.calculateOuSubtreeWithUsersByDn(dn, modifyTimeStamp);

var treeView = shadeDnSubtree.get().values().iterator().next().logTree("");
var treeView = shadeDnSubtree.get().values().iterator().next().toTree();
log.debug(treeView);

log.debug("Update zammad groups and users ...");
Expand All @@ -82,15 +84,65 @@ public String syncSubtreeByDn(String distinguishedName, String modifyTimeStamp,
log.debug("Sync assignment roles ...");
syncAssignmentRole();

if (fullsync) {
subtreeUtil.deleteUpdateZammadUser(shadeDnSubtree.get());
}

log.info("END sychronize Zammad groups and users with LDAP DN : " + dn);

return treeView;
}

/**
*
* In Zammad it is recommended to delete users for privacy issues only (https://admin-docs.zammad.org/en/latest/system/data-privacy.html).
*
* Be careful using this option. Every zammad user not found in DN subtree will be marked for deletion.
* Use only with DN as short as possible (or as close at DN root as possible) to fetch all user you need in Zammad ! Do not use with DNs selecting limited subtrees only !
*
* To delete users finally use Zammad automation with condition : user.ldapsync = "delete" (https://admin-docs.zammad.org/en/latest/manage/scheduler.html).
*
* @param distinguishedName
* @return ldapTreeView
*/
public String flagZammadUserToDelete(String distinguishedName) {

var dn = distinguishedName;
log.info("*****************************************");
log.info("START assign deletion flag Zammad to users with LDAP DN : " + dn);

log.debug("Calculate LDAP Subtree with DN ... " + dn);
var shadeDnSubtree = zammadLdapService.calculateOuSubtreeWithUsersByDn(dn, null);

var rootEntry = shadeDnSubtree.get().entrySet().iterator().next();
subtreeUtil.assignDeletionFlagZammadUser(rootEntry.getValue());

var treeView = rootEntry.getValue().toTree();
log.debug(treeView);

log.info("END assign deletion flag with LDAP DN : " + dn);

return treeView;

}

/**
* Calculate ldap subtree with users based on distinguished name.
*
* @param distinguishedName
* @param modifyTimeStamp Optional search attribute for ldap ou und user
* @return ldap tree as json
* @throws JsonProcessingException
*/
public String subtreeAsJson(String distinguishedName, String modifyTimeStamp) throws JsonProcessingException {

var dn = distinguishedName;
log.info("*****************************************");
log.info("START sychronize Zammad groups and users with LDAP DN : " + dn);

log.debug("Calculate LDAP Subtree with DN ... " + dn);
var shadeDnSubtree = zammadLdapService.calculateOuSubtreeWithUsersByDn(dn, modifyTimeStamp);

var json = shadeDnSubtree.get().values().iterator().next().toJson();
log.debug(json);

return json;
}

}
Loading

0 comments on commit 101fc93

Please sign in to comment.