initial commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.services.businesslogic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import de.bstly.we.businesslogic.PermissionManager;
|
||||
import de.bstly.we.businesslogic.Permissions;
|
||||
import de.bstly.we.model.Permission;
|
||||
import de.bstly.we.oidc.model.OidcClient;
|
||||
import de.bstly.we.oidc.model.QOidcClient;
|
||||
import de.bstly.we.oidc.repository.OidcClientRepository;
|
||||
import de.bstly.we.services.model.QService;
|
||||
import de.bstly.we.services.model.Service;
|
||||
import de.bstly.we.services.repository.ServiceRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class ServiceManager {
|
||||
|
||||
@Autowired
|
||||
private ServiceRepository serviceRepository;
|
||||
@Autowired
|
||||
private OidcClientRepository oidcClientRepository;
|
||||
@Autowired
|
||||
private PermissionManager permissionManager;
|
||||
private QOidcClient qOidcClient = QOidcClient.oidcClient;
|
||||
private QService qService = QService.service;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Service get(String name) {
|
||||
return serviceRepository.findById(name).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param service
|
||||
* @return
|
||||
*/
|
||||
public Service update(Service service) {
|
||||
return serviceRepository.save(service);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @return
|
||||
*/
|
||||
public List<Service> getForTarget(Long target) {
|
||||
List<Service> services = Lists.newArrayList();
|
||||
|
||||
if (target == null) {
|
||||
return services;
|
||||
}
|
||||
|
||||
if (permissionManager.hasPermission(target, Permissions.ROLE_ADMIN)) {
|
||||
services.addAll(serviceRepository.findAll());
|
||||
for (OidcClient client : oidcClientRepository
|
||||
.findAll(qOidcClient.loginUrl.isNotNull())) {
|
||||
Service service = new Service(client.getClientName(), client.getLoginUrl());
|
||||
service.setCategory(client.getCategory());
|
||||
services.add(service);
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
for (Permission permission : permissionManager.getNotExpiresByTarget(target)) {
|
||||
if (serviceRepository.existsById(permission.getName())) {
|
||||
services.add(get(permission.getName()));
|
||||
} else if (oidcClientRepository.exists(qOidcClient.clientName.eq(permission.getName())
|
||||
.and(qOidcClient.loginUrl.isNotNull()))) {
|
||||
OidcClient client = oidcClientRepository
|
||||
.findOne(qOidcClient.clientName.eq(permission.getName())).get();
|
||||
Service service = new Service(client.getClientName(), client.getLoginUrl());
|
||||
service.setCategory(client.getCategory());
|
||||
services.add(service);
|
||||
}
|
||||
|
||||
for (Service service : serviceRepository
|
||||
.findAll(qService.permission.eq(permission.getName()))) {
|
||||
services.add(service);
|
||||
}
|
||||
}
|
||||
|
||||
if (permissionManager.isFullUser(target)) {
|
||||
for (Service service : serviceRepository.findAll(qService.alwaysPermitted.isTrue())) {
|
||||
services.add(service);
|
||||
}
|
||||
|
||||
for (OidcClient client : oidcClientRepository
|
||||
.findAll(qOidcClient.alwaysPermitted.isTrue())) {
|
||||
Service service = new Service(client.getClientName(), client.getLoginUrl());
|
||||
service.setCategory(client.getCategory());
|
||||
services.add(service);
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public void delete(String name) {
|
||||
serviceRepository.deleteById(name);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param page
|
||||
* @param size
|
||||
* @param sortBy
|
||||
* @param descending
|
||||
* @return
|
||||
*/
|
||||
public Page<Service> get(int page, int size, String sortBy, boolean descending) {
|
||||
Sort sort = descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending();
|
||||
return serviceRepository.findAll(PageRequest.of(page, size, sort));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.services.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import de.bstly.we.controller.BaseController;
|
||||
import de.bstly.we.services.businesslogic.ServiceManager;
|
||||
import de.bstly.we.services.model.Service;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/services")
|
||||
public class ServiceController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ServiceManager serviceManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping
|
||||
public List<Service> getServices() {
|
||||
return serviceManager.getForTarget(getCurrentUserId());
|
||||
}
|
||||
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.services.controller;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import de.bstly.we.controller.BaseController;
|
||||
import de.bstly.we.controller.support.EntityResponseStatusException;
|
||||
import de.bstly.we.services.businesslogic.ServiceManager;
|
||||
import de.bstly.we.services.model.Service;
|
||||
|
||||
/**
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/services/manage")
|
||||
public class ServiceManagementController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ServiceManager serviceManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pageParameter
|
||||
* @param sizeParameter
|
||||
* @return
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@GetMapping
|
||||
public Page<Service> getAllServices(@RequestParam("page") Optional<Integer> pageParameter,
|
||||
@RequestParam("size") Optional<Integer> sizeParameter) {
|
||||
return serviceManager.get(pageParameter.orElse(0), sizeParameter.orElse(10), "name", true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param service
|
||||
* @return
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@PostMapping
|
||||
public Service createOrUpdateService(@RequestBody Service service) {
|
||||
if (service.getName() == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
return serviceManager.update(service);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param service
|
||||
*/
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@DeleteMapping
|
||||
public void deleteService(@RequestBody Service service) {
|
||||
if (serviceManager.get(service.getName()) == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.NOT_MODIFIED);
|
||||
|
||||
}
|
||||
|
||||
serviceManager.delete(service.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.services.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "services", uniqueConstraints = @UniqueConstraint(columnNames = { "name" }))
|
||||
public class Service {
|
||||
|
||||
@Id
|
||||
@Column(name = "name", unique = true, nullable = false)
|
||||
private String name;
|
||||
@Column(name = "url")
|
||||
private String url;
|
||||
@Column(name = "always_permitted", columnDefinition = "boolean default false")
|
||||
private boolean alwaysPermitted;
|
||||
@Column(name = "same_site", columnDefinition = "boolean default false")
|
||||
private boolean sameSite;
|
||||
@Column(name = "permission", nullable = true)
|
||||
private String permission;
|
||||
@Column(name = "category")
|
||||
private String category;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Service() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param url
|
||||
*/
|
||||
public Service(String name, String url) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name the name to set
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the url
|
||||
*/
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url the url to set
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the alwaysPermitted
|
||||
*/
|
||||
public boolean isAlwaysPermitted() {
|
||||
return alwaysPermitted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param alwaysPermitted the alwaysPermitted to set
|
||||
*/
|
||||
public void setAlwaysPermitted(boolean alwaysPermitted) {
|
||||
this.alwaysPermitted = alwaysPermitted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the sameSite
|
||||
*/
|
||||
public boolean isSameSite() {
|
||||
return sameSite;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sameSite the sameSite to set
|
||||
*/
|
||||
public void setSameSite(boolean sameSite) {
|
||||
this.sameSite = sameSite;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the permission
|
||||
*/
|
||||
public String getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param permission the permission to set
|
||||
*/
|
||||
public void setPermission(String permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the category
|
||||
*/
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param category the category to set
|
||||
*/
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.bstly.we.services.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.bstly.we.services.model.Service;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author _bastler@bstly.de
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
public interface ServiceRepository extends JpaRepository<Service, String>, QuerydslPredicateExecutor<Service> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user