<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2004-2007, Fred Steinberg, Brown Bear Software

package UserGroup;
use Calendar::MasterDB;
use Calendar::LDAPGroup;

sub new {
    my ($class, %args) = @_;
    my $self = {name        =&gt; '',
                description =&gt; '',
                %args};
    bless $self, $class;
}

sub name {
    my $self = shift;
    $self-&gt;{name} = shift if (@_);
    return $self-&gt;{name};
}
sub description {
    my $self = shift;
    $self-&gt;{description} = shift if (@_);
    return $self-&gt;{description};
}

sub id {
    my $self = shift;
    $self-&gt;{id} = shift if (@_);
    return $self-&gt;{id};
}

sub getGroup {
    my ($class, $id) = @_;
    return unless defined $id;
    my @groups = MasterDB-&gt;getUserGroups;
    foreach (@groups) {
        return $_ if ($id eq $_-&gt;id);
    }
    return undef;
}

sub getAll {
    my $class = shift;

    # Get all from LDAP
    my @ldapGroups = map {$class-&gt;new (name =&gt; $_,
                                       id   =&gt; "LDAP $_")}
                         LDAPGroup-&gt;getGroupNames;

    return (@ldapGroups, MasterDB-&gt;getUserGroups);
}

sub getByName {
    my ($class, $name) = @_;
    return unless defined $name;
    my @groups = $class-&gt;getAll;
    foreach (@groups) {
        return $_ if ($name eq $_-&gt;name);
    }
    return undef;

}

# Return map of all groupIDs =&gt; [users]
sub getMemberMap {
    my ($class) = @_;
    require Calendar::User;
    my %h;
    foreach my $user (User-&gt;getUsers) {
        foreach my $gid ($user-&gt;groupIDs (noLDAP =&gt; 1)) {
            $h{$gid} ||= [];
            push @{$h{$gid}}, $user;
        }
    }
    # And then all LDAP groups
    foreach my $gname (LDAPGroup-&gt;getGroupNames) {
        my @members = LDAPGroup-&gt;getGroupMembers ($gname);
        my $id = "LDAP $gname";
        $h{$id} ||= [];
        push @{$h{$id}}, map {User-&gt;create (name =&gt; $_)} @members;
    }

    return \%h;
}

sub serialize {
    my $self = shift;
    join $;, %$self;
}

sub unserialize {
    my ($classname, $string) = @_;
    my @values = split $;, $string;
    push @values, '' if (int(@values/2)*2 != @values);
    $classname-&gt;new (@values);
}

1;
</pre></body></html>