<commit_before># A permission is a single collection of actions, controllers, and views that
# allow a user to gain access to a feature in the application.
class Permission < ActiveRecord::Base
  belongs_to              :controller
  has_and_belongs_to_many :roles
  
  validates_presence_of   :controller_id
  validates_length_of     :action,
                            :minimum => 1,
                            :if => :action?
  validates_uniqueness_of :action,
                            :scope => :controller_id
  
  class << self
    # Is there a permission that exists which restricts the given url?
    def restricts?(options = '')
      controller_path, action = Controller.recognize_path(options)
      !Permission.find(:first, :include => :controller, :conditions => ['path = ? AND (action IS NULL OR action = ?)', controller_path, action]).nil?
    end
  end
end<commit_msg>Add Permission.authorized_for.<commit_after># A permission is a single collection of actions, controllers, and views that
# allow a user to gain access to a feature in the application.
class Permission < ActiveRecord::Base
  belongs_to              :controller
  has_and_belongs_to_many :roles
  
  validates_presence_of   :controller_id
  validates_length_of     :action,
                            :minimum => 1,
                            :if => :action?
  validates_uniqueness_of :action,
                            :scope => :controller_id
  
  class << self
    # Is there a permission that exists which restricts the given url?
    def restricts?(options = '')
      controller_path, action = Controller.recognize_path(options)
      !Permission.find(:first, :include => :controller, :conditions => ['path = ? AND (action IS NULL OR action = ?)', controller_path, action]).nil?
    end
    
    # Finds all roles that are authorized for the given url
    def authorized_for(options = '')
      controller_path, action = Controller.recognize_path(options)
      controller = Controller.new(:path => controller_path)
      
      find(:all,
        :include => :controller,
        :conditions => ['path IN (?) AND (action IS NULL OR action = ?)', controller.possible_path_matches, action]
      )
    end
  end
end
{'fraction_non_alphanumeric': 0.0877431026684758, 'fraction_numerical': 0.0009045680687471732, 'mean_word_length': 2.662251655629139, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, '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': '5736260', 'n_tokens_mistral': 592, 'n_tokens_neox': 582, 'n_words': 189}