addPermission (function)

Last updated: Fri, Apr 14, 2023 1:58 pm
Return to Knowledgebase

Purpose

Adds a permission level to a user

Location

users/helpers/permissions.php

Parameters

# Parameter Data Type Required Description
1 $permission_ids integer Yes Single id or array of permission ids
2 $members integer Yes Single id or array of user ids

Returns

Data Type Description of Returned Data
int The number of new records it created.

Example

addPermission([2,3,4], [2,36,38])

Further Documentation:

This is primarily used by the UI and is not generally used by developers. You can add an array of permissions to an array of users. In the example above, you would add permission ids 2,3, and 4 to users 2,36 and 38.

addPermission(2, 36);

The function takes two parameters: $permission_ids and $members.

$permission_ids is expected to be either a single integer value or an array of integer values representing permission IDs.

$members is expected to be either a single integer value or an array of integer values representing user IDs.

The function first initializes a counter variable $i to zero.
It checks if $permission_ids is an array. If it is, then the function loops through each permission ID in the array and executes a database query to insert a new record into the user_permission_matches table with the user ID ($members) and the current permission ID. If the query is successful, the counter $i is incremented.

If $permission_ids is not an array but $members is an array, then the function loops through each user ID in the array and executes a database query to insert a new record into the user_permission_matches table with the current user ID and the permission ID ($permission_ids). If the query is successful, the counter $i is incremented.

The function returns the final value of the counter $i, which represents the number of successful database insertions.