如果“父"与子文档中的用户名和密码匹配,则护照匹配子文档中的用户名和密码.文档没有任何匹配的用户名和密码 [英] passport match username and password in a sub document if the "parent" document doesn't have any matching username and passwords

查看:72
本文介绍了如果“父"与子文档中的用户名和密码匹配,则护照匹配子文档中的用户名和密码.文档没有任何匹配的用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑设置如下.将有一个登录页面,管理员可以访问,工作人员可以访问.管理员可以这样做,以便工作人员可以看到某些数据(管理员可以创建工作人员).现在的主要问题是将其与护照联系起来.我认为用户文档看起来像这样

I was thinking of setting up an like the following. There will be a login page that an admin can access and a worker can access. the admin can make it so that the workers could see certain data(the admin can create workers). The main problem right now is hooking this up with passport. I think the user document look something like this

{username: "user1", password : "pass1", role : "admin",
  workers :[
    {username : "worker1", password : "workerpass1"},
    {username : "worker2", password : "workerpass2"}]
}

Passport 执行类似以下操作来检查用户名和密码

Passport does something like the following to check for username and password

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function(err, user) {
   ........................

我想知道如何获得工人级别的访问权限,因为我认为上面的护照代码会查看顶部的用户名字段.

I'm wondering how I can get access to the worker level because I think that the passport code above will look at the top username field.

我想我想做这样的事情:如果在尝试登录时如果用户名密码不在顶级,请转到工作人员数组并测试以查看用户名和密码是否与那里匹配

I think I want to do something like this: if on login attempt if username an password is not in the top level go to the workers array and test to see if the username and password matches there

也许如果 User.findOne 没有找到用户做 User.worker.findOne() 我该怎么做?

maybe if User.findOne doesn't find the user do User.worker.findOne() how would I do that?

推荐答案

如果您正在寻找任何组合来匹配您基本上想要一个 $or 查询条件,带有 $elemMatch 从特定的 workers 条目中获取 usernamepassword 组合:

If you are looking for any of the combinations to match the you basically want an $or query condition, with an $elemMatch to get the username and password combination from the specific workers entry:

passport.use(new LocalStrategy(
    function(username,password,done) {
        User.findOne({
            "$or": [
                { "username": username, "password": password },
                {
                    "workers": { "$elemMatch": {
                        "username": username, "password": password
                    }}
                }
            ]
        },function(err,user) {
           // remaining code
        });
    }
))

所以这意味着 $or 数组中列出的条件中的任一个"可以匹配文档,并且 $elemMatch 用于确保两个如果主要的用户名"和密码"在数组外不匹配,则用户名和密码需要在数组元素上匹配.

So that means "either" of the conditions listed within the array of $or can match a document, and the $elemMatch is used to make sure that both "username" and "password" need to match on the array element, if the main "userame" and "password" was not a match outside of the array.

所以它是非此即彼",对数组的检查发生在查询方法的内部",而不是直接作为模型对象之外的属性.

So its "either/or", and the inspection of the array happens "inside" the query method, and not as a property directly off the model object.

这里还要注意,由于用户名"和密码"可能在数组对象中也",那么其他内置的验证方法需要被这里的查询逻辑覆盖.所以查询"需要验证匹配,当返回一个对象时,就是当你返回一个有效"的结果.

Also noting here that since "username" and "password" are possibly "also" within an array object, then other built in validation methods need to be overriden by the query logic here. So the "query" needs to validate the match, and when an object is returned that is when you return a "valid" result.

这就是定制的成本.否则,只需拥有单独的用户帐户,所有帐户都具有您要应用的相同角色.

That's the cost of customisation. Otherwise, just have separate user accounts, all with the same role you want to apply.

这篇关于如果“父"与子文档中的用户名和密码匹配,则护照匹配子文档中的用户名和密码.文档没有任何匹配的用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