Skip to content
Snippets Groups Projects
Select Git revision
  • 7ebe434a73aebfd4ac4af1a2ff02bc978d2a1f13
  • master default
  • uiUpdates
  • 1.0.0
4 results

app.rb

Blame
  • user avatar
    Tyler Lemburg authored
    7ebe434a
    History
    app.rb 3.75 KiB
    require 'sinatra'
    require 'rest-client'
    require 'rack/session/abstract/id'
    require 'rack/cas'
    require 'rack-cas'
    require 'rack-cas/session_store/active_record'
    require 'models/user'
    require 'models/service_space'
    
    module Rack
      module Session
        class RackCASActiveRecordStore < Rack::Session::Abstract::ID
          include RackCAS::ActiveRecordStore
        end
      end
    end
    
    use Rack::Session::RackCASActiveRecordStore
    use Rack::CAS, server_url: 'https://login.unl.edu/cas',
                   session_store: RackCAS::ActiveRecordStore
    
    Time.zone = "America/Chicago"
    
    DIRECTORY_URL = 'http://directory.unl.edu/'
    
    # this gives the user messages
    def flash(type, header, message)
      session["notice"] ||= []
      session["notice"] << {
        :type => type,
        :header => header,
        :message => message
      }
    end
    
    before do
      require_login
    
      # site defaults
      @title = 'UNL Resource Scheduler'
      @breadcrumbs = [
        {
          :href => 'http://www.unl.edu/',
          :text => 'UNL',
          :title => 'University of Nebraska&ndash;Lincoln'
        },
        {
          :href => '/',
          :text => 'UNL Resource Scheduler'
        }
      ]
    end
    
    def calculate_time(date_string, hour, minute, am_pm)
      hour ||= 0
      minute ||= 0
      am_pm ||= 'am'
    
      hour = hour.to_i % 12
      hour = hour + 12 if am_pm == 'pm'
    
      date_strings = date_string.split('/')
      date_string = "#{date_strings[2]}-#{date_strings[0]}-#{date_strings[1]}"
      date = Time.parse(date_string)
      Time.new(date.year, date.month, date.day, hour, minute, 0)
    end
    
    helpers do
      def load_service_space
        url_name = params[:service_space_url_name]
        space = ServiceSpace.find_by(:url_name => url_name)
        raise Sinatra::NotFound if space.nil?
        @space = space
    
        if !@user.in_space(@space)
          flash :error, 'Unauthorized', 'Sorry, you don\'t have access to this service space.'
          redirect '/'
        end
    
        @breadcrumbs << {:text => @space.name, :href => @space.href}
      end
    
      def require_login
        if session['cas'].nil? || session['cas']['user'].nil?
          halt 401
        else
          # check if the user already exists in this app's db
          @user = User.find_by(:username => session['cas']['user'], :creation_method => 'CAS')
          if @user.nil?
            # get this user's info from UNL Directory
            RestClient.get("#{DIRECTORY_URL}?uid=#{session['cas']['user']}&format=json") do |response, request, result|
              case response.code
              when 200
                info = JSON.parse(response.body)
                # create this user
                @user = User.create(
                  username: session['cas']['user'],
                  email: info['mail'][0],
                  first_name: info['givenName'][0],
                  last_name: info['sn'][0],
                  date_created: Time.now,
                  creation_method: 'CAS',
                  is_admin: false
                )
              else
                flash :error, 'User Not Found in UNL Directory', "This user was not found in the UNL Directory."
                redirect back
              end
            end
          end
        end
      end
    end
    
    not_found do
      @breadcrumbs << {:text => 'Not Found'}
      erb 'That page was not found.', :layout => :fixed
    end
    
    error do
      @breadcrumbs << {:text => 'Error'}
      flash(:danger, 'Sorry! There was an error.', "We apologize. A really bad error occurred and it didn't work. We're fixing this as we speak.")
      erb 'In the meantime, you can <a href="/">go back to the homepage</a>.', :layout => :fixed
    end
    
    get '/' do
      @breadcrumbs << {:text => 'Home'}
      # get the service spaces that this user has access to
      spaces = @user.service_spaces
      erb :home, :layout => :fixed, :locals => {
        spaces: spaces
      }
    end
    
    get '/images/:event_id/?' do
      event = Event.find_by(:id => params[:event_id])
      if event.nil? || event.imagedata.nil?
        raise Sinatra::NotFound
      end
    
      return event.imagedata
    end
    
    Dir.glob("#{ROOT}/routes/*.rb") { |file| require file }