diff options
| -rw-r--r-- | .travis.yml | 2 | ||||
| -rw-r--r-- | app/assets/javascripts/application.js | 4 | ||||
| -rw-r--r-- | config/initializers/client_side_validations.rb | 14 | ||||
| -rw-r--r-- | ui_dependencies.rb | 2 | ||||
| -rw-r--r-- | users/app/assets/javascripts/users.js.coffee | 56 | ||||
| -rw-r--r-- | users/app/models/user.rb | 11 | ||||
| -rw-r--r-- | users/app/views/users/new.html.haml | 4 | ||||
| -rwxr-xr-x | users/test/integration/api/python/flow_with_srp.py | 2 | 
8 files changed, 62 insertions, 33 deletions
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..984e24a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +services: +  - couchdb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 36a9979..23d7fef 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -20,4 +20,6 @@  $(function() {    $('a:not([data-remote]):not([data-behavior]):not([data-skip-pjax])').pjax('[data-pjax-container]'); -});
\ No newline at end of file +}); +//= require rails.validations +//= require rails.validations.simple_form diff --git a/config/initializers/client_side_validations.rb b/config/initializers/client_side_validations.rb new file mode 100644 index 0000000..2c73fa3 --- /dev/null +++ b/config/initializers/client_side_validations.rb @@ -0,0 +1,14 @@ +# ClientSideValidations Initializer + +# Uncomment to disable uniqueness validator, possible security issue +# ClientSideValidations::Config.disabled_validators = [:uniqueness] + +# Uncomment the following block if you want each input field to have the validation messages attached. +ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| +  unless html_tag =~ /^<label/ +    %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>}.html_safe +  else +    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe +  end +end + diff --git a/ui_dependencies.rb b/ui_dependencies.rb index 2c1b4aa..a05a8f9 100644 --- a/ui_dependencies.rb +++ b/ui_dependencies.rb @@ -3,6 +3,8 @@ gem "bootstrap-sass", "~> 2.1.0"  gem "jquery-rails"  gem "simple_form"  gem "pjax_rails" +gem 'client_side_validations' +gem 'client_side_validations-simple_form'  group :assets do    gem "haml-rails", "~> 0.3.4" diff --git a/users/app/assets/javascripts/users.js.coffee b/users/app/assets/javascripts/users.js.coffee index 24302fe..ab437f6 100644 --- a/users/app/assets/javascripts/users.js.coffee +++ b/users/app/assets/javascripts/users.js.coffee @@ -1,45 +1,47 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ -# +preventDefault = (event) -> +  event.preventDefault() -validate_password = (event) -> - -  password = $('#srp_password').val() -  confirmation = $('#srp_password_confirmation').val() -  login = $('#srp_username').val() - -  if password != confirmation -    alert "Password and Confirmation do not match!" -    $('#srp_password').focus() -    return false -  if password == login -    alert "Password and Login may not match!" -    $('#srp_password').focus() -    return false -  if password.length < 8 -    alert "Password needs to be at least 8 characters long!" -    $('#srp_password').focus() -    return false +validOrAbort = (event) -> +  errors = {} +   +  abortIfErrors = -> +    return if $.isEmptyObject(errors) +    # we're relying on client_side_validations here instead of printing +    # our own errors. This gets us translatable error messages. +    $('.control-group.error input, .control-group.error select, control-group.error textarea').first().focus() +    event.stopImmediatePropagation() +     +  validatePassword = -> +    password = $('#srp_password').val() +    confirmation = $('#srp_password_confirmation').val() +    login = $('#srp_username').val() +   +    if password != confirmation +      errors.password_confirmation = "Confirmation does not match!" +    if password == login +      errors.password = "Password and Login may not match!" +    if password.length < 8 +      errors.password = "Password needs to be at least 8 characters long!" + +  validatePassword() +  abortIfErrors() -  return true -  signup = (event) ->    srp = new SRP(jqueryRest())    srp.register ->      window.location = '/' -  false  login = (event) ->    srp = new SRP(jqueryRest())    srp.identify ->      window.location = '/' -  false  $(document).ready -> -  $('#new_user').submit validate_password +  $('#new_user').submit preventDefault +  $('#new_user').submit validOrAbort    $('#new_user').submit signup +  $('#new_session').submit preventDefault    $('#new_session').submit login diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 0f5d650..824c439 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -16,8 +16,11 @@ class User < CouchRest::Model::Base        :message => "Only letters, digits and _ allowed" }    validates :password_salt, :password_verifier, -    :format => { :with => /\A[\dA-Fa-f]+\z/, -      :message => "Only hex numbers allowed" } +    :format => { :with => /\A[\dA-Fa-f]+\z/, :message => "Only hex numbers allowed" } + +  validates :password, :presence => true, +    :confirmation => true, +    :format => { :with => /.{8}.*/, :message => "needs to be at least 8 characters long" }    timestamps! @@ -71,4 +74,8 @@ class User < CouchRest::Model::Base      APP_CONFIG['admins'].include? self.login    end +  protected +  def password +    password_verifier +  end  end diff --git a/users/app/views/users/new.html.haml b/users/app/views/users/new.html.haml index 835e99a..be14c52 100644 --- a/users/app/views/users/new.html.haml +++ b/users/app/views/users/new.html.haml @@ -1,9 +1,9 @@  .span8.offset2    %h2=t :signup -  = simple_form_for @user, :html => {:class => 'form-horizontal'} do |f| +  = simple_form_for @user, :validate => true, :html => {:class => 'form-horizontal'} do |f|      %legend=t :signup_message      = f.input :login, :input_html => { :id => :srp_username } -    = f.input :password, :required => true, :input_html => { :id => :srp_password } +    = f.input :password, :required => true, :validate => true, :input_html => { :id => :srp_password }      = f.input :password_confirmation, :required => true, :input_html => { :id => :srp_password_confirmation }      = f.button :submit, :value => t(:signup), :class => 'btn-primary'       = link_to t(:cancel), root_url, :class => :btn diff --git a/users/test/integration/api/python/flow_with_srp.py b/users/test/integration/api/python/flow_with_srp.py index 0a11aec..b599252 100755 --- a/users/test/integration/api/python/flow_with_srp.py +++ b/users/test/integration/api/python/flow_with_srp.py @@ -16,7 +16,7 @@ def id_generator(size=6, chars=string.ascii_uppercase + string.digits):    return ''.join(random.choice(chars) for x in range(size))  # using globals for a start -server = 'http://springbok/1/' +server = 'http://springbok.leap.se/1/'  login = id_generator()  password = id_generator() + id_generator()  | 
