<commit_before>#--
#   Copyright (C) 2012-2014 Gitorious AS
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

module AuthenticatedSystem
  protected
    # Returns true or false if the user is logged in.
    # Preloads @current_user with the user model if they're logged in.
    def logged_in?
      !!current_user
    end

    # Accesses the current user from the session.
    def current_user
      return @current_user if instance_variable_defined?(:@current_user)
      @current_user = login_from_session || login_from_basic_auth || login_from_cookie
    end

    # Store the given user in the session.
    def current_user=(user)
      if user
        session[:user_id] = user.id
        set_varnish_auth_cookie
      else
        session.delete(:user_id)
        cookies.delete(:auth_token)
        clear_varnish_auth_cookie
      end

      @current_user = user
    end

    def set_varnish_auth_cookie
      cookies["_logged_in"] = {
        :value => "true",
        :domain => ".#{Gitorious.host}",
        :expires => 3.weeks.from_now,
        :httponly => true,
        :secure => true
      }
    end

    def clear_varnish_auth_cookie
      cookies.delete "_logged_in", :domain => ".#{Gitorious.host}"
    end

    # Check if the user is authorized
    #
    # Override this method in your controllers if you want to restrict access
    # to only a few actions or if you want to check if the user
    # has the correct rights.
    #
    # Example:
    #
    #  # only allow nonbobs
    #  def authorized?
    #    current_user.login != "bob"
    #  end
    def authorized?
      logged_in?
    end

    # Filter method to enforce a login requirement.
    #
    # To require logins for all actions, use this in your controllers:
    #
    #   before_filter :login_required
    #
    # To require logins for specific actions, use this in your controllers:
    #
    #   before_filter :login_required, :only => [ :edit, :update ]
    #
    # To skip this in a subclassed controller:
    #
    #   skip_before_filter :login_required
    #
    def login_required
      authorized? || access_denied
    end

    # Redirect as appropriate when an access request fails.
    #
    # The default action is to redirect to the login screen.
    #
    # Override this method in your controllers if you want to have special
    # behavior in case the user is not authorized
    # to access the requested action.  For example, a popup window might
    # simply close itself.
    def access_denied
      respond_to do |accepts|
        accepts.html do
          store_location
          flash[:error] = "Action requires login"
          redirect_to(main_app.new_sessions_path)
        end
        accepts.xml do
          headers["Status"]           = "Unauthorized"
          headers["WWW-Authenticate"] = %(Basic realm="Web Password")
          render :text => "Could't authenticate you", :status => '401 Unauthorized'
        end
      end
      false
    end

    # Store the URI of the current request in the session.
    #
    # We can return to this location by calling #redirect_back_or_default.
    def store_location(location = request.fullpath)
      session[:return_to] = location
    end

    # Redirect to the URI stored by the most recent store_location call or
    # to the passed default.
    def redirect_back_or_default(default)
      session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default)
      session[:return_to] = nil
    end

    # Inclusion hook to make #current_user and #logged_in?
    # available as ActionView helper methods.
    def self.included(base)
      if base.respond_to?(:helper_method)
        base.send(:helper_method, :current_user, :logged_in?)
      end<commit_msg>Request basic auth when auth required for all formats except html<commit_after>#--
#   Copyright (C) 2012-2014 Gitorious AS
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

module AuthenticatedSystem
  protected
    # Returns true or false if the user is logged in.
    # Preloads @current_user with the user model if they're logged in.
    def logged_in?
      !!current_user
    end

    # Accesses the current user from the session.
    def current_user
      return @current_user if instance_variable_defined?(:@current_user)
      @current_user = login_from_session || login_from_basic_auth || login_from_cookie
    end

    # Store the given user in the session.
    def current_user=(user)
      if user
        session[:user_id] = user.id
        set_varnish_auth_cookie
      else
        session.delete(:user_id)
        cookies.delete(:auth_token)
        clear_varnish_auth_cookie
      end

      @current_user = user
    end

    def set_varnish_auth_cookie
      cookies["_logged_in"] = {
        :value => "true",
        :domain => ".#{Gitorious.host}",
        :expires => 3.weeks.from_now,
        :httponly => true,
        :secure => true
      }
    end

    def clear_varnish_auth_cookie
      cookies.delete "_logged_in", :domain => ".#{Gitorious.host}"
    end

    # Check if the user is authorized
    #
    # Override this method in your controllers if you want to restrict access
    # to only a few actions or if you want to check if the user
    # has the correct rights.
    #
    # Example:
    #
    #  # only allow nonbobs
    #  def authorized?
    #    current_user.login != "bob"
    #  end
    def authorized?
      logged_in?
    end

    # Filter method to enforce a login requirement.
    #
    # To require logins for all actions, use this in your controllers:
    #
    #   before_filter :login_required
    #
    # To require logins for specific actions, use this in your controllers:
    #
    #   before_filter :login_required, :only => [ :edit, :update ]
    #
    # To skip this in a subclassed controller:
    #
    #   skip_before_filter :login_required
    #
    def login_required
      authorized? || access_denied
    end

    # Redirect as appropriate when an access request fails.
    #
    # The default action is to redirect to the login screen.
    #
    # Override this method in your controllers if you want to have special
    # behavior in case the user is not authorized
    # to access the requested action.  For example, a popup window might
    # simply close itself.
    def access_denied
      respond_to do |accepts|
        accepts.html do
          store_location
          flash[:error] = "Action requires login"
          redirect_to(main_app.new_sessions_path)
        end
        accepts.all do # this is "catch all" for all formats not specified above
          request_http_basic_authentication("Gitorious")
          self.response_body = ""
        end
      end
      false
    end

    # Store the URI of the current request in the session.
    #
    # We can return to this location by calling #redirect_back_or_default.
    def store_location(location = request.fullpath)
      session[:return_to] = location
    end

    # Redirect to the URI stored by the most recent store_location call or
    # to the passed default.
    def redirect_back_or_default(default)
      session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default)
      session[:return_to] = nil
    end

    # Inclusion hook to make #current_user and #logged_in?
    # available as ActionView helper methods.
    def self.included(base)
      if base.respond_to?(:helper_method)
        base.send(:helper_method, :current_user, :logged_in?)
      end
{'fraction_non_alphanumeric': 0.07024793388429752, 'fraction_numerical': 0.002640036730945822, 'mean_word_length': 2.6123548922056385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7431672', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2444, 'n_words': 907}