XF 2.0 how to add addGlobalPermission in xenforo 2

HQCoder

Member
I added the following to the xf_permission_entry table.
$db->query("insert ignore into xf_permission_entry (user_group_id, user_id, permission_group_id, permission_id, permission_value, permission_value_int) values
(?, 0, 'general', 'viewxxx', 'allow', '0'),
(?, 0, 'general', 'addxxx', 'allow', '0')
", array(1,4));
But then I debug with the \XF::dump() command not showing the added permissions.
PHP:
$visitor = \XF::visitor();
echo "<pre>";
\XF::dump($visitor['PermissionSet']);
echo "<pre>";
Is it necessary to rebuild xf_permission_entry or is there another way?
 
Probably best not to directly update xf_permission_entry with an insert query, there is a function to handle this in AbstractSetup (don't recall exactly what it's called, but you can look at XFRM or XFMG to find it or through AbstractSetup)

Also, you don't need <pre> with \XF::dump. it handles it for you :)
 
thanks @Jake B.
Probably best not to directly update xf_permission_entry with an insert query, there is a function to handle this in AbstractSetup (don't recall exactly what it's called, but you can look at XFRM or XFMG to find it or through AbstractSetup)

Also, you don't need <pre> with \XF::dump. it handles it for you :)
PHP:
public function upgrade1010031Step2()
    {
        $db = $this->db();

        try
        {
            $userGroupIds = $db->fetchAllColumn("
                SELECT user_group_id
                FROM xf_user_group
            ");
            $categoryUpdates = $db->fetchPairs("
                SELECT resource_category_id, allow_submit_user_group_ids
                FROM xf_resource_category
                WHERE allow_submit_user_group_ids <> '-1'
            ");
            foreach ($categoryUpdates AS $categoryId => $groups)
            {
                $allowGroupIds = explode(',', $groups);
                foreach ($userGroupIds AS $userGroupId)
                {
                    $db->query("
                        REPLACE INTO xf_permission_entry_content
                            (content_type, content_id, user_group_id, user_id, permission_group_id, permission_id, permission_value, permission_value_int)
                        VALUES
                            ('resource_category', ?, ?, 0, 'resource', 'add', ?, 0)
                    ", [
                        $categoryId, $userGroupId, in_array($userGroupId, $allowGroupIds) ? 'content_allow' : 'reset'
                    ]);
                }
            }
        }
        catch (\XF\Db\Exception $e) {}
    }
XFRM use a query to update xf_permission_entry too :)
 
thanks @Jake B.

PHP:
public function upgrade1010031Step2()
    {
        $db = $this->db();

        try
        {
            $userGroupIds = $db->fetchAllColumn("
                SELECT user_group_id
                FROM xf_user_group
            ");
            $categoryUpdates = $db->fetchPairs("
                SELECT resource_category_id, allow_submit_user_group_ids
                FROM xf_resource_category
                WHERE allow_submit_user_group_ids <> '-1'
            ");
            foreach ($categoryUpdates AS $categoryId => $groups)
            {
                $allowGroupIds = explode(',', $groups);
                foreach ($userGroupIds AS $userGroupId)
                {
                    $db->query("
                        REPLACE INTO xf_permission_entry_content
                            (content_type, content_id, user_group_id, user_id, permission_group_id, permission_id, permission_value, permission_value_int)
                        VALUES
                            ('resource_category', ?, ?, 0, 'resource', 'add', ?, 0)
                    ", [
                        $categoryId, $userGroupId, in_array($userGroupId, $allowGroupIds) ? 'content_allow' : 'reset'
                    ]);
                }
            }
        }
        catch (\XF\Db\Exception $e) {}
    }
XFRM use a query to update xf_permission_entry too :)

That's for the category permissions, I think the function you want is $this->addGlobalPermission or something along those lines. Don't have xf2 open currently, but take a look for something like that in src/XF/AddOn/AbstractSetup :)
 
That's for the category permissions, I think the function you want is $this->addGlobalPermission or something along those lines. Don't have xf2 open currently, but take a look for something like that in src/XF/AddOn/AbstractSetup :)
thank you so much. I have found a solution from your help
 
Top Bottom