Skip to content

Commit

Permalink
UserDetailsServiceImpl -> Scala
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Oct 4, 2024
1 parent 360aa5e commit 522871d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 84 deletions.
80 changes: 0 additions & 80 deletions src/main/java/ru/org/linux/auth/UserDetailsServiceImpl.java

This file was deleted.

74 changes: 74 additions & 0 deletions src/main/scala/ru/org/linux/auth/UserDetailsServiceImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ru.org.linux.auth

import org.springframework.dao.DataAccessException
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Component
import ru.org.linux.user.*

import scala.jdk.CollectionConverters.SeqHasAsJava

object UserDetailsServiceImpl {
private def retrieveUserAuthorities(user: User) = {
val results = Vector.newBuilder[GrantedAuthority]

if (user.isActivated) {
results.addOne(new SimpleGrantedAuthority("ROLE_ANONYMOUS"))

if (user.canCorrect) {
results.addOne(new SimpleGrantedAuthority("ROLE_CORRECTOR"))
}

if (user.isModerator) {
results.addOne(new SimpleGrantedAuthority("ROLE_MODERATOR"))
}

if (user.isAdministrator) {
results.addOne(new SimpleGrantedAuthority("ROLE_ADMIN"))
}
}

results.result()
}
}

@Component
class UserDetailsServiceImpl(userDao: UserDao, userService: UserService, profileDao: ProfileDao) extends UserDetailsService {
@throws[UsernameNotFoundException]
@throws[DataAccessException]
override def loadUserByUsername(username: String): UserDetails = {
val user: User = if (username.contains("@")) {
userDao.getByEmail(username, true)
} else {
try {
userService.getUser(username)
} catch {
case _: UserNotFoundException =>
throw new UsernameNotFoundException(username)
}
}

if (user == null) {
throw new UsernameNotFoundException(username)
}

new UserDetailsImpl(user, UserDetailsServiceImpl.retrieveUserAuthorities(user).asJava, profileDao.readProfile(user.getId))
}
}
6 changes: 2 additions & 4 deletions src/main/scala/ru/org/linux/user/UserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ import java.util.Optional
import javax.annotation.Nullable
import javax.mail.internet.InternetAddress
import scala.collection.mutable
import scala.compat.java8.OptionConverters.RichOptionalGeneric
import scala.jdk.CollectionConverters.*
import scala.jdk.OptionConverters.RichOption
import scala.jdk.OptionConverters.*
import scala.util.{Failure, Success, Try}

@Service
object UserService {
val MaxFileSize: Int = 100 * 1024
val MinImageSize = 50
Expand Down Expand Up @@ -182,7 +180,7 @@ class UserService(siteConfig: SiteConfig, userDao: UserDao, ignoreListDao: Ignor

users.asScala.map { case (userId, lastlogin) =>
val user = getUserCached(userId)
(user, lastlogin.asScala.exists(_.isAfter(recentSeenDate)))
(user, lastlogin.toScala.exists(_.isAfter(recentSeenDate)))
}
}

Expand Down

0 comments on commit 522871d

Please sign in to comment.