Articles index » phpBB 3.0 » Permissions

Permissions

An article explaining how to add and check permissions.

Submitted by harmlessgoat22 on 19 Apr 2008, 14:39

Permissions
Permissions allow you to completely control who can do what on your forums. It is quite easy to add new permissions, on a per forum basis, user type permissions, moderator type permissions, and admin type permissions.

Adding Permissions
phpBB3 makes it extremely easy to add new permissions. As you can see in the below, there is just a few lines of code to add permissions. You include includes/acp/auth.php, then start the class, and call the method (acl_add_option()). In there, you make an array, and have two keys (you only need one if you are only using one). global is for (you guessed it) global permissions, that are used anywhere on the board. local are limited to one forum. So, for global, you could make things like ACP, UCP, and MCP permissions, and for local, you can decide whether a user can do something in a certain forum, or whether a moderator can do some mod thing in a certain forum.
Code: Select all
// Setup $auth_admin class so we can add permission options
include($phpbb_root_path 'includes/acp/auth.' $phpEx);
$auth_admin = new auth_admin();

// Add permissions
$auth_admin->acl_add_option(array(
    
'global'   => array('u_permission1''m_permission2''a_permission3'),
    
'local'     => array('f_permission1''m_permission2'),
));    

As you might have guessed, you start a permission with u_ for user permissions all across the board, f_ for forum permissions for users, m_ for mod permissions, and a_ for admin permissions.

One thing you need to do though is add a language file for your permission. The file goes into language/{LANG_NAME}/mods/, and must be named in following format: permissions_*.php. For example: language/en/mods/permissions_arcade.php, if you had an arcade MOD. Here's something that can be found in language/en/acp/permissions_phpbb.php:
Code: Select all
/**
*   MODDERS PLEASE NOTE
*
*   You are able to put your permission sets into a separate file too by
*   prefixing the new file with permissions_ and putting it into the acp
*   language folder.
*
*   An example of how the file could look like:
*
*   <code>
*
*   if (empty($lang) || !is_array($lang))
*   {
*      $lang = array();
*   }
*
*   // Adding new category
*   $lang['permission_cat']['bugs'] = 'Bugs';
*
*   // Adding new permission set
*   $lang['permission_type']['bug_'] = 'Bug Permissions';
*
*   // Adding the permissions
*   $lang = array_merge($lang, array(
*      'acl_bug_view'      => array('lang' => 'Can view bug reports', 'cat' => 'bugs'),
*      'acl_bug_post'      => array('lang' => 'Can post bugs', 'cat' => 'post'), // Using a phpBB category here
*   ));
*
*   </code>
*/

Here is an example permissions file (it doesn't add a new permission type though):
Code: Select all
<?php
/**
* permissions_pastebin [English]
*
* @package language
* @version $Id$
* @copyright (c) 2008 yourname
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* DO NOT CHANGE
*/
if (!defined('IN_PHPBB'))
{
   exit;
}

if (empty($lang) || !is_array($lang))
{
   $lang = array();
}

// Adding new category
$lang['permission_cat']['pastebin']   = 'Pastebin';

// Adding the permissions
$lang = array_merge($lang, array(
   // User perms
   'acl_u_pastebin_view'      => array('lang' => 'Can view snippets', 'cat' => 'pastebin'),
   'acl_u_pastebin_post'      => array('lang' => 'Can post snippets', 'cat' => 'pastebin'),
   'acl_u_pastebin_post_novc'   => array('lang' => 'Can post snippets without visual confirmation', 'cat' => 'pastebin'),
   
   // Moderator perms
   'acl_m_pastebin_edit'      => array('lang' => 'Can edit snippets', 'cat' => 'pastebin'),
   'acl_m_pastebin_delete'      => array('lang' => 'Can delete snippets', 'cat' => 'pastebin'),
));

?>

The permissions category is the tab that it is displayed in in the edit permissions screen.

Checking Permissions
Checking permissions is really quite easy. For all of the below, make sure that if you are in a function or class, $auth has been globaled.

acl_get
Code: Select all
if ($auth->acl_get('m_cool_permission') || $auth->acl_get('f_another_cool_permission'2))
{
// code to do if they do have the permissions
}
else
{
//code to do when they don't have the permissions
}    

Above, you can tell that 'm_cool_permission' is global, because it does not specify a forum id. In the other one 'f_another_cool_permission', it is followed by a comma, and then the forum_id it wishes to check the permissions for.

acl_gets
Another quite easy function, works slightly different.
Code: Select all
if ($auth->acl_gets('m_cool_permission''u_another_cool_permission') || $auth->acl_gets('f_another_cool_permission''f_another_crazy_permission''f_a_name_for_a_permission_that_is_probably_getting_really_old_now'$forum_id)    

As you can see, acl_gets checks multiple permissions at a time, making it useful if you need to check a ton of permissions. There is no limit to the amount of permissions you can check. Also, if you are checking local permissions, just follow the last one with a ', $id', with $id being the forum_id to check.
One thing you may like to note is that acl_gets only returns true if all the permissions listed are true. So, if you only need one of the permissions to be true to display, use acl_get, and ors in an If statement.

acl_getf
acl_getf() checks for all forums a user has the permission for.
Code: Select all
$auth->acl_getf('f_read');   

The above code would output all the forums that a user can read. Pretty simple, really. You can also add the option second parameter. By default, acl_getf() will return something like this...
Code: Select all
array(
    
forum_id => array('option' => 1),
    
forum_id => array('option2' => 0),
)   

If you put a true in the second option, it will only display the things where it is true, so it would look like this...
Code: Select all
array(
    
forum_id => array('option' => 1),
)   

acl_getf_global
This option checks whether a user has $permission on any forum, or globally. For example, if you check 'f_read' and a user can't read any forums, it will return false, but if they can read even one forum, it'll return true.
Code: Select all
$auth->acl_getf_global($permission);   


Conclusion
I hope you learned a lot reading this article (as I did, writing it). If anything is unclear, please feel free to post in the helpful forums here at phpBBmodders.net, and you will get a response by either me, or another user quickly. If anything seems unclear, feel free to PM me, and I will try and get an update in to the article as soon as possible. Thank you.
 

Changelog:

by eviL<3 on 24 Apr 2008, 13:31: Added a section about permission language files

License:

All articles in the knowledge base are licensed under the phpbbmodders beerware-nc license.

Back to category


Articles index » phpBB 3.0 » Permissions