<commit_before>require 'cgi'
require 'omniauth'
require 'mustache/sinatra'
require 'sinatra/base'
require 'sqlite3'

module Omnigollum
  module Views; class Layout < Mustache; end; end
  module Models
    class OmniauthUserInitError < StandardError; end

    class User
      attr_reader :uid, :name, :email, :nickname, :provider
    end

    class OmniauthUser < User
      def initialize (hash, options)
        # Validity checks, don't trust providers
        @uid = hash['uid'].to_s.strip
        raise OmniauthUserInitError, "Insufficient data from authentication provider, uid not provided or empty" if @uid.empty?

        @name = hash['info']['name'].to_s.strip if hash['info'].has_key?('name')
        @name = options[:default_name] if !@name || @name.empty?

        raise OmniauthUserInitError, "Insufficient data from authentication provider, name not provided or empty" if !@name || @name.empty?

        @email = hash['info']['email'].to_s.strip if hash['info'].has_key?('email')
        @email = options[:default_email] if !@email || @email.empty?

        raise OmniauthUserInitError, "Insufficient data from authentication provider, email not provided or empty" if !@email || @email.empty?

        @nickname = hash['info']['nickname'].to_s.strip if hash['info'].has_key?('nickname')

        @provider = hash['provider']

        self
      end
    end
  end

  module Helpers
    def user_authed?
      session.has_key? :omniauth_user
    end

    def user_has_permission?
      options = settings.send(:omnigollum)
      user = get_user
      db = SQLite3::Database.open "weaki.db"
      result = db.execute "SELECT regexp FROM Users INNER JOIN UsersRoles ON Users.email=UsersRoles.email INNER JOIN Roles ON UsersRoles.type=Roles.type WHERE Users.email = ? ;", user.email
      flag = false
      if !result.empty?
        result.each { |regexp|
          regexp = regexp.first
          r = Regexp.new regexp
          flag = (request.path =~ r)
          if flag
            break
          end
        }
        return flag
      else
        return false
      end
    end

    def user_auth
      @title   = 'Authentication is required'
      @subtext = 'Please choose a login service'
      show_login
    end

    def kick_back
      redirect !request.referrer.nil? && request.referrer !~ /#{Regexp.escape(settings.send(:omnigollum)[:route_prefix])}\/.*/ ?<commit_msg>Verificação de permissoes CRUD<commit_after>require 'cgi'
require 'omniauth'
require 'mustache/sinatra'
require 'sinatra/base'
require 'sqlite3'

module Omnigollum
  module Views; class Layout < Mustache; end; end
  module Models
    class OmniauthUserInitError < StandardError; end

    class User
      attr_reader :uid, :name, :email, :nickname, :provider
    end

    class OmniauthUser < User
      def initialize (hash, options)
        # Validity checks, don't trust providers
        @uid = hash['uid'].to_s.strip
        raise OmniauthUserInitError, "Insufficient data from authentication provider, uid not provided or empty" if @uid.empty?

        @name = hash['info']['name'].to_s.strip if hash['info'].has_key?('name')
        @name = options[:default_name] if !@name || @name.empty?

        raise OmniauthUserInitError, "Insufficient data from authentication provider, name not provided or empty" if !@name || @name.empty?

        @email = hash['info']['email'].to_s.strip if hash['info'].has_key?('email')
        @email = options[:default_email] if !@email || @email.empty?

        raise OmniauthUserInitError, "Insufficient data from authentication provider, email not provided or empty" if !@email || @email.empty?

        @nickname = hash['info']['nickname'].to_s.strip if hash['info'].has_key?('nickname')

        @provider = hash['provider']

        self
      end
    end
  end

  module Helpers
    def user_authed?
      session.has_key? :omniauth_user
    end

    def user_has_permission?
      options = settings.send(:omnigollum)
      user = get_user
      db = SQLite3::Database.open "weaki_v2.db"
      result = db.execute "select regex, crud from Users INNER JOIN UsersRoles ON Users.email=UsersRoles.email INNER JOIN Roles ON UsersRoles.role=Roles.name where Users.email = ?", user.email
      db.close
      flag = false
      if !result.empty?
        result.each { |row|
          regex_string = row[0]
          crud = row[1]
          regex = Regexp.new regex_string
          requestpath = request.path
          
          if requestpath =~ /^\/create\/.*/
            cleanpath = requestpath.slice "/create"
            flag = (cleanpath =~ regex) && crud >= 2
          elsif requestpath =~ /^\/edit\/.*/
            cleanpath = requestpath.slice "/edit"
            flag = (cleanpath =~ regex) && crud >= 1
          elsif requestpath =~ /^\/delete\/.*/
            cleanpath = requestpath.slice "/delete"
            flag = (cleanpath =~ regex) && crud >= 3
          else
            flag = (requestpath =~ regex) && crud >= 0
          end
            
          if flag
            break
          end
        }
        return flag
      else
        return false
      end
    end

    def user_auth
      @title   = 'Authentication is required'
      @subtext = 'Please choose a login service'
      show_login
    end

    def kick_back
      redirect !request.referrer.nil? && request.referrer !~ /#{Regexp.escape(settings.send(:omnigollum)[:route_prefix])}\/.*/ ?
{'fraction_non_alphanumeric': 0.11170605612998523, 'fraction_numerical': 0.0020310192023633676, 'mean_word_length': 2.5198180636777128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1154274', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1596, 'n_words': 446}