[Bio] / PRAST / WebPage / JobShare.pm Repository:
ViewVC logotype

Annotation of /PRAST/WebPage/JobShare.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (view) (download) (as text)

1 : jared 1.1 package PRAST::WebPage::JobShare;
2 :    
3 :     use base qw( WebPage );
4 :    
5 :     1;
6 :    
7 :     use strict;
8 :     use warnings;
9 :     use WebConfig;
10 :     use RAST::RASTShared qw( get_menu_job );
11 :    
12 :     =pod
13 :    
14 :     =head1 NAME
15 :    
16 :     JobShare - an instance of WebPage to allow users to grant access to their genomes to others
17 :    
18 :     =head1 DESCRIPTION
19 :    
20 :     Offers the user the ability to grant access to his genomes
21 :    
22 :     =head1 METHODS
23 :    
24 :     =over 4
25 :    
26 :     =item * B<init> ()
27 :    
28 :     Called when the web page is instanciated.
29 :    
30 :     =cut
31 :    
32 :     sub init {
33 :     my ($self) = @_;
34 :    
35 :     $self->title('Share a job');
36 :     $self->application->register_action($self, 'share_job', 'share_job');
37 :     $self->application->register_action($self, 'revoke_job', 'revoke_job');
38 :     $self->application->register_component('FilterSelect', 'Scopes');
39 :    
40 :     # sanity check on job
41 :     my $id = $self->application->cgi->param('job') || '';
42 :     my $job;
43 :     eval { $job = $self->app->data_handle('RAST')->Job->init({ id => $id }); };
44 :     unless ($job) {
45 :     $self->app->error("Unable to retrieve the job '$id'.");
46 :     }
47 :     $self->data('job', $job);
48 :    
49 :     # add links
50 :     &get_menu_job($self->app->menu, $job);
51 :    
52 :     return 1;
53 :     }
54 :    
55 :     =item * B<output> ()
56 :    
57 :     Returns the html output of the DelegateRights page.
58 :    
59 :     =cut
60 :    
61 :     sub output {
62 :     my ($self) = @_;
63 :    
64 :     my $job = $self->data('job');
65 :    
66 :     my $content = "<h1>Share a job</h1>";
67 :    
68 :     # short job info
69 :     $content .= "<p> &raquo <a href='?page=JobDetails&job=".$job->id."'>Back to the Job Details</a></p>";
70 :     $content .= "<p> &raquo <a href='?page=Jobs'>Back to the Jobs Overview</a></p>";
71 :    
72 :     $content .= "<p id='section_bar'><img src='".IMAGES."rast-info.png'/>Job Information</p>";
73 :     $content .= "<table>";
74 :     $content .= "<tr><th>Name - ID:</th><td>".$job->genome_id." - ".$job->genome_name."</td></tr>";
75 :     $content .= "<tr><th>Type:</th><td>".$job->type."</td></tr>";
76 :     $content .= "<tr><th>Job:</th><td> #".$job->id."</td></tr>";
77 :     $content .= "<tr><th>User:</th><td>".$job->owner->login."</td></tr>";
78 :     $content .= "</table>";
79 :    
80 :    
81 :     # short help text
82 :     $content .= '<p style="width: 70%;">To share the above job and its data with another user, please enter the email address of the user. Please note that you have to enter the email address which that person used to register at the RAST service. The user will receive an email that notifies him how to access the data. Once you have granted the right to view one of your RAST jobs to another user or group, the name will appear at the bottom of the page with the option to revoke it.</p>';
83 :    
84 :     # select user or group
85 :     $content .= "<p id='section_bar'><img src='".IMAGES."rast-info.png'/>Enter an email address</p>";
86 :     $content .= $self->start_form('share_job', { job => $job->id,
87 :     action => 'share_job' });
88 :     my $email = $self->app->cgi->param('email') || '';
89 :     $content .= "<p><strong>Enter an email address:</strong> <input name='email' type='textbox' value='$email'></p>";
90 :     $content .= "<p><input type='submit' name='share_job' value=' Share job with this user or group '></p>";
91 :     $content .= $self->end_form;
92 :    
93 :     # show people who can see this job at the moment
94 :     $content .= "<p id='section_bar'><img src='".IMAGES."rast-info.png'/>This job is currently available to:</p>";
95 :     my $rights = $self->application->dbmaster->Rights->get_objects( { name => 'view',
96 :     data_type => 'genome',
97 :     data_id => $job->genome_id
98 :     });
99 :     my $found_one = 0;
100 :     $content .= '<table>';
101 :     foreach my $r (@$rights) {
102 :     next if ($self->app->session->user->get_user_scope->_id eq $r->scope->_id);
103 :     $content .= "<tr><td>".$r->scope->name_readable."</td>";
104 :     if($r->delegated) {
105 :     $content .= "<td>".$self->start_form('revoke_job', { job => $job->id,
106 :     action => 'revoke_job',
107 :     scope => $r->scope->_id,
108 :     });
109 :     $content .= "<input type='submit' name='revoke_job' value=' Revoke '>";
110 :     $content .= "</td>";
111 :     }
112 :     else {
113 :     $content .= "<td></td>";
114 :     }
115 :     $content .= '</tr>';
116 :     $found_one = 1;
117 :     }
118 :    
119 :     unless($found_one) {
120 :     $content .= "<tr><td>This job is not shared with anyone at the moment.</td></tr>";
121 :     }
122 :     $content .= '</table>';
123 :    
124 :     return $content;
125 :    
126 :    
127 :     }
128 :    
129 :    
130 :     =pod
131 :    
132 :     =item * B<share_job>()
133 :    
134 :     Action method to grant the right to view and edit a genome to the selected scope
135 :    
136 :     =cut
137 :    
138 :     sub share_job {
139 :     my ($self) = @_;
140 :    
141 :     # get some info
142 :     my $job_id = $self->data('job')->id;
143 :     my $genome_id = $self->data('job')->genome_id;
144 :     my $genome_name = $self->data('job')->genome_name;
145 : paczian 1.2 my $application = $self->application;
146 : jared 1.1
147 :     my $email = $self->app->cgi->param('email');
148 :    
149 : paczian 1.2 my $master = $self->application->dbmaster;
150 :     my $scope = $master->Scope->get_objects( { name => $email } );
151 :     my $display_name = "";
152 :     if (scalar(@$scope)) {
153 :     $scope = $scope->[0];
154 :     $display_name = $scope->name;
155 :     } else {
156 : jared 1.1
157 : paczian 1.2 # check email format
158 :     unless ($email =~ /^[\w\-\.]+\@[\.a-zA-Z\-0-9]+\.[a-zA-Z]+$/) {
159 :     $self->application->add_message('warning', 'Please enter a valid email address.');
160 :     return 0;
161 :     }
162 : jared 1.1
163 : paczian 1.2 # check if have a user with that email
164 :     my $user = $master->User->init({ email => $email });
165 :     if (ref $user) {
166 :    
167 :     # send email
168 :     my $ubody = HTML::Template->new(filename => TMPL_PATH.'EmailSharedJobGranted.tmpl',
169 :     die_on_bad_params => 0);
170 :     $ubody->param('FIRSTNAME', $user->firstname);
171 :     $ubody->param('LASTNAME', $user->lastname);
172 :     $ubody->param('WHAT', "$genome_name ($genome_id)");
173 :     $ubody->param('WHOM', $self->app->session->user->firstname.' '.$self->app->session->user->lastname);
174 :     $ubody->param('LINK', $WebConfig::APPLICATION_URL."?page=JobDetails&job=$job_id");
175 :     $ubody->param('APPLICATION_NAME', $WebConfig::APPLICATION_NAME);
176 :    
177 :     $user->send_email( $WebConfig::ADMIN_EMAIL,
178 :     $WebConfig::APPLICATION_NAME.' - new data available',
179 :     $ubody->output
180 :     );
181 :    
182 :     $scope = $user->get_user_scope;
183 :     $display_name = $user->firstname." ".$user->lastname;
184 :     } else {
185 :     $application->add_message('warning', "User or group not found, aborting.");
186 :     return 0;
187 :     }
188 :     }
189 : jared 1.1
190 : paczian 1.2 # grant rights if necessary
191 :     my $rights = [ 'view', 'edit' ];
192 :     foreach my $name (@$rights) {
193 :     unless(scalar(@{$master->Rights->get_objects( { name => $name,
194 :     data_type => 'genome',
195 :     data_id => $genome_id,
196 :     scope => $scope } )})) {
197 :     my $right = $master->Rights->create( { granted => 1,
198 :     name => $name,
199 :     data_type => 'genome',
200 :     data_id => $genome_id,
201 :     scope => $scope,
202 :     delegated => 1, } );
203 :     unless (ref $right) {
204 :     $self->app->add_message('warning', 'Failed to create the right in the user database, aborting.');
205 :     return 0;
206 :     }
207 :     }
208 :     if ($self->data('job')->type eq 'Metagenome') {
209 : jared 1.1 unless(scalar(@{$master->Rights->get_objects( { name => $name,
210 : paczian 1.2 data_type => 'metagenome',
211 : jared 1.1 data_id => $genome_id,
212 : paczian 1.2 scope => $scope } )})) {
213 : jared 1.1 my $right = $master->Rights->create( { granted => 1,
214 :     name => $name,
215 : paczian 1.2 data_type => 'metagenome',
216 : jared 1.1 data_id => $genome_id,
217 : paczian 1.2 scope => $scope,
218 : jared 1.1 delegated => 1, } );
219 :     unless (ref $right) {
220 :     $self->app->add_message('warning', 'Failed to create the right in the user database, aborting.');
221 :     return 0;
222 :     }
223 :     }
224 :     }
225 :     }
226 :    
227 : paczian 1.2 $self->app->add_message('info', "Granted the right to view this job to $display_name.");
228 :     return 1;
229 : jared 1.1 }
230 :    
231 :    
232 :     =pod
233 :    
234 :     =item * B<revoke_job>()
235 :    
236 :     Action method to revoke the right to view and edit a genome to the selected scope
237 :    
238 :     =cut
239 :    
240 :     sub revoke_job {
241 :     my ($self) = @_;
242 :    
243 :     my $master = $self->application->dbmaster;
244 :    
245 :     # get the scope
246 :     my $s_id = $self->app->cgi->param('scope');
247 :     my $scope = $master->Scope->get_objects({ _id => $s_id });
248 :     unless(@$scope) {
249 :     $self->app->add_message('warning', 'There has been an error: missing a scope to revoke right on., aborting.');
250 :     return 0;
251 :     }
252 :     $scope = $scope->[0];
253 :    
254 :     # get genome id
255 :     my $genome_id = $self->data('job')->genome_id;
256 :    
257 :     # delete the rights, double check delegated
258 :     my $rights = [ 'view', 'edit' ];
259 :     foreach my $name (@$rights) {
260 :     foreach my $r (@{$master->Rights->get_objects( { name => $name,
261 :     data_type => 'genome',
262 :     data_id => $genome_id,
263 :     scope => $scope,
264 :     delegated => 1,
265 :     })}) {
266 :     $r->delete;
267 :     }
268 :     if ($self->data('job')->type eq 'Metagenome') {
269 :     foreach my $r (@{$master->Rights->get_objects( { name => $name,
270 :     data_type => 'metagenome',
271 :     data_id => $genome_id,
272 :     scope => $scope,
273 :     delegated => 1,
274 :     })}) {
275 :     $r->delete;
276 :     }
277 :     }
278 :     }
279 :    
280 : paczian 1.2 $self->app->add_message('info', "Revoked the right to view this job from ".$scope->name_readable.".");
281 : jared 1.1
282 :     return 1;
283 :    
284 :     }
285 :    
286 :    
287 :     =pod
288 :    
289 :     =item * B<required_rights>()
290 :    
291 :     Returns a reference to the array of required rights
292 :    
293 :     =cut
294 :    
295 :     sub required_rights {
296 :     my $rights = [ [ 'login' ], ];
297 :     if ($_[0]->data('job')) {
298 :     push @$rights, [ 'edit', 'genome', $_[0]->data('job')->genome_id, 1 ];
299 :     push @$rights, [ 'view', 'genome', $_[0]->data('job')->genome_id, 1 ];
300 :     }
301 :    
302 :     return $rights;
303 :     }
304 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3