首页 \ 问答 \ 用于MongoDB的CRUD操作认证(CRUD operation authentication for mongodb)

用于MongoDB的CRUD操作认证(CRUD operation authentication for mongodb)

我正在使用zend 3框架和mongodb。 使用mongodb / mongodb库连接到mongodb数据库。

我如何在zend和mongodb中添加验证,以便只有经过验证的用户才能使用zend rest apis在mongodb中执行CRUD操作?

从mongo shell我已经使用下面的查询添加了身份验证数据库工作正常。

use admin;
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"

use test;
db.createUser(
   {
     user: "testUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
);

db.auth("testUser", "password");

现在在Zend模型中,我使用了以下代码,该代码在没有db认证的情况下工作正常。

$mongoClient = new \MongoDB\Client();
$collection = $mongoClient->selectDatabase($dbName)->selectCollection($collectionName);
$cursor = $collection->findOne(['_id' => $_id]);

现在如何在执行上述查询之前将用户凭据传递给\MongoDB\Client()以对用户进行身份验证?


I'm using zend 3 framework and mongodb. Connected to mongodb database using mongodb/mongodb library.

How I can add validation in zend and mongodb so that only authenticated users can perform CRUD operation in mongodb using zend rest apis?

From mongo shell I've added authentication to db using below queries which works fine.

use admin;
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"

use test;
db.createUser(
   {
     user: "testUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
);

db.auth("testUser", "password");

Right now In Zend model I'm using following code which works fine without db authentication.

$mongoClient = new \MongoDB\Client();
$collection = $mongoClient->selectDatabase($dbName)->selectCollection($collectionName);
$cursor = $collection->findOne(['_id' => $_id]);

Now how I can pass user credentials to \MongoDB\Client() to authenticate user before executing above query?


原文:https://stackoverflow.com/questions/49228910
更新时间:2024-01-20 19:01

最满意答案

不在remove_if的情况下,因为语义不同。 std::remove_if实际上并没有从容器中清除任何东西,而list::remove_if却是这样,所以你肯定不希望前者调用后者。

无论是你还是实现都可以从字面上专门化容器的通用算法,因为算法是采用迭代器的函数模板,而容器本身就是类模板,其迭代器类型取决于模板参数。 所以为了使std::remove_if专门用于list<T>::iterator你需要remove_if部分特化,并且不存在函数模板的部分特化。

我不记得是否允许实现为特定的迭代器类型重载算法,但即使不是“官方”算法可以调用可能被重载的函数,也可以使用可能部分专用的类。 不幸的是,如果您编写了自己的容器并且已经发现了一种使标准算法对其效率更高的方法,那么这些技术都不会帮助您。

例如,假设你有一个带有随机访问迭代器的容器,但是你可以使用一种特别有效的排序技术来处理标准排序:可能是一种桶排序。 然后,你可能会考虑在类中使用一个自由函数template <typename T> void sort(MyContainer<T>::iterator first, MyContainer<T>::iterator last) ,并允许用户调用它using std::sort; sort(it1, it2); using std::sort; sort(it1, it2); 而不是std::sort(it1, it2); 。 问题是,如果他们在泛型代码中这样做,他们冒着别人写一些其他容器类型的函数会有一个名为sort的函数,甚至不会对范围进行排序(英文单词“sort”具有多个含义,之后所有)。 所以基本上你不能以一种能够提高用户定义容器效率的方式对一个迭代器范围进行一般排序。

当代码中的差异仅取决于迭代器的类别时(例如std::distance对于随机访问迭代器来说是快速的,否则就会变慢),这是通过使用称为“迭代器标签调度”的东西来完成的,这是最常见的在不同容器之间存在明显效率差异的情况下。

如果存在任何适用于标准容器的剩余情况(折扣结果不同或者效率只需要特定迭代器类别的情况),让我们拥有它们。


Not in the case of remove_if, since the semantics are different. std::remove_if doesn't actually erase anything from the container, whereas list::remove_if does, so you definitely don't want the former calling the latter.

Neither you nor the implementation can literally specialize the generic algorithms for containers because the algorithms are function templates that take iterators, and the containers are themselves class templates, whose iterator type depends on the template parameter. So in order to specialize std::remove_if generically for list<T>::iterator you would need a partial specialization of remove_if, and there ain't no such thing as a partial specialization of a function template.

I can't remember whether implementations are allowed to overload algorithms for particular iterator types, but even if not the "official" algorithm can call a function that could be overloaded, or it can use a class that could be partially specialized. Unfortunately none of these techniques help you if you've written your own container, and have spotted a way to make a standard algorithm especially efficient for it.

Suppose for example you have a container with a random-access iterator, but where you have an especially efficient sort technique that works with the standard ordering: a bucket sort, perhaps. Then you might think of putting a free function template <typename T> void sort(MyContainer<T>::iterator first, MyContainer<T>::iterator last) in the same namespace as the class, and allow people to call it with using std::sort; sort(it1, it2); instead std::sort(it1, it2);. The problem is that if they do that in generic code, they risk that someone else writing some other container type will have a function named sort that doesn't even sort the range (the English word "sort" has more than one meaning, after all). So basically you cannot generically sort an iterator range in a way that picks up on efficiencies for user-defined containers.

When the difference in the code depends only on the category of the iterator (for example std::distance which is fast for random access iterators and slow otherwise), this is done using something called "iterator tag dispatch", and that's the most common case where there's a clear efficiency difference between different containers.

If there are any remaining cases that apply to standard containers (discounting ones where the result is different or where the efficiency only requires a particular iterator category), let's have them.

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)