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

# Permissions.pm

# set - pass username, perm level
# get - pass username
# (get|set)UserHash
# (get|set)Anonymous
# (get|set)AuthenticatedUser
# permitted - username, level

my %levelValues = (None       =&gt; 0,
                   View       =&gt; 1,
                   Add        =&gt; 2,
                   Edit       =&gt; 3,
                   Admin      =&gt; 4,
                   Administer =&gt; 4);

package Permissions;

use strict;

# Pass in a Database object, or the name of the database.
sub new {
    my $class = shift;
    my ($theArg) = @_;

    my ($self, $db);

    $db = $theArg if (ref ($theArg) &amp;&amp; $theArg-&gt;isa ('Database'));
    $db = Database-&gt;new ($theArg) unless $db;

    $self = {};
    $self-&gt;{'db'} = $db;

    bless $self, $class;
    $self;
}

# Pass username and permission level.
sub set {
    my $self = shift;
    my ($userName, $level) = @_;

    die "Someone screwed up - permission $level not recognized.\n"
        unless (defined $levelValues{$level});

    if ($userName) {
        $self-&gt;{'db'}-&gt;setPermission ($userName, $level);
    } else {
        $self-&gt;setAnonymous ($level);
    }
}

# Pass username; returns current permission level. If permission for
# a user is not specified, return greater perm of Anonymous and
# Authenticated User. Does NOT check groups.
sub get {
    my $self = shift;
    my ($userName) = @_;
    return $self-&gt;getAnonymous unless $userName;
    my ($user) = $self-&gt;{'db'}-&gt;getPermission ($userName);
    return $user if ($user &amp;&amp; $levelValues{$user}); # so 'None' doesn't count
    my ($anon, $auth) = ($self-&gt;getAnonymous, $self-&gt;getAuthenticatedUser);
    return ($levelValues{$auth} &gt; $levelValues{$anon} ? $auth : $anon);
}

sub getUserHash {
    my $self = shift;
    $self-&gt;{'db'}-&gt;getPermittedUsers || {};
}

sub setUserHash {
    my $self = shift;
    my $hashRef = shift;
    $self-&gt;{'db'}-&gt;setPermittedUsers ($hashRef);
}

sub getAnonymous {
    my $self = shift;
    $self-&gt;{'db'}-&gt;getPermission ('AnonymousUser') || 'None';
}

sub getAuthenticatedUser {
    my $self = shift;
    $self-&gt;{'db'}-&gt;getPermission ('AuthenticatedUser') || 'None';
}

sub setAnonymous {
    my $self = shift;
    my ($level) = @_;
    $self-&gt;{'db'}-&gt;setPermission ('AnonymousUser', $level);
}

sub setAuthenticatedUser {
    my $self = shift;
    my ($level) = @_;
    $self-&gt;{'db'}-&gt;setPermission ('AuthenticatedUser', $level);
}

# Pass group; returns permission level. Return 'none' if permission for
# a group is not specified.
sub getGroup {
    my ($self, $groupOrID) = @_;
    return 'None' unless defined $groupOrID;
    my $id = ref $groupOrID ? $groupOrID-&gt;id : $groupOrID;
    return $self-&gt;{db}-&gt;getGroupPermission ($id) || 'None';
}
sub setGroup {
    my ($self, $groupOrID, $level) = @_;
    return unless (defined $groupOrID);
    die "Permission level '$level' not recognized.\n"
        unless (defined $levelValues{$level});
    my $id = ref $groupOrID ? $groupOrID-&gt;id : $groupOrID;
    $self-&gt;{db}-&gt;setGroupPermission ($id, $level);
}

# Pass username and access level; return undef if access denied.
# If $userName is undef, check for Anonymous
# If $userName is AnonymousUser, check for Anonymous
# If $userName is AuthenticatedUser, check for AuthenticatedUser
# Note that Add implies View, Edit imples Add, Admin implies Edit
# Anyone with Sys Admin permission can do anything.
sub permitted {
    my $self = shift;
    my ($userOrName, $requested) = @_;

    die "Someone screwed up - permission $requested not found.\n"
        unless (defined $levelValues{$requested});

    my ($user, $userName);
    if (defined $userOrName) {
        if (ref ($userOrName) eq 'User') {
            $user     = $userOrName;
            $userName = $user-&gt;name;
        } else {
            $user     = User-&gt;getUser ($userOrName);
            $userName = $userOrName;
        }
    }

    my $have;
    if (!$userName || $userName eq 'AnonymousUser') {
        $have = $self-&gt;getAnonymous;
    } elsif ($userName eq 'AuthenticatedUser') {
        $have = $self-&gt;getAuthenticatedUser;
    } else {
        $have = $self-&gt;get ($userName);
    }

    return 1 if ($levelValues{$have} &gt;= $levelValues{$requested});

    # Check user group perms
    my @user_group_ids;
    if ($user) {
        @user_group_ids = $user-&gt;groupIDs;
        foreach (@user_group_ids) {
            my $have = $self-&gt;getGroup ($_);
            return 1 if ($levelValues{$have} &gt;= $levelValues{$requested});
        }
    }

    return undef if $self-&gt;{db}-&gt;isa ('MasterDB');

    # OK, now see if any User Group the user is in has permission in any
    #  Calendar group this calendar is in
    if (@user_group_ids) {
        my @cal_groups = $self-&gt;{db}-&gt;getPreferences-&gt;getGroups;
        my $master = MasterDB-&gt;new;
        foreach my $cal_group (@cal_groups) {
            # Get hashref of {user group IDs =&gt; perm in cal group}
            my $calgroup_perms = MasterDB-&gt;get_cal_group_perms ($cal_group);
            foreach my $ugroup_id (@user_group_ids) {
                my $have = $calgroup_perms-&gt;{$ugroup_id} || 'None';
                return 1 if ($levelValues{$have} &gt;= $levelValues{$requested});
            }
        }
    }

    return Permissions-&gt;new (MasterDB-&gt;new)-&gt;permitted ($user, 'Admin');
}

sub userPermitted {
    my ($self, $userOrName, $requested) = @_;
    my $uname = ref ($userOrName) ? $userOrName-&gt;name : $userOrName;
    my $have;
    if (!$uname || $uname eq 'AnonymousUser') {
        $have = $self-&gt;getAnonymous;
    } else {
        $have = $self-&gt;get ($uname);
    }
    return 1 if ($levelValues{$have} &gt;= $levelValues{$requested});
}

sub groupPermitted {
    my ($self, $group_or_id, $requested) = @_;
    my $have = $self-&gt;getGroup ($group_or_id);
    return 1 if ($levelValues{$have} &gt;= $levelValues{$requested});
}

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