[LON-CAPA-cvs] cvs: loncom /interface lonhelper.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 16 May 2003 17:20:51 -0000


bowersj2		Fri May 16 13:20:51 2003 EDT

  Modified files:              
    /loncom/interface	lonhelper.pm 
  Log:
  Allow specification of required priviledges to use a helper.
  
  
Index: loncom/interface/lonhelper.pm
diff -u loncom/interface/lonhelper.pm:1.30 loncom/interface/lonhelper.pm:1.31
--- loncom/interface/lonhelper.pm:1.30	Thu May 15 12:14:52 2003
+++ loncom/interface/lonhelper.pm	Fri May 16 13:20:51 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # .helper XML handler to implement the LON-CAPA helper
 #
-# $Id: lonhelper.pm,v 1.30 2003/05/15 16:14:52 bowersj2 Exp $
+# $Id: lonhelper.pm,v 1.31 2003/05/16 17:20:51 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -53,7 +53,10 @@
 messages, resource selections, or date queries.
 
 The helper tag is required to have one attribute, "title", which is the name
-of the helper itself, such as "Parameter helper". 
+of the helper itself, such as "Parameter helper". The helper tag may optionally
+have a "requiredpriv" attribute, specifying the priviledge a user must have
+to use the helper, or get denied access. See loncom/auth/rolesplain.tab for
+useful privs. Default is full access, which is often wrong!
 
 =head2 State tags
 
@@ -257,10 +260,17 @@
     # xml parsing
     &Apache::lonxml::xmlparse($r, 'helper', $file);
 
+    my $allowed = $helper->allowedCheck();
+    if (!$allowed) {
+        $ENV{'user.error.msg'} = $ENV{'request.uri'}.':'.$helper->{REQUIRED_PRIV}.
+            ":0:0:Permission denied to access this helper.";
+        return HTTP_NOT_ACCEPTABLE;
+    }
+
     $helper->process();
 
     $r->print($helper->display());
-   return OK;
+    return OK;
 }
 
 sub registerHelperTags {
@@ -284,7 +294,7 @@
 
     registerHelperTags();
 
-    Apache::lonhelper::helper->new($token->[2]{'title'});
+    Apache::lonhelper::helper->new($token->[2]{'title'}, $token->[2]{'requiredpriv'});
     return '';
 }
 
@@ -343,6 +353,7 @@
     my $self = {};
 
     $self->{TITLE} = shift;
+    $self->{REQUIRED_PRIV} = shift;
     
     # If there is a state from the previous form, use that. If there is no
     # state, use the start state parameter.
@@ -467,6 +478,16 @@
     }
 }
 
+sub allowedCheck {
+    my $self = shift;
+
+    if (!defined($self->{REQUIRED_PRIV})) { 
+        return 1;
+    }
+
+    return Apache::lonnet::allowed($self->{REQUIRED_PRIV}, $ENV{'request.course.id'});
+}
+
 sub changeState {
     my $self = shift;
     $self->{STATE} = shift;
@@ -549,23 +570,22 @@
 HEADER
     if (!$state->overrideForm()) { $result.="<form name='helpform' method='POST'>"; }
     $result .= <<HEADER;
-        <table border="0"><tr><td>
+        <table border="0" width='100%'><tr><td>
         <h2><i>$stateTitle</i></h2>
 HEADER
 
-    $result .= "<table><tr><td rowspan='2' valign='top'>";
+    $result .= "<table cellpadding='10' width='100%'><tr><td rowspan='2' valign='top'>";
 
     if (!$state->overrideForm()) {
         $result .= $self->_saveVars();
     }
     $result .= $state->render();
 
-    $result .= "</td><td valign='top'>";
+    $result .= "</td><td valign='top' align='right'>";
 
     # Warning: Copy and pasted from below, because it's too much trouble to 
     # turn this into a subroutine
     if (!$state->overrideForm()) {
-        $result .= '<center>';
         if ($self->{STATE} ne $self->{START_STATE}) {
             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
         }
@@ -576,17 +596,15 @@
         else {
             $result .= '<nobr><input name="back" type="button" ';
             $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';
-            $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" /></nobr>&nbsp;';
+            $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" /></nobr>';
         }
-        $result .= "</center>\n";
     }
 
-    $result .= "</td></tr><tr><td valign='bottom'>";
+    $result .= "</td></tr><tr><td valign='bottom' align='right'>";
 
     # Warning: Copy and pasted from above, because it's too much trouble to 
     # turn this into a subroutine
     if (!$state->overrideForm()) {
-        $result .= '<center>';
         if ($self->{STATE} ne $self->{START_STATE}) {
             #$result .= '<input name="SUBMIT" type="submit" value="&lt;- Previous" />&nbsp;&nbsp;';
         }
@@ -599,7 +617,6 @@
             $result .= 'value="&lt;- Previous" onclick="history.go(-1)" /> ';
             $result .= '<input name="SUBMIT" type="submit" value="Next -&gt;" /></nobr>';
         }
-        $result .= "</center>\n";
     }
 
     #foreach my $key (keys %{$self->{VARS}}) {
@@ -762,6 +779,12 @@
 
 Again, see the course initialization helper for examples.
 
+B<getValue method>
+
+If the element stores the name of the variable in a 'variable' member, which
+the provided ones all do, you can retreive the value of the variable by calling
+this method.
+
 =cut
 
 BEGIN {
@@ -848,6 +871,11 @@
     return 0;
 }
 
+sub getValue {
+    my $self = shift;
+    return $helper->{VARS}->{$self->{'variable'}};
+}
+
 1;
 
 package Apache::lonhelper::message;
@@ -2417,13 +2445,13 @@
         for my $element (@{$state->{ELEMENTS}}) {
             if (defined($element->{FINAL_CODE})) {
                 # Compile the code.
-                my $code = 'sub { my $helper = shift; ' . $element->{FINAL_CODE} .
-                    '}';
+                my $code = 'sub { my $helper = shift; my $element = shift; ' 
+                    . $element->{FINAL_CODE} . '}';
                 $code = eval($code);
                 die 'Error while executing final code for element with var ' .
                     $element->{'variable'} . ', Perl said: ' . $@ if $@;
 
-                my $result = &$code($helper);
+                my $result = &$code($helper, $element);
                 if ($result) {
                     push @results, $result;
                 }