<commit_before>#
#   Copyright 2012 Red Hat, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

class UserGroup < ActiveRecord::Base
  class << self
    include CommonFilterMethods
  end
  # name will correspond to the group name if we're using LDAP, otherwise it's
  # entered by the admin creating the group

  # members association is only maintained for local groups
  has_and_belongs_to_many :members, :join_table => "members_user_groups",
                                    :class_name => "User",
                                    :association_foreign_key => "member_id"
  has_one :entity, :as => :entity_target, :dependent => :destroy

  MEMBERSHIP_SOURCE_LDAP = "LDAP"
  MEMBERSHIP_SOURCE_LOCAL = "local"
  MEMBERSHIP_SOURCES = [MEMBERSHIP_SOURCE_LOCAL, MEMBERSHIP_SOURCE_LDAP]

  validates_presence_of :name
  # scope name by membership_source to prevent errors if users are later added
  # to external ldap groups that have the same name as local groups
  validates_uniqueness_of :name, :scope => :membership_source

  validates_presence_of :membership_source
  validates_inclusion_of :membership_source, :in => MEMBERSHIP_SOURCES
  after_save :update_entity

  PRESET_FILTERS_OPTIONS = [
    {:title => "user_groups.preset_filters.local", :id => "local_user_groups", :query => where("user_groups.membership_source = ?", MEMBERSHIP_SOURCE_LOCAL)},
    {:title => "user_groups.preset_filters.ldap", :id => "ldap_user_groups", :query => where("user_groups.membership_source = ?", MEMBERSHIP_SOURCE_LDAP)}
  ]

  def update_entity
    self.entity = Entity.new(:entity_target => self) unless self.entity
    self.entity.name = "#{self.name} (#{self.membership_source})"
    self.entity.save!
  end

  def self.local_groups_active?
    SETTINGS_CONFIG[:groups][:enable_local]
  end

  def self.ldap_groups_active?
    SETTINGS_CONFIG[:groups][:enable_ldap] &&<commit_msg>Add length validations for UserGroup attributes :name,:description.<commit_after>#
#   Copyright 2012 Red Hat, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

class UserGroup < ActiveRecord::Base
  class << self
    include CommonFilterMethods
  end
  # name will correspond to the group name if we're using LDAP, otherwise it's
  # entered by the admin creating the group

  # members association is only maintained for local groups
  has_and_belongs_to_many :members, :join_table => "members_user_groups",
                                    :class_name => "User",
                                    :association_foreign_key => "member_id"
  has_one :entity, :as => :entity_target, :dependent => :destroy

  MEMBERSHIP_SOURCE_LDAP = "LDAP"
  MEMBERSHIP_SOURCE_LOCAL = "local"
  MEMBERSHIP_SOURCES = [MEMBERSHIP_SOURCE_LOCAL, MEMBERSHIP_SOURCE_LDAP]

  validates_presence_of :name
  validates :name, :description, :length => { :maximum   => 255 }
  # scope name by membership_source to prevent errors if users are later added
  # to external ldap groups that have the same name as local groups
  validates_uniqueness_of :name, :scope => :membership_source

  validates_presence_of :membership_source
  validates_inclusion_of :membership_source, :in => MEMBERSHIP_SOURCES
  after_save :update_entity

  PRESET_FILTERS_OPTIONS = [
    {:title => "user_groups.preset_filters.local", :id => "local_user_groups", :query => where("user_groups.membership_source = ?", MEMBERSHIP_SOURCE_LOCAL)},
    {:title => "user_groups.preset_filters.ldap", :id => "ldap_user_groups", :query => where("user_groups.membership_source = ?", MEMBERSHIP_SOURCE_LDAP)}
  ]

  def update_entity
    self.entity = Entity.new(:entity_target => self) unless self.entity
    self.entity.name = "#{self.name} (#{self.membership_source})"
    self.entity.save!
  end

  def self.local_groups_active?
    SETTINGS_CONFIG[:groups][:enable_local]
  end

  def self.ldap_groups_active?
    SETTINGS_CONFIG[:groups][:enable_ldap] &&
{'fraction_non_alphanumeric': 0.09024390243902439, 'fraction_numerical': 0.003861788617886179, 'mean_word_length': 4.06275720164609, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4094685', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1470, 'n_words': 438}