AdminController.java
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.mvc.admin;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.PasswordPolicy;
import org.genesys.filerepository.persistence.ImageGalleryPersistence;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys.filerepository.persistence.RepositoryFolderRepository;
import org.gringlobal.model.SysUser;
import org.gringlobal.persistence.InventoryMaintenancePolicyRepository;
import org.gringlobal.persistence.SiteRepository;
import org.gringlobal.persistence.kpi.ExecutionRepository;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.ElasticsearchService;
import org.gringlobal.service.InventoryService;
import org.gringlobal.service.UserService;
import org.gringlobal.worker.GenesysDownloader;
import org.gringlobal.worker.UsdaGeographyUpdater;
import org.gringlobal.worker.UsdaTaxonomyUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequestMapping("/admin")
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Slf4j
public class AdminController {
@Value("${build.name}")
private String buildName;
@Autowired(required = false)
private UsdaTaxonomyUpdater usdaTaxonomyUpdater;
@Autowired(required = false)
private UsdaGeographyUpdater usdaGeographyUpdater;
@Autowired(required = false)
private GenesysDownloader genesysDownloader;
@Autowired(required = false)
private ElasticsearchService elasticsearchService;
@Autowired
private UserService userService;
@Autowired
private CustomAclService aclService;
@Autowired
private SiteRepository siteRepository;
@Autowired
private InventoryMaintenancePolicyRepository inventoryPolicyRepository;
@Autowired
private RepositoryFolderRepository folderRepository;
@Autowired
private RepositoryFilePersistence fileRepository;
@Autowired
private ImageGalleryPersistence imageGalleryRepository;
@Autowired
private ExecutionRepository kpiExecutionRepository;
@Autowired
private AccessionService accessionService;
@Autowired
private InventoryService inventoryService;
@GetMapping("/")
public String index(ModelMap model) {
model.addAttribute("buildName", buildName);
if (elasticsearchService != null) {
model.addAttribute("indexedEntities", elasticsearchService.getIndexedEntities());
}
return "/admin/index";
}
@PostMapping(path = "/action", params = { "action=stop" })
public String stopServer() throws Exception {
System.exit(-1);
return "redirect:/";
}
@PostMapping(path = "/action", params = { "action=update-usda-taxonomy" })
public String updateUsdaTaxonomy() throws Exception {
usdaTaxonomyUpdater.update();
log.warn("Done!");
return "redirect:/admin/";
}
@PostMapping(path = "/action", params = { "action=recalculate-accenumb" })
public String recalculateAccessionNumbers() {
accessionService.recalculateAllAccessionNumbers();
return "redirect:/admin/";
}
@PostMapping(path = "/action", params = { "action=recalculate-inventorynumb" })
public String recalculateInventoryNumbers() {
inventoryService.recalculateAllInventoryNumbers();
return "redirect:/admin/";
}
@PostMapping(path = "/action", params = { "action=update-usda-geography" })
public String updateUsdaGeography() throws Exception {
usdaGeographyUpdater.update();
return "redirect:/admin/";
}
@PostMapping(path = "/action", params = { "action=from-genesys" })
public String downloadFromGenesys(@RequestParam(name = "instituteCode", required = true) String instituteCode, @RequestParam(name="authorizationToken", required = true) String authorizationToken) throws Exception {
genesysDownloader.download(instituteCode, authorizationToken);
return "redirect:/admin/";
}
@PostMapping(path = "/action", params = { "action=add-user" })
public String addUser(@RequestParam(name="username") String username, @RequestParam(name="password") String password) throws PasswordPolicy.PasswordPolicyException {
log.warn("Adding user {}", username);
SysUser user = new SysUser();
user.setUsername(username);
user.setPassword(password);
user = userService.create(user);
log.warn("Added user id={} username={}", user.getId(), user.getUsername());
return "redirect:/admin/";
}
@PostMapping(value = "/acl", params = { "site" })
@Transactional
public String aclFixSiteAcl() {
log.warn("Adding ACL for Sites");
siteRepository.findAll().forEach(site -> aclService.createOrUpdatePermissions(site));
return "redirect:/admin/";
}
@PostMapping(value = "/acl", params = { "inventory-policy" })
@Transactional
public String aclFixInventoryPolicyAcl() {
log.warn("Adding ACL for InventoryMaintenancePolicies");
inventoryPolicyRepository.findAll().forEach(policy -> aclService.createOrUpdatePermissions(policy));
return "redirect:/admin/";
}
@PostMapping(value = "/acl", params = { "repository" })
@Transactional
public String aclFixRepositoryAcl() {
log.warn("Adding ACL for Repository folders");
folderRepository.findAll().forEach(folder -> aclService.createOrUpdatePermissions(folder));
log.warn("Adding ACL for Repository files");
fileRepository.findAll().forEach(file -> aclService.createOrUpdatePermissions(file));
log.warn("Adding ACL for Image galleries");
imageGalleryRepository.findAll().forEach(gallery -> aclService.createOrUpdatePermissions(gallery));
return "redirect:/admin/";
}
@PostMapping(value = "/acl", params = { "kpi" })
@Transactional
public String aclFixKPIAcl() {
log.warn("Adding ACL support to KPI Execution");
kpiExecutionRepository.findAll().forEach(execution -> {
log.warn("Making KPI Execution {} ACL-ready", execution.getName());
aclService.createOrUpdatePermissions(execution);
});
return "redirect:/admin/";
}
}