SystemDataBackend.rb 1.4 KB
require 'User'
require 'Group'
require 'Site'
require 'MailAlias'
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 mailAliases
    load(:mailAlias)
  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 mailAliasesBySite(site)
    mailAliases.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
    class_str    = kind.to_s
    class_str[0] = class_str[0..0].capitalize

    @backend.load(kind) do |data|
      @data[kind].push(
        eval(class_str + '.new(data)')
      )
    end
  end

end