jobstar-se / valijson Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
EXAMPLE USAGE: RUBY ON RAILS BACK-END ************************************************************************************* class AccountController < ApplicationController class Reqs PASSWORD_RE = /^[a-z0-9\!@#\$%^&*_+.]+$/i INVITATION_NAME_RE = /^[a-z0-9ÅÄÖåäö&_ ]+$/i REQ_NAME = ['string', 'req', ['min',4], ['max',150], ['match', INVITATION_NAME_RE]] REQ_PASSWORD = ['string', 'req', ['min', 5], ['max', 64], ['match', PASSWORD_RE] ] REQ_EMAIL = ['email', 'req'] REGISTER_INVITATION_LOGGED_IN = { 'name' => REQ_NAME , } REGISTER_INVITATION_SIGNUP = { 'name' => REQ_NAME, 'email' => REQ_EMAIL, 'password' => REQ_PASSWORD, } REGISTER_INVITATION_LOGIN = { 'name' => REQ_NAME, 'email' => REQ_EMAIL, 'password' => REQ_PASSWORD, } LOGIN = { 'email' => REQ_EMAIL, 'password' => REQ_PASSWORD, } REGAIN_ACCESS = { 'email' => REQ_EMAIL } RESET_PASSWORD = { 'new_password' => REQ_PASSWORD } end def reset_password raise 'illegal' if !request.post? h = params[:form] err = Validate.form( h, Reqs::RESET_PASSWORD ) || {} if err.blank? user = User.find_by_email( h[:email] ) if ( !user ) err[:email] = :no_such_user elsif ( user.password!=Digest::SHA2.hexdigest( user.salt + h[:password] ) and user.temporary_password!=h[:password] ) err[:password] = :incorrect end end if err.blank? user.set_password( h ) end render :json => ( err.blank? ? { :response_code => 1 } : { :response_code => 0, :errors => err } ) end def regain_access raise 'illegal' if !request.post? h = params[:form] err = Validate.form( h, Reqs::REGAIN_ACCESS ) || {} if ( err.blank? ) user = User.find_by_email( h[:email] ) if ( !user ) err[:email] = :no_such_user end if err.blank? user.send_temporary_password end end render :json => ( err.blank? ? { :response_code => 1 } : { :response_code => 0, :erors => err } ) end def login raise 'illegal' if !request.post? h = params[:form] err = Validate.form( h, Reqs::LOGIN ) || {} if ( err.blank? ) user = User.find_by_email( h[:email] ) temporary = false if ( !user ) err[:email] = :no_such_user elsif ( user.password!=Digest::SHA2.hexdigest( user.salt + h[:password] ) ) if ( user.temporary_password==h[:password] ) temporary = true else err[:password] = :incorrect end end if err.blank? session[:current_user_id] = user.id end end render :json => ( err.blank? ? { :response_code => 1, :current_user_email => user.email, :temporary => ( temporary ? 1 : 0 ) } : { :response_code => 0, :errors => err } ) end def logout session[:current_user_id] = nil redirect_to '/', :status => 302 return end def register_invitation raise 'illegal' if !request.post? h = params[:form] if ( logged_in? ) reqs = Reqs::REGISTER_INVITATION_LOGGED_IN else reqs = h[:login]=='1' ? Reqs::REGISTER_INVITATION_LOGIN : Reqs::REGISTER_INVITATION_SIGNUP end err = Validate.form( h, reqs ) account_created = false if err.blank? err = {} h[:nameid] = h[:name].gsub( /[\s\/.?&]/, '_' ).mb_chars.downcase.to_s err[:name] = :not_unique if Invitation.find_by_sid( h[:nameid] ) err[:name] = :not_unique if Invitation.find_by_nameid( h[:nameid] ) if ( logged_in? ) user = current_user else if ( h[:login]=='1' ) user = User.find_by_email( h[:email] ) err[:email] = :no_such_user if !user if ( user and user.password!=Digest::SHA2.hexdigest( user.salt + h[:password] ) ) err[:password] = :incorrect end else err[:email] = :not_unique if User.find_by_email( h[:email] ) if err.blank? user = User.create( h ) account_created = true end end session[:current_user_id] = user.id if err.blank? end end if err.blank? invitation = Invitation.find_by_sid( h[:sid] ) raise h.inspect if !invitation || invitation.name user.user_x_invitations.create( :invitation_id => invitation.id ) invitation.update_attributes( :name => h[:name], :nameid => h[:nameid] ) end render :json => ( err.blank? ? { :response_code => 1, :current_user_email => user.email, :invitation_name => invitation.name, :account_created => account_created ? 1 : 0 } : { :response_code => 0, :errors => err } ) end end EXAMPLE USAGE: RUBY ON RAILS FRONT-END ********************************************************************************* var register_logged_in_reqs = new Hash( <%= AccountController::Reqs::REGISTER_INVITATION_LOGGED_IN.to_json %> ); var register_login_reqs = new Hash( <%= AccountController::Reqs::REGISTER_INVITATION_LOGIN.to_json %> ); var register_signup_reqs = new Hash( <%= AccountController::Reqs::REGISTER_INVITATION_SIGNUP.to_json %> ); var register_form = new Form( $('#register_form'), { 'reqs' : ( current_user_email ? register_logged_in_reqs : register_login_reqs ), 'submit_type' : 'ajax', 'submitting_text' : 'Saving...', 'ajax' : { 'url' : '/account/register_invitation', 'success_hook' : success_fn } }); function Form( form, config ) { var self = this; this.form = form; this.config = config; this.submit_button = form.find( "input[name='form[submit]']" ); form.find( "input[type=text], input[type=password]" ).bind( 'keyup blur', function() { var name = $(this).attr( 'name' ); name = name.substring( 5, name.length-1 ); var error = Validate.field( form, name, config.reqs.get( name ) ); var field = form.find( '.' + name ); if ( error ) { if ( field.find( 'input' ).hasClass( 'error' ) ) { field.find( '.error-message' ).text( error ); } } else { field.find( '.error-message' ).text( '' ); field.find( "input" ).removeClass( 'error' ); } }); this.draw_errors = function( errors ) { var keys = errors.keys(); for ( var i = 0; i<keys.length; i++ ) { var name = keys[i]; var error = errors.get( name ); var field = form.find( '.' + name ); field.find( '.error-message' ).text( error ); field.find( "input" ).addClass( 'error' ); } } this.submit_button.click( function() { var errors = Validate.form( form, config.reqs ); if ( errors ) { self.draw_errors( errors ); return false; } else { var button_text = self.submit_button.val(); self.submit_button.val( self.config.submitting_text ); self.submit_button.attr( { disabled: true } ).addClass( 'disabled' ); if ( self.config.submit_type=='ajax' ) { $.ajax( { url: self.config.ajax.url, type: 'POST', data: form.serialize(), success: function( data ) { if ( data.response_code==0 ) { self.draw_errors( new Hash( data.errors ) ); self.submit_button.attr( { disabled: false } ).removeClass( 'disabled' ).val( button_text ); } else { self.config.ajax.success_hook( data ); } } }); return false; } } return true; }); }; Copyright (c) 2010 Peder Linder. See LICENSE for details.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.