diff options
author | vlad <vlad@driver.xyz> | 2017-07-13 02:27:55 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-07-13 02:27:55 -0700 |
commit | 93eb4829c0d11959709e18a7b489343550633e83 (patch) | |
tree | 49ced7ef72180cbd29e2a8126684dedbd958a00c /src/main/scala/xyz/driver/pdsuidomain/formats | |
parent | 3d902b5197db861c30325c159dc10cfb211ae209 (diff) | |
download | rest-query-93eb4829c0d11959709e18a7b489343550633e83.tar.gz rest-query-93eb4829c0d11959709e18a7b489343550633e83.tar.bz2 rest-query-93eb4829c0d11959709e18a7b489343550633e83.zip |
Updates for authentication
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala | 33 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiUser.scala | 19 |
2 files changed, 25 insertions, 27 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala index f31efb3..05395b4 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala @@ -11,9 +11,10 @@ import scala.collection._ import scala.util.Try import User._ import xyz.driver.pdsuicommon.json.JsonValidationException +import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat import xyz.driver.pdsuicommon.validation.{AdditionalConstraints, JsonValidationErrors} -final case class ApiPartialUser(email: Option[String], name: Option[String], roleId: Option[String]) { +final case class ApiPartialUser(email: Option[String], name: Option[String], roles: Option[Seq[String]]) { def applyTo(orig: User): Try[User] = Try { val validation = Map( @@ -33,9 +34,9 @@ final case class ApiPartialUser(email: Option[String], name: Option[String], rol def toDomain(id: StringId[User] = StringId(UUID.randomUUID().toString)): Try[User] = Try { val validation = Map( - JsPath \ "email" -> AdditionalConstraints.optionNonEmptyConstraint(email), - JsPath \ "name" -> AdditionalConstraints.optionNonEmptyConstraint(name), - JsPath \ "roleId" -> AdditionalConstraints.optionNonEmptyConstraint(roleId) + JsPath \ "email" -> AdditionalConstraints.optionNonEmptyConstraint(email), + JsPath \ "name" -> AdditionalConstraints.optionNonEmptyConstraint(name), + JsPath \ "roles" -> AdditionalConstraints.optionNonEmptyConstraint(roles) ) val validationErrors: JsonValidationErrors = validation.collect({ @@ -48,7 +49,7 @@ final case class ApiPartialUser(email: Option[String], name: Option[String], rol id = id, email = userEmail, name = name.get, - role = roleId.map(UserRole.roleFromString).get, + roles = roles.toSeq.flatMap(_.map(UserRole.roleFromString)).toSet, passwordHash = PasswordHash(createPassword), latestActivity = None, deleted = None @@ -63,17 +64,15 @@ object ApiPartialUser { implicit val format: Format[ApiPartialUser] = ( (JsPath \ "email").formatNullable[String](Format(Reads.email, Writes.StringWrites)) and - (JsPath \ "name").formatNullable[String](Format( - Reads.filterNot[String](ValidationError("Username is too long (max length is 255 chars)", 255))(_.length > 255), - Writes.StringWrites - )) and - (JsPath \ "roleId").formatNullable[String]( - Format(Reads - .of[String] - .filter(ValidationError("unknown role"))({ - case x if UserRole.roleFromString.isDefinedAt(x) => true - case _ => false - }), - Writes.of[String])) + (JsPath \ "name").formatNullable[String]( + Format( + Reads.filterNot[String](ValidationError("Username is too long (max length is 255 chars)", 255))( + _.length > 255), + Writes.StringWrites + )) and + (JsPath \ "roleId").formatNullable[Seq[String]]( + Format( + seqJsonFormat[String].filter(ValidationError("unknown role"))(_.forall(UserRole.roleFromString.isDefinedAt)), + Writes.of[Seq[String]])) )(ApiPartialUser.apply, unlift(ApiPartialUser.unapply)) } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiUser.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiUser.scala index 8dbedfe..c21edd1 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiUser.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiUser.scala @@ -3,14 +3,17 @@ package xyz.driver.pdsuidomain.formats.json.user import java.time.{ZoneId, ZonedDateTime} import xyz.driver.pdsuicommon.domain.User +import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat import play.api.data.validation.ValidationError import play.api.libs.functional.syntax._ import play.api.libs.json._ +import scala.collection.Seq + final case class ApiUser(id: String, email: String, name: String, - roleId: String, + roles: Seq[String], latestActivity: Option[ZonedDateTime]) object ApiUser { @@ -19,14 +22,10 @@ object ApiUser { (JsPath \ "id").format[String] and (JsPath \ "email").format[String](Reads.email) and (JsPath \ "name").format[String] and - (JsPath \ "roleId").format[String]( - Format(Reads - .of[String] - .filter(ValidationError("unknown role"))({ - case x if UserRole.roleFromString.isDefinedAt(x) => true - case _ => false - }), - Writes.of[String])) and + (JsPath \ "roles").format( + Format( + seqJsonFormat[String].filter(ValidationError("unknown role"))(_.forall(UserRole.roleFromString.isDefinedAt)), + Writes.of[Seq[String]])) and (JsPath \ "latestActivity").formatNullable[ZonedDateTime] )(ApiUser.apply, unlift(ApiUser.unapply)) @@ -34,7 +33,7 @@ object ApiUser { user.id.id, user.email.value, user.name, - UserRole.roleToString(user.role), + user.roles.map(UserRole.roleToString).toSeq, user.latestActivity.map(ZonedDateTime.of(_, ZoneId.of("Z"))) ) } |