Field permission hook example

Use Permission hooks to define the behavior of a field at run time. Typically, you define the behavior of the field by using the Behaviors grid in the Designer. The values that you enter apply equally to all members of a user group. With a permission hook, you can specify the behavior of a field with more precision. In the following example, if the current user belongs to the managers group and does not belong to the engineering group, this hook makes the field optional. If the user is not in at least one group, the hook fails.

Note: Because hooks run with administrator privileges, this example works even if the behavior of the fields is read-only.

VBScript


Function field1_Permission(fieldname, username)

    ' fieldname As String

    ' username As String

    ' field_Permission As Long

    ' entityDef = defect


    ' Assign the default return value

   field1_Permission = AD_MANDATORY

    set curSession = GetSession

    userGroups = curSession.GetUserGroups()

    for each group in userGroups

      if group = "managers" And group <> "engineers" Then

        field1_Permission = AD_OPTIONAL

      End If

    Next

End Function 

Perl


sub field1_Permission  {

    my($fieldname, $username) = @_;

    my $result;

    # $fieldname as string scalar

    # $username as string scalar

    # $result as long scalar

    # entityDef is Defect

    # Assign the default return value

   $result = $CQPerlExt::CQ_MANDATORY;

    $curSession = $entity->GetSession();

    $userGroups = $curSession->GetUserGroups();

    foreach $group (@$userGroups)  {

      if ($group eq "managers" && $group ne "engineers") {

         $result = $CQPerlExt::CQ_OPTIONAL;
      }
    }
    return $result;
}