# Permission

The `Xmf\Module\Helper\Permission` class is a module aware helper for simplified access to system group permissions in a module.

System group permissions use a string *$gperm\_name* as a permission name, and an integer *$gperm\_itemid* to identify a item that the permission applies to. The permissions are further identified by the mid of the module the permission relates to, and the groupid the permission applies to.

This helper streamlines using permissions by automatically using module and group information that can be inferred.

`Xmf\Module\Helper\Permission` extends [Xmf\Module\Helper\AbstractHelper](https://github.com/xoops/xmf-cookbook/tree/2971b4bb568db7c6791e293e50ffc917d75ed81f/en/book/module/abstracthelper.php).

## new Permission(*$dirname*)

Creates the permission helper for the module specified by name as *$dirname*. If the string *$dirname* is empty, the current module in XOOPS will be user.

## checkPermission(*$gperm\_name*, *$gperm\_itemid*, *$trueifadmin*)

Check if the user has the permission named *$gperm\_name* for the item identified by *$gperm\_itemid*.

Normally an admin user always has permission. If *$trueifadmin*, an optional boolean, is specified as *false*, the admin user's permissions will be checked like any normal user.

Returns true if the user has the permission, otherwise false.

## getItemIds(*$gperm\_name*, *$gperm\_groupid*)

Returns an array of items for which the group(s) specified in *$gperm\_groupid* have the permission named in *$gperm\_name*.

## checkPermissionRedirect(*$gperm\_name*, *$gperm\_itemid*, *$url*, *$time*, *$message*, *$trueifadmin*)

Check if the user has the permission named *$gperm\_name* for the item identified by *$gperm\_itemid*. If not, the session will be redirected to the module relative URL specified in *$url* with the message in *$message*.

## getGroupsForItem(*$gperm\_name*, *$gperm\_itemid*)

Get array of groups granted permissions for the permission named *$gperm\_name* for the item identified by *$gperm\_itemid*.

Normally an admin user always has permission. If *$trueifadmin*, an optional boolean, is specified as *false*, the admin user's permissions will be checked like any normal user.

Returns an array of integer group ids.

## savePermissionForItem(*$gperm\_name*, *$gperm\_itemid*, *$groups*)

Grant the permission named *$gperm\_name* for the item identified by *$gperm\_itemid* to all of the groups identified in the array *$groups*.

## deletePermissionForItem(*$gperm\_name*, *$gperm\_itemid*)

Delete the permission named *$gperm\_name* for the item identified by *$gperm\_itemid* for all groups.

Alternately, *$gperm\_name* can be an array of permission names, and all the named permissions for the specified item will be deleted for all groups.

The intended use of this method is to in response to the item *$gperm\_itemid* being deleted.

public object

## getGroupSelectFormForItem(*$gperm\_name*, *$gperm\_itemid*, *$caption*, *$name*, *$include\_anon*, *$size*, *$multiple*)

Creates a XoopsFormSelectGroup form element to select groups with permission to a specific *$gperm\_name* and *$gperm\_itemid*. The form element will be preset with existing permissions.

The caption for the element is specified in *$caption*.

If the name *$name* is empty or omitted, a default name created by `defaultFieldName()` will be used.

Set *$include\_anon* (default false) to true to include the anonymous group. The size in *$size* (default 5) sets number of vertical rows shown in the element. If *$multiple* (default true) is true, multiple groups can be assigned to the permission.

## defaultFieldName(*$gperm\_name*, *$gperm\_itemid*)

Generate a default name to be used for the XoopsFormSelectGroup based on *$gperm\_name* and *$gperm\_itemid*.

If you do not specify the name in `getGroupSelectFormForItem()` this allows you to determine the element name to retrieve the user input.

Returns a string.
