All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blossomproject.ui.api.administration.UsersApiController Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.blossomproject.ui.api.administration;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.blossomproject.core.common.dto.AbstractDTO;
import com.blossomproject.core.common.search.SearchEngineImpl;
import com.blossomproject.core.user.UserCreateForm;
import com.blossomproject.core.user.UserDTO;
import com.blossomproject.core.user.UserService;
import com.blossomproject.core.user.UserUpdateForm;
import com.blossomproject.ui.stereotype.BlossomApiController;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.apache.tika.Tika;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by Maël Gargadennnec on 04/05/2017.
 */
@BlossomApiController
@RequestMapping("/administration/users")
public class UsersApiController {

  private final UserService userService;
  private final SearchEngineImpl searchEngine;
  private final Tika tika;

  public UsersApiController(UserService userService,
    SearchEngineImpl searchEngine, Tika tika) {
    this.userService = userService;
    this.searchEngine = searchEngine;
    this.tika = tika;
  }

  @GetMapping
  @PreAuthorize("hasAuthority('administration:users:read')")
  public Page list(
    @RequestParam(value = "q", required = false) String q,
    @PageableDefault(size = 25) Pageable pageable) {

    if (Strings.isNullOrEmpty(q)) {
      return this.userService.getAll(pageable);
    }
    return this.searchEngine.search(q, pageable).getPage();
  }

  @PostMapping
  @PreAuthorize("hasAuthority('administration:users:create')")
  public ResponseEntity create(@NotNull @Valid @RequestBody UserCreateForm userCreateForm)
    throws Exception {
    Preconditions.checkArgument(userCreateForm != null);
    return new ResponseEntity<>(userService.create(userCreateForm), HttpStatus.CREATED);
  }

  @GetMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:users:read')")
  public ResponseEntity get(@PathVariable Long id) {
    Preconditions.checkArgument(id != null);
    UserDTO user = userService.getOne(id);
    if (user == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
      return new ResponseEntity<>(user, HttpStatus.OK);
    }
  }

  @PutMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:users:write')")
  public ResponseEntity update(@PathVariable Long id,
    @Valid @RequestBody UserUpdateForm userUpdateForm) {
    Preconditions.checkArgument(id != null);
    UserDTO user = userService.getOne(id);
    if (user == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
      return new ResponseEntity<>(userService.update(id, userUpdateForm), HttpStatus.OK);
    }
  }

  @DeleteMapping("/{id}")
  @PreAuthorize("hasAuthority('administration:users:delete')")
  public ResponseEntity, Long>> delete(@PathVariable Long id,
    @RequestParam(value = "force", defaultValue = "false", required = false) boolean force) {
    UserDTO user = this.userService.getOne(id);
    if (user == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    Optional, Long>> result = this.userService.delete(user, force);
    if (!result.isPresent() || result.get().isEmpty()) {
      return new ResponseEntity<>(HttpStatus.OK);
    } else {
      return new ResponseEntity<>(result.get(), HttpStatus.CONFLICT);
    }
  }

  @GetMapping(value = "/{id}/avatar")
  @ResponseBody
  public ResponseEntity displayAvatar(@PathVariable Long id)
    throws IOException {
    InputStream avatar = this.userService.loadAvatar(id);
    return ResponseEntity.ok()
      .contentType(MediaType.parseMediaType(this.tika.detect(avatar)))
      .body(new InputStreamResource(avatar));
  }

  @PostMapping("/{id}/_avatar/_edit")
  @PreAuthorize("hasAuthority('administration:users:write')")
  public void updateAvatar(@PathVariable Long id, @RequestParam("avatar") MultipartFile file)
    throws IOException {
    UserDTO user = this.userService.getOne(id);
    if (user == null) {
      throw new NoSuchElementException(String.format("User=%s not found", id));
    }
    this.userService.updateAvatar(id, file.getBytes());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy