Commit 992e824b authored by Ghitha Dinan's avatar Ghitha Dinan

Merge branch 'PMRMS-123' into 'master'

Pmrms 123

See merge request !5
parents 65c9aade c9198993
...@@ -6,6 +6,8 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean ...@@ -6,6 +6,8 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
...@@ -35,4 +37,20 @@ class SpringWebConfig : WebMvcConfigurer { ...@@ -35,4 +37,20 @@ class SpringWebConfig : WebMvcConfigurer {
registrationBean.order = 1 registrationBean.order = 1
return registrationBean return registrationBean
} }
override fun addViewControllers(registry: ViewControllerRegistry) {
registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs")
registry.addRedirectViewController("/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui")
registry.addRedirectViewController(
"/swagger-resources/configuration/security",
"/swagger-resources/configuration/security"
)
registry.addRedirectViewController("/swagger-resources", "/swagger-resources")
}
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/swagger-ui.html**")
.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html")
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/")
}
} }
package id.go.kemenag.madrasah.pmrms.auth.config
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.*
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spi.service.contexts.SecurityContext
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2
import java.util.*
@Suppress("UNCHECKED_CAST", "DEPRECATION")
@Configuration
@EnableSwagger2
class SwaggerConfig {
@Value("\${swagger.host}")
private val host: String? = null
@Value("\${swagger.protocol}")
private val protocol: String? = null
@Bean
fun api(): Docket? {
return Docket(DocumentationType.SWAGGER_2)
.host(host)
.select()
.apis(RequestHandlerSelectors.basePackage("id.go.kemenag.madrasah.pmrms.auth.controller"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.protocols(setOf(protocol))
.securitySchemes(listOf(apiKey()) as List<SecurityScheme>?)
.securityContexts(Collections.singletonList(securityContext()))
}
private fun apiInfo(): ApiInfo {
return ApiInfo(
"PMRMS API - Auth",
"Auth Service for Application PMRMS",
"1.0",
"Terms of service",
Contact("Application PMRMS", "-", "-"),
"",
"",
Collections.emptyList()
)
}
private fun apiKey(): ApiKey {
return ApiKey("Bearer", "Authorization", "header")
}
private fun securityContext(): SecurityContext? {
return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/.*")).build()
}
private fun defaultAuth(): List<SecurityReference?> {
return listOf(SecurityReference("Bearer", arrayOfNulls(0)))
}
}
package id.go.kemenag.madrasah.pmrms.auth.constant
import java.io.File
const val ROOT_DIR = "D:\\app-files\\pmrms\\files\\"
val UPLOAD_DIR = "${ROOT_DIR}uploads${File.separator}"
val UPLOAD_USER_DIR = "${UPLOAD_DIR}users"
...@@ -14,5 +14,5 @@ val USER_ADMIN_ALLOWED_PATH = ...@@ -14,5 +14,5 @@ val USER_ADMIN_ALLOWED_PATH =
) )
val AUDIENCE_FILTER_PATH = mapOf( val AUDIENCE_FILTER_PATH = mapOf(
"admin" to USER_ADMIN_ALLOWED_PATH "user-admin" to USER_ADMIN_ALLOWED_PATH
) )
package id.go.kemenag.madrasah.pmrms.auth.contoller package id.go.kemenag.madrasah.pmrms.auth.controller
import id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest import id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData import id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
import id.go.kemenag.madrasah.pmrms.auth.service.AuthService import id.go.kemenag.madrasah.pmrms.auth.service.AuthService
import io.swagger.annotations.Api
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid import javax.validation.Valid
@Api(tags = ["Auth"], description = "Auth API")
@RestController @RestController
@RequestMapping(path = ["auth"]) @RequestMapping(path = ["auth"])
class AuthController { class AuthController {
...@@ -22,4 +21,9 @@ class AuthController { ...@@ -22,4 +21,9 @@ class AuthController {
fun login(@Valid @RequestBody request: LoginRequest): ResponseEntity<ReturnData> { fun login(@Valid @RequestBody request: LoginRequest): ResponseEntity<ReturnData> {
return service.login(request) return service.login(request)
} }
@GetMapping(value = ["detail"], produces = ["application/json"])
fun detail(): ResponseEntity<ReturnData> {
return service.detail()
}
} }
package id.go.kemenag.madrasah.pmrms.auth.contoller package id.go.kemenag.madrasah.pmrms.auth.controller
import id.go.kemenag.madrasah.pmrms.auth.exception.BaseException import id.go.kemenag.madrasah.pmrms.auth.exception.BaseException
import id.go.kemenag.madrasah.pmrms.auth.model.response.ErrorMessage import id.go.kemenag.madrasah.pmrms.auth.model.response.ErrorMessage
......
package id.go.kemenag.madrasah.pmrms.auth.helpers
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import id.go.kemenag.madrasah.pmrms.auth.pojo.Users
import org.springframework.security.core.context.SecurityContextHolder
import java.net.MalformedURLException
import java.net.URL
import javax.servlet.http.HttpServletRequest
fun getUserLogin(): Users? {
return try {
val principal = SecurityContextHolder.getContext().authentication.principal as Any
val objectMapper = ObjectMapper()
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
objectMapper.readValue(principal.toString(), Users::class.java)
} catch (e: Exception) {
null
}
}
@Throws(MalformedURLException::class)
fun getBaseUrl(request: HttpServletRequest): String {
try {
val requestURL = URL(request.requestURL.toString())
val port = if (requestURL.port == -1) "" else ":" + requestURL.port
return requestURL.protocol + "://" + requestURL.host + port + request.contextPath + "/"
} catch (e: Exception) {
throw e
}
}
fun getFullUrl(request: HttpServletRequest): String {
val requestURL = StringBuilder(request.requestURL.toString())
val queryString = request.queryString
return if (queryString == null) {
requestURL.toString()
} else {
requestURL.append('?').append(queryString).toString()
}
}
package id.go.kemenag.madrasah.pmrms.auth.pojo
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnore
import id.go.kemenag.madrasah.pmrms.auth.constant.VALIDATOR_MSG_REQUIRED
import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
import javax.validation.constraints.NotEmpty
@Entity
@Table(name = "role", schema = "auth")
data class Role(
@Id
@Column(name = "id")
var id: String? = UUID.randomUUID().toString(),
@Column(name = "name")
@field:NotEmpty(message = "Nama $VALIDATOR_MSG_REQUIRED")
var name: String? = null,
@Column(name = "created_at")
@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7")
var createdAt: Date? = Date(),
@Column(name = "updated_at")
@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7")
var updatedAt: Date? = Date(),
@Column(name = "active")
@JsonIgnore
var active: Boolean? = true
)
...@@ -2,11 +2,9 @@ package id.go.kemenag.madrasah.pmrms.auth.pojo ...@@ -2,11 +2,9 @@ package id.go.kemenag.madrasah.pmrms.auth.pojo
import com.fasterxml.jackson.annotation.JsonFormat import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonIgnore
import org.hibernate.annotations.Where
import java.util.* import java.util.*
import javax.persistence.Column import javax.persistence.*
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
@Entity @Entity
@Table(name = "users", schema = "auth") @Table(name = "users", schema = "auth")
...@@ -28,6 +26,10 @@ data class Users( ...@@ -28,6 +26,10 @@ data class Users(
@Column(name = "last_name") @Column(name = "last_name")
var lastName: String? = null, var lastName: String? = null,
@OneToMany(mappedBy = "userId")
@Where(clause = "active = true")
var roles: MutableSet<UsersRole>? = null,
@Column(name = "created_at") @Column(name = "created_at")
@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7") @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7")
var createdAt: Date? = Date(), var createdAt: Date? = Date(),
......
package id.go.kemenag.madrasah.pmrms.auth.pojo
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnore
import java.util.*
import javax.persistence.*
@Entity
@Table(name = "users_role", schema = "auth")
data class UsersRole(
@Id
@Column(name = "id")
var id: String? = UUID.randomUUID().toString(),
@Column(name = "user_id")
var userId: String? = null,
@Column(name = "role_id")
var roleId: String? = null,
@ManyToOne
@JoinColumn(name = "role_id", insertable = false, updatable = false, nullable = true)
var role: Role? = null,
@Column(name = "created_at")
@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7")
var createdAt: Date? = Date(),
@Column(name = "updated_at")
@get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss", timezone = "GMT+7")
var updatedAt: Date? = Date(),
@Column(name = "active")
@JsonIgnore
var active: Boolean? = true
)
...@@ -7,7 +7,10 @@ import com.fasterxml.jackson.databind.ObjectMapper ...@@ -7,7 +7,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
import id.go.kemenag.madrasah.pmrms.auth.constant.EXPIRATION_TIME import id.go.kemenag.madrasah.pmrms.auth.constant.EXPIRATION_TIME
import id.go.kemenag.madrasah.pmrms.auth.constant.SECRET import id.go.kemenag.madrasah.pmrms.auth.constant.SECRET
import id.go.kemenag.madrasah.pmrms.auth.constant.TOKEN_PREFIX import id.go.kemenag.madrasah.pmrms.auth.constant.TOKEN_PREFIX
import id.go.kemenag.madrasah.pmrms.auth.constant.VALIDATOR_MSG_NOT_FOUND
import id.go.kemenag.madrasah.pmrms.auth.exception.BadRequestException import id.go.kemenag.madrasah.pmrms.auth.exception.BadRequestException
import id.go.kemenag.madrasah.pmrms.auth.helpers.getUserLogin
import id.go.kemenag.madrasah.pmrms.auth.helpers.responseNotFound
import id.go.kemenag.madrasah.pmrms.auth.helpers.responseSuccess import id.go.kemenag.madrasah.pmrms.auth.helpers.responseSuccess
import id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest import id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData import id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
...@@ -62,4 +65,16 @@ class AuthService { ...@@ -62,4 +65,16 @@ class AuthService {
return jwtres return jwtres
} }
fun detail(): ResponseEntity<ReturnData> {
try {
val data = repoUser.findByIdAndActive(getUserLogin()?.id)
if (data.isPresent) {
return responseSuccess(data = data.get())
}
return responseNotFound("User $VALIDATOR_MSG_NOT_FOUND")
} catch (e: Exception) {
throw e
}
}
} }
# SERVER CONFIG # SERVER CONFIG
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
spring.profiles.active=development spring.profiles.active=development
server.port=8080 #server.port=8080
server.port=8081
# DATABASE CONFIG # DATABASE CONFIG
# local # local
...@@ -28,13 +29,6 @@ spring.jpa.properties.hibernate.format_sql=true ...@@ -28,13 +29,6 @@ spring.jpa.properties.hibernate.format_sql=true
spring.servlet.multipart.max-file-size=500MB spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB spring.servlet.multipart.max-request-size=500MB
# MAIl CONFIG
spring.mail.host=mail.smtp2go.com
spring.mail.port=2525
spring.mail.username=kominfo-05
spring.mail.password=eXFwenN6cm4yYmMw
spring.mail.from=no-reply@layanan.go.id
spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=25000 spring.mail.properties.mail.smtp.connectiontimeout=25000
spring.mail.properties.mail.smtp.timeout=25000 spring.mail.properties.mail.smtp.timeout=25000
...@@ -45,3 +39,8 @@ spring.mail.properties.mail.smtp.starttls.enable=true ...@@ -45,3 +39,8 @@ spring.mail.properties.mail.smtp.starttls.enable=true
app.name = PMRMS app.name = PMRMS
server.max-http-header-size=10000KB server.max-http-header-size=10000KB
# Swagger
swagger.host=localhost:8081
swagger.protocol=http
spring.main.allow-circular-references= true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
package id.go.kemenag.madrasah.pmrms.auth
import id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
import id.go.kemenag.madrasah.pmrms.auth.service.AuthService
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
@SpringBootTest
class AuthServiceTest {
@Autowired
private lateinit var service: AuthService
@Test
fun testLogin() {
val test: ResponseEntity<ReturnData> = service.login(LoginRequest("admin@madrasah.kemenag.go.id", "123456"))
Assertions.assertThat(test.statusCode).isNotEqualTo(HttpStatus.INTERNAL_SERVER_ERROR)
}
@Test
fun testDetail() {
val test: ResponseEntity<ReturnData> = service.detail()
Assertions.assertThat(test.statusCode).isNotEqualTo(HttpStatus.INTERNAL_SERVER_ERROR)
}
}
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