Show
Ignore:
Timestamp:
02/11/10 17:34:27 (2 years ago)
Author:
gbastien
Message:

* Merged recent changes in the trunk into this branch
* New token architecture not yet fully functional, nor tested, but ...

  • can now edit token templates for different context of tokens (from the edit netork interface at the bottom of page)
  • Users connected through a wifidog gateway can view their connection token information
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/newtoken/wifidog/classes/TokenTemplate.php

    r1445 r1446  
    4848require_once('classes/Token.php'); 
    4949 
     50// Token templates context constants 
     51define('TT_CONTEXT_USER_ALLOWED', 1); 
     52define('TT_CONTEXT_USER_VALIDATING', 2); 
     53define('TT_CONTEXT_USER_FORGOT_PASSWORD', 3); 
     54define('TT_CONTEXT_GUEST', 4); 
     55define('TT_CONTEXT_SPLASH_ONLY', 5); 
     56 
     57 
    5058/** 
    5159 * Abstract a token template 
     
    7785    } 
    7886 
    79     static function createNewObject() { 
    80         echo "<h1>Use TokenTemplate::createTokenTemplate() instead</h1>"; 
     87    static function createNewObject($tt_id = null, $network = null, $context = null) { 
     88         
     89        $db = AbstractDb::getObject(); 
     90        if (empty ($tt_id)) { 
     91            $tt_id = get_guid(); 
     92        } 
     93        $tt_id = $db->escapeString($tt_id); 
     94        if (is_null($network)) { 
     95            $netwok = Network::getDefaultNetwork(); 
     96        } 
     97        $network_id = $db->escapeString($network->getId()); 
     98 
     99        if (is_null($context)) { 
     100            $contexts = self::getContexts(); 
     101            $context = TT_CONTEXT_USER_ALLOWED; 
     102        } 
     103         
     104     
     105        $sql = "INSERT INTO token_templates (token_template_id, token_template_network, token_template_context)  
     106                VALUES ('$tt_id', '$network_id', '$context')"; 
     107 
     108        if (!$db->execSqlUpdate($sql, false)) { 
     109            throw new Exception(_('Unable to insert the new token template in the database!')); 
     110        } 
     111        $object = self::getObject($tt_id); 
     112        return $object; 
    81113    } 
    82114     
     
    120152    } 
    121153 
    122     /** Create a new User in the database 
    123      * @param $id The id to be given to the new user 
    124      * @return the newly created User object, or null if there was an error 
    125      * TODO: implement this 
    126      */ 
    127     static function createTokenTemplate($id, $username, Network $network, $email, $password) { 
    128       /*  $db = AbstractDb::getObject(); 
    129  
    130         $object = null; 
    131         $id_str = $db->escapeString($id); 
    132         $username_str = $db->escapeString($username); 
    133         $account_origin_str = $db->escapeString($account_origin->getId()); 
    134         $email_str = $db->escapeString($email); 
    135  
    136         $password_hash = $db->escapeString(User :: passwordHash($password)); 
    137         $status = ACCOUNT_STATUS_VALIDATION; 
    138         $token = User :: generateToken(); 
    139  
    140         $db->execSqlUpdate("INSERT INTO users (user_id,username, account_origin,email,pass,account_status,validation_token,reg_date) VALUES ('$id_str','$username_str','$account_origin_str','$email_str','$password_hash','$status','$token',CURRENT_TIMESTAMP)"); 
    141  
    142         $object = self::getObject($id); 
    143         return $object;*/ 
     154     
     155    static function getContexts() { 
     156        return array(TT_CONTEXT_USER_ALLOWED => "Authenticated user", 
     157                TT_CONTEXT_USER_VALIDATING => "Validating user", 
     158                TT_CONTEXT_USER_FORGOT_PASSWORD => "User forgot password", 
     159                TT_CONTEXT_GUEST => "Guest user (from another network)", 
     160                TT_CONTEXT_SPLASH_ONLY => "Splash-only user", ); 
     161 
    144162    } 
    145163 
     
    235253    } 
    236254 
    237     function getCreationDate() { 
    238         return $this->_row['token_template_creation_date']; 
    239     } 
    240  
    241     public function setCreationDate($value) { 
    242         // Init values 
    243         $retVal = true; 
    244  
    245         if ($value != $this->getCreationDate()) { 
    246             $db = AbstractDb::getObject(); 
    247             $value = $db->escapeString($value); 
    248             $retVal = $db->execSqlUpdate("UPDATE token_templates SET creation_date = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
    249             $this->refresh(); 
    250         } 
    251         return $retVal; 
    252     } 
    253      
    254255    function getMaxIncomingData() { 
    255256        return $this->_row['token_max_incoming_data']; 
     
    313314        if ($value != $this->getMaxConnectionDuration()) { 
    314315            $db = AbstractDb::getObject(); 
    315             $value = $db->escapeString($value); 
    316             $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_connection_duration = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
     316            $value = ($value != ''?"'".$db->escapeString($value)."'": 'null'); 
     317            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_connection_duration = {$value} WHERE token_template_id = '{$this->getId()}'", false); 
    317318            $this->refresh(); 
    318319        } 
     
    330331        if ($value != $this->getMaxUsageDuration()) { 
    331332            $db = AbstractDb::getObject(); 
    332             $value = $db->escapeString($value); 
     333            $value = ($value != ''?"'".$db->escapeString($value)."'": 'null'); 
    333334            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_usage_duration = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
    334335            $this->refresh(); 
     
    338339     
    339340    function getMaxWallClockDuration() { 
    340         return $this->_row['token_max_usage_duration']; 
     341        return $this->_row['token_max_wall_clock_duration']; 
    341342    } 
    342343 
     
    347348        if ($value != $this->getMaxWallClockDuration()) { 
    348349            $db = AbstractDb::getObject(); 
    349             $value = $db->escapeString($value); 
    350             $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_usage_duration = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
     350            $value = ($value != ''?"'".$db->escapeString($value)."'": 'null'); 
     351            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_wall_clock_duration = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
    351352            $this->refresh(); 
    352353        } 
     
    363364        if ($value != $this->getMaxAge()) { 
    364365            $db = AbstractDb::getObject(); 
     366            $value = ($value != ''?"'".$db->escapeString($value)."'": 'null'); 
     367            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_age = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
     368            $this->refresh(); 
     369        } 
     370        return $retVal; 
     371    } 
     372     
     373    function getContext() { 
     374        return $this->_row['token_template_context']; 
     375    } 
     376 
     377    public function setContext($value) { 
     378        $retVal = true; 
     379 
     380        if ($value != $this->getContext()) { 
     381            $db = AbstractDb::getObject(); 
    365382            $value = $db->escapeString($value); 
    366             $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_max_age = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
     383            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_template_context = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
     384            $this->refresh(); 
     385        } 
     386        return $retVal; 
     387    } 
     388     
     389    function getDescription() { 
     390        return $this->_row['token_template_description']; 
     391    } 
     392 
     393    public function setDescription($value) { 
     394        $retVal = true; 
     395 
     396        if ($value != $this->getDescription()) { 
     397            $db = AbstractDb::getObject(); 
     398            $value = $db->escapeString($value); 
     399            $retVal = $db->execSqlUpdate("UPDATE token_templates SET token_template_description = '{$value}' WHERE token_template_id = '{$this->getId()}'", false); 
    367400            $this->refresh(); 
    368401        } 
     
    386419        return $retVal; 
    387420    } 
     421     
    388422 
    389423    public static function generateToken() { 
     
    443477        return $html; 
    444478    } 
    445  
    446     /** Get the selected user, IF one was selected and is valid 
    447      * @param $user_prefix A identifier provided by the programmer to recognise it's generated form 
    448      * @param &$errMsg An error message will be appended to this is the username is not empty, but the user doesn't exist. 
    449      * @return the User object, or null if the user is invalid or none was selected 
    450      * TODO: implement this 
     479     
     480    /** 
     481     * Get an interface to add/edit token templates linked to a network 
     482     * 
     483     * @param string $user_prefix            A identifier provided by the 
     484     *                                       programmer to recognise it's 
     485     *                                       generated HTML form 
     486     * @param Network $network               The network to get the ui for 
     487     * @return string HTML markup 
     488 
    451489     */ 
    452     static function processSelectUserUI($user_prefix, &$errMsg) { 
    453         /*$object = null; 
    454         try { 
    455             $network = Network :: processSelectUI($user_prefix); 
    456             $name = "select_user_{$user_prefix}_username"; 
    457             if (!empty ($_REQUEST[$name])) { 
    458                 $username = $_REQUEST[$name]; 
    459                 return self :: getUserByUsernameOrEmail($username, $errMsg); 
    460             } else 
    461             return null; 
    462         } catch (Exception $e) { 
    463             return null; 
    464         }*/ 
    465     } 
    466  
    467     // TODO: implement this 
     490    public static function getTemplatesForNetworkUI($user_prefix, $network) { 
     491 
     492        $db = AbstractDb :: getObject(); 
     493 
     494        $networktemplates = self::getTemplatesForNetwork($network); 
     495        $object_id = $db->escapeString($network->getId()); 
     496         
     497        // Get the list of all contexts to see which are taken 
     498        $contexts = self::getContexts(); 
     499        $html = "<table class='token_template_management'>\n"; 
     500        $html .= "<th>" . _('Context') . '</th><th>' . _('Actions') . '</th>' . "\n"; 
     501         
     502        
     503        foreach ($networktemplates as $template) { 
     504             
     505            $html .= "<tr class='token_template'>\n"; 
     506            /* Display page */ 
     507            $html .= "<td>" . $contexts[$template->getContext()] . "</td>\n"; 
     508            $html .= "<td>"; 
     509            $name = $object_id . "_tt_" . $template->getId() . "_" . $template->getContext() . "_edit"; 
     510            $html .= "<input type='button' class='submit' name='$name' value='" . _("Edit") . "' onClick='window.open(\"" . GENERIC_OBJECT_ADMIN_ABS_HREF . "?object_class=TokenTemplate&action=edit&object_id=" . $template->getId() . "\");'>\n"; 
     511            $html .= "</td>\n"; 
     512            $html .= "</tr>\n"; 
     513            unset($contexts[$template->getContext()]); 
     514        } 
     515         
     516        // Add new token for remaining context 
     517        if (!empty($contexts)) { 
     518            $choices = array(); 
     519            $i = 0; 
     520            foreach ($contexts as $key => $value) { 
     521                $choices[$i][0] = $key; 
     522                $choices[$i][1] = $value; 
     523                $i++; 
     524            } 
     525            $html .= "<tr>"; 
     526            $html .= "<td>" . FormSelectGenerator::generateFromArray($choices, null, $user_prefix, 'tt_add', false) . "</td>\n"; 
     527            $name = $object_id . "_tt_add"; 
     528            $html .= "<td><input type='submit' class='submit' name='$name' value='" . _("Add") . "'></td>\n"; 
     529            $html .= "</tr>\n"; 
     530    
     531        } 
     532        $html .= "</table>\n"; 
     533         
     534        return $html; 
     535    } 
     536     
     537    /** 
     538     * processes the interface to add / edit token templates 
     539     *  
     540     * @param string $user_prefix            A identifier provided by the 
     541     *                                       programmer to recognise it's 
     542     *                                       generated HTML form 
     543     * @param Network $network               The network to get the ui for 
     544     * @return string HTML markup 
     545 
     546     */ 
     547    public static function processTemplatesForNetworkUI($user_prefix, $network) { 
     548 
     549        $db = AbstractDb :: getObject(); 
     550 
     551        $networktemplates = self::getTemplatesForNetwork($network); 
     552        $object_id = $db->escapeString($network->getId()); 
     553         
     554        // Get the list of all contexts to see which are taken 
     555        $contexts = self::getContexts(); 
     556         
     557        $name = $object_id . "_tt_add"; 
     558        if (!empty($_REQUEST[$name])) { 
     559            $context = FormSelectGenerator::getResult($user_prefix, 'tt_add'); 
     560            self::createNewObject(null, $network, $context); 
     561        } 
     562   
     563    } 
     564 
    468565    public function getAdminUI() { 
     566        Security::requirePermission(Permission::P('NETWORK_PERM_EDIT_NETWORK_CONFIG'), $this->getNetwork()); 
     567        require_once('classes/InterfaceElements.php'); 
     568        // Init values 
     569        $html = ''; 
     570         
    469571        /* 
    470         $db = AbstractDb::getObject(); 
    471         $currentUser = self :: getCurrentUser(); 
    472         $userPreferencesItems = array(); 
    473         $finalHtml = ''; 
    474         if(Security::hasPermission(Permission::P('NETWORK_PERM_VIEW_STATISTICS'), $this->getNetwork())) { 
    475             // Statistics  
    476             $content = "<a href='".BASE_SSL_PATH."admin/stats.php?Statistics=".$this->getNetwork()->getId()."&distinguish_users_by=user_id&stats_selected_users=".$this->getUsername()."&UserReport=on&user_id=".$this->getId()."&action=generate'>"._("Get user statistics")."</a>\n"; 
    477             $administrationItems[] = InterfaceElements::genSectionItem($content); 
    478  
    479             // Account status  
    480             $title = _("Account Status"); 
    481             $help = _("Note that Error is for internal use only"); 
    482             $name = "user_" . $this->getId() . "_accountstatus"; 
    483             global $account_status_to_text; 
    484             $content = FormSelectGenerator::generateFromKeyLabelArray($account_status_to_text, $this->getAccountStatus(), $name, null, false); 
    485             $administrationItems[] = InterfaceElements::genSectionItem($content, $title, $help); 
    486  
    487             $finalHtml .= InterfaceElements::genSection($administrationItems, _("Administrative options")); 
    488         } 
    489  
    490         if (($this == $currentUser && !$this->isSplashOnlyUser() )|| Security::hasPermission(Permission::P('NETWORK_PERM_EDIT_ANY_USER'), $this->getNetwork())) { 
    491             // Username  
    492             $title = _("Username"); 
    493             $name = "user_" . $this->getId() . "_username"; 
    494             $content = "<input type='text' name='$name' value='" . htmlentities($this->getUsername()) . "' size=30><br/>\n"; 
    495             $content .= _("Be careful when changing this: it's the username you use to log in!"); 
    496             $userPreferencesItems[] = InterfaceElements::genSectionItem($content, $title); 
    497  
    498  
    499             // Email  
    500             $title = _("Email"); 
    501             $name = "email_" . $this->getId() . "_email"; 
    502             $content = "<input type='text' name='$name' disabled='disabled' value='" . htmlentities($this->getEmail()) . "' size=30><br/>\n"; 
    503             $content .= _("If you wish to change this address, please Email Support!"); 
    504             $userPreferencesItems[] = InterfaceElements::genSectionItem($content, $title); 
    505  
    506  
    507  
    508             // Change password  
    509             $changePasswordItems=array(); 
    510             if($this == $currentUser) {//Don't enter the old password if changing password for another user 
    511                 $title = _("Your current password"); 
    512                 $name = "user_" . $this->getId() . "_oldpassword"; 
    513                 $content = "<input type='password' name='$name' size='20'>\n"; 
    514                 $changePasswordItems[] = InterfaceElements::genSectionItem($content, $title); 
    515             } 
    516  
    517             $title = _("Your new password"); 
    518             $name = "user_" . $this->getId() . "_newpassword"; 
    519             $content = "<input type='password' name='$name' size='20'>\n"; 
    520             $changePasswordItems[] = InterfaceElements::genSectionItem($content, $title); 
    521  
    522             $title = _("Your new password (again)"); 
    523             $name = "user_" . $this->getId() . "_newpassword_again"; 
    524             $content = "<input type='password' name='$name' size='20'>\n"; 
    525             $changePasswordItems[] = InterfaceElements::genSectionItem($content, $title); 
    526  
    527             $userPreferencesItems[] = InterfaceElements::genSection($changePasswordItems, _("Change my password")); 
    528  
    529             $finalHtml .= InterfaceElements::genSection($userPreferencesItems, _("User preferences"), false, false, get_class($this)); 
    530  
    531             //N.B: For now, let pretend we have only one profile per use... 
    532             $profiles = $this->getAllProfiles(); 
    533             $current_profile = null; 
    534             if(!empty($profiles)) { 
    535                 $current_profile = $profiles[0]; 
    536             } 
    537  
    538             if($current_profile != null) { 
    539                 $finalHtml .= $current_profile->getAdminUI(); 
    540                 $name = "user_" . $this->getId() . "_delete_profile_".$current_profile->getId(); 
    541                 $value = _("Completely delete my public profile"); 
    542                 $finalHtml .= "<div class='admin_element_tools'>"; 
    543                 $finalHtml .= '<input type="submit" class="submit" name="' . $name . '" value="' . $value . '">'; 
    544                 $finalHtml .= "</div>"; 
    545             } 
    546             else {                    // Get the list of profile templates for the users' network 
    547                 $profile_templates = ProfileTemplate::getAllProfileTemplates($this->getNetwork()); 
    548                 if(!empty($profile_templates)) { 
    549                     $name = "user_" . $this->getId() . "_add_profile"; 
    550                     $value = _("Create my public profile"); 
    551                     $finalHtml .= "<div class='admin_element_tools'>"; 
    552                     $finalHtml .= '<input type="submit" class="submit" name="' . $name . '" value="' . $value . '">'; 
    553                     $finalHtml .= "</div>"; 
    554                 } 
    555             } 
    556         } 
    557  
    558         return $finalHtml;*/ 
    559     } 
    560  
    561     // TODO Implement this 
     572         * Verify if an error message was registered before outputting the form 
     573         */ 
     574        if (isset($this->errorMsg)) { 
     575             $html .= "<div class='errormsg'>" . $this->errorMsg . "</div>"; 
     576        } 
     577        $tt_id = $this->getId(); 
     578         
     579        /* 
     580         * Begin with admin interface 
     581         */ 
     582        $html .= "<fieldset class='admin_container ".get_class($this)."'>\n"; 
     583        $html .= "<legend>"._("Token template")."</legend>\n"; 
     584        $html .= "<ul class='admin_element_list'>\n"; 
     585 
     586        /* 
     587         * Template information 
     588         */ 
     589        $html_tt_information = array(); 
     590 
     591        // template id 
     592        $title = _("Token Template Id"); 
     593        $data = htmlspecialchars($tt_id, ENT_QUOTES); 
     594        $html_tt_information[] = InterfaceElements::generateAdminSectionContainer("tt_id", $title, $data); 
     595         
     596        // template context 
     597        $title = _("Token Template Context"); 
     598        $contexts = self::getContexts(); 
     599        $data = htmlspecialchars(_($contexts[$this->getContext()]), ENT_QUOTES); 
     600        $html_tt_information[] = InterfaceElements::generateAdminSectionContainer("tt_context", $title, $data); 
     601         
     602         // template description 
     603        $_title = _("Description"); 
     604        $name = "tt_" . $tt_id . "_description"; 
     605        $data = "<textarea name='$name' cols=80 rows=5 id='tt_description_textarea'>\n".$this->getDescription()."\n</textarea>\n"; 
     606        $html_tt_information[] = InterfaceElements::generateAdminSectionContainer("tt_description", $_title, $data); 
     607         
     608        // is reusable 
     609        $title = _("Template reusable"); 
     610        $help = _("Are tokens generated from this template reusable?"); 
     611        $data = InterfaceElements::generateInputCheckbox("tt_" . $tt_id . "_reusable", "", _("Yes"), $this->getIsReusable(), "tt_reusable_radio"); 
     612        $html_tt_information[] = InterfaceElements::generateAdminSectionContainer("tt_reusable", $title, $data, $help); 
     613         
     614        // Build section 
     615        $html .= InterfaceElements::generateAdminSectionContainer("tt_information", _("Information about the token template"), implode(null, $html_tt_information)); 
     616         
     617         
     618        /* 
     619         * TT's data information 
     620         */ 
     621        $html_tt_data_properties = array(); 
     622            
     623        // max incoming data 
     624        $title = _("Max incoming data"); 
     625        $help = _("The maximum number of bytes of data this token can receive"); 
     626        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_incoming", $this->getMaxIncomingData(), "tt_max_incoming_input"); 
     627        $html_tt_data_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_incoming", $title, $data, $help); 
     628         
     629        // max outoing data 
     630        $title = _("Max outgoing data"); 
     631        $help = _("The maximum number of bytes of data this token can send"); 
     632        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_outgoing", $this->getMaxOutgoingData(), "tt_max_outgoing_input"); 
     633        $html_tt_data_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_outgoing", $title, $data, $help); 
     634         
     635         // max total data 
     636        $title = _("Max total data"); 
     637        $help = _("The maximum total number of bytes this token can send and receive"); 
     638        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_total_data", $this->getMaxTotalData(), "tt_max_total_data_input"); 
     639        $html_tt_data_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_data", $title, $data, $help); 
     640         
     641        // Build section 
     642        $html .= InterfaceElements::generateAdminSectionContainer("tt_data", _("Data limits"), implode(null, $html_tt_data_properties)); 
     643        
     644         
     645        /* 
     646         * TT's data information 
     647         */ 
     648        $html_tt_duration_properties = array(); 
     649            
     650        // max incoming data 
     651        $title = _("Max connection duration"); 
     652        $help = _("The length of a single connection."); 
     653        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_conn_duration", $this->getMaxConnectionDuration(), "tt_max_conn_duration_input"); 
     654        $html_tt_duration_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_conn_duration", $title, $data, $help); 
     655         
     656        // max outoing data 
     657        $title = _("Max usage"); 
     658        $help = _("The maximum time a user can be online (counting only when used).  Allows access by the hour."); 
     659        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_usage", $this->getMaxUsageDuration(), "tt_max_usage_input"); 
     660        $html_tt_duration_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_usage", $title, $data, $help); 
     661         
     662         // max total data 
     663        $title = _("Max wall clock duration"); 
     664        $help = _("The maximum amount of time a token can be used, starting count when first used.  Allows weekly, daily tokens."); 
     665        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_wc_duration", $this->getMaxWallClockDuration(), "tt_max_wc_duration_input"); 
     666        $html_tt_duration_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_wc_duration", $title, $data, $help); 
     667         
     668        // max total data 
     669        $title = _("Max age"); 
     670        $help = _("The maximum age before a token expires, starting count when issued."); 
     671        $data = InterfaceElements::generateInputText("tt_" . $tt_id . "_max_age", $this->getMaxAge(), "tt_max_age_input"); 
     672        $html_tt_duration_properties[] = InterfaceElements::generateAdminSectionContainer("tt_max_age", $title, $data, $help); 
     673         
     674        // Build section 
     675        $html .= InterfaceElements::generateAdminSectionContainer("tt_duration", _("Duration limits"), implode(null, $html_tt_duration_properties), 
     676                 _("For these values, any postgresql interval is acceptable, for example '1 day', '1 month', '3 week'")); 
     677        
     678        return $html; 
     679    } 
     680 
    562681    public function processAdminUI() { 
    563        /* $db = AbstractDb::getObject(); 
    564         $currentUser = self :: getCurrentUser(); 
    565         if (Security::hasPermission(Permission::P('NETWORK_PERM_EDIT_ANY_USER'), $this->getNetwork())) { 
    566             // Account status  
    567             $name = "user_" . $this->getId() . "_accountstatus"; 
    568             $status = FormSelectGenerator::getResult($name, null); 
    569             $this->setAccountStatus($status); 
    570         } 
    571  
    572         if ($this == $currentUser || Security::requirePermission(Permission::P('NETWORK_PERM_EDIT_ANY_USER'), $this->getNetwork())) { 
    573             // Username  
    574             $name = "user_" . $this->getId() . "_username"; 
    575             $this->setUsername($_REQUEST[$name]); 
    576  
    577             // Change password  
    578             $nameOldpassword = "user_" . $this->getId() . "_oldpassword"; 
    579             $nameNewpassword = "user_" . $this->getId() . "_newpassword"; 
    580             $nameNewpasswordAgain = "user_" . $this->getId() . "_newpassword_again"; 
    581             if($_REQUEST[$nameNewpassword]!=null){ 
    582                 if ($this == $currentUser && $this->getPasswordHash() != User::passwordHash($_REQUEST[$nameOldpassword])) { 
    583                     throw new Exception(_("Wrong password.")); 
    584                 } 
    585                 if ($_REQUEST[$nameNewpassword] != $_REQUEST[$nameNewpasswordAgain]){ 
    586                     throw new Exception(_("Passwords do not match.")); 
    587                 } 
    588                 $this->setPassword($_REQUEST[$nameNewpassword]); 
    589             } 
    590  
    591             // Pretend there is only one 
    592             $profiles = $this->getAllProfiles(); 
    593             if(!empty($profiles)) { 
    594                 $current_profile = $profiles[0]; 
    595                 if($current_profile != null) { 
    596                     $current_profile->processAdminUI(); 
    597                     $name = "user_" . $this->getId() . "_delete_profile_".$current_profile->getId(); 
    598                     if(!empty($_REQUEST[$name])) { 
    599                         $errmsg=null; 
    600                         $current_profile->delete($errmsg); 
    601                     } 
    602                 } 
    603             } 
    604             else { 
    605                 $name = "user_" . $this->getId() . "_add_profile"; 
    606                 if(!empty($_REQUEST[$name])) { 
    607                     // Get the list of profile templates for the users' network 
    608                     $profile_templates = ProfileTemplate::getAllProfileTemplates($this->getNetwork()); 
    609                     if(!empty($profile_templates)) { 
    610                         // Create a blank profile and link it to the user 
    611                         $current_profile = Profile::createNewObject(null, $profile_templates[0]); 
    612                         $this->addProfile($current_profile); 
    613                     } 
    614                 } 
    615  
    616             } 
    617  
    618         }*/ 
     682        Security::requirePermission(Permission::P('NETWORK_PERM_EDIT_NETWORK_CONFIG'), $this->getNetwork()); 
     683        $tt_id = $this->getId(); 
     684             
     685        // template description 
     686        $name = "tt_" . $tt_id . "_description"; 
     687        $this->setDescription($_REQUEST[$name]); 
     688             
     689        // is reusable 
     690        $name = "tt_" . $tt_id . "_reusable"; 
     691        $this->setIsReusable(empty ($_REQUEST[$name]) ? false : true); 
     692         
     693        // max incoming data 
     694        $name = "tt_" . $tt_id . "_max_incoming"; 
     695        $this->setMaxIncomingData($_REQUEST[$name]); 
     696             
     697        // max outoing data 
     698        $name = "tt_" . $tt_id . "_max_outgoing"; 
     699        $this->setMaxOutgoingData($_REQUEST[$name]); 
     700          
     701        // max total data 
     702        $name = "tt_" . $tt_id . "_max_total_data"; 
     703        $this->setMaxTotalData($_REQUEST[$name]); 
     704         
     705        // max incoming data 
     706        $name = "tt_" . $tt_id . "_max_conn_duration"; 
     707        $this->setMaxConnectionDuration($_REQUEST[$name]); 
     708         
     709        // max outoing data 
     710        $name = "tt_" . $tt_id . "_max_usage"; 
     711        $this->setMaxUsageDuration($_REQUEST[$name]); 
     712        
     713        // max total data 
     714        $name = "tt_" . $tt_id . "_max_wc_duration"; 
     715        $this->setMaxWallClockDuration($_REQUEST[$name]); 
     716         
     717        // max total data 
     718        $name = "tt_" . $tt_id . "_max_age"; 
     719        $this->setMaxAge($_REQUEST[$name]); 
     720         
    619721    } 
    620722     
     
    626728     */ 
    627729    public function canGenerateForUser($user, $mac) { 
    628         return true; 
     730        // First make sure the user has the right status for this template 
     731        $retval = true; 
     732        if ($user instanceof User) { 
     733            switch($this->getContext()) { 
     734                case TT_CONTEXT_USER_ALLOWED: 
     735                    $retval = ($user->getAccountStatus() == ACCOUNT_STATUS_ALLOWED);                        
     736                    break; 
     737                case TT_CONTEXT_USER_VALIDATING: 
     738                    $retval = ($user->getAccountStatus() == ACCOUNT_STATUS_VALIDATION);  
     739                    break; 
     740                case TT_CONTEXT_USER_FORGOT_PASSWORD: 
     741                     
     742                    break; 
     743                case TT_CONTEXT_GUEST: 
     744                    $retval = ($user->getNetwork()->getId() != Network::getCurrentNetwork()->getId()); 
     745                    break; 
     746                case TT_CONTEXT_SPLASH_ONLY: 
     747                    $retval = ($user->isSplashOnlyUser());  
     748                    break; 
     749                default: 
     750                    $retval = false; 
     751            } 
     752             
     753        } 
     754        return $retval; 
    629755       /* $retval = false; 
    630756        // A new token can be generated if 
     
    687813    } 
    688814 
    689     /** Set Smarty template values.  Standardization routine.  
    690      * // TODO: implement this*/ 
    691     public static function assignSmartyValues($smarty, $user = null) { 
    692         /*if (!$user) 
    693         $user = User :: getCurrentUser(); 
    694         $session = Session :: getObject(); 
    695         $smarty->assign('userOriginallyRequestedURL', $session ? $session->get(SESS_ORIGINAL_URL_VAR) : ''); 
    696         $smarty->assign('userId', $user ? $user->getId() : ''); 
    697         $smarty->assign('userName', $user ? $user->getUsername() : '');*/ 
    698         /** 
    699          * Define user security levels for the template 
    700          * 
    701          * These values are used in the default template of WiFoDog but could be 
    702          * used in a customized template to restrict certain links to specific 
    703          * user access levels.  Note however that they will all be deprecateb by the 
    704          * new roles system. 
    705          */ 
    706         /*$smarty->assign('userIsValid', $user && !$user->isSplashOnlyUser() ? true : false); 
    707         $smarty->assign('userDEPRECATEDisSuperAdmin', $user && $user->DEPRECATEDisSuperAdmin()); 
    708  
    709         if (isset ($_REQUEST['debug_request']) && ($user && $user->DEPRECATEDisSuperAdmin())) { 
    710             // Tell Smarty everything it needs to know 
    711             $smarty->assign('debugRequested', true); 
    712             $smarty->assign('debugOutput', print_r($_REQUEST, true)); 
    713         }*/ 
    714     } 
    715815} 
    716816