Access permission setting and checking

Fred's picture

Setting and checking access permissions is incredibly easy in Drupal. First, you need to add a checkbox to the Access Control page. Simply add the following to your module (replace yourmodule with the name of your module):

/**
 * Implementation of hook_perm()
 * Allows the setting of role specific permissions
 */
function yourmodule_perm() {
  return array('a permission to set', 'another permission to set');
}

Then do the following to your code when you need to check for access permissions:
/**
 * Some function in yourmodule
 * @param $param1 Something passed to your function
 * @param $param2 Something else passed to your function
 * @return string
 */
function cool_function($param1, $param2) {
  if (user_access('a permission to be set')) {
    // process something here
  }
  return $some_variable;
}

Couldn't be easier!