cirandas.net

ref: master

plugins/stoa/controllers/stoa_plugin_controller.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
require 'stoa_plugin/person_fields'

class StoaPluginController < PublicController
  append_view_path File.join(File.dirname(__FILE__) + '/../views')

  include StoaPlugin::PersonFields

  def authenticate
    if request.post?
      if params[:login].blank?
        person = Person.find_by_usp_id(params[:usp_id])
        login = person ? person.user.login : nil
      else
        login = params[:login]
      end
      user = User.authenticate(login, params[:password], environment)
      if user
        result = StoaPlugin::PersonApi.new(user.person, self).fields(selected_fields(params[:fields], user))
        result.merge!(:ok => true)
      else
        result = { :error => _('Incorrect user/password pair.'), :ok => false }
      end
      render :text => result.to_json
    else
      render :text => { :error => _('Conection requires post method.'), :ok => false }.to_json
    end
  end

  def check_usp_id
    begin
      render :text => { :exists => StoaPlugin::UspUser.exists?(params[:usp_id]) && Person.find_by(usp_id: params[:usp_id]).nil? }.to_json
    rescue Exception => exception
      render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json
    end
  end

  def check_cpf
    begin
      render :text => { :exists => StoaPlugin::UspUser.find_by(codpes: params[:usp_id]).cpf.present? }.to_json
    rescue Exception => exception
      render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json
    end
  end

end