summaryrefslogtreecommitdiff
path: root/app/controllers/sip_accounts_controller.rb
blob: a83208bdef01fb3eeced1fb7485bcf65f3b2759c (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
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

  def index
  end

  def show
  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.voicemail_pin = random_pin
    @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'))
  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
  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 :back, :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

end