summaryrefslogtreecommitdiff
path: root/app/controllers/sip_accounts_controller.rb
blob: cd34953d875f70f76e11091483d035c05a5510dd (plain)
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
130
131
132
133
134
class SipAccountsController < ApplicationController
  load_resource :user
  load_resource :tenant
  load_and_authorize_resource :sip_account, :through => [:user, :tenant ]
 
  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs

  helper_method :sort_column, :sort_descending

  def index
    if @parent.class == Tenant
      @sip_accounts = @parent.tenant_user_sip_accounts.order(sort_column + ' ' + (sort_descending ? 'DESC' : 'ASC')).paginate(
        :page => params[:page],
        :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
      )
    else
      @sip_accounts = @parent.sip_accounts.order(sort_column + ' ' + (sort_descending ? 'DESC' : 'ASC')).paginate(
        :page => params[:page],
        :per_page => GsParameter.get('DEFAULT_PAGINATION_ENTRIES_PER_PAGE')
      )
    end
  end

  def show
    @register_protocols = {
      :tel    => "#{request.protocol}#{request.host_with_port}/sip_accounts/#{@sip_account.try(:id)}/calls/new?url=%s",
      :callto => "#{request.protocol}#{request.host_with_port}/sip_accounts/#{@sip_account.try(:id)}/calls/new?url=%s",
    }
  end

  def new
    @sip_account = @parent.sip_accounts.build
    @sip_account.caller_name = @parent
    @sip_account.call_waiting = GsParameter.get('CALL_WAITING')
    @sip_account.clir = GsParameter.get('DEFAULT_CLIR_SETTING')
    @sip_account.clip = GsParameter.get('DEFAULT_CLIP_SETTING')
    @sip_account.callforward_rules_act_per_sip_account = GsParameter.get('CALLFORWARD_RULES_ACT_PER_SIP_ACCOUNT_DEFAULT')
    if @parent.class == User
      @sip_account.hotdeskable = true
    end

    # Make sure that we don't use an already taken auth_name
    #  
    loop do
      @sip_account.auth_name = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_AUTH_NAME'))
      
      break unless SipAccount.exists?(:auth_name => @sip_account.auth_name)
    end
    @sip_account.password = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_PASSWORD'))

    @sip_account.voicemail_account = VoicemailAccount.where(:voicemail_accountable_type => @parent.class.name, :voicemail_accountable_id => @parent.id).first
    @sip_account.language_code = @parent.language.try(:code)
    possible_voicemail_accounts
  end

  def create
    @sip_account = @parent.sip_accounts.build(params[:sip_account])

    if @sip_account.auth_name.blank?
      loop do
        @sip_account.auth_name = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_AUTH_NAME'))
        
        break unless SipAccount.exists?(:auth_name => @sip_account.auth_name)
      end
    end
    if @sip_account.password.blank?
      @sip_account.password = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_PASSWORD'))
    end
    
    if @sip_account.save
      m = method( :"#{@parent.class.name.underscore}_sip_account_path" )
      redirect_to m.( @parent, @sip_account ), :notice => t('sip_accounts.controller.successfuly_created', :resource => @parent)
    else
      render :new
    end
  end

  def edit
    possible_voicemail_accounts
  end

  def update
    if @sip_account.update_attributes(params[:sip_account])
      m = method( :"#{@parent.class.name.underscore}_sip_account_path" )
      redirect_to m.( @parent, @sip_account ), :notice  => t('sip_accounts.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @sip_account.destroy
    m = method( :"#{@parent.class.name.underscore}_sip_accounts_url" )
    redirect_to m.(@parent), :notice => t('sip_accounts.controller.successfuly_destroyed')
  end

  private
  def set_and_authorize_parent
    @parent = @user || @tenant
    authorize! :read, @parent
  end

  def spread_breadcrumbs
    if @user
      add_breadcrumb t("users.index.page_title"), tenant_users_path(@user.current_tenant)
      add_breadcrumb @user, tenant_user_path(@user.current_tenant, @user)
      add_breadcrumb t("sip_accounts.index.page_title"), user_sip_accounts_path(@user)
      if @sip_account && !@sip_account.new_record?
        add_breadcrumb @sip_account, user_sip_account_path(@user, @sip_account)
      end
    end
    if @tenant
      add_breadcrumb t("sip_accounts.index.page_title"), tenant_sip_accounts_path(@tenant)
      if @sip_account && !@sip_account.new_record?
        add_breadcrumb @sip_account, tenant_sip_account_path(@tenant, @sip_account)
      end
    end
  end

  def possible_voicemail_accounts
    @possible_voicemail_accounts = @sip_account.voicemail_accounts
    @possible_voicemail_accounts = @possible_voicemail_accounts + @sip_account.sip_accountable.voicemail_accounts
  end

  def sort_descending
    params[:desc].to_s == 'true'
  end

  def sort_column
    SipAccount.column_names.include?(params[:sort]) ? params[:sort] : 'id'
  end

end