cirandas.net

ref: dockerize

app/controllers/admin/users_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
 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
require 'csv'

class UsersController < AdminController

  protect 'manage_environment_users', :environment

  include UsersHelper

  def index
    @filter = params[:filter] || 'all_users'
    scope = environment.people.no_templates
    if @filter == 'admin_users'
      scope = scope.admins
    elsif @filter == 'activated_users'
      scope = scope.activated
    elsif @filter == 'deactivated_users'
      scope = scope.deactivated
    end
    scope = scope.order('name ASC')
    @q = params[:q]
    @collection = find_by_contents(:people, environment, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results]
  end

  def set_admin_role
    person = environment.people.find(params[:id])
    environment.add_admin(person)
    redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
  end

  def reset_admin_role
    person = environment.people.find(params[:id])
    environment.remove_admin(person)
    redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
  end

  def activate
    person = environment.people.find(params[:id])
    person.user.activate
    redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
  end

  def deactivate
    person = environment.people.find(params[:id])
    person.user.deactivate
    redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
  end

  def destroy_user
    if request.post?
      person = environment.people.find_by id: params[:id]
      if person && person.destroy
        session[:notice] = _('The profile was deleted.')
      else
        session[:notice] = _('Could not remove profile')
      end
    end
    redirect_to :action => :index, :q => params[:q], :filter => params[:filter]
  end

  def download
    respond_to do |format|
      format.html
      format.xml do
        users = User.where(:environment_id => environment.id).includes(:person)
        send_data users.to_xml(
            :skip_types => true,
            :only => %w[email login created_at updated_at],
            :include => { :person => {:only => %w[name updated_at created_at address birth_date contact_phone identifier lat lng] } }),
          :type => 'text/xml',
          :disposition => "attachment; filename=users.xml"
      end
      format.csv do
        # using a direct connection with the dbms to optimize
        command = User.send(:sanitize_sql, ["SELECT profiles.name, users.email FROM profiles
                                             INNER JOIN users ON profiles.user_id=users.id
                                             WHERE profiles.environment_id = ?", environment.id])
        users = User.connection.execute(command)
        csv_content = "name;email\n"
        users.each { |u|
          csv_content << CSV.generate_line([u['name'], u['email']], {:col_sep => ';'})
        }
        render :text => csv_content, :content_type => 'text/csv', :layout => false
      end
    end
  end

  def send_mail
    if request.post?
      @mailing = environment.mailings.build(params[:mailing])
      @mailing.recipients_roles = []
      @mailing.recipients_roles << "profile_admin" if params[:recipients][:profile_admins].include?("true")
      @mailing.recipients_roles << "environment_administrator" if params[:recipients][:env_admins].include?("true")
      @mailing.locale = locale
      @mailing.person = user
      if @mailing.save
        session[:notice] = _('The e-mails are being sent')
        redirect_to :action => 'index'
      else
        session[:notice] = _('Could not create the e-mail')
      end
    end
  end

  private

  def per_page
    10
  end

end