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/json/user/ApiPartialUser.scala | |
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/json/user/ApiPartialUser.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/user/ApiPartialUser.scala | 33 |
1 files changed, 16 insertions, 17 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)) } |