]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor/Friends.pm
also return the permissions applied to linked users on retrieval
[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/id 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 my $perm_check_query = { 
42     select => {cub =>  ['btype'] }, 
43     from => {
44         cub => {
45             cubi => {field => 'bucket', fkey => 'id'}
46         }
47     }, 
48     limit => 1
49 };
50
51 sub retrieve_friends {
52     my($self, $e, $user_id) = @_;
53
54     # users I have links to
55     $out_links_query->{where}->{'+cub'}->{owner} = $user_id;
56     my @out_linked = map {$_->{target_user}} @{$e->json_query($out_links_query)};
57
58     # users who link to me
59     $in_links_query->{where}->{'+cubi'}->{target_user} = $user_id;
60     my @in_linked = map {$_->{owner}} @{$e->json_query($in_links_query)};
61
62     # determine which users are confirmed, pending outbound 
63     # requests, and pending inbound requests
64     my @confirmed;
65     my @pending_out;
66     my @pending_in;
67
68     for my $out_link (@out_linked) {
69         if(grep {$_ == $out_link} @in_linked) {
70             push(@confirmed, $out_link);
71         } else {
72             push(@pending_out, $out_link);
73         }
74     }
75
76     for my $in_link (@in_linked) {
77         push(@pending_in, $in_link)
78             unless grep {$_ == $in_link} @confirmed;
79     }
80
81     return {
82         confirmed => $self->load_linked_user_perms($e, $user_id, @confirmed),
83         pending_out => $self->load_linked_user_perms($e, $user_id, @pending_out),
84         pending_in => $self->load_linked_user_perms($e, $user_id, @pending_in)
85     };
86 }
87
88 # given a base user and set of linked users, returns the trimmed linked user
89 # records, plus the perms (by name) each user has been granted
90 sub load_linked_user_perms {
91     my($self, $e, $user_id, @users) = @_;
92     my $items = [];
93     my $select = {select => {au => \@keep_user_fields}};
94
95     for my $d_user (@users) {
96
97         # fetch all of the bucket items linked from base user to 
98         # delegate user with the folks: prefix on the bucket type
99         $perm_check_query->{where} = {
100             '+cubi' => {target_user => $d_user},
101             '+cub' => {btype => {like => 'folks:%'}, owner => $user_id}
102         };
103
104         push(@$items, {
105                 user => $e->retrieve_actor_user([$d_user, $select]),
106                 permissions => [ 
107                     # trim the folks: prefix from the bucket type
108                     map {substr($_->{btype}, 6)} @{$e->json_query($perm_check_query)} 
109                 ]
110             }
111         );
112     }
113     return $items;
114 }
115
116
117 my $direct_links_query = { 
118     select => {cub =>  ['id'] }, 
119     from => {
120         cub => {
121             cubi => {field => 'bucket', fkey => 'id'}
122         }
123     }, 
124     where => {
125         '+cubi' => {target_user => undef}, 
126         '+cub' => {btype => 'folks', owner => undef}
127     },
128     limit => 1
129 };
130
131 sub confirmed_friends {
132     my($self, $e, $user1_id, $user2_id) = @_;
133
134     $direct_links_query->{where}->{'+cub'}->{owner} = $user1_id;
135     $direct_links_query->{where}->{'+cubi'}->{target_user} = $user2_id;
136
137     if($e->json_query($direct_links_query)->[0]) {
138         
139         $direct_links_query->{where}->{'+cub'}->{owner} = $user2_id;
140         $direct_links_query->{where}->{'+cubi'}->{target_user} = $user1_id;
141         return 1 if $e->json_query($direct_links_query)->[0];
142     }
143
144     return 0;
145 }
146
147
148
149 # returns 1 if delegate_user is allowed to perform 'perm' for base_user
150 sub friend_perm_allowed {
151     my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
152     return 0 unless $self->confirmed_friends($base_user_id, $delegate_user_id);
153     $perm_check_query->{where} = {
154         '+cubi' => {target_user => $delegate_user_id},
155         '+cub' => {btype => "folks:$perm", owner => $base_user_id}
156     };
157     return 1 if $e->json_query($perm_check_query)->[0];
158     return 0;
159 }
160
161 sub apply_friend_perm {
162     my($self, $e, $base_user_id, $delegate_user_id, $perm) = @_;
163
164     my $bucket = $e->search_container_user_bucket(
165         {owner => $base_user_id, btype => "folks:$perm"})->[0];
166
167     if($bucket) {
168         # is the permission already set?
169         return undef if $e->search_container_user_bucket_item(
170             {bucket => $bucket->id, target_user => $delegate_user_id})->[0];
171
172     } else {
173         # make sure the perm-specific bucket exists for this user
174         $bucket = Fieldmapper::container::user_bucket->new;
175         $bucket->owner($base_user_id);
176         $bucket->btype("folks:$perm");
177         $bucket->name("folks:$perm");
178         $e->create_container_user_bucket($bucket) or return $e->die_event;
179     }
180
181     my $item = Fieldmapper::container::user_bucket_item->new;
182     $item->bucket($bucket->id);
183     $item->target_user($delegate_user_id);
184     $e->create_container_user_bucket_item($item) or return $e->die_event;
185     return undef;
186 }
187
188 23;