mirror of
https://github.com/BrenBroZAYT/gameyfin.git
synced 2026-06-14 16:20:04 +00:00
Layout updates
Added more themed components Refactored Superadmin creation
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.grimsi.gameyfin.users
|
||||
|
||||
import de.grimsi.gameyfin.config.Roles
|
||||
import de.grimsi.gameyfin.users.entities.Role
|
||||
import de.grimsi.gameyfin.users.persistence.RoleRepository
|
||||
import jakarta.transaction.Transactional
|
||||
import org.springframework.stereotype.Service
|
||||
@@ -18,4 +19,12 @@ class RoleService(
|
||||
val r = roleRepository.findByRolename(role.roleName) ?: return 0
|
||||
return r.users.size
|
||||
}
|
||||
|
||||
fun toRoles(roles: Collection<String>): List<Role> {
|
||||
return roles.mapNotNull { r -> roleRepository.findByRolename(r) }
|
||||
}
|
||||
|
||||
fun toRole(role: String): Role {
|
||||
return roleRepository.findByRolename(role) ?: throw RuntimeException("Role $role does not exist")
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,51 @@
|
||||
package de.grimsi.gameyfin.users
|
||||
|
||||
import de.grimsi.gameyfin.config.Roles
|
||||
import de.grimsi.gameyfin.users.dto.UserInfo
|
||||
import de.grimsi.gameyfin.users.dto.UserRegistration
|
||||
import de.grimsi.gameyfin.users.entities.User
|
||||
import dev.hilla.Endpoint
|
||||
import jakarta.annotation.security.PermitAll
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.security.core.Authentication
|
||||
import org.springframework.security.core.GrantedAuthority
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@Endpoint
|
||||
class UserEndpoint(
|
||||
private val userService: UserService
|
||||
private val userService: UserService,
|
||||
private val roleService: RoleService,
|
||||
) {
|
||||
|
||||
@PermitAll
|
||||
fun getUserInfo(): UserInfo {
|
||||
val auth: Authentication = SecurityContextHolder.getContext().authentication
|
||||
val authorities: List<String> = auth.authorities.map { g: GrantedAuthority -> g.authority }
|
||||
return UserInfo(auth.name, authorities)
|
||||
return UserInfo(username = auth.name, roles = authorities)
|
||||
}
|
||||
|
||||
@PermitAll
|
||||
fun registerUser(registration: UserRegistration): Boolean {
|
||||
fun registerUser(registration: UserRegistration): UserInfo {
|
||||
val user: User = registerUser(registration, listOf(Roles.USER))
|
||||
return userService.toUserInfo(user)
|
||||
}
|
||||
|
||||
@PermitAll
|
||||
fun registerInitialSuperAdmin(registration: UserRegistration): UserInfo {
|
||||
if (roleService.getUserCountForRole(Roles.SUPERADMIN) > 0) throw ResponseStatusException(HttpStatus.UNAUTHORIZED)
|
||||
val superAdmin: User = registerUser(registration, listOf(Roles.SUPERADMIN))
|
||||
return userService.toUserInfo(superAdmin)
|
||||
}
|
||||
|
||||
private fun registerUser(registration: UserRegistration, roles: List<Roles>): User {
|
||||
val user = User(
|
||||
username = registration.username,
|
||||
password = registration.password,
|
||||
email = registration.email,
|
||||
roles = userService.toRoles(registration.roles)
|
||||
roles = roles.map { r -> roleService.toRole(r.roleName) }
|
||||
)
|
||||
|
||||
userService.registerUser(user)
|
||||
|
||||
return true
|
||||
return userService.registerUser(user)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package de.grimsi.gameyfin.users
|
||||
|
||||
import de.grimsi.gameyfin.users.dto.UserInfo
|
||||
import de.grimsi.gameyfin.users.entities.Role
|
||||
import de.grimsi.gameyfin.users.entities.User
|
||||
import de.grimsi.gameyfin.users.persistence.RoleRepository
|
||||
import de.grimsi.gameyfin.users.persistence.UserRepository
|
||||
import jakarta.transaction.Transactional
|
||||
import org.springframework.security.core.GrantedAuthority
|
||||
@@ -18,7 +18,6 @@ import org.springframework.stereotype.Service
|
||||
@Transactional
|
||||
class UserService(
|
||||
private val userRepository: UserRepository,
|
||||
private val roleRepository: RoleRepository,
|
||||
private val passwordEncoder: PasswordEncoder
|
||||
) : UserDetailsService {
|
||||
|
||||
@@ -42,8 +41,12 @@ class UserService(
|
||||
return userRepository.save(user)
|
||||
}
|
||||
|
||||
fun toRoles(roles: Collection<String>): List<Role> {
|
||||
return roles.mapNotNull { r -> roleRepository.findByRolename(r) }
|
||||
fun toUserInfo(user: User): UserInfo {
|
||||
return UserInfo(
|
||||
username = user.username,
|
||||
email = user.email,
|
||||
roles = user.roles.map { r -> r.rolename }
|
||||
)
|
||||
}
|
||||
|
||||
private fun toAuthorities(roles: Collection<Role>): List<GrantedAuthority> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.grimsi.gameyfin.users.dto
|
||||
|
||||
data class UserInfo(
|
||||
val name: String,
|
||||
val authorities: List<String>
|
||||
val username: String,
|
||||
val email: String? = null,
|
||||
val roles: List<String>
|
||||
)
|
||||
@@ -3,6 +3,5 @@ package de.grimsi.gameyfin.users.dto
|
||||
data class UserRegistration(
|
||||
val username: String,
|
||||
val password: String,
|
||||
val email: String,
|
||||
val roles: List<String>
|
||||
val email: String
|
||||
)
|
||||
@@ -8,7 +8,7 @@ server:
|
||||
tracking-modes: cookie
|
||||
|
||||
spring:
|
||||
# Workaround for https://github.dev/hilla/issues/842
|
||||
# Workaround for https://github.com/vaadin/hilla/issues/842
|
||||
devtools.restart.additional-exclude: dev/hilla/openapi.json
|
||||
jpa:
|
||||
defer-datasource-initialization: true
|
||||
|
||||
Reference in New Issue
Block a user