]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm
added apply-perms method and utility code (untested) to verify a perm is set
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Actor / Friends.pm
1 package OpenILS::Application::Actor::Friends;
2 use strict; use warnings;
3 use OpenILS::Application::AppUtils;
4 use OpenILS::Utils::CStoreEditor q/:funcs/;
5 use OpenSRF::Utils::Logger q/$logger/;
6 use OpenILS::Utils::Fieldmapper;
7 my $U = "OpenILS::Application::AppUtils";
8
9 # ----------------------------------------------------------------
10 # Shared Friend utilities.  Thar be no methods published here...
11 # ----------------------------------------------------------------
12
13 # export these fields for friend display
14 my @keep_user_fields = qw/usrname first_given_name second_given_name family_name alias/;
15
16 my $out_links_query = {
17     select => {cubi => ['target_user']}, 
18     from => {
19         cub => {
20             cubi => {field => 'bucket', fkey => 'id'}
21         }
22     }, 
23     where => {
24         '+cub' => {btype => 'folks', owner => undef}
25     }
26 };
27
28 my $in_links_query = { 
29     select => {cub =>  ['owner'] }, 
30     from => {
31         cub => {
32             cubi => {field => 'bucket', fkey => 'id'}
33         }
34     }, 
35     where => {
36         '+cubi' => {target_user => undef}, 
37         '+cub' => {btype => 'folks'}
38     }
39 };
40
41
42 sub retrieve_friends {
43     my($self, $e, $user_id) = @_;
44
45     # users I have links to
46     $out_links_query->{where}->{'+cub'}->{owner} = $user_id;
47     my @out_linked = map {$_->{target_user}} @{$e->json_query($out_links_query)};
48
49     # users who link to me
50     $in_links_query->{where}->{'+cubi'}->{target_user} = $user_id;
51     my @in_linked = map {$_->{owner}} @{$e->json_query($in_links_query)};
52
53     my @confirmed;
54     my @pending_out;
55     my @pending_in;
56
57     for my $out_link (@out_linked) {
58         if(grep {$_ == $out_link} @in_linked) {
59             push(@confirmed, $out_link);
60         } else {
61             push(@pending_out, $out_link);
62         }
63     }
64
65     for my $in_link (@in_linked) {
66         push(@pending_in, $in_link)
67             unless grep {$_ == $in_link} @confirmed;
68     }
69
70     my $select = {select => {au => \@keep_user_fields}};
71
72     my $confirmed = (@confirmed) ? 
73         $e->search_actor_user([{id => \@confirmed}, $select]) : [];
74
75     my $pending_out = (@pending_out) ?
76         $e->search_actor_user([{id => \@pending_out}, $select]) : [];
77
78     my $pending_in = (@pending_in) ? 
79         $e->search_actor_user([{id => \@pending_in}, $select]) : [];
80
81     return {
82         confirmed => $confirmed,
83         pending_out => $pending_out,
84         pending_in =>$pending_in
85     };
86 }
87
88 my $direct_links_query = { 
89     select => {cub =>  ['id'] }, 
90     from => {
91         cub => {
92             cubi => {field => 'bucket', fkey => 'id'}
93         }
94     }, 
95     where => {
96         '+cubi' => {target_user => undef}, 
97         '+cub' => {btype => 'folks', owner => undef}
98     },
99     limit => 1
100 };
101
102 sub confirmed_friends {
103     my($self, $e, $user1_id, $user2_id) = @_;
104
105     $direct_links_query->{where}->{'+cub'}->{owner} = $user1_id;
106     $direct_links_query->{where}->{'+cubi'}->{target_user} = $user2_id;
107
108     if($e->json_query($direct_links_query)->[0]) {
109         
110         $direct_links_query->{where}->{'+cub'}->{owner} = $user2_id;
111         $direct_links_query->{where}->{'+cubi'}->{target_user} = $user1_id;
112         return 1 if $e->json_query($direct_links_query)->[0];
113     }
114
115     return 0;
116 }
117
118
119 my $perm_check_query = { 
120     select => {cub =>  ['id'] }, 
121     from => {
122         cub => {
123             cubi => {field => 'bucket', fkey => 'id'}
124         }
125     }, 
126     limit => 1
127 };
128
129 # returns 1 if delegate_user is allowed to perform 'perm' for base_user
130 sub friend_perm_allowed {
131     my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
132     return 0 unless $self->confirmed_friends($base_user_id, $delegate_user_id);
133     $perm_check_query->{where} = {
134         '+cubi' => {target_user => $delegate_user_id},
135         '+cub' => {btype => "folks:$perm", owner => $base_user_id}
136     };
137     return 1 if $e->json_query($perm_check_query)->[0];
138     return 0;
139 }
140
141 sub apply_friend_perm {
142     my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
143
144     my $bucket = $e->search_container_user_bucket(
145         {owner => $base_user_id, btype => "folks:$perm"})->[0];
146
147     if($bucket) {
148         # is the permission already set?
149         return undef if $e->search_container_user_bucket_item(
150             {bucket => $bucket->id, target_user => $delegate_user_id})->[0];
151
152     } else {
153         # make sure the perm-specific bucket exists for this user
154         $bucket = Fieldmapper::container::user_bucket->new;
155         $bucket->owner($base_user_id);
156         $bucket->btype("folks:$perm");
157         $bucket->name("folks:$perm");
158         $e->create_container_user_bucket($bucket) or return $e->die_event;
159     }
160
161     my $item = Fieldmapper::container::user_bucket_item->new;
162     $item->bucket($bucket->id);
163     $item->target_user($delegate_user_id);
164     $e->create_container_user_bucket_item($item) or return $e->die_event;
165     return undef;
166 }
167
168 23;