Sha256: 5cdc92b9fe236d58f71c9b4372b28059eba333e3586ee2695c02b825e391e41e

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

module GDS
  module SSO
    module ControllerMethods
      class PermissionDeniedException < StandardError
      end

      def self.included(base)
        base.rescue_from PermissionDeniedException do |e|
          render "authorisations/unauthorised", layout: "unauthorised", status: :forbidden, locals: { message: e.message }
        end
        base.helper_method :user_signed_in?
        base.helper_method :current_user
      end


      def authorise_user!(permission)
        # Ensure that we're authenticated (and by extension that current_user is set).
        # Otherwise current_user might be nil, and we'd error out
        authenticate_user!

        if not current_user.has_permission?(permission)
          raise PermissionDeniedException, "Sorry, you don't seem to have the #{permission} permission for this app."
        end
      end

      def require_signin_permission!
        authorise_user!('signin')
      rescue PermissionDeniedException
        render "authorisations/cant_signin", layout: "unauthorised", status: :forbidden
      end

      def authenticate_user!
        warden.authenticate!
      end

      def user_remotely_signed_out?
        warden && warden.authenticated? && warden.user.remotely_signed_out?
      end

      def user_signed_in?
        warden && warden.authenticated? && ! warden.user.remotely_signed_out?
      end

      def current_user
        warden.user if user_signed_in?
      end

      def logout
        warden.logout
      end

      def warden
        request.env['warden']
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
gds-sso-13.0.0 lib/gds-sso/controller_methods.rb
gds-sso-12.1.0 lib/gds-sso/controller_methods.rb
gds-sso-12.0.0 lib/gds-sso/controller_methods.rb
gds-sso-11.2.1 lib/gds-sso/controller_methods.rb
gds-sso-11.2.0 lib/gds-sso/controller_methods.rb
gds-sso-11.1.0 lib/gds-sso/controller_methods.rb
gds-sso-11.0.0 lib/gds-sso/controller_methods.rb
gds-sso-10.1.0 lib/gds-sso/controller_methods.rb
gds-sso-10.0.1 lib/gds-sso/controller_methods.rb
gds-sso-10.0.0 lib/gds-sso/controller_methods.rb
gds-sso-9.4.0 lib/gds-sso/controller_methods.rb
gds-sso-9.3.0 lib/gds-sso/controller_methods.rb
gds-sso-9.2.7 lib/gds-sso/controller_methods.rb
gds-sso-9.2.6 lib/gds-sso/controller_methods.rb
gds-sso-9.2.5 lib/gds-sso/controller_methods.rb
gds-sso-9.2.4 lib/gds-sso/controller_methods.rb
gds-sso-9.2.3 lib/gds-sso/controller_methods.rb
gds-sso-9.2.2 lib/gds-sso/controller_methods.rb
gds-sso-9.2.1 lib/gds-sso/controller_methods.rb
gds-sso-9.2.0 lib/gds-sso/controller_methods.rb