[open-ils-commits] r11422 - in trunk/Open-ILS/src/perlmods/OpenILS/Application: . Actor

svn at svn.open-ils.org svn at svn.open-ils.org
Fri Dec 5 16:33:48 EST 2008


Author: erickson
Date: 2008-12-05 16:33:44 -0500 (Fri, 05 Dec 2008)
New Revision: 11422

Modified:
   trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor.pm
   trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm
Log:
added apply-perms method and utility code (untested) to verify a perm is set

Modified: trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm	2008-12-05 18:18:14 UTC (rev 11421)
+++ trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm	2008-12-05 21:33:44 UTC (rev 11422)
@@ -3,6 +3,7 @@
 use OpenILS::Application::AppUtils;
 use OpenILS::Utils::CStoreEditor q/:funcs/;
 use OpenSRF::Utils::Logger q/$logger/;
+use OpenILS::Utils::Fieldmapper;
 my $U = "OpenILS::Application::AppUtils";
 
 # ----------------------------------------------------------------
@@ -84,4 +85,84 @@
     };
 }
 
+my $direct_links_query = { 
+    select => {cub =>  ['id'] }, 
+    from => {
+        cub => {
+            cubi => {field => 'bucket', fkey => 'id'}
+        }
+    }, 
+    where => {
+        '+cubi' => {target_user => undef}, 
+        '+cub' => {btype => 'folks', owner => undef}
+    },
+    limit => 1
+};
+
+sub confirmed_friends {
+    my($self, $e, $user1_id, $user2_id) = @_;
+
+    $direct_links_query->{where}->{'+cub'}->{owner} = $user1_id;
+    $direct_links_query->{where}->{'+cubi'}->{target_user} = $user2_id;
+
+    if($e->json_query($direct_links_query)->[0]) {
+        
+        $direct_links_query->{where}->{'+cub'}->{owner} = $user2_id;
+        $direct_links_query->{where}->{'+cubi'}->{target_user} = $user1_id;
+        return 1 if $e->json_query($direct_links_query)->[0];
+    }
+
+    return 0;
+}
+
+
+my $perm_check_query = { 
+    select => {cub =>  ['id'] }, 
+    from => {
+        cub => {
+            cubi => {field => 'bucket', fkey => 'id'}
+        }
+    }, 
+    limit => 1
+};
+
+# returns 1 if delegate_user is allowed to perform 'perm' for base_user
+sub friend_perm_allowed {
+    my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
+    return 0 unless $self->confirmed_friends($base_user_id, $delegate_user_id);
+    $perm_check_query->{where} = {
+        '+cubi' => {target_user => $delegate_user_id},
+        '+cub' => {btype => "folks:$perm", owner => $base_user_id}
+    };
+    return 1 if $e->json_query($perm_check_query)->[0];
+    return 0;
+}
+
+sub apply_friend_perm {
+    my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
+
+    my $bucket = $e->search_container_user_bucket(
+        {owner => $base_user_id, btype => "folks:$perm"})->[0];
+
+    if($bucket) {
+        # is the permission already set?
+        return undef if $e->search_container_user_bucket_item(
+            {bucket => $bucket->id, target_user => $delegate_user_id})->[0];
+
+    } else {
+        # make sure the perm-specific bucket exists for this user
+        $bucket = Fieldmapper::container::user_bucket->new;
+        $bucket->owner($base_user_id);
+        $bucket->btype("folks:$perm");
+        $bucket->name("folks:$perm");
+        $e->create_container_user_bucket($bucket) or return $e->die_event;
+    }
+
+    my $item = Fieldmapper::container::user_bucket_item->new;
+    $item->bucket($bucket->id);
+    $item->target_user($delegate_user_id);
+    $e->create_container_user_bucket_item($item) or return $e->die_event;
+    return undef;
+}
+
 23;

Modified: trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor.pm	2008-12-05 18:18:14 UTC (rev 11421)
+++ trunk/Open-ILS/src/perlmods/OpenILS/Application/Actor.pm	2008-12-05 21:33:44 UTC (rev 11422)
@@ -3083,5 +3083,36 @@
     return OpenILS::Application::Actor::Friends->retrieve_friends($e, $user_id);
 }
 
+
+
+__PACKAGE__->register_method (
+	method		=> 'apply_friend_perms',
+	api_name	=> 'open-ils.actor.friends.perms.apply',
+	signature	=> {
+        desc => q/
+        /
+    }
+);
+sub apply_friend_perms {
+    my($self, $conn, $auth, $user_id, $delegate_id, @perms) = @_;
+    my $e = new_editor(authtoken => $auth, xact => 1);
+    return $e->event unless $e->checkauth;
+
+    if($user_id != $e->requestor->id) {
+        my $user = $e->retrieve_actor_user($user_id) or return $e->die_event;
+        return $e->die_event unless $e->allowed('VIEW_USER', $user->home_ou);
+    }
+
+    for my $perm (@perms) {
+        my $evt = 
+            OpenILS::Application::Actor::Friends->apply_friend_perm(
+                $e, $user_id, $delegate_id, $perm);
+        return $evt if $evt;
+    }
+
+    $e->commit;
+    return 1;
+}
+
 1;
 



More information about the open-ils-commits mailing list