0

On my websites I have forums that mirror the structure of og-based groups. I am using advanced forums, OG Forum D7 and a decent amount of custom code.

My problem is to restrict access/visibility to forums based on the og role. While I know how to do it with forum topics which are stored as nodes, I have problems with forums which under a hood are taxonomy terms.

I am looking for a place where I can hook the code restricting visibility of a taxonomy term.

function mymodule_taxonomy_access($taxonomyterm, $op, $account) {
    if( $op == 'view' )
        $gid = get_grop_from_term($taxonomyterm);
        if( user_has_forum_access($gid, $account))
                return ACCESS_ALLOW;
        //else
        return ACCESS_DENY;
    }
}

I have no idea in which module I should look for the right hook: taxonomy, forum, forum_access, core (I think that taxonomy terms are entities)?

Taxonomy Access Control while being very powerful, provides no connection with organic groups.

UPDATE I have found that TCL Lite is using the hook_query_TAG_alter to hide taxonomy terms. The hook is editing all queries made to the database and is effectively filtering the taxonomy_terms that any query can see. This looks like an awfully heavy solution.

Can anyone suggest a better approach?

2
  • 1
    You should check out this Community post. Although it will not answer your specific question you might get some ideas.
    – Triskelion
    May 27, 2013 at 17:14
  • I have implemented all that they want to do. In the future (if my project is continued) I am going to completely get rid of OG modules and use only taxonomy for groups, subgroups, roles and access control. For the time being though I need a quick solution, so I only need to be able to control access to taxonomy terms from the level of my module.
    – Pawel G
    May 28, 2013 at 7:39

1 Answer 1

0

After a small fight I managed to have it solved. There are few issues to have in mind like caching or preventing infinite recursion (you don't want to change the queries that you use inside the query altering function).

The code is based on TAC module.

function mymodule_query_term_access_alter($query) {
    static $mymodule_recursion_flag = FALSE;

    //prevent infinite recursion altering db_queries inside of this function
    if($mymodule_recursion_flag) return;

    // Take no action if there is no term table in the query.
    $alias = '';
    $tables =& $query->getTables();
    foreach ($tables as $i => $table) {
        if (strpos($table['table'], 'taxonomy_term_') === 0) {
            $alias = $table['alias'];
        }
    }
    if (empty($alias)) {
        return;
    }

    //store once obtained results
    static $mymodule_tids = null;
    if($mymodule_tids == null){
        //set the recursion flag
        $mymodule_recursion_flag = TRUE;
        // Fetch a list of all terms the user should not see
        $mymodule_tids = mymodule_get_taxonomy_black_list();
        //unset the recursion flag
        $mymodule_recursion_flag = FALSE;
    }
    // No restrictions - user can list all terms
    if (empty($mymodule_tids)) {
        return;
    }
    // Otherwise, filter the terms
    else {
        $query->condition($alias . ".tid", $mymodule_tids, "NOT IN");
    }
}

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.