Commit 7e2ac70a9d5e6130e2d61fe0547e95535383a42b

Authored by Braulio Bhavamitra
1 parent e32dafb8

Add driven_signup plugin

plugins/driven_signup/controllers/admin/driven_signup_plugin/admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +class DrivenSignupPlugin::AdminController < AdminController
  2 +
  3 + no_design_blocks
  4 +
  5 + protect 'edit_environment_features', :environment
  6 +
  7 + def index
  8 +
  9 + end
  10 +
  11 + def new
  12 + @auth = environment.driven_signup_auths.build
  13 + end
  14 +
  15 + def edit
  16 + @auth = environment.driven_signup_auths.where(id: params[:id]).first
  17 + @auth ||= environment.driven_signup_auths.build
  18 + @auth.update_attributes params[:auth]
  19 + end
  20 +
  21 + def destroy
  22 + @auth = environment.driven_signup_auths.where(token: params[:token]).first
  23 + @auth.destroy if @auth
  24 + end
  25 +
  26 +end
... ...
plugins/driven_signup/controllers/driven_signup_plugin_admin_controller.rb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +DrivenSignupPluginAdminController = DrivenSignupPlugin::AdminController
... ...
plugins/driven_signup/controllers/public/driven_signup_plugin/account_controller.rb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +class DrivenSignupPlugin::AccountController < PublicController
  2 +
  3 + def signup
  4 + return render_access_denied unless Rails.env.development? or request.post?
  5 + return render_access_denied unless self.environment.driven_signup_auths.where(token: params[:token]).first
  6 +
  7 + session[:driven_signup] = true
  8 + session[:base_organization] = params[:base_organization]
  9 + session[:find_suborganization] = params[:find_suborganization]
  10 + session[:suborganization_members_limit] = params[:suborganization_members_limit]
  11 + session[:user_template] = params[:user_template]
  12 +
  13 + user_attributes = [:login, :email]
  14 + user_params = params[:signup].slice *user_attributes
  15 + profile_params = params[:signup].except *user_attributes
  16 +
  17 + if current_user and user_params[:email].squish == current_user.email
  18 + current_user.driven_signup_complete
  19 + redirect_to session.delete(:after_signup_redirect_to)
  20 + else
  21 + self.current_user = nil
  22 + redirect_to controller: :account, action: :signup, user: user_params, profile_data: profile_params
  23 + end
  24 + end
  25 +
  26 + protected
  27 +
  28 + def default_url_options
  29 + # avoid rails' use_relative_controller!
  30 + {use_route: '/'}
  31 + end
  32 +
  33 +end
... ...
plugins/driven_signup/db/migrate/20150625143118_create_driven_signup_plugin_token.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class CreateDrivenSignupPluginToken < ActiveRecord::Migration
  2 +
  3 + def change
  4 + create_table :driven_signup_plugin_auths do |t|
  5 + t.integer :environment_id
  6 + t.string :name
  7 + t.string :token
  8 +
  9 + t.timestamps
  10 + end
  11 + add_index :driven_signup_plugin_auths, :environment_id
  12 + add_index :driven_signup_plugin_auths, :token
  13 + add_index :driven_signup_plugin_auths, [:environment_id, :token]
  14 + end
  15 +
  16 +end
... ...
plugins/driven_signup/lib/driven_signup_plugin.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +module DrivenSignupPlugin
  2 +
  3 + extend Noosfero::Plugin::ParentMethods
  4 +
  5 + def self.plugin_name
  6 + _'Driven signup'
  7 + end
  8 +
  9 + def self.plugin_description
  10 + _'Allow external websites to manage the signup'
  11 + end
  12 +
  13 +end
... ...
plugins/driven_signup/lib/driven_signup_plugin/base.rb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +class DrivenSignupPlugin::Base < Noosfero::Plugin
  2 +
  3 +end
... ...
plugins/driven_signup/lib/ext/environment.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'environment'
  2 +
  3 +class Environment
  4 +
  5 + has_many :driven_signup_auths, class_name: 'DrivenSignupPlugin::Auth', dependent: :destroy
  6 +
  7 +end
... ...
plugins/driven_signup/lib/ext/user.rb 0 → 100644
... ... @@ -0,0 +1,46 @@
  1 +require_dependency 'user'
  2 +
  3 +class User
  4 +
  5 + after_create :driven_signup_complete
  6 +
  7 + protected
  8 +
  9 + def driven_signup_complete
  10 + return unless self.session and self.session.delete(:driven_signup)
  11 +
  12 + base_organization = self.environment.profiles.where(identifier: self.session.delete(:base_organization)).first
  13 + return unless base_organization
  14 + organization = base_organization
  15 +
  16 + if self.session.delete :find_suborganization
  17 + members_limit = self.session.delete(:suborganization_members_limit).to_i || 50
  18 + suborganizations = self.environment.profiles.
  19 + where('identifier <> ?', base_organization.identifier).
  20 + where('identifier LIKE ?', "#{base_organization.identifier}%").
  21 + order('identifier ASC')
  22 + pp suborganizations
  23 + suborganizations.each do |suborganization|
  24 + if suborganization.members.count < members_limit
  25 + organization = suborganization
  26 + break
  27 + end
  28 + end
  29 + end
  30 +
  31 + if template = self.environment.profiles.where(identifier: self.session.delete(:user_template)).first
  32 + self.person.articles.destroy_all
  33 + self.person.apply_template template
  34 + end
  35 +
  36 + # directly affiliate
  37 + organization.affiliate self.person, Profile::Roles.member(self.environment.id)
  38 +
  39 + self.person.redirection_after_login = 'custom_url'
  40 + self.person.custom_url_redirection = Noosfero::Application.routes.url_for organization.url
  41 + self.person.save
  42 +
  43 + self.session[:after_signup_redirect_to] = organization.url
  44 + end
  45 +
  46 +end
