Commit 15e1a975 by Jan Hrabal

WIPo

parent bd2660d8
...@@ -240,8 +240,7 @@ CREATE TABLE ATTACHMENT ( ...@@ -240,8 +240,7 @@ CREATE TABLE ATTACHMENT (
FILENAME VARCHAR(2000), FILENAME VARCHAR(2000),
UPLOADED DATE, UPLOADED DATE,
CONSTRAINT PK_ATTACHMENT PRIMARY KEY(ID), CONSTRAINT PK_ATTACHMENT PRIMARY KEY(ID)
CONSTRAINT FK_ATTACHMENT_UNIT FOREIGN KEY (UNIT_ID) REFERENCES UNIT(ID)
); );
CREATE INDEX ATTACHMENT_IDX ON ATTACHMENT(UNIT_ID, OBJECT_TYPE, OBJECT_ID); CREATE INDEX ATTACHMENT_IDX ON ATTACHMENT(UNIT_ID, OBJECT_TYPE, OBJECT_ID);
......
...@@ -5,39 +5,41 @@ import java.util.List; ...@@ -5,39 +5,41 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractHibernateAttachmentService implements AttachmentService { public abstract class AbstractHibernateAttachmentService implements AttachmentService {
private AttachmentRepository repository; private AttachmentRepository repository;
@Override @Override
public Attachment saveAttachment(Attachment attachment, InputStream content) { public Attachment saveAttachment(Attachment attachment, InputStream content) {
// TODO Auto-generated method stub saveAttachment(attachment);
return null; saveData(attachment, content);
return attachment;
} }
protected abstract void saveData(Attachment attachment, InputStream stream);
@Override @Override
public Attachment saveAttachment(Attachment attachment) { public Attachment saveAttachment(Attachment attachment) {
// TODO Auto-generated method stub repository.save(attachment);
return null; return attachment;
} }
@Override @Override
public List<Attachment> fetchAttachments(String userId, String objectType, Long objectId) { public List<Attachment> fetchAttachments(Long unitId, String objectType, Long objectId) {
// TODO Auto-generated method stub return repository.findForUnitIdAndObject(unitId, objectType, objectId);
return null;
} }
@Override @Override
public Attachment fetchAttachmentInfo(Long attachmentId) { public Attachment fetchAttachment(Long attachmentId) {
// TODO Auto-generated method stub return repository.findById(attachmentId);
return null;
} }
@Override @Override
public void deleteAttachment(Long attachmentId) { public void deleteAttachment(Long attachmentId) {
// TODO Auto-generated method stub throw new UnsupportedOperationException("Not implemented");
} }
......
...@@ -82,6 +82,10 @@ public class AttachmentRepository extends AbstractHibernateRepository { ...@@ -82,6 +82,10 @@ public class AttachmentRepository extends AbstractHibernateRepository {
Attachment a = findForUnitIdAndId(unitId, attachmentId); Attachment a = findForUnitIdAndId(unitId, attachmentId);
a.setName(name); a.setName(name);
} }
public Attachment findById(Long id) {
return entityManager.find(Attachment.class, id);
}
} }
...@@ -9,11 +9,11 @@ public interface AttachmentService { ...@@ -9,11 +9,11 @@ public interface AttachmentService {
Attachment saveAttachment(Attachment attachment); 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); 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 { ...@@ -20,9 +20,9 @@ public class AttachmentApiController {
private AttachmentService service; private AttachmentService service;
@GetMapping("/attachments/{userId}/{objectType}/{objectId}") @GetMapping("/attachments/{unitId}/{objectType}/{objectId}")
public @ResponseBody List<Attachment> listAttachments(@PathVariable("userId") String userId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) { public @ResponseBody List<Attachment> listAttachments(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) {
return service.fetchAttachments(userId, objectType, objectId); return service.fetchAttachments(unitId, objectType, objectId);
} }
......
...@@ -168,6 +168,11 @@ public abstract class AbstractHibernateRepository { ...@@ -168,6 +168,11 @@ public abstract class AbstractHibernateRepository {
} }
public <ID, T> T findById(Class<T> clazz, ID id) {
return entityManager.find(clazz, id);
}
/** /**
* TODO. * TODO.
* *
......
...@@ -52,6 +52,9 @@ public interface AuthService { ...@@ -52,6 +52,9 @@ public interface AuthService {
*/ */
void resetPassword(String login, String token, String newPassword) throws AuthenticationException; void resetPassword(String login, String token, String newPassword) throws AuthenticationException;
void changePassword(String login, String currentPassword, String newPassword) throws AuthenticationException;
/** /**
* TODO * TODO
* *
......
...@@ -243,8 +243,19 @@ public class AuthApiController { ...@@ -243,8 +243,19 @@ public class AuthApiController {
@PutMapping("/auth/password") @PutMapping("/auth/password")
public void changePassword(@RequestBody ChangePassword chp) { public ResponseEntity<Void> changePassword(@RequestBody ChangePassword chp) {
//authService. 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 { ...@@ -146,5 +146,21 @@ public class AppUserAuthService implements AuthService {
public void setAuthListeners(Collection<AuthServiceListener> authListeners) { public void setAuthListeners(Collection<AuthServiceListener> authListeners) {
this.authListeners = 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 { ...@@ -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. * 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