behavior_defineAuthorizedUsers_accessControl

PHOTO EMBED

Wed Nov 13 2024 18:07:15 GMT+0000 (Coordinated Universal Time)

Saved by @belleJar #groovy

// restrict editing only to authorized users

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript

// Set the base script annotation for field behaviours
@BaseScript FieldBehaviours fieldBehaviours

// Get the custom field by name
def customField = getFieldByName("My Field")

// Retrieve the username of the currently logged-in user
def username = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getUsername()

// Define an array of authorized usernames
def authorizedUsernames = ["Service_Account", "belle_jar"]

// Check if the current user is one of the authorized users
if (!authorizedUsernames.contains(username)) {
    // If the user is not in the list, make the field read-only
    customField.setReadOnly(false)
} else {
    // If the user is in the list, allow editing
    customField.setReadOnly(true)
}
content_copyCOPY

Set Access control retraining editing only to authorized users