Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
15e1a975
Commit
15e1a975
authored
Sep 26, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIPo
parent
bd2660d8
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
125 additions
and
22 deletions
+125
-22
init.sql
sql/init.sql
+1
-2
AbstractHibernateAttachmentService.java
...h/boot/attachment/AbstractHibernateAttachmentService.java
+14
-12
AttachmentRepository.java
...ain/java/com/jh/boot/attachment/AttachmentRepository.java
+4
-0
AttachmentService.java
src/main/java/com/jh/boot/attachment/AttachmentService.java
+3
-3
FileBasedAttachmentService.java
...va/com/jh/boot/attachment/FileBasedAttachmentService.java
+52
-0
AttachmentApiController.java
...a/com/jh/boot/attachment/api/AttachmentApiController.java
+3
-3
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+5
-0
AuthService.java
src/main/java/com/jh/boot/security/AuthService.java
+3
-0
AuthApiController.java
...main/java/com/jh/boot/security/api/AuthApiController.java
+13
-2
AppUserAuthService.java
...java/com/jh/boot/security/service/AppUserAuthService.java
+16
-0
Utils.java
src/main/java/com/jh/boot/utils/Utils.java
+11
-0
No files found.
sql/init.sql
View file @
15e1a975
...
@@ -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
);
...
...
src/main/java/com/jh/boot/attachment/AbstractHibernateAttachmentService.java
View file @
15e1a975
...
@@ -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"
);
}
}
...
...
src/main/java/com/jh/boot/attachment/AttachmentRepository.java
View file @
15e1a975
...
@@ -83,5 +83,9 @@ public class AttachmentRepository extends AbstractHibernateRepository {
...
@@ -83,5 +83,9 @@ public class AttachmentRepository extends AbstractHibernateRepository {
a
.
setName
(
name
);
a
.
setName
(
name
);
}
}
public
Attachment
findById
(
Long
id
)
{
return
entityManager
.
find
(
Attachment
.
class
,
id
);
}
}
}
src/main/java/com/jh/boot/attachment/AttachmentService.java
View file @
15e1a975
...
@@ -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
user
Id
,
String
objectType
,
Long
objectId
);
List
<
Attachment
>
fetchAttachments
(
Long
unit
Id
,
String
objectType
,
Long
objectId
);
Attachment
fetchAttachment
Info
(
Long
attachmentId
);
Attachment
fetchAttachment
(
Long
attachmentId
);
InputStream
fetchAttachmentBody
(
Long
attachmentId
);
InputStream
fetchAttachmentBody
(
Attachment
attachment
);
void
deleteAttachment
(
Long
attachmentId
);
void
deleteAttachment
(
Long
attachmentId
);
...
...
src/main/java/com/jh/boot/attachment/FileBasedAttachmentService.java
0 → 100644
View file @
15e1a975
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
;
}
}
src/main/java/com/jh/boot/attachment/api/AttachmentApiController.java
View file @
15e1a975
...
@@ -20,9 +20,9 @@ public class AttachmentApiController {
...
@@ -20,9 +20,9 @@ public class AttachmentApiController {
private
AttachmentService
service
;
private
AttachmentService
service
;
@GetMapping
(
"/attachments/{u
ser
Id}/{objectType}/{objectId}"
)
@GetMapping
(
"/attachments/{u
nit
Id}/{objectType}/{objectId}"
)
public
@ResponseBody
List
<
Attachment
>
listAttachments
(
@PathVariable
(
"u
serId"
)
String
user
Id
,
@PathVariable
(
"objectType"
)
String
objectType
,
@PathVariable
(
"objectId"
)
Long
objectId
)
{
public
@ResponseBody
List
<
Attachment
>
listAttachments
(
@PathVariable
(
"u
nitId"
)
Long
unit
Id
,
@PathVariable
(
"objectType"
)
String
objectType
,
@PathVariable
(
"objectId"
)
Long
objectId
)
{
return
service
.
fetchAttachments
(
u
ser
Id
,
objectType
,
objectId
);
return
service
.
fetchAttachments
(
u
nit
Id
,
objectType
,
objectId
);
}
}
...
...
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
15e1a975
...
@@ -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.
*
*
...
...
src/main/java/com/jh/boot/security/AuthService.java
View file @
15e1a975
...
@@ -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
*
*
...
...
src/main/java/com/jh/boot/security/api/AuthApiController.java
View file @
15e1a975
...
@@ -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
();
}
}
...
...
src/main/java/com/jh/boot/security/service/AppUserAuthService.java
View file @
15e1a975
...
@@ -147,4 +147,20 @@ public class AppUserAuthService implements AuthService {
...
@@ -147,4 +147,20 @@ public class AppUserAuthService implements AuthService {
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
());
}
}
}
src/main/java/com/jh/boot/utils/Utils.java
View file @
15e1a975
...
@@ -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.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment