We created a user record type with a custom field to store the id of an external system. Next, we want to create a webhook that finds user records of this type by the external id and updates some other custom field in this record.
We tried with code like this:
Code: Select all
import org.cyclos.entities.users.User
import org.cyclos.entities.users.UserRecord
import org.cyclos.entities.users.UserRecordType
import org.cyclos.model.system.fields.CustomFieldVO
import org.cyclos.model.system.fields.CustomFieldValueForSearchDTO
import org.cyclos.model.users.records.UserRecordQuery
import org.cyclos.model.users.recordtypes.RecordTypeVO
import org.cyclos.model.users.users.UserVO
// Set up the variables for this example - in reality these come from different sources.
String userName = 'johnDoe'
String recordTypeName = 'exampleRecord'
String someIdentifierField = 'someExternalId'
String identifierValue = 'abcdef'
String someOtherField = 'someVisibleField'
try{
// Find the userrecord of type exampleRecord for the given user which has the given external identifier as customField value.
def user = conversionHandler.convert(User, userName)
UserRecordType userRecordType = entityManagerHandler.find(UserRecordType, recordTypeName)
def q = new UserRecordQuery()
q.type = new RecordTypeVO(userRecordType.id)
q.user = new UserVO(user.id)
q.setUnlimited()
def param = new CustomFieldValueForSearchDTO()
def customField = new CustomFieldVO()
customField.internalName = "${recordTypeName}.${someIdentifierField}"
param.setField(customField)
param.setStringValue("\"${identifierValue}\"")
def params = new HashSet<CustomFieldValueForSearchDTO>()
params.add(param)
q.setCustomValues(params)
def recordsFound = recordService.search(q)
// We should find exactly one record. If not, throw an exception.
if (recordsFound?.totalCount != 1) {
throw new Exception("Search for ${recordTypeName} userrecord for user ${user.username} and ${someIdentifierField} ${identifierValue} resulted in ${recordsFound?.totalCount} records.")
}
// Update other information in our exampleRecord.
UserRecord userRecord = entityManagerHandler.find(UserRecord, recordsFound[0].id)
def exampleRecordInfo = scriptHelper.wrap(userRecord)
exampleRecordInfo."${someOtherField}" = "someValue"
return "${recordTypeName} is updated."
} catch (Exception e){
return "Exception with message: ${e.getMessage()}"
}
a) Would this (using a UserRecordQuery like this) be the best way to search userrecords, or is there a better/easier/faster way, you think?
b) Searching on a custom field like this only works when the custom field is set to 'Include as search filter'. That means it becomes visible in the search filter in the list of userrecords. We would not want that, because in our case the external identifier is not very relevant to people searching in userrecords (although we don't want to hide the field from them altogether); only for the underlying checks in the code it is relevant. Would there be a way to make this work without the field showing up in the search filter?
c) As an extra security measure we may want to check the external identifier is never used by anyone else. We could probably use the code above and leave out the line that filters the query by user:
Code: Select all
q.user = new UserVO(user.id)
Thanks so much!
Sandra
Using Cyclos 4.11.1