Passed
Push — master ( c31cf1...d3eb06 )
by Jesus
03:56
created

ApplicationHelper.valid_url?   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
require 'bbb_api'
20
require 'uri'
21
require 'i18n/language/mapping'
22
23
module ApplicationHelper
24
  include MeetingsHelper
25
  include BbbApi
26
  include I18n::Language::Mapping
27
28
  # Gets all configured omniauth providers.
29
  def configured_providers
30
    Rails.configuration.providers.select do |provider|
31
      Rails.configuration.send("omniauth_#{provider}")
32
    end
33
  end
34
35
  # Determines which providers can show a login button in the login modal.
36
  def iconset_providers
37
    providers = configured_providers & [:google, :twitter, :office365, :ldap]
38
39
    providers.delete(:twitter) if session[:old_twitter_user_id]
40
41
    providers
42
  end
43
44
  # Generates the login URL for a specific provider.
45
  def omniauth_login_url(provider)
46
    if provider == :ldap
47
      ldap_signin_path
48
    else
49
      "#{Rails.configuration.relative_url_root}/auth/#{provider}"
50
    end
51
  end
52
53
  # Determine if Greenlight is configured to allow user signups.
54
  def allow_user_signup?
55
    Rails.configuration.allow_user_signup
56
  end
57
58
  # Determines if the BigBlueButton endpoint is the default.
59
  def bigbluebutton_endpoint_default?
60
    Rails.configuration.bigbluebutton_endpoint_default == Rails.configuration.bigbluebutton_endpoint
61
  end
62
63
  # Returns language selection options
64
  def language_options
65
    locales = I18n.available_locales
66
    language_opts = [['<<<< ' + t("language_default") + ' >>>>', "default"]]
67
    locales.each do |locale|
68
      language_mapping = I18n::Language::Mapping.language_mapping_list[locale.to_s.gsub("_", "-")]
69
      language_opts.push([language_mapping["nativeName"], locale.to_s])
70
    end
71
    language_opts.sort
72
  end
73
74
  # Parses markdown for rendering.
75
  def markdown(text)
76
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
77
      no_intra_emphasis: true,
78
      fenced_code_blocks: true,
79
      disable_indented_code_blocks: true,
80
      autolink: true,
81
      tables: true,
82
      underline: true,
83
      highlight: true)
84
85
    markdown.render(text).html_safe
86
  end
87
88
  def allow_greenlight_accounts?
89
    return Rails.configuration.allow_user_signup unless Rails.configuration.loadbalanced_configuration
90
    return false unless @user_domain && !@user_domain.empty? && Rails.configuration.allow_user_signup
91
    return false if @user_domain == "greenlight"
92
    # Proceed with retrieving the provider info
93
    begin
94
      provider_info = retrieve_provider_info(@user_domain, 'api2', 'getUserGreenlightCredentials')
95
      provider_info['provider'] == 'greenlight'
96
    rescue => e
97
      logger.info e
98
      false
99
    end
100
  end
101
102
  # Return all the translations available in the client side through javascript
103
  def current_translations
104
    @translations ||= I18n.backend.send(:translations)
105
    @translations[I18n.locale].with_indifferent_access[:javascript] || {}
106
  end
107
108
  # Returns the page that the logo redirects to when clicked on
109
  def home_page
110
    return root_path unless current_user
111
    return admins_path if current_user.has_role? :super_admin
112
    current_user.main_room
113
  end
114
115
  def role_colour(role)
116
    role.colour || Rails.configuration.primary_color_default
117
  end
118
119
  def translated_role_name(role)
120
    if role.name == "denied"
121
      I18n.t("roles.banned")
122
    elsif role.name == "pending"
123
      I18n.t("roles.pending")
124
    elsif role.name == "admin"
125
      I18n.t("roles.admin")
126
    elsif role.name == "user"
127
      I18n.t("roles.user")
128
    else
129
      role.name
130
    end
131
  end
132
133
  def can_reset_password
134
    # Check if admin is editting user and user is a greenlight account
135
    Rails.configuration.enable_email_verification &&
136
      Rails.application.routes.recognize_path(request.env['PATH_INFO'])[:action] == "edit_user" &&
137
      @user.greenlight_account?
138
  end
139
140
  def google_analytics_url
141
    "https://www.googletagmanager.com/gtag/js?id=#{ENV['GOOGLE_ANALYTICS_TRACKING_ID']}"
142
  end
143
144
  def valid_url?(input)
145
    uri = URI.parse(input)
146
    !uri.host.nil?
147
  rescue URI::InvalidURIError
148
    false
149
  end
150
end
151