2

Does anyone know of a module similar to the "Hide Block by Role" module, but for nodes?

The scenario I'm looking to achieve is:

  • User 1 has Role A and Role B
  • User 2 has Role A and Role C

I need to be able to create a node and limit access to Role A and Role B, but also hide/block it for Role C. So, User 1 would see the node but User 2 would not. Currently, because User 2 has Role A, he is able to see the page which is why I need something similar to "Hide Block By Role".

I'm currently using the Content Access module to control access.

1

Rules (the module) is your friend ... Here is a prototype of a rule (in Rules export format) that you could use (assuming that the Role ID (rid) for Role B is "4", adapt the value of this rid to fit your case):

{ "rules_limit_access_to_some_urls" : {
    "LABEL" : "Limit access to some URLs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "NOT user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      },
      { "data_is" : {
          "data" : [ "site:current-page:path" ],
          "op" : "IN",
          "value" : { "value" : [
              "node\/123",
              "node\/456"
            ]
          }
        }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "access_denied" } },
      { "drupal_message" : {
          "message" : "Access to the content located at \u0022\u003Cstrong\u003E[site:current-page:path]\u003C\/strong\u003E\u0022 is not allowed.",
          "type" : "warning"
        }
      }
    ]
  }
}

After you enable the above rule, you can grant Access (view) permission to the nodes "node/123" and "node/456" for users with role A (using the Content Access module). That will already prevent access to these nodes for any user who does not have that role A.

The above rule will ensure that users who do not have role B, will NOT be able to actually "view" these nodes. Instead they'll get a message starting with "Access to the content located at ..." (change this message to whatever fits).

|improve this answer|||||
  • Thanks, this has been a big help! Can rules also be applied to views? This effectively blocks the user from viewing the page, but I'd also like to hide links to the pages in any views for those same users. – Mike Nov 9 '16 at 13:17
  • Thanks @Pierre.Vriens :). I don't think I have any more questions, aside from how I can hide items in a view for a specific role. I'll probably post a new question about that, I haven't had much luck searching online. – Mike Nov 9 '16 at 19:03
  • @Mike : maybe this answer somehow helps you with imiting access to such views. Though it's probably only a partial solution for your case. If it doesn't help: ask a new quesion! – Pierre.Vriens Nov 9 '16 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.