... ...
plugins/driven_signup/models/driven_signup_plugin/auth.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class DrivenSignupPlugin::Auth < ActiveRecord::Base
  2 +
  3 + attr_accessible :name, :token
  4 +
  5 + belongs_to :environment
  6 +
  7 + validates_presence_of :environment
  8 + validates_presence_of :token
  9 + validates_uniqueness_of :token, scope: :environment_id
  10 +
  11 + def token
  12 + self[:token] ||= SecureRandom.hex 16
  13 + end
  14 +
  15 +end
... ...
plugins/driven_signup/public/javascripts/driven_signup.js 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +driven_signup = {
  2 +
  3 + admin: {
  4 + append: function(auth) {
  5 + return $('#auth-new').before(auth)
  6 + },
  7 +
  8 + find: function(token) {
  9 + return $('#driven-signup-tokens [data-token='+token+']')
  10 + },
  11 +
  12 + update: function(token, auth){
  13 + return this.find(token).replaceWith(auth)
  14 + },
  15 +
  16 + remove: function(token) {
  17 + return this.find(token).remove()
  18 + },
  19 + },
  20 +
  21 +}
  22 +
... ...
plugins/driven_signup/public/stylesheets/driven_signup.scss 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +#driven-signup-tokens {
  2 +
  3 + &.table {
  4 + display: table;
  5 +
  6 + .row {
  7 + display: table-row;
  8 +
  9 + .cell {
  10 + display: table-cell;
  11 + }
  12 + }
  13 + }
  14 +}
... ...
plugins/driven_signup/test/functional/account_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +require 'test_helper'
  2 +
  3 +# Re-raise errors caught by the controller.
  4 +class AccountController; def rescue_action(e) raise e end; end
  5 +
  6 +class AccountControllerTest < ActionController::TestCase
  7 +
  8 + def setup
  9 + @controller = AccountController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 +
  13 + e = Environment.default
  14 + e.enable 'skip_new_user_email_confirmation', true
  15 + disable_signup_bot_check e
  16 + end
  17 +
  18 + should 'use the parameters' do
  19 + community = create Community, name: 'base', identifier: 'base1'
  20 + subcommunity = create Community, name: 'sub', identifier: 'base11'
  21 + subcommunity.reload
  22 +
  23 + # simulate DrivenSignupPlugin::AccountController
  24 + session[:driven_signup] = true
  25 + session[:base_organization] = community.identifier
  26 + session[:find_suborganization] = true
  27 + session[:suborganization_members_limit] = 50
  28 +
  29 + post :signup, user: {login: 'quire', password: 'quire', password_confirmation: 'quire', name: 'quire', email: 'test@example.com'}
  30 + assert_response :redirect
  31 + assert_redirected_to subcommunity.url
  32 +
  33 + user = Profile['quire']
  34 + assert user
  35 + assert_includes subcommunity.members, user
  36 + end
  37 +
  38 + private
  39 +
  40 + def disable_signup_bot_check environment = Environment.default
  41 + environment.min_signup_delay = 0
  42 + environment.save!
  43 + end
  44 +
  45 +end
... ...
plugins/driven_signup/views/driven_signup_plugin/account/signup.html.slim 0 → 100644
plugins/driven_signup/views/driven_signup_plugin/admin/_auth.html.slim 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +div.row
  2 + = form_for auth, as: :auth, remote: true, url: {action: :edit, id: auth.id},
  3 + html: {data: {token: auth.token}} do |f|
  4 +
  5 + span.cell= f.text_field :name, placeholder: _('name this token')
  6 + span.cell= f.text_field :token, value: auth.token
  7 + span.cell= f.submit nil, class: 'btn btn-default btn-success'
  8 + span.cell= link_to _('Remove'), {action: :destroy, token: auth.token}, remote: true,
  9 + class: 'btn btn-default btn-danger', confirm: _('Are you sure you want to delete this authorization?')
  10 +
... ...
plugins/driven_signup/views/driven_signup_plugin/admin/destroy.js.erb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +driven_signup.admin.remove(<%=@auth.token.to_json%>)
  2 +
... ...
plugins/driven_signup/views/driven_signup_plugin/admin/edit.js.erb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +driven_signup.admin.update(<%=@auth.token.to_json%>, <%= render('auth', auth: @auth).to_json %>)
  2 +
... ...
plugins/driven_signup/views/driven_signup_plugin/admin/index.html.slim 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 += content_for :head do
  2 + = stylesheet_link_tag 'plugins/driven_signup/stylesheets/driven_signup'
  3 + = javascript_include_tag 'plugins/driven_signup/javascripts/driven_signup'
  4 +
  5 +div#driven-signup-tokens.table
  6 + - environment.driven_signup_auths.each do |auth|
  7 + = render 'auth', auth: auth
  8 +
  9 + div.row id="auth-new"
  10 + td= link_to _('New'), {action: :new}, remote: true, class: 'btn btn-default fa-add'
  11 +
... ...
plugins/driven_signup/views/driven_signup_plugin/admin/new.js.erb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +driven_signup.admin.append(<%= render('auth', auth: @auth).to_json %>)
  2 +
... ...