2013-02-22 48 views
6

ArangoDB एक लचीला बहु-मॉडल डेटाबेस सर्वर है जिसमें बहुत अच्छी सुविधाएं और बहुत अच्छे दस्तावेज़ हैं। यह एक युवा, बहुत ही आशाजनक ओपन सोर्स प्रोजेक्ट है जो बढ़ते समुदाय के साथ है लेकिन शुरू करने के लिए कई वास्तविक विश्व उदाहरण नहीं हैं।ARangoDB के साथ PHP में उपयोगकर्ता पंजीकरण और प्रमाणीकरण कैसे करें?

एक आम असली दुनिया उदाहरण, उपयोगकर्ता पंजीकरण और प्रमाणीकरण है। वहां अधिकांश अनुप्रयोगों में इसकी आवश्यकता है।

तो, ARangoDB के साथ PHP में उपयोगकर्ता पंजीकरण और प्रमाणीकरण कैसे करें?

उत्तर

6

आप निम्न उदाहरण कोड सीधे चला सकते हैं और यह कुछ काल्पनिक उपयोगकर्ता डेटा प्रदान करके उपयोगकर्ता पंजीकरण और प्रमाणीकरण के माध्यम से चलाएगा।
यह प्रत्येक चरण को प्रदर्शित करेगा जो यह कर रहा है। संग्रह-निर्माण से, उपयोगकर्ता पंजीकरण, प्रमाणीकरण और अंत में संग्रह को फिर से साफ करना।
ऐसी कई टिप्पणियां भी हैं जो बताती हैं कि क्या किया जा रहा है, इसे समझने में आसान बनाने के लिए।

बस इस कोड को एक फ़ाइल में डालें, अपने पर्यावरण के अनुसार autoload.php पर पथ कॉन्फ़िगर करें और ब्राउज़र के साथ इसके लिंक पर जाएं। इस कोड को ArangoDB 1.2 and up के साथ-साथ ArangoDB-PHP client version 1.2 और ऊपर की आवश्यकता है।
यह उम्मीद ArangoDB स्थानीय होस्ट पर चलने की और

Note1 बंदरगाह 8529. पर सुन: स्क्रिप्ट स्वचालित रूप से 'उन' संग्रह और 'उपयोगकर्ता नाम' पर एक अद्वितीय छोड़-सूची सूचकांक पैदा करता है। यह संग्रह को अंत में भी छोड़ देगा।
यदि आप स्वचालित रूप से हाथ से संग्रह बनाना चाहते हैं, तो आपको उन हिस्सों पर टिप्पणी करने की आवश्यकता है जहां संग्रह और अनुक्रमणिका बनाई गई है और साथ ही साथ संग्रह को छोड़ दिया गया है। कि ArangoDB (arangosh) के लिए एक खोल खोल
के बाद और उस में निम्न कमांड चलाएँ:

arangosh> db._createDocumentCollection('users'); 
arangosh> db.users.ensureUniqueSkiplist("username"); 

यदि आप संग्रह, प्रकार ड्रॉप करना चाहते हैं:

arangosh> db.users.drop(); 


टिप्पणी 2 : मैंने इसे सरल रखने के लिए जानबूझकर अधिक ओओ शैली, जैसे उपयोगकर्ता ऑब्जेक्ट्स, एड्रेस ऑब्जेक्ट्स आदि को शुरू करने से बचा है।

तो आखिर में यहां लिपि है।

<?php 

namespace triagens\ArangoDb; 


// use this and change it to the path to autoload.php of the arangodb-php client if you're using the client standalone... 
// require __DIR__ . '/../vendor/triagens/ArangoDb/autoload.php'; 

// ...or use this and change it to the path to autoload.php in the vendor directory if you're using Composer/Packagist 
require __DIR__ . '/../vendor/autoload.php'; 


