Sha256: 8e64f32a58e1b8e033b74d2b85857e34083f0dd197806f260183a0a9bf51c376

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

require 'active_support/concern'

module GDS
  module SSO
    module User
      extend ActiveSupport::Concern

      def self.below_rails_4?
        Gem.loaded_specs['rails'] && Gem.loaded_specs['rails'].version < Gem::Version.new("4.0")
      end

      included do
        if GDS::SSO::User.below_rails_4? && respond_to?(:attr_accessible)
          attr_accessible :uid, :email, :name, :permissions, :organisation_slug, as: :oauth
        end
      end

      def has_permission?(permission)
        if permissions
          permissions.include?(permission)
        end
      end

      def self.user_params_from_auth_hash(auth_hash)
        {
          'uid'           => auth_hash['uid'],
          'email'         => auth_hash['info']['email'],
          'name'          => auth_hash['info']['name'],
          'permissions'   => auth_hash['extra']['user']['permissions'],
          'organisation_slug'  => auth_hash['extra']['user']['organisation_slug'],
        }
      end

      def clear_remotely_signed_out!
        self.update_attribute(:remotely_signed_out, false)
      end

      def set_remotely_signed_out!
        self.update_attribute(:remotely_signed_out, true)
      end

      module ClassMethods
        def find_for_gds_oauth(auth_hash)
          user_params = GDS::SSO::User.user_params_from_auth_hash(auth_hash.to_hash)
          user = self.where(:uid => user_params['uid']).first ||
                 self.where(:email => user_params['email']).first

          if user
            if GDS::SSO::User.below_rails_4?
              user.update_attributes(user_params, as: :oauth)
            else
              user.update_attributes(user_params)
            end
            user
          else # Create a new user.
            if GDS::SSO::User.below_rails_4?
              self.create!(user_params, as: :oauth)
            else
              self.create!(user_params)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gds-sso-9.4.0 lib/gds-sso/user.rb
gds-sso-9.3.0 lib/gds-sso/user.rb
gds-sso-9.2.7 lib/gds-sso/user.rb
gds-sso-9.2.6 lib/gds-sso/user.rb
gds-sso-9.2.5 lib/gds-sso/user.rb