foreman/app/models/usergroup.rb @ 783fb4aa
9c0e127b | Paul Kelly | class Usergroup < ActiveRecord::Base
|
|
229d7436 | Joseph Magen | audited :allow_mass_assignment => true
|
|
acfbc458 | Marek Hulan | include Authorizable
|
|
8b737c9c | Joseph Magen | extend FriendlyId
|
|
friendly_id :name
|
|||
e768c976 | Tomas Strachota | include Parameterizable::ByIdName
|
|
acfbc458 | Marek Hulan | ||
3034e8e2 | Ori Rabin | validates_lengths_from_database
|
|
e07f9a12 | Dominic Cleal | before_destroy EnsureNotUsedBy.new(:hosts), :ensure_last_admin_group_is_not_deleted
|
|
e0d618ef | Joseph Magen | ||
acfbc458 | Marek Hulan | has_many :user_roles, :dependent => :destroy, :foreign_key => 'owner_id', :conditions => {:owner_type => self.to_s}
|
|
has_many :roles, :through => :user_roles, :dependent => :destroy
|
|||
90ed6bdf | Joseph Mitchell Magen | ||
has_many :usergroup_members, :dependent => :destroy
|
|||
acfbc458 | Marek Hulan | has_many :users, :through => :usergroup_members, :source => :member, :source_type => 'User', :dependent => :destroy
|
|
has_many :usergroups, :through => :usergroup_members, :source => :member, :source_type => 'Usergroup', :dependent => :destroy
|
|||
d5953972 | Jan Pazdziora | has_many :external_usergroups, :dependent => :destroy, :inverse_of => :usergroup
|
|
acfbc458 | Marek Hulan | ||
has_many :cached_usergroup_members
|
|||
has_many :usergroup_parents, :dependent => :destroy, :foreign_key => 'member_id',
|
|||
:conditions => "member_type = 'Usergroup'", :class_name => 'UsergroupMember'
|
|||
has_many :parents, :through => :usergroup_parents, :source => :usergroup, :dependent => :destroy
|
|||
3f77babd | Joseph Mitchell Magen | has_many_hosts :as => :owner
|
|
3034e8e2 | Ori Rabin | validates :name, :uniqueness => true, :presence => true
|
|
9c0e127b | Paul Kelly | ||
# The text item to see in a select dropdown menu
|
|||
81e2d3f3 | Ohad Levy | alias_attribute :select_title, :to_s
|
|
bb3572ff | Daniel Lobato | default_scope -> { order('usergroups.name') }
|
|
scope :visible, -> { }
|
|||
1ca44bd3 | Daniel Lobato | scoped_search :on => :name, :complete_value => :true
|
|
e07f9a12 | Dominic Cleal | validate :ensure_uniq_name, :ensure_last_admin_remains_admin
|
|
9c0e127b | Paul Kelly | ||
bb3572ff | Daniel Lobato | accepts_nested_attributes_for :external_usergroups, :reject_if => ->(a) { a[:name].blank? }, :allow_destroy => true
|
|
d5953972 | Jan Pazdziora | ||
9c0e127b | Paul Kelly | # This methods retrieves all user addresses in a usergroup
|
|
# Returns: Array of strings representing the user's email addresses
|
|||
def recipients
|
|||
57efe613 | Tomer Brisker | all_users.map(&:mail).flatten.uniq.sort
|
|
9c0e127b | Paul Kelly | end
|
|
3a36bdf6 | Stephen Benjamin | def recipients_for(notification)
|
|
57efe613 | Tomer Brisker | all_users.select { |user| user if user.receives?(notification) }.uniq.sort
|
|
3a36bdf6 | Stephen Benjamin | end
|
|
9c0e127b | Paul Kelly | # This methods retrieves all users in a usergroup
|
|
# Returns: Array of users
|
|||
4f7a4d0b | David Davis | def all_users(group_list = [self], user_list = [])
|
|
9c0e127b | Paul Kelly | retrieve_users_and_groups group_list, user_list
|
|
57efe613 | Tomer Brisker | user_list.uniq.sort
|
|
9c0e127b | Paul Kelly | end
|
|
# This methods retrieves all usergroups in a usergroup
|
|||
9fd7478e | Paul Kelly | # Returns: Array of unique usergroups
|
|
4f7a4d0b | David Davis | def all_usergroups(group_list = [self], user_list = [])
|
|
9c0e127b | Paul Kelly | retrieve_users_and_groups group_list, user_list
|
|
57efe613 | Tomer Brisker | group_list.uniq.sort
|
|
9c0e127b | Paul Kelly | end
|
|
70acceb5 | Greg Sutcliffe | def expire_topbar_cache(sweeper)
|
|
users.each { |u| u.expire_topbar_cache(sweeper) }
|
|||
end
|
|||
6421fa1c | Daniel Lobato | def add_users(userlist)
|
|
2ef6f4da | Daniel Lobato | users << User.where(:lower_login => userlist.map(&:downcase))
|
|
6421fa1c | Daniel Lobato | end
|
|
def remove_users(userlist)
|
|||
2ef6f4da | Daniel Lobato | self.users = self.users - User.where(:lower_login => userlist.map(&:downcase))
|
|
6421fa1c | Daniel Lobato | end
|
|
9c0e127b | Paul Kelly | protected
|
|
abd8f1d1 | Daniel Lobato | ||
9c0e127b | Paul Kelly | # Recurses down the tree of usergroups and finds the users
|
|
# [+group_list+]: Array of Usergroups that have already been processed
|
|||
# [+users+] : Array of users accumulated at this point
|
|||
# Returns : Array of non unique users
|
|||
def retrieve_users_and_groups(group_list, user_list)
|
|||
for group in usergroups
|
|||
next if group_list.include? group
|
|||
group_list << group
|
|||
group.retrieve_users_and_groups(group_list, user_list)
|
|||
end
|
|||
user_list.concat users
|
|||
end
|
|||
017e1049 | Ohad Levy | def ensure_uniq_name
|
|
01352c23 | Bryan Kearney | errors.add :name, _("is already used by a user account") if User.where(:login => name).first
|
|
9c0e127b | Paul Kelly | end
|
|
e07f9a12 | Dominic Cleal | def ensure_last_admin_remains_admin
|
|
if !new_record? && admin_changed? && !admin && other_admins.empty?
|
|||
errors.add :admin, _("cannot be removed from the last admin account")
|
|||
logger.warn "Unable to remove admin privileges from the last admin account"
|
|||
false
|
|||
end
|
|||
end
|
|||
def ensure_last_admin_group_is_not_deleted
|
|||
if admin? && other_admins.empty?
|
|||
errors.add :base, _("Can't delete the last admin user group")
|
|||
logger.warn "Unable to delete the last admin user group"
|
|||
false
|
|||
end
|
|||
end
|
|||
def other_admins
|
|||
User.unscoped.only_admin.except_hidden - all_users
|
|||
end
|
|||
9c0e127b | Paul Kelly | end
|