[LON-CAPA-cvs] cvs: loncom /auth lonacc.pm publiccheck.pm restrictedaccess.pm

albertel lon-capa-cvs@mail.lon-capa.org
Mon, 17 Jul 2006 19:49:14 -0000


albertel		Mon Jul 17 15:49:14 2006 EDT

  Modified files:              
    /loncom/auth	lonacc.pm publiccheck.pm restrictedaccess.pm 
  Log:
  - trying to reduce the number of places those re occur
  
  
Index: loncom/auth/lonacc.pm
diff -u loncom/auth/lonacc.pm:1.86 loncom/auth/lonacc.pm:1.87
--- loncom/auth/lonacc.pm:1.86	Fri Jul 14 17:38:26 2006
+++ loncom/auth/lonacc.pm	Mon Jul 17 15:49:14 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Cookie Based Access Handler
 #
-# $Id: lonacc.pm,v 1.86 2006/07/14 21:38:26 albertel Exp $
+# $Id: lonacc.pm,v 1.87 2006/07/17 19:49:14 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -138,7 +138,21 @@
 }
 
 sub portfolio_access {
+    my ($r,$requrl) = @_;
+    my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
+    my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
+    if ($result eq 'ok') {
+	return OK;
+    } elsif ($result =~ /^[^:]+:guest_/) {
+	&passphrase_access_checker($r,$result,$requrl);
+	return OK;
+    }
+    return FORBIDDEN;
+}
+
+sub get_portfolio_access {
     my ($udom,$unum,$file_name,$group) = @_;
+ 
     my $current_perms = &Apache::lonnet::get_portfile_permissions($udom,$unum);
     my %access_controls = &Apache::lonnet::get_access_controls(
                                              $current_perms,$group,$file_name);
@@ -322,6 +336,34 @@
     return; 
 }
 
+sub parse_portfolio_url {
+    my ($url) = @_;
+
+    my ($type,$udom,$unum,$group,$file_name);
+    
+    if ($url =~  m-/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$-) {
+	$type = 1;
+        $udom = $1;
+        $unum = $2;
+        $file_name = $3;
+    } elsif ($url =~ m-/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$-) {
+	$type = 2;
+        $udom = $1;
+        $unum = $2;
+        $group = $3;
+        $file_name = $3.'/'.$4;
+    }
+    if (wantarray) {
+	return ($type,$udom,$unum,$file_name,$group);
+    }
+    return $type;
+}
+
+sub is_portfolio_url {
+    my ($url) = @_;
+    return scalar(&parse_portfolio_url($url));
+}
+
 sub handler {
     my $r = shift;
     my $requrl=$r->uri;
@@ -384,23 +426,9 @@
 
 # ---------------------------------------------------------------- Check access
             my $now = time;
-            if ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$|) {
-                my $result = &portfolio_access($1,$2,$3);
-                if ($result eq 'ok') {
-                    return OK;
-                } elsif ($result =~ /^[^:]+:guest_/) {
-                    &passphrase_access_checker($r,$result,$requrl);
-		    return OK;
-                }
-            } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$|) {
-                my $result = &portfolio_access($1,$2,$3.'/'.$4,$3);
-                if ($result eq 'ok') {
-                    return OK;
-                } elsif ($result =~ /^[^:]+:guest_/) {
-                    &passphrase_access_checker($r,$result,$requrl);
-		    return OK;
-                }
-            }
+	    if (&is_portfolio_url($requrl)) {
+		return &portfolio_access($r,$requrl);
+	    }
             if ($requrl!~/^\/adm|public|prtspool\//) {
 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                 if ($access eq '1') {
@@ -514,23 +542,10 @@
 	return OK;
     }
 # ------------------------------------- See if this is a viewable portfolio file
-    if ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$|) {
-        my $result = &portfolio_access($1,$2,$3);
-        if ($result eq 'ok') {
-            return OK;
-        } elsif ($result =~ /^[^:]+:guest_/) {
-            &passphrase_access_checker($r,$result,$requrl);
-	    return OK;
-        }
-    } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$|) {
-        my $result = &portfolio_access($1,$2,$3.'/'.$4,$3);
-        if ($result eq 'ok') {
-            return OK;
-        } elsif ($result =~ /^[^:]+:guest_/) {
-            &passphrase_access_checker($r,$result,$requrl);
-	    return OK;
-	}
+    if (&is_portfolio_url($requrl)) {
+	return &portfolio_access($r,$requrl);
     }
+
 # -------------------------------------------------------------- Not authorized
     $requrl=~/\.(\w+)$/;
 #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
Index: loncom/auth/publiccheck.pm
diff -u loncom/auth/publiccheck.pm:1.6 loncom/auth/publiccheck.pm:1.7
--- loncom/auth/publiccheck.pm:1.6	Fri Jul 14 17:39:16 2006
+++ loncom/auth/publiccheck.pm	Mon Jul 17 15:49:14 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Cookie Based Access Handler
 #
-# $Id: publiccheck.pm,v 1.6 2006/07/14 21:39:16 albertel Exp $
+# $Id: publiccheck.pm,v 1.7 2006/07/17 19:49:14 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -60,18 +60,14 @@
 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
         &process_public($r,$requrl);
         return OK;
-    } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$|) {
-        my $access = &process_portfolio($1,$2,$3);
+    } elsif (&Apache::lonacc::is_portfolio_url($requrl)) {
+	my (undef,$udom,$unum,$file_name,$group) = 
+	    &Apache::lonacc::parse_portfolio_url($requrl);
+        my $access = &process_portfolio($udom,$unum,$file_name,$group);
         if ($access) {
             &process_public($r,$requrl,$access);
             return OK;
         } 
-    } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$|) {
-        my $access = &process_portfolio($1,$2,$3.'/'.$4,$3);
-        if ($access) {
-            &process_public($r,$requrl,$access);
-            return OK;
-        }
     } elsif ($requrl eq '/adm/restrictedaccess') {
         &process_public($r,$requrl);
     } 
Index: loncom/auth/restrictedaccess.pm
diff -u loncom/auth/restrictedaccess.pm:1.2 loncom/auth/restrictedaccess.pm:1.3
--- loncom/auth/restrictedaccess.pm:1.2	Fri Jul 14 17:38:26 2006
+++ loncom/auth/restrictedaccess.pm	Mon Jul 17 15:49:14 2006
@@ -102,21 +102,15 @@
 
 sub check_pass {
     my ($r,$origurl) = @_;
-    my ($udom,$unum,$group,$file_name,$result,$end);
-    if ($origurl =~  m-/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$-) {
-        $udom = $1;
-        $unum = $2;
-        $file_name = $3;
-    } elsif ($origurl =~ m-/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$-) {
-        $udom = $1;
-        $unum = $2;
-        $group = $3;
-        $file_name = $3.'/'.$4;
-    }
+    my (undef,$udom,$unum,$file_name,$group) = 
+	&Apache::lonacc::parse_portfolio_url($origurl);
+
     my $curr_perms = &Apache::lonnet::get_portfile_permissions($udom,$unum);
     my %acc_controls = &Apache::lonnet::get_access_controls($curr_perms,
                                                             $group,$file_name);
     my $access_hash = $acc_controls{$file_name};
+
+    my ($result,$end);
     foreach my $key (sort(keys(%{$access_hash}))) {
         if ($key =~ /^[^:]+:guest_(\d+)/) {
             $end = $1;