webCOMAND

auth::is_authorized()

Check if a user has a specific Privilege, which may be associated with a specific object and field.

Prototype

boolean is_authorized(string $privilege, cObject $object = NULL, mixed $field = NULL)

Parameters

  • privilege - A string with the Package Namespace and Identifier of a Privilege in the form "namespace::identifier" (e.g. "io_comand_auth::WRITE").
  • object - Optional object to check when the privilege is a content privilege.
  • field - Optional field to check for a specific field authorization within the object.  The field can be specified as a Content Type Field OID, Identifier or object.

Return

Returns TRUE if the user is authorized, otherwise FALSE.

Example

$repo = comand::repo();
$auth = $repo->user->auth();
$can_comment = $auth->is_authorized('com_example_blog::CanComment');

$write_auth = 'io_comand_auth::WRITE';
$post = $repo->get_first('FROM BlogPost WHERE OID=123');
$can_edit = $auth->is_authorized($write_auth, $post, 'Comments');

if($can_comment && $can_edit) {
    $comment = $repo->new_object('Comment');
    $comment->Timestamp = \io_comand_util\time::get_db_timestamp();
    $comment->User = $repo->user;
    $comment->Message = 'No comment.';
    $post->Comments []= $comment;
}

Related

user::auth()