wisemapping-open-source/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java

210 lines
9.3 KiB
Java
Raw Normal View History

/*
2012-10-05 01:48:01 +02:00
* Copyright [2012] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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 com.wisemapping.rest;
import com.wisemapping.exceptions.WiseMappingException;
2013-03-29 19:44:49 +01:00
import com.wisemapping.model.*;
import com.wisemapping.rest.model.RestUser;
2013-03-24 19:03:19 +01:00
import com.wisemapping.service.MindmapService;
import com.wisemapping.service.UserService;
2013-03-24 19:03:19 +01:00
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
2012-06-20 18:28:45 +02:00
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
2012-03-12 14:48:54 +01:00
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
2013-03-24 19:03:19 +01:00
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Pattern;
@Controller
public class AdminController extends BaseController {
2012-06-20 18:28:45 +02:00
@Qualifier("userService")
@Autowired
private UserService userService;
2013-03-24 19:03:19 +01:00
@Qualifier("mindmapService")
@Autowired
private MindmapService mindmapService;
@RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/json", "text/html", "application/xml"})
@ResponseBody
public ModelAndView getUserById(@PathVariable long id) throws IOException {
final User userBy = userService.getUserBy(id);
if (userBy == null) {
throw new IllegalArgumentException("User could not be found");
}
return new ModelAndView("userView", "user", new RestUser(userBy));
}
@RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/json", "text/html", "application/xml"})
@ResponseBody
public ModelAndView getUserByEmail(@PathVariable String email) throws IOException {
final User user = userService.getUserBy(email);
if (user == null) {
throw new IllegalArgumentException("User '" + email + "' could not be found");
}
return new ModelAndView("userView", "user", new RestUser(user));
}
@RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
2012-03-12 14:48:54 +01:00
@ResponseStatus(value = HttpStatus.CREATED)
public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws WiseMappingException {
if (user == null) {
throw new IllegalArgumentException("User could not be found");
}
// User already exists ?
final String email = user.getEmail();
if (userService.getUserBy(email) != null) {
throw new IllegalArgumentException("User already exists with this email.");
}
// Run some other validations ...
2012-03-12 14:48:54 +01:00
final User delegated = user.getDelegated();
final String lastname = delegated.getLastname();
if (lastname == null || lastname.isEmpty()) {
throw new IllegalArgumentException("lastname can not be null");
}
final String firstName = delegated.getFirstname();
if (firstName == null || firstName.isEmpty()) {
throw new IllegalArgumentException("firstname can not be null");
}
2012-03-12 14:48:54 +01:00
// Finally create the user ...
2013-03-18 03:17:55 +01:00
delegated.setAuthenticationType(AuthenticationType.DATABASE);
userService.createUser(delegated, false, true);
response.setHeader("Location", "/service/admin/users/" + user.getId());
}
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changePassword(@RequestBody String password, @PathVariable long id) throws WiseMappingException {
if (password == null) {
throw new IllegalArgumentException("Password can not be null");
}
final User user = userService.getUserBy(id);
if (user == null) {
throw new IllegalArgumentException("User '" + id + "' could not be found");
}
user.setPassword(password);
userService.changePassword(user);
}
2014-01-13 02:53:42 +01:00
@RequestMapping(method = RequestMethod.DELETE, value = "admin/users/{id}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
2014-01-10 02:38:59 +01:00
public void deleteUserByEmail(@PathVariable long id) throws WiseMappingException {
final User user = userService.getUserBy(id);
if (user == null) {
throw new IllegalArgumentException("User '" + id + "' could not be found");
}
2014-01-10 02:38:59 +01:00
final List<Collaboration> collaborations = mindmapService.findCollaborations(user);
for (Collaboration collaboration : collaborations) {
final Mindmap mindmap = collaboration.getMindMap();
mindmapService.removeMindmap(mindmap,user);
}
userService.deleteUser(user);
}
2013-03-24 19:03:19 +01:00
@ResponseStatus(value = HttpStatus.NO_CONTENT)
2013-03-24 19:42:25 +01:00
@RequestMapping(method = RequestMethod.GET, value = "admin/database/purge")
2013-03-24 20:33:58 +01:00
public void purgeDB(@RequestParam(required = true) Integer minUid, @RequestParam(required = true) Integer maxUid, @RequestParam(required = true) Boolean apply) throws WiseMappingException, UnsupportedEncodingException {
2013-03-24 19:03:19 +01:00
2013-03-24 20:33:58 +01:00
for (int i = minUid; i < maxUid; i++) {
2013-03-24 23:28:20 +01:00
try {
2013-03-25 02:49:55 +01:00
System.out.println("Looking for user:" + i);
final User user = userService.getUserBy(i);
if (user != null) {
// Do not process admin accounts ...
if (user.getEmail().contains("wisemapping")) {
continue;
}
2013-03-24 23:24:24 +01:00
// Iterate over the list of maps ...
final List<Collaboration> collaborations = mindmapService.findCollaborations(user);
for (Collaboration collaboration : collaborations) {
final Mindmap mindmap = collaboration.getMindMap();
if (MindmapFilter.MY_MAPS.accept(mindmap, user)) {
final Calendar yearAgo = Calendar.getInstance();
2013-03-25 02:49:55 +01:00
yearAgo.add(Calendar.MONTH, -4);
2013-03-24 23:24:24 +01:00
// The use has only two maps... When they have been modified ..
System.out.println("Checking map id:" + mindmap.getId());
if (mindmap.getLastModificationTime().before(yearAgo) && !mindmap.isPublic()) {
2013-03-25 02:49:55 +01:00
System.out.println("Old map months map:" + mindmap.getId());
2013-03-24 23:24:24 +01:00
if (isWelcomeMap(mindmap) || isSimpleMap(mindmap)) {
System.out.println("Purged map id:" + mindmap.getId() + ", userId:" + user.getId());
if (apply) {
mindmapService.removeMindmap(mindmap, user);
}
2013-03-24 20:20:56 +01:00
}
2013-03-24 19:42:25 +01:00
}
2013-03-25 02:49:55 +01:00
// Purge history ...
2013-03-28 16:28:53 +01:00
mindmapService.purgeHistory(mindmap.getId());
2013-03-24 19:03:19 +01:00
}
}
2013-03-25 02:49:55 +01:00
}
2013-03-24 23:28:20 +01:00
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (WiseMappingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (RuntimeException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
2013-03-29 19:44:49 +01:00
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
2013-03-24 19:03:19 +01:00
}
}
2013-03-27 02:58:45 +01:00
}
2013-03-24 19:03:19 +01:00
2013-03-27 02:58:45 +01:00
@ResponseStatus(value = HttpStatus.NO_CONTENT)
@RequestMapping(method = RequestMethod.GET, value = "admin/database/purge/history")
2013-03-29 19:44:49 +01:00
public void purgeHistory(@RequestParam(required = true) Integer mapId) throws WiseMappingException, IOException {
2013-03-24 19:03:19 +01:00
2013-03-28 16:28:53 +01:00
mindmapService.purgeHistory(mapId);
2013-03-24 19:03:19 +01:00
}
2013-03-27 02:58:45 +01:00
2013-03-24 19:03:19 +01:00
private boolean isWelcomeMap(@NotNull Mindmap mindmap) throws UnsupportedEncodingException {
// Is welcome map ?
final String xmlStr = mindmap.getXmlStr();
2013-03-24 20:48:14 +01:00
boolean oldWelcomeMap = xmlStr.contains("Welcome to WiseMapping") && xmlStr.contains("My Wisemaps");
return oldWelcomeMap;
2013-03-24 19:03:19 +01:00
}
public boolean isSimpleMap(@NotNull Mindmap mindmap) throws UnsupportedEncodingException {
String xmlStr = mindmap.getXmlStr();
String[] topics = xmlStr.split(Pattern.quote("<topic"));
return topics.length <= 3;
}
}