2012-04-07 12:45:35 -03:00
|
|
|
/*
|
2022-04-11 17:50:19 -03:00
|
|
|
* Copyright [2022] [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.
|
|
|
|
*/
|
2012-02-21 14:22:43 -03:00
|
|
|
|
2012-04-07 12:45:35 -03:00
|
|
|
package com.wisemapping.rest;
|
2012-02-21 14:22:43 -03:00
|
|
|
|
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
2014-01-16 20:09:22 -03:00
|
|
|
import com.wisemapping.model.AuthenticationType;
|
|
|
|
import com.wisemapping.model.Collaboration;
|
|
|
|
import com.wisemapping.model.Mindmap;
|
|
|
|
import com.wisemapping.model.User;
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.rest.model.RestUser;
|
2013-03-24 15:03:19 -03:00
|
|
|
import com.wisemapping.service.MindmapService;
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.service.UserService;
|
2024-01-21 15:42:02 -08:00
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2012-02-21 14:22:43 -03:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2012-06-20 13:28:45 -03:00
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
2012-02-21 20:04:17 -03:00
|
|
|
import org.springframework.http.HttpStatus;
|
2023-07-28 22:46:38 -07:00
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
2021-12-24 18:03:23 -08:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2012-02-21 14:22:43 -03:00
|
|
|
|
2013-03-24 15:03:19 -03:00
|
|
|
import java.util.List;
|
2012-02-21 14:22:43 -03:00
|
|
|
|
2023-11-27 21:08:28 -08:00
|
|
|
@RestController
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping("/api/restfull/admin")
|
2023-07-28 22:46:38 -07:00
|
|
|
@PreAuthorize("isAuthenticated() and hasRole('ROLE_ADMIN')")
|
2012-02-21 20:04:17 -03:00
|
|
|
public class AdminController extends BaseController {
|
2012-06-20 13:28:45 -03:00
|
|
|
@Qualifier("userService")
|
2012-02-21 14:22:43 -03:00
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
|
2013-03-24 15:03:19 -03:00
|
|
|
@Qualifier("mindmapService")
|
|
|
|
@Autowired
|
|
|
|
private MindmapService mindmapService;
|
|
|
|
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/users/{id}", produces = {"application/json"})
|
2012-02-21 14:22:43 -03:00
|
|
|
@ResponseBody
|
2024-02-04 10:42:16 -08:00
|
|
|
public RestUser getUserById(@PathVariable int id) {
|
2012-02-21 14:22:43 -03:00
|
|
|
final User userBy = userService.getUserBy(id);
|
|
|
|
if (userBy == null) {
|
|
|
|
throw new IllegalArgumentException("User could not be found");
|
|
|
|
}
|
2014-01-16 20:09:22 -03:00
|
|
|
return new RestUser(userBy);
|
2012-02-21 14:22:43 -03:00
|
|
|
}
|
|
|
|
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = "/users/email/{email:.+}", produces = {"application/json"})
|
2012-02-21 14:22:43 -03:00
|
|
|
@ResponseBody
|
2024-02-04 10:42:16 -08:00
|
|
|
public RestUser getUserByEmail(@PathVariable String email) {
|
2012-02-21 17:41:51 -03:00
|
|
|
final User user = userService.getUserBy(email);
|
|
|
|
if (user == null) {
|
|
|
|
throw new IllegalArgumentException("User '" + email + "' could not be found");
|
2012-02-21 14:22:43 -03:00
|
|
|
}
|
2014-01-16 20:09:22 -03:00
|
|
|
return new RestUser(user);
|
2012-02-21 14:22:43 -03:00
|
|
|
}
|
|
|
|
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = "/users", consumes = {"application/json"}, produces = {"application/json"})
|
2012-03-12 10:48:54 -03:00
|
|
|
@ResponseStatus(value = HttpStatus.CREATED)
|
2021-12-24 18:03:23 -08:00
|
|
|
public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws WiseMappingException {
|
2012-02-21 14:22:43 -03:00
|
|
|
if (user == null) {
|
|
|
|
throw new IllegalArgumentException("User could not be found");
|
|
|
|
}
|
2012-02-21 17:41:51 -03:00
|
|
|
|
|
|
|
// User already exists ?
|
|
|
|
final String email = user.getEmail();
|
2012-02-21 20:04:17 -03:00
|
|
|
if (userService.getUserBy(email) != null) {
|
2012-02-21 17:41:51 -03:00
|
|
|
throw new IllegalArgumentException("User already exists with this email.");
|
|
|
|
}
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
// Run some other validations ...
|
2012-03-12 10:48:54 -03: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();
|
2012-03-14 01:49:05 -03:00
|
|
|
if (firstName == null || firstName.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("firstname can not be null");
|
|
|
|
}
|
2012-03-12 10:48:54 -03:00
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
|
|
|
|
final String password = delegated.getPassword();
|
|
|
|
if (password == null || password.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("password can not be null");
|
|
|
|
}
|
|
|
|
|
2012-03-14 01:49:05 -03:00
|
|
|
// Finally create the user ...
|
2013-03-17 23:17:55 -03:00
|
|
|
delegated.setAuthenticationType(AuthenticationType.DATABASE);
|
2013-03-17 18:51:33 -03:00
|
|
|
userService.createUser(delegated, false, true);
|
2024-01-15 17:30:31 -08:00
|
|
|
response.setHeader("Location", "/api/restfull/admin/users/" + user.getId());
|
2012-02-21 17:41:51 -03:00
|
|
|
}
|
2012-02-21 16:36:19 -03:00
|
|
|
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping(method = RequestMethod.PUT, value = "/users/{id}/password", consumes = {"text/plain"})
|
2012-02-21 20:04:17 -03:00
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
2024-02-04 10:42:16 -08:00
|
|
|
public void changePassword(@RequestBody String password, @PathVariable int id) {
|
2012-02-21 17:41:51 -03:00
|
|
|
if (password == null) {
|
|
|
|
throw new IllegalArgumentException("Password can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
final User user = userService.getUserBy(id);
|
2012-02-21 20:04:17 -03:00
|
|
|
if (user == null) {
|
|
|
|
throw new IllegalArgumentException("User '" + id + "' could not be found");
|
|
|
|
}
|
2012-02-21 17:41:51 -03:00
|
|
|
user.setPassword(password);
|
|
|
|
userService.changePassword(user);
|
|
|
|
}
|
|
|
|
|
2024-02-04 10:42:16 -08:00
|
|
|
@RequestMapping(method = RequestMethod.DELETE, value = "/users/{id}")
|
2012-02-21 20:04:17 -03:00
|
|
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
2022-01-16 20:01:56 -08:00
|
|
|
public void deleteUserByEmail(@PathVariable int id) throws WiseMappingException {
|
2012-02-21 17:41:51 -03:00
|
|
|
final User user = userService.getUserBy(id);
|
|
|
|
if (user == null) {
|
|
|
|
throw new IllegalArgumentException("User '" + id + "' could not be found");
|
|
|
|
}
|
2014-01-09 22:38:59 -03:00
|
|
|
|
|
|
|
final List<Collaboration> collaborations = mindmapService.findCollaborations(user);
|
|
|
|
for (Collaboration collaboration : collaborations) {
|
|
|
|
final Mindmap mindmap = collaboration.getMindMap();
|
2022-04-11 17:50:19 -03:00
|
|
|
mindmapService.removeMindmap(mindmap, user);
|
2014-01-09 22:38:59 -03:00
|
|
|
}
|
2014-01-25 12:31:14 -03:00
|
|
|
userService.removeUser(user);
|
2012-02-21 14:22:43 -03:00
|
|
|
}
|
|
|
|
}
|