// This function will provide us with our pre-configured connection options. 
function getConnectionOptions() 
{ 
    $traceFunc = function ($type, $data) { 
     print "TRACE FOR " . $type . PHP_EOL; 
    }; 

    return array(
     ConnectionOptions::OPTION_ENDPOINT  => 'tcp://localhost:8529/', 
     // endpoint to connect to 
     ConnectionOptions::OPTION_CONNECTION => 'Close', 
     // can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections) 
     ConnectionOptions::OPTION_AUTH_TYPE  => 'Basic', 
     // use basic authorization 
     /* 
     ConnectionOptions::OPTION_AUTH_USER  => '',      // user for basic authorization 
     ConnectionOptions::OPTION_AUTH_PASSWD  => '',      // password for basic authorization 
     ConnectionOptions::OPTION_PORT   => 8529,     // port to connect to (deprecated, should use endpoint instead) 
     ConnectionOptions::OPTION_HOST   => "localhost",    // host to connect to (deprecated, should use endpoint instead) 
     */ 
     ConnectionOptions::OPTION_TIMEOUT  => 5, 
     // timeout in seconds 
     //ConnectionOptions::OPTION_TRACE   => $traceFunc,    // tracer function, can be used for debugging 
     ConnectionOptions::OPTION_CREATE  => false, 
     // do not create unknown collections automatically 
     ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST, 
     // last update wins 
    ); 
} 


// This function tries to persist the user data into the database upon registration 
// it will fail if a user with the same username already exists. 
function register($connection, $username, $password, $registrationData) 
{ 
    // This would be where you call the function that encrypts your password like you did for storage earlier 
    $hashedPassword = md5($password); 

    // assign the collection to a var (or type it directly into the methods parameters) 
    $collectionId = 'users'; 

    //create an example document or an array in order to pass to the following byExample method 
    $document = Document::createFromArray(
     array('username' => $username, 'password' => $hashedPassword, 'data' => $registrationData) 
    ); 

    // Get an instance of the collection handler 
    $documentHandler = new DocumentHandler($connection); 

    try { 
     // query the given $collectionId by example using the previously declared $exampleDocument array 
     $result = $documentHandler->add($collectionId, $document); 

     // return the result; 
     return $result; 
    } catch (Exception $e) { 

     if ($e->getCode()) { 
      echo ('User already exists... '); 
     } else { 
      // any other error 
      echo ('An error occured. Exception: ' . $e); 
     } 
    } 
} 


// This function tries to authenticate the user and will return an array with its data 
function authenticate($connection, $username, $password) 
{ 
    // This would be where you call the function that encrypts your password like you did for storage earlier 
    $hashedPassword = md5($password); 

    // assign the collection to a var (or type it directly into the methods parameters) 
    $collectionId = 'users'; 

    //create an example document or an array in order to pass to the following byExample method 
    $exampleDocumentArray = array('username' => $username, 'password' => $hashedPassword); 

    // Get an instance of the collection handler 
    $documentHandler = new CollectionHandler($connection); 

    try { 
     // query the given $collectionId by example using the previously declared $exampleDocument array 
     $cursor = $documentHandler->byExample($collectionId, $exampleDocumentArray); 
     // check if the count of the cursor is one or not. 
     if ($cursor->getCount() == 1) { 
      // do some fancy login stuff here... 

      // get the current document from the cursor 
      $userDocument = $cursor->current(); 

      // set session uid to the document key that was set automatically by ArangoDB, 
      // since we didn't provide our own on registration 
      $_SESSION['uid'] = $userDocument->getKey(); 

      // extract and return the document in form of an array 
      return $userDocument->getAll(); 
     } else { 
      return false; 
     } 
    } catch (Exception $e) { 
     echo ('An error occured. Exception: ' . $e . '<br>'); 
    } 
} 


// register the connection to ArangoDB 
$connection = new Connection(getConnectionOptions()); 


// register a collection handler to work with the 'users' collection 
$collectionHandler = new CollectionHandler($connection); 


