public function run() { // get instance with test database $mongo = Mongo::getInstance(array('dbName' => 'jormakaleviTest2')); // create user /** * @var User $user */ $user = Mongo::factory('jormakalevi\\User', array('username' => 'testset2')); // give all rights to User class $user->addPermission(null, get_class($user)); // set as active user $mongo->setUser($user); // create 1000 User objects to database, measure how long it took $num = 1000; $timerID = $this->_start(); for ($i = 1; $i <= $num; $i++) { $u = $mongo::factory('jormakalevi\\User', array('username' => 'testuser_' . $i)); $u->save(); } $spent = $this->_stop($timerID); echo 'Created ' . $num . ' User documents to database in ' . sprintf('%f', $spent) . ' seconds' . PHP_EOL; $randomNum = mt_rand(1, $num); $randomUser = $mongo::factory('jormakalevi\\User')->findOneClass(array('username' => 'testuser_' . $randomNum)); // uncomment the following line if you want to see user object //echo json_encode($randomUser, JSON_PRETTY_PRINT) . PHP_EOL; assert(strcmp($randomUser->username, 'testuser_' . $randomNum) === 0, 'Username should match.'); // drop test database $mongo->getDB()->drop(); }
/** * @param array $options * @return Mongo */ public static function getInstance(array $options = array()) { if (is_null(Mongo::$_instance)) { Mongo::$_instance = new Mongo($options); } return Mongo::$_instance; }
public function run() { // get instance with test database $mongo = Mongo::getInstance(array('dbName' => 'jormakaleviTest1')); // create user /** * @var User $user */ $user = Mongo::factory('jormakalevi\\User', array('username' => 'testset1')); // give all rights to User class $user->addPermission(null, get_class($user)); // set as active user $mongo->setUser($user); // try to find existing, saved user - using permissions $existingUser = $mongo::factory('jormakalevi\\User')->findOneClass(array('username' => 'testset1')); if (!is_null($existingUser)) { $user = $existingUser; } // test that User found/created is an instance of User assert($user instanceof User, 'User is not an instance of user.'); // user should be able to save users, because both create and update permissions are set assert($user->save(), 'Saving user failed.'); // test if user is able to eat User documents try { $userCanEatUsers = $user->can('jormakalevi\\User', 'eat'); } catch (\Exception $e) { $userCanEatUsers = false; } assert($userCanEatUsers === false, 'Users cannot eat users (or probably not use any other foobar permission types)'); // test that user is not able to create Documents, because no such permission has been given assert(false === $user->can('jormakalevi\\Document', 'create'), 'User should not be able to create this class.'); // drop test database $mongo->getDB()->drop(); }
/** * @param array $query * @param array $sort * @param null|int $limit * @param null|int $skip * @return array */ public function find(array $query = array(), array $sort = array(), $limit = null, $skip = null) { if (!Mongo::getInstance()->isAuthDisabled()) { // TODO: add support for $and and $or queries if (isset($query['className'])) { if (is_array($query['className']) && !empty($query['className']['$in'])) { $newIn = array(); foreach ($query['className']['$in'] as $className) { if (Mongo::getInstance()->getUser()->can($className, 'read')) { $newIn[] = $className; } } if (empty($newIn)) { return array(); } $query['className']['$in'] = $newIn; } elseif (is_string($query['className']) && !Mongo::getInstance()->getUser()->can($query['className'], 'read')) { return array(); } } else { $query['className'] = array('$in' => Mongo::getInstance()->getUser()->getPermissions('read')); } } $cursor = $this->_getCollection()->find($query, array('sort' => $sort, 'limit' => $limit, 'skip' => $skip)); $results = array(); foreach ($cursor as $r) { $r = $r->getArrayCopy(); if (empty($r['className'])) { // TODO: logging continue; } $results[] = Mongo::factory($r['className'], $r); } return $results; }