XF 2.2 Set permission by default to Yes

You can’t. Permissions don’t have default values.

You can programmatically set the permission value during installation of an add-on with PHP code.

Check for our usages of applyGlobalPermission and applyGlobalPermissionInt and applyContentPermission for examples.

Primarily we set the permission values based on existing permission values so it only applies to groups where it makes sense.
 
I did what @Chris D said, but it applied to all user groups, including to unregistered members.

Is it possible to set the permission according to user group or limit only to registered members user group?
 
You need to pass all four arguments if you don't want it to apply to all user groups. The last two arguments are the permission group and ID that you want to depend on.
 
I still didn't understand. There is no dependence on my side.

For example, what is "general" and "view" on this permission?
$this->applyGlobalPermission('forum', 'viewOthers', 'general', 'view');

The user groups are: unregistered, registered, administrative and moderating, that are set in xf_user_group as 1, 2, 3 and 4 respectively and I can't see mentions to them in the method above.

Can you please give a practical example how to set for registered members only?
 
There is no dependence on my side.
If you want to apply permissions conditionally you will need to depend on an existing permission.

For example, what is "general" and "view" on this permission?
It corresponds to a permission definition:

screenshot-caysBO.webp

Can you please give a practical example how to set for registered members only?
Depend on a permission that registered users have that other groups don't, like (forum, postThread).
 
Ok, now I got it, but in my opinion should focus on user groups not on other permissions in this case.
I ended up using the method query specifying the user group in the installation step. It worked here. Does it make sense?

Code:
    public function installStep7()
    {
        $db = $this->db();

        $applyGroupId = 'fox_social';
        $applyPermissionIds = [
            'canAddProfile',
            'canSendMessage',
            'showLatestPosts'
        ];

        foreach ($applyPermissionIds as $applyPermissionId) {
            $db->query("
            REPLACE INTO xf_permission_entry
                (user_group_id, user_id, permission_group_id, permission_id, permission_value, permission_value_int)
            SELECT 2, user_id, ?, ?, 'allow', 0
            FROM xf_permission_entry
            WHERE user_group_id = 2
        ", [$applyGroupId, $applyPermissionId]);
        }
    }
 
Ok, now I got it, but in my opinion should focus on user groups not on other permissions in this case.
Some sites may have more complicated user-group set ups (users must be promoted into a new usergroup to create content, multiple/different hierarchies, etc.), so depending on an existing permission is usually a safer bet. Nothing inherently wrong with your approach though.
 
Back
Top Bottom