-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
312 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 1 addition & 28 deletions
29
src/main/java/com/vladislav/crm/functions/CreateExcelFromLeadsFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,12 @@ | ||
package com.vladislav.crm.functions; | ||
|
||
import com.vladislav.crm.entities.Lead; | ||
import org.apache.poi.hssf.usermodel.HSSFRow; | ||
import org.apache.poi.hssf.usermodel.HSSFSheet; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
@Component | ||
public class CreateExcelFromLeadsFunction implements Function<Collection<Lead>, Pair<HSSFWorkbook, HSSFSheet>> { | ||
|
||
@Override | ||
public Pair<HSSFWorkbook, HSSFSheet> apply(Collection<Lead> leads) { | ||
final HSSFWorkbook workbook = new HSSFWorkbook(); | ||
final HSSFSheet sheet = workbook.createSheet("Сделки"); | ||
|
||
final HSSFRow rowHead = sheet.createRow(0); | ||
rowHead.createCell(0).setCellValue("Идентификатор"); | ||
rowHead.createCell(1).setCellValue("Название"); | ||
rowHead.createCell(2).setCellValue("Бюджет"); | ||
rowHead.createCell(3).setCellValue("Статус"); | ||
|
||
int i = 0; | ||
for (Lead lead : leads) { | ||
i++; | ||
final HSSFRow row = sheet.createRow(i); | ||
row.createCell(0).setCellValue(lead.getId()); | ||
row.createCell(1).setCellValue(lead.getName()); | ||
row.createCell(2).setCellValue(lead.getSale().toString()); | ||
row.createCell(3).setCellValue(lead.getStatus().getName()); | ||
} | ||
|
||
return Pair.of(workbook, sheet); | ||
} | ||
public interface CreateExcelFromLeadsFunction extends Function<Collection<Lead>, Pair<HSSFWorkbook, HSSFSheet>> { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/vladislav/crm/functions/GenerateAccessTokenFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.vladislav.crm.functions; | ||
|
||
import com.vladislav.crm.entities.User; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface GenerateAccessTokenFunction extends Function<User, String> { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/vladislav/crm/functions/GenerateRefreshTokenFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.vladislav.crm.functions; | ||
|
||
import com.vladislav.crm.entities.RefreshToken; | ||
import com.vladislav.crm.entities.User; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface GenerateRefreshTokenFunction extends Function<User, RefreshToken> { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/vladislav/crm/functions/ParseJwtTokenFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.vladislav.crm.functions; | ||
|
||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.function.Function; | ||
|
||
public interface ParseJwtTokenFunction extends Function<String, UserDetails> { | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/vladislav/crm/functions/impl/CreateExcelFromLeadsFunctionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.vladislav.crm.functions.impl; | ||
|
||
import com.vladislav.crm.entities.Lead; | ||
import com.vladislav.crm.functions.CreateExcelFromLeadsFunction; | ||
import org.apache.poi.hssf.usermodel.HSSFRow; | ||
import org.apache.poi.hssf.usermodel.HSSFSheet; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
|
||
@Component | ||
public class CreateExcelFromLeadsFunctionImpl implements CreateExcelFromLeadsFunction { | ||
|
||
@Override | ||
public Pair<HSSFWorkbook, HSSFSheet> apply(Collection<Lead> leads) { | ||
final HSSFWorkbook workbook = new HSSFWorkbook(); | ||
final HSSFSheet sheet = workbook.createSheet("Сделки"); | ||
|
||
final HSSFRow rowHead = sheet.createRow(0); | ||
rowHead.createCell(0).setCellValue("Идентификатор"); | ||
rowHead.createCell(1).setCellValue("Название"); | ||
rowHead.createCell(2).setCellValue("Бюджет"); | ||
rowHead.createCell(3).setCellValue("Статус"); | ||
|
||
int i = 0; | ||
for (Lead lead : leads) { | ||
i++; | ||
final HSSFRow row = sheet.createRow(i); | ||
row.createCell(0).setCellValue(lead.getId()); | ||
row.createCell(1).setCellValue(lead.getName()); | ||
row.createCell(2).setCellValue(lead.getSale().toString()); | ||
row.createCell(3).setCellValue(lead.getStatus().getName()); | ||
} | ||
|
||
return Pair.of(workbook, sheet); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/vladislav/crm/functions/impl/GenerateAccessTokenFunctionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.vladislav.crm.functions.impl; | ||
|
||
import com.vladislav.crm.entities.User; | ||
import com.vladislav.crm.functions.GenerateAccessTokenFunction; | ||
import io.jsonwebtoken.Jwts; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.crypto.SecretKey; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
|
||
@Component | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class GenerateAccessTokenFunctionImpl implements GenerateAccessTokenFunction { | ||
|
||
private final SecretKey jwtSecretKey; | ||
|
||
@Value("${app.jwt.access-token-lifetime}") | ||
private Integer accessTokenLifetime; | ||
|
||
@Override | ||
public String apply(User user) { | ||
final Instant createdAt = Instant.now(); | ||
final Instant expiredAt = createdAt.plus(accessTokenLifetime, ChronoUnit.MINUTES); | ||
|
||
return Jwts.builder() | ||
.claim("userId", user.getId()) | ||
.claim("username", user.getUsername()) | ||
.claim("authorities", user.getAuthorities()) | ||
.setIssuedAt(Date.from(createdAt)) | ||
.setExpiration(Date.from(expiredAt)) | ||
.signWith(jwtSecretKey) | ||
.compact(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/vladislav/crm/functions/impl/GenerateRefreshTokenFunctionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.vladislav.crm.functions.impl; | ||
|
||
import com.vladislav.crm.entities.RefreshToken; | ||
import com.vladislav.crm.entities.User; | ||
import com.vladislav.crm.functions.GenerateRefreshTokenFunction; | ||
import com.vladislav.crm.repositories.RefreshTokenRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class GenerateRefreshTokenFunctionImpl implements GenerateRefreshTokenFunction { | ||
|
||
@Value("${app.jwt.refresh-token-lifetime}") | ||
private Integer refreshTokenLifetime; | ||
|
||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Override | ||
public RefreshToken apply(User user) { | ||
final Instant createdAt = Instant.now(); | ||
final Instant expiredAt = createdAt.plus(refreshTokenLifetime, ChronoUnit.DAYS); | ||
|
||
final RefreshToken refreshToken = new RefreshToken().setUserUnsafe(user) | ||
.setValidUntil(LocalDateTime.ofInstant(expiredAt, ZoneId.systemDefault())); | ||
|
||
return refreshTokenRepository.save(refreshToken); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/vladislav/crm/functions/impl/ParseJwtTokenFunctionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.vladislav.crm.functions.impl; | ||
|
||
import com.vladislav.crm.entities.User; | ||
import com.vladislav.crm.functions.ParseJwtTokenFunction; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jws; | ||
import io.jsonwebtoken.JwtParser; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class ParseJwtTokenFunctionImpl implements ParseJwtTokenFunction { | ||
|
||
private final JwtParser jwtParser; | ||
|
||
@Override | ||
@SuppressWarnings("unchecked cast") | ||
public UserDetails apply(String token) { | ||
final Jws<Claims> jws = jwtParser.parseClaimsJws(token); | ||
final Claims claims = jws.getBody(); | ||
|
||
final User user = new User(); | ||
|
||
user.setId(claims.get("userId", Long.class)); | ||
user.setUsername(claims.get("username", String.class)); | ||
|
||
final List<String> authorities = (List<String>) claims.get("authorities"); | ||
user.setAuthorities(authorities.stream().map(User.Authority::valueOf).collect(Collectors.toList())); | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/main/java/com/vladislav/crm/services/TokenService.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.