// create the 'users' collection... 
// remark those lines if you want to create the collection by hand. 
echo "creating 'users' collection..."; 
try { 
    $collection = new Collection(); 
    $collection->setName('users'); 
    $collectionHandler->create($collection); 
    echo "created.<br>"; 
} catch (Exception $e) { 
    echo ('Could not create collection. Exception: ' . $e . '<br>'); 
} 


// create unique skip list index in 'users' collection on field ''username'... 
// remark those lines if you want to create the index by hand. 
echo "creating unique skip list index in 'users' collection on field ''username'... "; 
try { 
    $collection = new Collection(); 
    $collection->setName('users'); 
    $collectionHandler->index('users', 'skiplist', array('username'), true); 
    echo "created.<br>"; 
} catch (Exception $e) { 
    echo ('Could not create skip list index. Exception: ' . $e . '<br>'); 
} 


// let's assume those variables hold your username/password 
$userNameProvided = 'jane'; 
$passwordProvided = 'mysecretpassword'; 

// here we pass some structured registration data 
$registrationData = array(
    'name'  => 'Jane', 
    'surname' => 'Doe', 
    'addresses' => array(
     'email' => array('[email protected]', '[email protected]'), 
     'home' => array(
      array('street' => 'Brooklyn Ave.', 'number' => 10), 
      array('street' => '54th Street', 'number' => 340, 'is_primary' => true) 
     ) 
    ) 
); 

// First register 
echo "trying to register user for the first time... "; 
$result = register($connection, $userNameProvided, $passwordProvided, $registrationData); 
if ($result) { 
    echo " " . $userNameProvided . " registered<br>"; 
} else { 
    echo "failed<br>"; 
} 


// Trying to register user with same username a second time 
echo "trying to register user with same username a second time... "; 
$result = register($connection, $userNameProvided, $passwordProvided, $registrationData); 
if ($result) { 
    echo "registered<br>"; 
} else { 
    echo "failed<br>"; 
} 


// now authenticate with the correct username/password combination 
echo "trying to authenticate with the correct username/password combination... "; 
if ($userArray = authenticate($connection, $userNameProvided, $passwordProvided)) { 
    echo "login successful. "; 
    echo '<br>'; 
    // do some fancy after-login stuff here... 
    echo "<br>Welcome back " . $userArray['username'] . '!<br>'; 
    if (count($userArray['data']['addresses']['email']) > 0) { 
     echo "Your primary mail address is " . $userArray['data']['addresses']['email'][0] . '<br>'; 
    } 
    foreach ($userArray['data']['addresses']['home'] as $key => $value) { 
     if (array_key_exists('is_primary', $value)) { 
      $homeAddress = $userArray['data']['addresses']['home'][$key]; 
      echo "Your primary home address is " . $homeAddress['number'] . ', ' . $homeAddress['street'] . '<br>'; 
      // if found, break out of the loop. There can be only one... primary address! 
      break; 
     } 
    } 
} else { 
    // re-display login form. +1 the wrong-login counter... 
    echo "wrong username or password<br>"; 
} 
echo '<br>'; 

// now authenticate with the wrong username/password combination 
echo "trying to authenticate with the wrong username/password combination... "; 
if (authenticate($connection, $userNameProvided, 'I am a wrong password')) { 
    // do some fancy after-login stuff here... 
    echo "login successful<br>"; 
} else { 
    // re-display login form. +1 the wrong-login counter... 
    echo "wrong username or password<br>"; 
} 


// truncate the collection... not needed if dropping, but only here to empty the collection of its tests 
// in case you decide to not create and drop the collection through this script, but by hand. 
echo "truncating collection..."; 
try { 
    $collectionHandler->truncate('users'); 
    echo "truncated.<br>"; 
} catch (Exception $e) { 
    die ('Could not truncate collection. Exception: ' . $e . '<br>'); 
} 


// finally drop the collection... 
// remark those lines if you want to drop the collection by hand. 
echo "dropping collection..."; 
try { 
    $collectionHandler->drop('users'); 
    echo "dropped.<br>"; 
} catch (Exception $e) { 
    die ('Could not drop collection. Exception: ' . $e . '<br>'); 
}