SystemDataBackend.rb 1.55 KB
require 'User'
require 'Group'
require 'Site'
require 'MailAliasRole'
require 'MailAliasPerson'
require 'MailAccount'

class SystemDataBackend

  def initialize(backend)
    @backend = backend
    @data    = Hash.new
  end

  def users
    load(:User)
  end

  def groups
    load(:Group)
  end

  def sites
    load(:Site)
  end

  def mailAliasRoles
    load(:MailAliasRole)
  end

  def mailAliasPeople
    load(:MailAliasPerson)
  end

  def mailAccounts
    load(:MailAccount)
  end

  def userByName(name)
    users.find{|user| user.name == name}
  end

  def groupByName(name)
    groups.find{|group| group.name == name}
  end

  def siteByName(name)
    sites.find{|site| site.name == name}
  end

  def usersInGroup(group)
    case
    when group.members.instance_of?(Array):
      Hash[group.members.map{|uid| [uid, userByName(uid)] if userByName(uid)}]
    when goup.members.nil?:
      {}
    else
      {group.members => userByName(group.members)} if userByName(group.members)
    end
  end

  def mailAliasRolesBySite(site)
    mailAliasRoles.find_all{|mail| mail.site == site.name}
  end

  def mailAliasPeopleBySite(site)
    mailAliasPeople.find_all{|mail| mail.site == site.name}
  end

  def mailAccountsBySite(site)
    mailAccounts.find_all{|mail| mail.site == site.name}
  end

  private

  def load(kind)
    load!(kind) if ! @data[kind]
    @data[kind]
  end

  def load!(kind)
    @data[kind] = Array.new
    @backend.load(kind) do |data|
      data[:backend] = @backend
      @data[kind].push(
        eval(kind.to_s + '.new(data)')
      )
    end
  end

end