cirandas.net

ref: master

plugins/stoa/lib/stoa_plugin.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require_dependency 'person'

class StoaPlugin < Noosfero::Plugin

  def self.plugin_name
    "Stoa"
  end

  def self.plugin_description
    _("Add Stoa features")
  end

  def stylesheet?
    true
  end

  def signup_extra_contents
    proc {
      content_tag(:div, labelled_form_field(_('USP number'), text_field(:profile_data, :usp_id, :id => 'usp_id_field')) +
      content_tag(:small, _('The usp id grants you special powers in the network. Don\'t forget to fill it with a valid number if you have one.'), :id => 'usp-id-balloon') +
      content_tag('p', _("Either this usp number is being used by another user or is not valid"), :id => 'usp-id-invalid') +
      content_tag('p', _('Checking usp number...'), :id => 'usp-id-checking'), :id => 'signup-usp-id') +
      content_tag('div', required(labelled_form_field(_('Birth date (yyyy-mm-dd)'), text_field_tag('birth_date', ''))) +
      content_tag(:small, _('Confirm your birth date. Pay attention to the format: yyyy-mm-dd.'), :id => 'usp-birth-date-balloon'), :id => 'signup-birth-date', :style => 'display: none') +
      content_tag('div', required(labelled_form_field(_('CPF'), text_field_tag('cpf', ''))) +
      content_tag(:small, _('Confirm your CPF number.'), :id => 'usp-cpf-balloon'), :id => 'signup-cpf', :style => 'display: none') +
      javascript_include_tag('plugins/stoa/javascripts/jquery.observe_field', 'plugins/stoa/javascripts/signup_complement')
    }
  end

  def profile_info_extra_contents
    if context.profile.person?
      usp_id = context.profile.usp_id
      lambda {
        content_tag('div', labelled_form_field(_('USP number'), text_field_tag('profile_data[usp_id]', usp_id, :id => 'usp_id_field', :disabled => usp_id.present?)) +
        content_tag(:small, _('The usp id grants you special powers in the network. Don\'t forget to fill it if you have one.')) +
        content_tag('div', labelled_check_box(c_('Public'), '', '', false, :disabled => true, :title => _('This field must be private'), :class => 'disabled'), :class => 'field-privacy-selector'), :class => 'field-with-privacy-selector') +
        content_tag('div', required(labelled_form_field(_('Birth date (yyyy-mm-dd)'), text_field_tag('birth_date', ''))), :id => 'signup-birth-date', :style => 'display: none') +
        content_tag('div', required(labelled_form_field(_('CPF'), text_field_tag('cpf', ''))), :id => 'signup-cpf', :style => 'display:none') +
        javascript_include_tag('plugins/stoa/javascripts/jquery.observe_field', 'plugins/stoa/javascripts/signup_complement')
      }
    end
  end

  def login_extra_contents
    proc {
      content_tag('div', labelled_form_field(_('USP number / Username'), text_field_tag('usp_id_login', '', :id => 'stoa_field_login')) +
      labelled_form_field(c_('Password'), password_field_tag('password', '', :id => 'stoa_field_password')), :id => 'stoa-login-fields')
    }
  end

  def alternative_authentication
    person = Person.find_by usp_id: context.params[:usp_id_login]
    if person
      user = User.authenticate(person.user.login, context.params[:password])
    else
      user = User.authenticate(context.params[:usp_id_login], context.params[:password])
    end
    user
  end

  def account_controller_filters
    block = proc do
      params[:profile_data] ||= {}
      params[:profile_data][:invitation_code] = params[:invitation_code]
      invitation = Task.pending.where(code: params[:invitation_code]).first
      if request.post?
        if !invitation && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]])
          # `self` below is evaluated in the context of account_controller
          @person = Person.new(:environment => self.environment)
          @person.errors.add(:usp_id, _(' validation failed'))
          render :action => :signup
        end
      end
    end

    [{ :type => 'before_filter',
      :method_name => 'validate_usp_id',
      :options => {:only => 'signup'},
      :block => block }]
  end

  def profile_editor_controller_filters
    block = proc do
      if request.post?
        if !params[:profile_data][:usp_id].blank? && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]])
          @profile_data = profile
          @profile_data.attributes = params[:profile_data]
          @profile_data.valid?
          @profile_data.errors.add(:usp_id, _(' validation failed'))
          @profile_data.usp_id = nil
          @possible_domains = profile.possible_domains
          render :action => :edit
        end
      end
    end

    [{ :type => 'before_filter',
      :method_name => 'validate_usp_id',
      :options => {:only => 'edit'},
      :block => block }]
  end

  def invite_controller_filters
    [{ :type => 'before_filter',
      :method_name => 'check_usp_id_existence',
      :block => proc {render_access_denied if !user || user.usp_id.blank?} }]
  end

  def control_panel_buttons
    { :title => c_('Invite friends'),
      :icon => 'invite-friends',
      :url => {:controller => 'invite',
               :action => 'invite_friends'} } if context.send(:user) && context.send(:user).usp_id.present?
  end

  def remove_invite_friends_button
    true
  end

  def change_password_fields
    {:field => :usp_id, :name => _('USP Number'), :model => 'person'}
  end

  def search_friend_fields
    [{:field => :usp_id, :name => _('USP Number')}]
  end

end