<commit_before># -*- coding: utf-8 -*-
require 'wiki/utils'
require 'wiki/extensions'

module Wiki
  class User
    attr_reader :name, :groups
    attr_accessor :email

    def anonymous?
      @groups.include? 'anonymous'
    end

    def initialize(name, email, groups = [])
      @name = name
      @email = email
      @groups = groups || []
      @groups << 'user' if !anonymous?
    end

    def change_password(oldpassword, password, confirm)
      User.validate_password(password, confirm)
      User.service.change_password(self, oldpassword, password)
    end

    def to_git_user
      Gitrb::User.new(name, email)
    end

    def modify(&block)
      copy = dup
      block.call(copy)
      validate
      User.service.update(copy)
      instance_variables.each do |name|
        instance_variable_set(name, copy.instance_variable_get(name))
      end
    end

    def validate
      forbid(:invalid_email.t => email !~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
             :invalid_name.t  => name !~ /[\w.\-+_]+/)
    end

    @services = {}

    class<< self
      def validate_password(password, confirm)
        forbid(:passwords_do_not_match.t => password != confirm,
               :empty_password.t => password.blank?)
      end

      def define_service(name, &block)
        service = Class.new
        service.class_eval(&block)
        @services[name.to_s] = service
      end

      lazy_reader :service do
        serv = @services[Config.auth.service]
        raise(RuntimeError, "Authentication service #{Config.auth.service} not found") if !serv
        serv.new
      end

      def anonymous(request)
        ip = request.ip || 'unknown-ip'
        name = request.env['rack.hostbyip'] ? "#{request.env['rack.hostbyip']} (#{ip})" : ip
        new(name, "anonymous@#{ip}", %w(anonymous))
      end
<commit_msg>authentication service doesn't have to implement all methods. only authenticate() should be mandatory.<commit_after># -*- coding: utf-8 -*-
require 'wiki/utils'
require 'wiki/extensions'

module Wiki
  class User
    attr_reader :name, :groups
    attr_accessor :email

    def anonymous?
      @groups.include? 'anonymous'
    end

    def initialize(name, email, groups = [])
      @name = name
      @email = email
      @groups = groups || []
      @groups << 'user' if !anonymous?
    end

    def change_password(oldpassword, password, confirm)
      User.validate_password(password, confirm)
      User.service.change_password(self, oldpassword, password)
    end

    def to_git_user
      Gitrb::User.new(name, email)
    end

    def modify(&block)
      copy = dup
      block.call(copy)
      validate
      User.service.update(copy)
      instance_variables.each do |name|
        instance_variable_set(name, copy.instance_variable_get(name))
      end
    end

    def validate
      forbid(:invalid_email.t => email !~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
             :invalid_name.t  => name !~ /[\w.\-+_]+/)
    end

    @services = {}

    class NullService
      def method_missing(name, *args)
        raise StandardError, "Authentication service does not support #{name}"
      end
    end

    class<< self
      def validate_password(password, confirm)
        forbid(:passwords_do_not_match.t => password != confirm,
               :empty_password.t => password.blank?)
      end

      def define_service(name, &block)
        service = Class.new(NullService)
        service.class_eval(&block)
        @services[name.to_s] = service
      end

      lazy_reader :service do
        serv = @services[Config.auth.service]
        raise(RuntimeError, "Authentication service #{Config.auth.service} not found") if !serv
        serv.new
      end

      def anonymous(request)
        ip = request.ip || 'unknown-ip'
        name = request.env['rack.hostbyip'] ? "#{request.env['rack.hostbyip']} (#{ip})" : ip
        new(name, "anonymous@#{ip}", %w(anonymous))
      end

{'fraction_non_alphanumeric': 0.13392405063291138, 'fraction_numerical': 0.0020253164556962027, 'mean_word_length': 2.481057268722467, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5784212', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1213, 'n_words': 275}