Commit 15e1a975 by Jan Hrabal

WIPo

parent bd2660d8
......@@ -240,8 +240,7 @@ CREATE TABLE ATTACHMENT (
FILENAME VARCHAR(2000),
UPLOADED DATE,
CONSTRAINT PK_ATTACHMENT PRIMARY KEY(ID),
CONSTRAINT FK_ATTACHMENT_UNIT FOREIGN KEY (UNIT_ID) REFERENCES UNIT(ID)
CONSTRAINT PK_ATTACHMENT PRIMARY KEY(ID)
);
CREATE INDEX ATTACHMENT_IDX ON ATTACHMENT(UNIT_ID, OBJECT_TYPE, OBJECT_ID);
......
......@@ -5,39 +5,41 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractHibernateAttachmentService implements AttachmentService {
private AttachmentRepository repository;
@Override
public Attachment saveAttachment(Attachment attachment, InputStream content) {
// TODO Auto-generated method stub
return null;
saveAttachment(attachment);
saveData(attachment, content);
return attachment;
}
protected abstract void saveData(Attachment attachment, InputStream stream);
@Override
public Attachment saveAttachment(Attachment attachment) {
// TODO Auto-generated method stub
return null;
repository.save(attachment);
return attachment;
}
@Override
public List<Attachment> fetchAttachments(String userId, String objectType, Long objectId) {
// TODO Auto-generated method stub
return null;
public List<Attachment> fetchAttachments(Long unitId, String objectType, Long objectId) {
return repository.findForUnitIdAndObject(unitId, objectType, objectId);
}
@Override
public Attachment fetchAttachmentInfo(Long attachmentId) {
// TODO Auto-generated method stub
return null;
public Attachment fetchAttachment(Long attachmentId) {
return repository.findById(attachmentId);
}
@Override
public void deleteAttachment(Long attachmentId) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Not implemented");
}
......
......@@ -82,6 +82,10 @@ public class AttachmentRepository extends AbstractHibernateRepository {
Attachment a = findForUnitIdAndId(unitId, attachmentId);
a.setName(name);
}
public Attachment findById(Long id) {
return entityManager.find(Attachment.class, id);
}
}
......@@ -9,11 +9,11 @@ public interface AttachmentService {
Attachment saveAttachment(Attachment attachment);
List<Attachment> fetchAttachments(String userId, String objectType, Long objectId);
List<Attachment> fetchAttachments(Long unitId, String objectType, Long objectId);
Attachment fetchAttachmentInfo(Long attachmentId);
Attachment fetchAttachment(Long attachmentId);
InputStream fetchAttachmentBody(Long attachmentId);
InputStream fetchAttachmentBody(Attachment attachment);
void deleteAttachment(Long attachmentId);
......
package com.jh.boot.attachment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Objects;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
public class FileBasedAttachmentService extends AbstractHibernateAttachmentService {
private String dataDirectory;
@Override
public InputStream fetchAttachmentBody(Attachment attachment) {
try {
return new FileInputStream(file(attachment));
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found", e);
}
}
@Override
protected void saveData(Attachment attachment, InputStream stream) {
try (FileOutputStream fos = new FileOutputStream(file(attachment))) {
IOUtils.copy(stream, fos);
} catch (Exception e) {
throw new RuntimeException("Cannot write data", e);
}
}
protected File file(Attachment attachment) {
Objects.requireNonNull(attachment, "Attachment must not be null");
return Paths.get(dataDirectory, s(attachment.getUnitId()), attachment.getObjectType(), s(attachment.getObjectId()), attachment.getFilename()).toFile();
}
protected String s(Number n) {
return n == null ? "0" : String.valueOf(n);
}
@Value("${attachment.dataDir:/app/data}")
public void setDataDirectory(String dataDirectory) {
this.dataDirectory = dataDirectory;
}
}
......@@ -20,9 +20,9 @@ public class AttachmentApiController {
private AttachmentService service;
@GetMapping("/attachments/{userId}/{objectType}/{objectId}")
public @ResponseBody List<Attachment> listAttachments(@PathVariable("userId") String userId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) {
return service.fetchAttachments(userId, objectType, objectId);
@GetMapping("/attachments/{unitId}/{objectType}/{objectId}")
public @ResponseBody List<Attachment> listAttachments(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) {
return service.fetchAttachments(unitId, objectType, objectId);
}
......
......@@ -168,6 +168,11 @@ public abstract class AbstractHibernateRepository {
}
public <ID, T> T findById(Class<T> clazz, ID id) {
return entityManager.find(clazz, id);
}
/**
* TODO.
*
......
......@@ -52,6 +52,9 @@ public interface AuthService {
*/
void resetPassword(String login, String token, String newPassword) throws AuthenticationException;
void changePassword(String login, String currentPassword, String newPassword) throws AuthenticationException;
/**
* TODO
*
......
......@@ -243,8 +243,19 @@ public class AuthApiController {
@PutMapping("/auth/password")
public void changePassword(@RequestBody ChangePassword chp) {
//authService.
public ResponseEntity<Void> changePassword(@RequestBody ChangePassword chp) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
AppUser user = null;
if (auth instanceof AppUserAuthentication) {
user = ((AppUserAuthentication) auth).getUser();
}
if (user != null) {
authService.changePassword(user.getEmail(), chp.getCurrentPassword(), chp.getPassword());
return ResponseEntity.accepted().build();
}
return ResponseEntity.notFound().build();
}
......
......@@ -146,5 +146,21 @@ public class AppUserAuthService implements AuthService {
public void setAuthListeners(Collection<AuthServiceListener> authListeners) {
this.authListeners = authListeners;
}
@Override
public void changePassword(String login, String currentPassword, String newPassword) throws AuthenticationException {
AppUser user = appUserRepository.findByLogin(login);
if (user == null) {
throw new BadCredentialsException("AUTH.USER_NOT_FOUND");
}
if (!PasswordUtils.checkPassword(currentPassword, user.getPassword(), user.getPasswordSalt())) {
throw new BadCredentialsException("AUTH.BAD_PASSWORD");
}
PasswordHash hash = PasswordUtils.hashPassword(newPassword);
user.setPassword(hash.getHash());
user.setPasswordSalt(hash.getSalt());
}
}
......@@ -34,6 +34,17 @@ public class Utils {
}
}
public static Long parseLong(String i, Long defaultValue) {
if (i == null) {
return defaultValue;
}
try {
return Long.parseLong(i);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Checks if is true.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment