pyc-website
main website for pyc inc.
git clone https://9o.is/git/pyc-website.git
commit af8399aacf38ab00c8c0755c3708dd61f9397b11 parent fd49b2bbf547212d0c4b21a56b2cbd7e426cfe04 Author: Jul <jul@9o.is> Date: Sun, 29 Jun 2014 00:33:15 -0400 fixed input validation for user model Diffstat:
| M | src/main/scala/inc/pyc/model/User.scala | | | 10 | +++++----- |
| M | src/main/scala/inc/pyc/snippet/UserSnip.scala | | | 66 | +++++++++++++++++++++++++++++++++--------------------------------- |
| M | src/main/webapp/templates-hidden/parts/user-settings-form.html | | | 4 | ++-- |
3 files changed, 40 insertions(+), 40 deletions(-)
diff --git a/src/main/scala/inc/pyc/model/User.scala b/src/main/scala/inc/pyc/model/User.scala @@ -21,21 +21,21 @@ class User private () extends ProtoAuthUser[User] with ObjectIdPk[User] with USA def userIdAsString: String = id.toString - object fname extends StringField(this, 64) { + object fname extends StringField(this, 32) { override def validations = - valMaxLen(64, "First Name must be 64 characters or less") _ :: + valMaxLen(32, "First Name must be 32 characters or less") _ :: super.validations } - object lname extends StringField(this, 64) { + object lname extends StringField(this, 32) { override def validations = - valMaxLen(64, "First Name must be 64 characters or less") _ :: + valMaxLen(32, "Last Name must be 32 characters or less") _ :: super.validations } object postal extends PostalCodeField(this, usa) - object phone extends StringField(this, 10) { + object phone extends OptionalStringField(this, 10) { override def validations = valRegex(Pattern.compile("[0-9]{10}"), "Phone number must be 10 digits long") _ :: super.validations diff --git a/src/main/scala/inc/pyc/snippet/UserSnip.scala b/src/main/scala/inc/pyc/snippet/UserSnip.scala @@ -65,27 +65,25 @@ trait AngularUserSnippet extends AngularSnippet { }): JValue /** Validates and saves the currently signed in user. */ - protected def validateAndSave(): JValue = serve { + protected def validateAndSave(f: () => Unit): JValue = validateUser { user => - validateUser ({ - user.save() - NgAlert.success - }) + f() + user.save() + NgAlert.success } /** Validates and updates the currently signed in user. */ - protected def validateAndUpdate(): JValue = serve { + protected def validateAndUpdate(f: () => Unit): JValue = validateUser { user => - validateUser ({ - user.update - NgAlert.success - }) + f() + user.update + NgAlert.success } - protected def validateUser(f: JValue): JValue = serve { + protected def validateUser(f: User => JValue): JValue = serve { user => user.validate match { - case Nil => f + case Nil => f(user) case errors => NgAlert.danger("Invalid submission", errors) } @@ -257,30 +255,30 @@ class UserSettings extends AngularCurrentUser { user => for { JString(fname) <- model - } yield { - user.fname(fname) - validateAndUpdate() - } + } yield + validateAndUpdate { () => + user.fname(fname) + } } def lname(model: JValue): JValue = serve { user => for { JString(lname) <- model - } yield { - user.lname(lname) - validateAndUpdate() - } + } yield + validateAndUpdate { () => + user.lname(lname) + } } def username(model: JValue): JValue = serve { user => for { JString(username) <- model - } yield { - user.username(username) - validateAndUpdate() - } + } yield + validateAndUpdate { () => + user.username(username) + } } } @@ -327,10 +325,10 @@ class PhoneVerification extends AngularCurrentUser { user => for { JString(postal) <- model - } yield { - user.postal(postal) - validateAndUpdate() - } + } yield + validateAndUpdate { () => + user.postal(postal) + } } def sendsms(model: JValue): JValue = serve { @@ -342,8 +340,9 @@ class PhoneVerification extends AngularCurrentUser { " verify your PYC account. This is a 1-time message." if(Twilio.sms(phone, msg)) { - user.phone(phone) - validateAndUpdate() + validateAndUpdate { () => + user.phone(phone) + } } else { NgAlert.danger( <i class="fa-fw fa fa-thumbs-o-down"></i> ++ @@ -361,9 +360,10 @@ class PhoneVerification extends AngularCurrentUser { } yield if(this.smscode == smscode) { - user.purchaseLimit(USAPurchaseLimit.D1000) - user.phoneverified(true) - validateAndUpdate() + validateAndUpdate { () => + user.purchaseLimit(USAPurchaseLimit.D1000) + user.phoneverified(true) + } } else { NgAlert.danger( <i class="fa-fw fa fa-thumbs-o-down"></i> ++ diff --git a/src/main/webapp/templates-hidden/parts/user-settings-form.html b/src/main/webapp/templates-hidden/parts/user-settings-form.html @@ -5,7 +5,7 @@ <label>First Name</label> <label class="input" ng-class="{{ stateSuccessError('fname') }}"> <i ng-show="fname_loading" class="icon-append fa fa-spinner fa-spin"></i> - <input name="fname" ng-model="model.fname" type="text" ng-blur="updateUserSettings('fname')"> + <input name="fname" ng-model="model.fname" type="text" ng-blur="updateUserSettings('fname')" ng-maxlength="30"> </label> </section> @@ -13,7 +13,7 @@ <label>Last Name</label> <label class="input" ng-class="{{ stateSuccessError('lname') }}"> <i ng-show="lname_loading" class="icon-append fa fa-spinner fa-spin"></i> - <input name="lname" ng-model="model.lname" type="text" ng-blur="updateUserSettings('lname')"> + <input name="lname" ng-model="model.lname" type="text" ng-blur="updateUserSettings('lname')" ng-maxlength="30"> </label> </section>