XF 2.2 Add-on help: Permissions for thread type creation (article and question thread types)

Wildcat Media

Well-known member
I'm trying to build an add-on that will allow me to set permissions on who can create article and question threads. My knowledge of this stuff is at a "...For Dummies" level, so I'm flying solo here trying to figure out how to get this to work.

So far I have "borrowed" code from @Idhae here which is saved in /src/addons/WMG/ArticleCreatePermission/XF/Entity/Forum.php :

PHP:
<?php
namespace WMG\ArticleCreatePermission\XF\Entity;

class Forum extends XFCP_Forum
{
    public function getCreatableThreadTypes(){
        $parent = parent::getCreatableThreadTypes();
        if(\XF::visitor()->hasNodePermission($this->node_id, 'wmg_articleCreate')){
            return $parent;
        }
        else {
            if($key = array_search('article' ,$parent) !== false){
                unset($parent[$key]);
            }
            return $parent;
        }
    }
}

I created this in Permission Definitions:

1607111454659.webp

And I added this to Class Extensions (execution order 10):

1607111522465.webp

I have a test forum with all four thread types enabled. With a registered user (non-staff) account, I can create all four thread types.

Result: With the add-on enabled, and setting permission to Inherit, No or Never, it is removing the Poll thread type, but leaving the Article thread type. With permission set to Yes, the Poll thread type reappears.

I created an almost identical add-on that will add a permission for the Question thread type. Same result--it removes the Poll thread type.

Am I missing a step, or is the logic in the PHP code doing something unintended?
 
Hi,
This code line is wrong:
PHP:
if($key = array_search('article' ,$parent) !== false)

I have to look what exactly...I hope I can help you tomorrow.
 
$parent contains:

Code:
array(4) {
  [0] => string(10) "discussion"
  [1] => string(4) "poll"
  [2] => string(7) "article"
  [3] => string(8) "question"
}

Within the else{if{, $key contains:

Code:
bool(true)

...and after unsetting the key for the article thread type, $parent contains:

Code:
array(3) {
  [0] => string(10) "discussion"
  [2] => string(7) "article"
  [3] => string(8) "question"
}

The array_search should be returning a key of [2], since [2] => "article". But it's unsetting a key of [1] => "poll".

So...if I change this line:

if($key = array_search('article' ,$parent) !== false){

...to this:

if($key = array_search('article' ,$parent)){

...I get the proper key removed.

Code:
//$parent before array_search
array(4) {
  [0] => string(10) "discussion"
  [1] => string(4) "poll"
  [2] => string(7) "article"
  [3] => string(8) "question"
}

$key after array_search
int(2)

$parent after array_search
array(3) {
  [0] => string(10) "discussion"
  [1] => string(4) "poll"
  [3] => string(8) "question"
}

....and here's the end result:

1607132961107.png

My only concern is that if there is anything catastrophic that will happen by removing the !==false statement.

I suspect the boolean "true" was being passed as a "1" to $key, which kept making the Poll article type disappear.
 
One observation now that I've deployed it into the live forum as an add-on: permissions don't seem to affect the nodes when I set the permissions on the category...but only partially...? If I set it so only staff (admins, mods, contributors) can create article threads, in a category, the forum nodes below don't get that "yes" permission. Yet I set the Registered usergroup to "No" for permissions, and that seems to be inherited. Or at any rate, standard members don't see the Article thread type.

So as is, it needs to be applied to each individual general discussion forum. Not a big deal, as the add-on does what I need it to.
 
that's how i implemented it
It's working on a live forum now, with no issues, built and installed as an add-on. Thanks for the code!

A similar add-on can be created by renaming the addon and new permission, and changing 'article' to 'question'. That is working for me as well.

I would release these as add-ons but everything is here in the thread for someone to make their own. Although, I could be coerced if needed... 😁
 
I could have sworn there was a permission for poll creation! (It's a good thing we haven't had to revoke that permission--we've certainly had some poll abuse over the years.) I checked XF 1.5 and it doesn't have that permission either.
 
Top Bottom