<commit_before># SysMO: lib/authorization.rb
# Code taken from myExperiment and adopted for SysMO requirements.
# Greatly simplified 22/7/2010
# **********************************************************************************
# * myExperiment: lib/is_authorized.rb
# *
# * Copyright (c) 2007 University of Manchester and the University of Southampton.
# * See license.txt for details.
# **********************************************************************************

module Seek
  module Permissions
    module Authorization
      def self.authorization_supported?(thing)
        !thing.nil? && thing.authorization_supported?
      end

      def self.is_authorized?(action, thing, user = nil)
        authorized_as_creator?(action, thing, user) ||
          authorized_by_policy?(action, thing) ||
          authorized_by_permission?(action, thing, user)
      end

      private

      def self.authorized_as_creator?(action, thing, user = nil)
        if user
          # Is the uploader?
          if thing.contributor == user || thing.contributor == user.person
            return true
          # Is a creator?
          elsif thing.is_downloadable? && thing.creators.include?(user.person) &&
              access_type_allows_action?(action, Policy::EDITING)
            return true
          end
        end
<commit_msg>Extra protection vs. nil user<commit_after># SysMO: lib/authorization.rb
# Code taken from myExperiment and adopted for SysMO requirements.
# Greatly simplified 22/7/2010
# **********************************************************************************
# * myExperiment: lib/is_authorized.rb
# *
# * Copyright (c) 2007 University of Manchester and the University of Southampton.
# * See license.txt for details.
# **********************************************************************************

module Seek
  module Permissions
    module Authorization
      def self.authorization_supported?(thing)
        !thing.nil? && thing.authorization_supported?
      end

      def self.is_authorized?(action, thing, user = nil)
        authorized_as_creator?(action, thing, user) ||
          authorized_by_policy?(action, thing) ||
          authorized_by_permission?(action, thing, user)
      end

      private

      def self.authorized_as_creator?(action, thing, user = nil)
        if user
          # Is the uploader?
          if thing.contributor && (thing.contributor == user || thing.contributor == user.person)
            return true
          # Is a creator?
          elsif thing.is_downloadable? && user.person && thing.creators.include?(user.person) &&
              access_type_allows_action?(action, Policy::EDITING)
            return true
          end
        end

{'fraction_non_alphanumeric': 0.20036764705882354, 'fraction_numerical': 0.008088235294117648, 'mean_word_length': 3.1605504587155964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '958904', 'n_tokens_mistral': 714, 'n_tokens_neox': 680, 'n_words': 180}