<commit_before>module Rbac
  class Authorizer
    include Vmdb::Logging

    def self.role_allows(*args)
      new.role_allows(*args)
    end

    def initialize

    end

    def role_allows(user:, feature:, any: nil)
      auth = any.present? ? user.role_allows_any?(:identifiers => [feature]) : user.role_allows?(:identifier => feature)
      _log.debug("Auth #{auth ? "successful" : "failed"} for user '#{user.userid}', role '#{user.miq_user_role.try(:name)}', feature identifier '#{feature}'")

      auth
    end

    def user_role_allows?(user, options = {})
      return false if user.miq_user_role.nil?
      return true if user.miq_user_role.allows?(options)

      ident = options[:identifier]
      parent = MiqProductFeature.feature_parent(ident)
      return false if parent.nil?

      if MiqProductFeature.feature_hidden(ident)
        # return true for common features that are hidden and are under hidden parent
        # return true if any visible siblings are entitled
        if MiqProductFeature.feature_hidden(parent)
          true
        else
          user.miq_user_role.allows_any?(:identifiers => MiqProductFeature.feature_children(parent))
        end
      end
    end

    def user_role_allows_any?(user, options = {})
      binding.pry if ENV["LOLZ"]
      return false if user.miq_user_role.nil?
      user.miq_user_role.allows_any?(options)<commit_msg>Use legacy options hash signature<commit_after>module Rbac
  class Authorizer
    include Vmdb::Logging

    def self.role_allows(*args)
      new.role_allows(*args)
    end

    def initialize

    end

    def role_allows(options = {})
      # Known options:
      user    = options[:user]
      feature = options[:feature]
      any     = options[:any]

      auth = if any.present?
               user.role_allows_any?(:identifiers => [feature])
             else
               user.role_allows?(:identifier => feature)
             end
      _log.debug("Auth #{auth ? "successful" : "failed"} for user '#{user.userid}', role '#{user.miq_user_role.try(:name)}', feature identifier '#{feature}'")

      auth
    end

    def user_role_allows?(user, options = {})
      return false if user.miq_user_role.nil?
      return true if user.miq_user_role.allows?(options)

      ident = options[:identifier]
      parent = MiqProductFeature.feature_parent(ident)
      return false if parent.nil?

      if MiqProductFeature.feature_hidden(ident)
        # return true for common features that are hidden and are under hidden parent
        # return true if any visible siblings are entitled
        if MiqProductFeature.feature_hidden(parent)
          true
        else
          user.miq_user_role.allows_any?(:identifiers => MiqProductFeature.feature_children(parent))
        end
      end
    end

    def user_role_allows_any?(user, options = {})
      binding.pry if ENV["LOLZ"]
      return false if user.miq_user_role.nil?
      user.miq_user_role.allows_any?(options)
{'fraction_non_alphanumeric': 0.09919028340080972, 'fraction_numerical': 0.0, 'mean_word_length': 2.825806451612903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3790397', 'n_tokens_mistral': 899, 'n_tokens_neox': 874, 'n_words': 211}