How to stop users from flagging other users if they have a custom taxonomy term? I'm working on a project where users vote other users using the Flag module. Each user has a department term reference field. I would like to stop a user from flagging another user if they have the same department. Is it possible? Please help! Thanks in advanced!
Finally I managed to solve my problem by creating a Rule.
Here are the steps you can follow to achieve this:


Create a new Rule and select as the Event "A user has been flagged, under 'your flag'";
Add a condition "Entity has field" and select "flagging-user" as the entity and your field as the "Field";
Add a new condition "List contains item" and select as a list your created field but it must be like this "flagging-user:field-your-field";
Now at the same condition select as the item one of your field value;
Add a AND condition;
Add two more conditions exactly like you did at step 2, 3 and 4 but change "flagging-user" to "flagged-user".
Finally, add an action with "Unflag a User" and select your flag first, then at the User select "flagged-user", at the "User on whose behalf to flag" select "flagging-user" and at "Skip permission check" check "True".


That's it. Hope I helped someone else.

As requested here's an exported code of the actual Rule:

{ "rules_nu_poate_vota_manager" : {
"LABEL" : "Nu poate vota manager",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "flag" ],
"ON" : { "flag_flagged_apreciere" : [] },
"IF" : [
  { "entity_has_field" : { "entity" : [ "flagging-user" ], "field" : "field_raion" } },
  { "list_contains" : { "list" : [ "flagging-user:field-raion" ], "item" : "18" } },
  { "AND" : [] },
  { "entity_has_field" : { "entity" : [ "flagged-user" ], "field" : "field_raion" } },
  { "list_contains" : { "list" : [ "flagged-user:field-raion" ], "item" : "18" } }
],
"DO" : [
  { "flag_unflaguser" : {
      "flag" : "apreciere",
      "user" : [ "flagged-user" ],
      "flagging_user" : [ "flagging_user" ],
      "permission_check" : "1"
    }
  },
  { "drupal_message" : {
      "message" : "Nu po\u0163i aprecia colegii de echip\u0103!",
      "type" : "warning"
    }
  }
]


}
}