Skip to content

Commit

Permalink
#66, #67: оставшиеся entity-сервисы
Browse files Browse the repository at this point in the history
  • Loading branch information
sedovalx committed Jun 14, 2015
1 parent 7bbe564 commit 292dbfd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
3 changes: 3 additions & 0 deletions sources/server/app/config/ServiceModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ class ServiceModule extends AbstractModule with ScalaModule {
bind[CarService].to[CarServiceImpl].in[Singleton]
bind[DriverService].to[DriverServiceImpl].in[Singleton]
bind[OperationService].to[OperationServiceImpl].in[Singleton]
bind[RentStatusService].to[RentStatusServiceImpl].in[Singleton]
bind[RentService].to[RentServiceImpl].in[Singleton]
bind[SystemUserService].to[SystemUserServiceImpl].in[Singleton]
}
}
5 changes: 3 additions & 2 deletions sources/server/app/config/SilhouetteModule.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package config

import com.google.inject.{AbstractModule, Provides}
import com.mohiva.play.silhouette.api.util.Clock
import com.mohiva.play.silhouette.api.util.{PasswordHasher, Clock}
import com.mohiva.play.silhouette.api.{Environment, EventBus}
import com.mohiva.play.silhouette.impl.authenticators.{JWTAuthenticator, JWTAuthenticatorService, JWTAuthenticatorSettings}
import com.mohiva.play.silhouette.impl.util.SecureRandomIDGenerator
import com.mohiva.play.silhouette.impl.util.{BCryptPasswordHasher, SecureRandomIDGenerator}
import models.generated.Tables.SystemUser
import net.codingwell.scalaguice.ScalaModule
import play.api.Play
Expand All @@ -15,6 +15,7 @@ import scala.concurrent.ExecutionContext.Implicits.global

class SilhouetteModule extends AbstractModule with ScalaModule {
override def configure(): Unit = {
bind[PasswordHasher].to[BCryptPasswordHasher].in[Singleton]
bind[LoginInfoService].to[LoginInfoServiceImpl]
}

Expand Down
10 changes: 9 additions & 1 deletion sources/server/app/repository/GenericCRUD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ trait GenericCRUD[E <: Entity[E], T <: Table[E] { val id: Rep[Int] }, F] {
def read(filter: Option[F] = None): Future[Seq[E]] = {
db.run(tableQuery.result)
}
}

/**
* Проверяет, есть ли элементы в репозитории
* @return true, если репозиторий пуст
*/
def isEmpty: Future[Boolean] = {
db.run(tableQuery.length.result).map(_ > 0)
}
}
4 changes: 0 additions & 4 deletions sources/server/app/repository/SystemUserRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class SystemUserRepo extends GenericCRUD[SystemUser, SystemUserTable, SystemUser
db.run(tableQuery.filter(_.login === login).result.headOption)
}

def isEmpty: Future[Boolean] = {
db.run(tableQuery.length.result).map(_ > 0)
}

/**
* Вернуть отфильтрованных пользователей
* @return список пользователей, попавших под фильтр
Expand Down
56 changes: 56 additions & 0 deletions sources/server/app/service/entity/SystemUserService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package service.entity

import javax.inject.Inject
import javax.security.auth.login.AccountNotFoundException

import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import models.generated.Tables
import models.generated.Tables.{SystemUser, SystemUserFilter, SystemUserTable}
import play.api.libs.json.Json
import repository.SystemUserRepo

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait SystemUserService extends EntityService[SystemUser, SystemUserTable, SystemUserRepo, SystemUserFilter] {
def hasUsers: Future[Boolean]
}

class SystemUserServiceImpl @Inject() (val repo: SystemUserRepo, passwordHasher: PasswordHasher) extends SystemUserService {

implicit val passwordFormat = Json.format[PasswordInfo]

def hasUsers: Future[Boolean] = repo.isEmpty

override protected def beforeCreate(entity: Tables.SystemUser, creatorId: Option[Int]): Future[Tables.SystemUser] = {
super.beforeCreate(entity, creatorId) map { toSave =>
// перед сохранением нужно посчитать хеш пароля
val authInfo = passwordHasher.hash(toSave.passwordHash)
val passwordHash = Json.toJson(authInfo).toString()
toSave.copy(passwordHash = passwordHash)
}
}

override protected def beforeUpdate(entity: Tables.SystemUser, editorId: Option[Int]): Future[Tables.SystemUser] = {
super.beforeUpdate(entity, editorId) flatMap { toSave =>
// если поменялся пароль, то нужно пересчитать хеш
if (toSave.passwordHash != null) {
val authInfo = passwordHasher.hash(toSave.passwordHash)
val passwordHash = Json.toJson(authInfo).toString()
Future.successful(toSave.copy(passwordHash = passwordHash))
} else {
findOrThrow(entity.id) map { existing =>
toSave.copy(passwordHash = existing.passwordHash)
}
}
}
}

private def findOrThrow(id: Int) =
this.findById(id) map {
case Some(u) => u
case None => throw new AccountNotFoundException(s"Account id=$id")
}
}


0 comments on commit 292dbfd

Please sign in to comment.