Show
Ignore:
Timestamp:
07/21/07 00:59:33 (5 years ago)
Author:
benoitg
Message:
  • At long last, implement #9: Automatic new node creation. When attempting to login from an unknown node, the user (if he has the permissions) will be prompted to create the node, or "steal" en existing one (for hardware swaps).
  • Refactor Node:getSelectNodeUI().
  • Menu.php: Fix small oversight causing menu not to clear.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/wifidog/classes/Node.php

    r1260 r1261  
    278278     * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form 
    279279     * 
    280      * @param $sql_additional_where Addidional where conditions to restrict the candidate objects 
    281      * @param $type_interface:  select, select_multiple or table 
    282      * @param $selectedNodes; Node object or array of node objects to be pre-selected (not 
     280     * @param string $userData=null Array of contextual data optionally sent to the method. 
     281     *  The function must still function if none of it is present. 
     282     * This method understands: 
     283     *  $userData['preSelectedObject'] An optional object to pre-select. 
     284     *  $userData['additionalWhere'] Additional SQL conditions for the 
     285     *                                    objects to select 
     286     *  $userData['additionalJoin'] Additional SQL JOIN conditions for the 
     287     *                                    objects to select 
     288     *  $userData['preSelectedObjects'] An optional object or array of objects to pre-select. (not 
    283289     * supported by type_interface=table) 
     290     *  $userData['typeInterface'] select, select_multiple or table.  Default is "select" 
     291     * 
    284292     * * @return html markup 
    285293     */ 
    286     public static function getSelectNodeUI($user_prefix, $sql_additional_join = null, $sql_additional_where = null,$selectedNodes = null, $type_interface = "select") 
    287     { 
     294    public static function getSelectUI($user_prefix, $userData=null) 
     295    { 
     296        !empty($userData['additionalJoin'])?$sql_additional_join=$userData['additionalJoin']:$sql_additional_join=null; 
     297        !empty($userData['additionalWhere'])?$sql_additional_where=$userData['additionalWhere']:$sql_additional_where=null; 
     298        !empty($userData['preSelectedObjects'])?$selectedNodes=$userData['preSelectedObjects']:$selectedNodes=null; 
     299        !empty($userData['typeInterface'])?$type_interface=$userData['typeInterface']:$type_interface="select"; 
     300 
    288301        $db = AbstractDb::getObject(); 
    289302        $html = ''; 
     
    380393     * @return the node object 
    381394     */ 
    382     static function processSelectNodeUI($user_prefix) 
     395    static function processSelectUI($user_prefix) 
    383396    { 
    384397        $object = null; 
     
    455468        return $retval; 
    456469    } 
     470 
     471 
     472    /** Get an interface to deal with missing nodes.  If the user has the permissions, he will be asked to create a new node for that gateway id, or assign that gateway id to an existing node. 
     473     * @param $gwId The unknown gwId 
     474     * @return html markup 
     475     */ 
     476    public static function getStealOrCreateNewUI($gwId) 
     477    { 
     478        $permissionArray[]=array(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), null); 
     479        $permissionArray[]=array(Permission::P('NODE_PERM_EDIT_GATEWAY_ID'), null); 
     480        Security::requireAnyPermission($permissionArray); 
     481        $db = AbstractDb::getObject(); 
     482        $html = ''; 
     483        $allowedNetworks = Security::getObjectsWithPermission(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG')); 
     484        $allowedNodes = Security::getObjectsWithPermission(Permission::P('NODE_PERM_EDIT_GATEWAY_ID')); 
     485        $html .= "<p>"._("Here is what you can do to fix this:")."</p>\n"; 
     486        $html .= "<ul>\n"; 
     487        if($allowedNetworks) { 
     488            //Add a new node for unknown node id 
     489            $html .= "<li>".sprintf(_("You can create a new node with %s as it's associated gateway id.  This is typical for new installations."), $gwId)."<br/>\n"; 
     490 
     491            $networkAdditionalWhere=" AND (FALSE\n"; 
     492            foreach ($allowedNetworks as $network) { 
     493                $idStr = $db->escapeString($network->getId()); 
     494                $networkAdditionalWhere .= " OR network_id='$idStr'\n"; 
     495            } 
     496            $networkAdditionalWhere .= ")\n"; 
     497            $userData['preSelectedObject']=null; 
     498            $userData['allowEmpty']=true; 
     499            $userData['additionalWhere']=$networkAdditionalWhere; 
     500            $name = "{$gwId}_new_node_network"; 
     501            $networkSelectUI = Network :: getSelectUI($name, $userData); 
     502            $html .= sprintf(_("Add a new node in %s"), $networkSelectUI)." \n"; 
     503            $name = "{$gwId}_new_node_submit"; 
     504            $value = _("Add node"); 
     505            $html .= "<input type='submit' size='10' name='{$name}' value='$value'>\n"; 
     506            $html .= "</li>\n"; 
     507        } 
     508 
     509        if($allowedNetworks || $allowedNodes){ 
     510            //"Steal" an existing node for this ID (typically for hardware replacement) 
     511            $html .= "<li>".sprintf(_("You can \"steal\" an existing node.  The node's gateway id will be replaced with %s.  This is typical when replacing hardware."), $gwId)."<br/>\n"; 
     512            if($allowedNetworks) { 
     513                $additionalWhere=$networkAdditionalWhere; 
     514            } 
     515            else { 
     516                $additionalWhere=" AND (FALSE\n"; 
     517                foreach ($allowedNetworks as $network) { 
     518                    $idStr = $db->escapeString($node->getId()); 
     519                    $additionalWhere .= " OR node_id='$idStr'\n"; 
     520                } 
     521                $additionalWhere .= ")\n"; 
     522            } 
     523 
     524            $userData['preSelectedObject']=null; 
     525            $userData['allowEmpty']=true; 
     526            $userData['additionalWhere']=$additionalWhere; 
     527            $name = "{$gwId}_steal_node"; 
     528            $html .= Node :: getSelectUI($name, $userData); 
     529            $name = "{$gwId}_steal_node_submit"; 
     530            $value = _("Steal node"); 
     531            $html .= "<input type='submit' size='10' name='{$name}' value='$value'>\n"; 
     532            $html .= "</li>\n"; 
     533        } 
     534        $html .= "</ul>\n"; 
     535        return $html; 
     536 
     537    } 
     538 
     539    /** 
     540     * Process the interface to deal with missing nodes. 
     541     * @param $gwId The unknown gwId 
     542     * @param $nodeIsNew Output parameter.  Will be set to true if a new node was created to resolve the situation 
     543     * @return the created or stolen node object, or null if none was created (or stolen). 
     544     */ 
     545    public static function processStealOrCreateNewUI($gwId, &$nodeIsNew=null) 
     546    { 
     547        // Init values 
     548        $retval = null; 
     549        $nodeIsNew = false; 
     550        $name = "{$gwId}_new_node_submit"; 
     551        if(!empty($_REQUEST[$name])) { 
     552            //Create new node 
     553            $name = "{$gwId}_new_node_network"; 
     554            $network = Network :: processSelectUI($name); 
     555            if($network) { 
     556                Security::requirePermission(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), $network); 
     557                //echo  _("Adding node"); 
     558                $node = Node::createNewObject($gwId, $network); 
     559                $nodeIsNew = true; 
     560                $retval = $node; 
     561            } 
     562        } 
     563        $name = "{$gwId}_steal_node_submit"; 
     564        if(!empty($_REQUEST[$name])){ 
     565            //"Steal" an existing node for this ID (typically for hardware replacement) 
     566            $name = "{$gwId}_steal_node"; 
     567            $node = Node :: processSelectUI($name); 
     568            if($node) { 
     569                $permissionArray[]=array(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), $node->getNetwork()); 
     570                $permissionArray[]=array(Permission::P('NODE_PERM_EDIT_GATEWAY_ID'), $node); 
     571                Security::requireAnyPermission($permissionArray); 
     572                //echo _("Stealing node $node"); 
     573                $node->setGatewayId($gwId); 
     574                $retval = $node; 
     575            } 
     576        } 
     577 
     578 
     579        return $retval; 
     580    } 
     581 
     582 
    457583 
    458584    /**