summaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: 2aadf19204779477fa9c5f5017c8dd4b41f0bfc5 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class ApplicationController < ActionController::Base
  
  protect_from_forgery

  before_filter :set_locale

  before_filter :go_to_setup_if_new_installation
  before_filter :home_breadcrumb
  
  helper_method :current_user
  
  helper_method :guess_local_ip_address
  helper_method :guess_local_host
  
  helper_method :'have_https?'

  helper_method :random_pin
  
  
  #TODO Add check_authorization. See
  # https://github.com/ryanb/cancan
  # https://github.com/ryanb/cancan/wiki/Ensure-Authorization
  # and Gemeinschaft 4
  
  # Generate a new name for an Object
  #
  def generate_a_new_name(parent, child = nil)
    if child
      i = parent.send(child.class.name.underscore.pluralize).count
      loop do
        i += 1
        if I18n.t("#{child.class.name.underscore.pluralize}.new_name_scaffold").include?('translation missing')
          @guess_a_new_name = I18n.t(child.class.name.underscore.pluralize + '.name') + " #{i}"
        else
          @guess_a_new_name = I18n.t("#{child.class.name.underscore.pluralize}.new_name_scaffold", :counter => i.to_s)
        end
        break unless parent.send(child.class.name.underscore.pluralize).where(:name => "#{@guess_a_new_name}").count > 0
      end
    else
      i = parent.class.count
      loop do
        i += 1
        if I18n.t("#{parent.class.name.underscore.pluralize}.new_name_scaffold").include?('translation missing')
          @guess_a_new_name = I18n.t(parent.class.name.underscore.pluralize + '.name') + " #{i}"
        else
          @guess_a_new_name = I18n.t("#{parent.class.name.underscore.pluralize}.new_name_scaffold", :counter => i.to_s)
        end
        break unless parent.class.where(:name => "#{@guess_a_new_name}").count > 0
      end
    end
    return @guess_a_new_name
  end

  # Generate a new random PIN
  #
  def random_pin
    if GsParameter.get('MINIMUM_PIN_LENGTH') > 0
      (1..GsParameter.get('MINIMUM_PIN_LENGTH')).map{|i| (0 .. 9).to_a.sample}.join
    end
  end
  
  # return the IP address (preferred) or hostname at which the
  # current request arrived
  def server_host
    return (
      request.env['SERVER_ADDR'] ||
      request.env['SERVER_NAME'] ||
      request.env['HTTP_HOST']
    )
  end
  
  def have_https?
    return Connectivity::port_open?( server_host(), 443 )
  end
  
  
  def guess_local_ip_address
    ret = nil
    begin
      ipsocket_addr_info = UDPSocket.open {|s| s.connect("255.255.255.254", 1); s.addr(false) }
      ret = ipsocket_addr_info.last if ipsocket_addr_info
    rescue
    end
    return ret
  end
  
  def guess_local_host
    ret = guess_local_ip_address()
    if ! ret
      begin
        if request
          ret = request.env['SERVER_NAME']
        end
      rescue
      end
    end
    if ret && [
      '',
      'localhost',
      '127.0.0.1',
      '0.0.0.0',
    ].include?(ret)
      ret = nil
    end
    return ret
  end
  
  rescue_from CanCan::AccessDenied do |exception|
    if current_user
      redirect_to root_url, :alert => 'Access denied! Please ask your admin to grant you the necessary rights.'
    else
      if Tenant.count == 0 && User.count == 0
        # This is a brand new system. We need to run a setup first.
        redirect_to wizards_new_initial_setup_path
      else
        # You need to login first.
        redirect_to log_in_path, :alert => 'Access denied! You need to login first.'
      end
    end
  end
  
  private  
  
  def current_user
    if session[:user_id] || GsParameter.get('SingleSignOnEnvUserNameKey').blank?
      if session[:user_id] && User.where(:id => session[:user_id]).any?
        return User.where(:id => session[:user_id]).first
      else
        session[:user_id] = nil
        return nil
      end
    else
      if User.where(:user_name => request.env[GsParameter.get('SingleSignOnEnvUserNameKey')]).any?
        auth_user = User.where(:user_name => request.env[GsParameter.get('SingleSignOnEnvUserNameKey')]).first
        session[:user_id] = auth_user.id
        return auth_user
      else
        return nil
      end
    end
  end  
  
  def go_to_setup_if_new_installation
    if Rails.env != 'test'
      if GemeinschaftSetup.all.count == 0
        redirect_to new_gemeinschaft_setup_path
      end
    end
  end
  
  def home_breadcrumb
    if current_user
      if current_user && Tenant.find(current_user.current_tenant_id)
        add_breadcrumb( current_user.current_tenant, tenant_path(current_user.current_tenant) )
      else
        add_breadcrumb I18n.t('pages.controller.index.name'), :root_path
      end
    end
  end

  def set_locale
    if current_user && Language.find(current_user.language_id)
      I18n.locale = current_user.language.code.downcase
    else
      logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
      I18n.locale = request.compatible_language_from(Language.all.map{|x| x.code})
    end
    logger.debug "* Locale set to '#{I18n.locale}'"
  end
  
end