foreman/app/models/usergroup.rb @ 8a65dff7
9c0e127b | Paul Kelly | class Usergroup < ActiveRecord::Base
|
|
9fd7478e | Paul Kelly | include Authorization
|
|
9c0e127b | Paul Kelly | has_many_polymorphs :members, :from => [:usergroups, :users ], :as => :member,
|
|
:through => :usergroup_member, :foreign_key => :usergroup_id, :dependent => :destroy
|
|||
has_many :hosts, :as => :owner
|
|||
validates_uniqueness_of :name
|
|||
before_destroy Ensure_not_used_by.new(:hosts, :usergroups)
|
|||
# The text item to see in a select dropdown menu
|
|||
81e2d3f3 | Ohad Levy | alias_attribute :select_title, :to_s
|
|
5410421d | Ohad Levy | default_scope :order => 'LOWER(usergroups.name)'
|
|
9c0e127b | Paul Kelly | ||
# This methods retrieves all user addresses in a usergroup
|
|||
# Returns: Array of strings representing the user's email addresses
|
|||
def recipients
|
|||
all_users.map(&:mail).flatten.sort.uniq
|
|||
end
|
|||
# This methods retrieves all users in a usergroup
|
|||
# Returns: Array of users
|
|||
def all_users(group_list=[self], user_list=[])
|
|||
retrieve_users_and_groups group_list, user_list
|
|||
user_list.sort.uniq
|
|||
end
|
|||
# This methods retrieves all usergroups in a usergroup
|
|||
9fd7478e | Paul Kelly | # Returns: Array of unique usergroups
|
|
9c0e127b | Paul Kelly | def all_usergroups(group_list=[self], user_list=[])
|
|
retrieve_users_and_groups group_list, user_list
|
|||
group_list.sort.uniq
|
|||
end
|
|||
protected
|
|||
# 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
|
|||
def validate
|
|||
if User.all.map(&:login).include?(self.name)
|
|||
errors.add :name, "is already used by a user account"
|
|||
end
|
|||
end
|
|||
215177dc | Marcello de Sousa | def as_json(options={})
|
|
super({:only => [:name, :id]}.merge(options))
|
|||
end
|
|||
9c0e127b | Paul Kelly | end
|