Skip to content

Commit

Permalink
add name-search to filter, add expiresat to token
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Dec 24, 2024
1 parent 9c688b2 commit 5a08d38
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class AdminReceiptController {


@GetMapping("/all")
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int, @Param("status") status: String?, @Param("committee") committee: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<List<ReceiptInfo>> {
fun getAllReceipts(@Param("page") from: Int, @Param("count") count: Int, @Param("status") status: String?, @Param("committee") committee: String?, @Param("search") search: String?, @Param("sortOrder") sortOrder: String?, @Param("sortField") sortField: String?): ResponseEntity<List<ReceiptInfo>> {
if (!authenticationService.checkBankomMembership()) {
return ResponseEntity.status(403).build()
}

return ResponseEntity.ok(receiptAdminService.getAll(from, count, status, committee, sortField, sortOrder))
return ResponseEntity.ok(receiptAdminService.getAll(from, count, status, committee, search, sortField, sortOrder))
}

@GetMapping("/get/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AuthenticationController {
return try {
ResponseEntity.ok(onlineUserService.checkUser())
} catch (e: Exception) {
print(e)
ResponseEntity.badRequest().build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.autobank.data.authentication;

import java.time.Instant

class AuthenticatedUserResponse(
val success: Boolean,
val isadmin: Boolean,
val issuperadmin: Boolean
val issuperadmin: Boolean,
val expiresat: Instant?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import jakarta.persistence.criteria.Predicate

class ReceiptInfoSpecification(
private val status: String?,
private val committeeName: String?
private val committeeName: String?,
private val search: String?
) : Specification<ReceiptInfo> {
override fun toPredicate(
root: jakarta.persistence.criteria.Root<ReceiptInfo>,
Expand All @@ -21,11 +22,15 @@ class ReceiptInfoSpecification(
} else if (status != null) {
predicates.add(criteriaBuilder.equal(root.get<String>("latestReviewStatus"), status))
}
println(committeeName)

if (committeeName != null) {
predicates.add(criteriaBuilder.equal(root.get<String>("committeeName"), committeeName))
}

if (search != null) {
predicates.add(criteriaBuilder.like(root.get<String>("receiptName"), "%$search%"))
}


return criteriaBuilder.and(*predicates.toTypedArray())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.springframework.security.oauth2.server.resource.authentication.JwtAut
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.exchange
import java.time.Instant


@Service
Expand Down Expand Up @@ -134,6 +135,16 @@ class AuthenticationService {
return superadminEmails.split(",").contains(getUserDetails().email)
}

fun getExpiresAt(): Instant? {
val authentication = SecurityContextHolder.getContext().authentication
return if (authentication is JwtAuthenticationToken) {
val token = authentication.token
token.getClaim("exp")
} else {
null
}
}

data class Result(
val name_long: String = ""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ class OnlineUserService(
}

fun checkUser(): AuthenticatedUserResponse {
var storedUser = onlineUserRepository.findByOnlineId(authenticationService.getUserSub())
val storedUser = onlineUserRepository.findByOnlineId(authenticationService.getUserSub())
if (storedUser == null) {
storedUser = createOnlineUser()
try {
createOnlineUser()
} catch (e: Exception) {
return AuthenticatedUserResponse(success = false, false, false, null)
}
}
return AuthenticatedUserResponse(success = true, authenticationService.checkBankomMembership(), authenticationService.checkSuperAdmin())

return AuthenticatedUserResponse(success = true, authenticationService.checkBankomMembership(), authenticationService.checkSuperAdmin(), expiresat = authenticationService.getExpiresAt())
}

fun createOnlineUser(): OnlineUser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ReceiptAdminService {
@Autowired
lateinit var attachmentService: AttachmentService

fun getAll(from: Int, count: Int, status: String?, committeeName: String?, sortField: String?, sortOrder: String?): List<ReceiptInfo>? {
fun getAll(from: Int, count: Int, status: String?, committeeName: String?, search: String?, sortField: String?, sortOrder: String?): List<ReceiptInfo>? {


val sort = if (!sortField.isNullOrEmpty()) {
Expand All @@ -43,9 +43,9 @@ class ReceiptAdminService {

val pageable = PageRequest.of(from, count, sort)

return if (status != null || committeeName != null) {
return if (status != null || committeeName != null || search != null) {
receiptInfoViewRepository.findAll(
ReceiptInfoSpecification(status, committeeName), pageable
ReceiptInfoSpecification(status, committeeName, search), pageable
).toList()
} else {
receiptInfoViewRepository.findAll(pageable).toList()
Expand Down

0 comments on commit 5a08d38

Please sign in to comment.