summaryrefslogtreecommitdiff
path: root/app/controllers/gemeinschaft_setups_controller.rb
blob: 89a82cbbdf6919b4031fc1f0c16574456896364b (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
class GemeinschaftSetupsController < ApplicationController
  load_and_authorize_resource :gemeinschaft_setup

  skip_before_filter :go_to_setup_if_new_installation
  # before_filter :redirect_if_not_a_fresh_installation

  def new
    @user = @gemeinschaft_setup.build_user(
                                            :user_name => t('gemeinschaft_setups.initial_setup.admin_name'), 
                                            :male => true,
                                            :email => 'admin@localhost',
                                          )
    @sip_domain = @gemeinschaft_setup.build_sip_domain(
      :host  => guess_local_host(),
      :realm => guess_local_host(),
    )
    @gemeinschaft_setup.country  = Country.find_by_name('Germany')
    @gemeinschaft_setup.language = Language.find_by_name('Deutsch')
  end

  def create
    if @gemeinschaft_setup.save
      super_tenant = Tenant.create(
                                    :name => GsParameter.get('SUPER_TENANT_NAME'),
                                    :country_id  => @gemeinschaft_setup.country.id, 
                                    :language_id => @gemeinschaft_setup.language_id,
                                    :description => t('gemeinschaft_setups.initial_setup.super_tenant_description'),
                                  )

      # GsNode
      GsNode.create(:name => 'Homebase', :ip_address => @gemeinschaft_setup.sip_domain.host, 
                    :push_updates_to => false, :accepts_updates_from => false, 
                    :site => 'Homebase', :element_name => 'Homebase')
      
      # Admin
      user = @gemeinschaft_setup.user
      super_tenant.tenant_memberships.create(:user_id => user.id)
      user.update_attributes(:current_tenant_id => super_tenant.id)

      # Create the Super-Tenant's group:
      super_tenant_super_admin_group = super_tenant.user_groups.create(:name => t('gemeinschaft_setups.initial_setup.super_admin_group_name'))
      super_tenant_super_admin_group.user_group_memberships.create(:user_id => user.id)

      # Set a couple of URLs in the GsParameter table
      GsParameter.where(:name => 'phone_book_entry_image_url').first.update_attributes(:value => "http://#{@gemeinschaft_setup.sip_domain.host}/uploads/phone_book_entry/image")
      GsParameter.where(:name => 'ringtone_url').first.update_attributes(:value => "http://#{@gemeinschaft_setup.sip_domain.host}")
      GsParameter.where(:name => 'user_image_url').first.update_attributes(:value => "http://#{@gemeinschaft_setup.sip_domain.host}/uploads/user/image")

      # Restart FreeSWITCH
      # require 'freeswitch_event'
      # FreeswitchAPI.execute('fsctl', 'shutdown restart')

      # Auto-Login:
      session[:user_id] = user.id
      
      # Redirect to the user
      redirect_to new_tenant_url, :notice => t('gemeinschaft_setups.initial_setup.successful_setup')
    else
      render :new
    end
  end
  
  private
  
  def redirect_if_not_a_fresh_installation
    if GemeinschaftSetup.all.count > 0
      if current_user
        redirect_to root_url    , :alert => t('gemeinschaft_setups.initial_setup.access_denied_only_available_on_a_new_system')
      else
        redirect_to log_in_path , :alert => t('gemeinschaft_setups.initial_setup.access_denied_only_available_on_a_new_system')
      end
    end
  end
  
end