Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for pre / post middleware in search #15

Open
jlopezr opened this issue Mar 3, 2017 · 3 comments
Open

Add a test for pre / post middleware in search #15

jlopezr opened this issue Mar 3, 2017 · 3 comments

Comments

@jlopezr
Copy link
Contributor

jlopezr commented Mar 3, 2017

@Abaena please can you put a comment with the pre / post code that you are using.

@Abaena
Copy link

Abaena commented Mar 3, 2017

// pre middleware

function filterQuery(Model, req, res, next) {
  if(req.user && req.user.role == "admin") {
    return next();
  } else {
    req.body.query["isActive"] = true;
    return next();
  }
}

// post middleware

function filterActive(config, req, res, doc, next){
  if(doc) {
    if (!req.user || !req.user.role || req.user.role != 'admin') {
        if (doc.constructor == Array) {
            var _docs = doc.filter(function (d) {
                return d.isActive == true;
            });
            next(null, config, req, res, _docs);
        } else {
            if (doc.isActive==true) {
                next(null, config, req, res, doc);
            } else {
                next(null, config, req, res, null);
            }
        }
    }else{
        next(null, config, req, res, doc);
    }
  }else{
    next(null, config, req, res, doc);
  }
}

// injector configuration

search: {
    profiles:{
        _default:{
            populate: ["tags","category",authorWithoutPwd],
            pre: [filterQuery],
            // post: [filterActive],
            mongo: {
                projection: {_id:1, title:1, isActive:1, date:1},
                options: {
                    sort: '-date'
                }
            }
        }
    }
},

@GerardSoleCa
Copy link
Member

Hi,

What is exactly happening here? I mean, found a bug, have a problem with "routeinjector hooks"?

@jlopezr
Copy link
Contributor Author

jlopezr commented Mar 4, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants