-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sedovalx
committed
Jun 14, 2015
1 parent
7bbe564
commit 292dbfd
Showing
5 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
|
||
|