Jenkins RoleBasedAuthorizationStrategy add user to Role Groovy Script - jenkins

I am trying to find a groovy script to add an existing user to a Role using RoleBasedAuthorizationStrategy. Any help would be greatly appreciated.

I ran into the same need. After doing some web searching and looking at the plugin's code from GitHub, I found one link that provided some insight: https://issues.jenkins-ci.org/browse/JENKINS-23709. Based on that I hacked together a quick Groovy script that assigns a specific user to a specific role. Been awhile since I've done Groovy, so pardon the dust. Feel free to use this as an example for your own needs.
import jenkins.model.*
import hudson.security.*
import java.util.*
import com.michelin.cio.hudson.plugins.rolestrategy.*
import java.lang.reflect.*
def roleName = "guest"
def userName = "bot-release"
def findGuestRoleEntry(grantedRoles, roleName)
{
for (def entry : grantedRoles)
{
Role role = entry.getKey()
if (role.getName().equals(roleName))
{
return entry
}
}
return null
}
def authStrategy = Jenkins.instance.getAuthorizationStrategy()
if(authStrategy instanceof RoleBasedAuthorizationStrategy){
RoleBasedAuthorizationStrategy roleAuthStrategy = (RoleBasedAuthorizationStrategy) authStrategy
// Make constructors available
Constructor[] constrs = Role.class.getConstructors();
for (Constructor<?> c : constrs) {
c.setAccessible(true);
}
// Make the method assignRole accessible
Method assignRoleMethod = RoleBasedAuthorizationStrategy.class.getDeclaredMethod("assignRole", String.class, Role.class, String.class);
assignRoleMethod.setAccessible(true);
def grantedRoles = authStrategy.getGrantedRoles(RoleBasedAuthorizationStrategy.GLOBAL);
if (grantedRoles != null)
{
// println "Got grantedRoles for " + RoleBasedAuthorizationStrategy.GLOBAL
def roleEntry = findGuestRoleEntry(grantedRoles, roleName);
if (roleEntry != null)
{
// println "Found role " + roleName
def sidList = roleEntry.getValue()
if (sidList.contains(userName))
{
println "User " + userName + " already assigned to role " + roleName
} else {
println "Adding user " + userName + " to role " + roleName
roleAuthStrategy.assignRole(RoleBasedAuthorizationStrategy.GLOBAL, roleEntry.getKey(), userName);
println "OK"
}
Jenkins.instance.save()
} else {
println "Unable to find role " + roleName
}
} else {
println "Unable to find grantedRoles for " + RoleBasedAuthorizationStrategy.GLOBAL
}
} else {
println "Role Strategy Plugin not found!"
}

Related

How to change the Git URL in all Jenkins jobs

Does anyone have an updated version of ceilfors answer that works for both AbstractProject and WorkflowJob?
This is the solution I came up with. It was tested on Jenkins 2.355
The test was run from the script console.
For testing purposes, I limited the test to one Freestyle (AbstractProject) and one Pipeline (WorkflowJob) job each. You would need to modify the code below.
I hope others find this useful
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import hudson.plugins.git.*
import jenkins.*
import jenkins.model.*
def modifyGitUrl(url) {
def updatedUrl = url.toString().replace("git#gitlab", "git#github")
// println "updatedUrl = ${updatedUrl}"
return updatedUrl
}
Jenkins.instance.getAllItems(Job.class).each {
project = it.getFullName()
if(project.toString().equals("PL_Quick_Testing") || project.toString().equals("A_Freestyle_Job")) {
try {
if (it instanceof AbstractProject){
def oldScm = it.scm
def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, it.credentialsId)
}
def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
it.scm = newScm
it.save()
println "Done"
} else if (it instanceof WorkflowJob) {
def oldScm = it.getTypicalSCM()
def definition = it.getDefinition()
String scriptPath = it.getDefinition().getScriptPath()
def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
new UserRemoteConfig(modifyGitUrl(oldScm.userRemoteConfigs.url[0]), it.name, it.refspec, it.credentialsId)
}
def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
def newDefinition = new org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition(newScm, scriptPath)
it.definition = newDefinition
it.save()
println "Done"
} else {
println("${project} has no SCM")
}
} catch (Exception e) {
// e.printStackTrace()
}
}
}

Is there a way, in a list of jobs, to change a job's parameter to required?

I am using the script console of hudson and jenkins.
And I need make a parameter called "NAME" become required at the jobs where that parameter already exists. But I do not know any method that can help me.
def instance = hudson.model.Hudson.instance;
def allJobs = instance.getView("All");
allJobs.items.each {
if (it.containsParameter('NAME')){ /// this exists?
println(it.getName());
it.set??? /// what can I do?
}
}
I need that way for when someone excute the job the parameter "NAME" do not be empty or null.
you can get the desired result with below code:
def instance = hudson.model.Hudson.instance;
def allJobs = instance.getView("All");
allJobs.items.each {
prop = it.getProperty(ParametersDefinitionProperty.class)
if(prop != null) {
for(param in prop.getParameterDefinitions()) {
try {
if(param.name.equals('NAME')){
println(it.name + ":" + param.name + " " + param.defaultValue)
if(!param.defaultValue.trim()){
println("default value is blank")
}
}
}
catch(Exception e) {
println e
}
}
}
}

How can I access the node.depth attribute in the (Grails plugin) <RichUI:treeview> tag?

The plugin says that you can use 'node.depth' from the tag to determine what label level has been clicked (country or province). I can't seem to access node.depth from the RichUI:treeview tag. My code works fine when the value of 1 or 2 is hard coded into the onLabelClick. But when I specify node.depth as the parameter, nothing gets passed to the javascript. How can I access node.depth? My alert says that "level is undefined"
<richui:treeView id="tree" xml="${data}"
onLabelClick="treeClickHandler(node.depth, id)" showRoot="false"/>
function treeClickHandler(level, id){
alert("level is " + level + " and id is " + id);
if (level == 1){
postForCountryIdeas(id);
}
else{
postForProvControls(id);
}
}
def index() {
def countryList = Country.list()
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def writer2 = new StringWriter()
def xml2 = new MarkupBuilder(writer2)
xml2.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml2.countrys {
countryList.each{item->
xml2.country(name:"${item.name}", id: item.id){
item.provinces.each{ prov->
province(name:"${prov.name}", id: prov.id)
}
}
}
}
[data: writer2.toString()]
}
The solution was to use node.node.depth instead of node.depth as per the docs.

NPE after grails clean

The application works just fine, but after grails clean it starts throwing an error if I execute grails war or grail web-app:
|Loading Grails 2.4.3
|Configuring classpath
.
|Environment set to production
.................................
|Packaging Grails application
..
|Compiling 18 source files
.Error
|
Compilation error: startup failed:
General error during class generation: NPE while processing BcvjobService.groovy
groovy.lang.GroovyRuntimeException: NPE while processing BcvjobService.groovy
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:252)
at org.codehaus.groovy.control.CompilationUnit$16.call(CompilationUnit.java:805)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1047)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:583)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:561)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:538)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:517)
at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:59)
at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:215)
at org.codehaus.groovy.ant.Groovyc.runCompiler(Groovyc.java:1161)
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:1212)
at org.codehaus.groovy.grails.compiler.Grailsc.compile(Grailsc.java:78)
at org.codehaus.groovy.ant.Groovyc.execute(Groovyc.java:827)
...
Googling didn't help in the least, so I can't even understand where to start, and any help would be greatly appreciated.
Here is BcvjobService.groovy:
package bcvapp
import grails.transaction.Transactional
import groovy.lang.Closure;
import groovy.io.FileType
import groovy.util.AntBuilder
import java.awt.event.ItemEvent;
import java.util.List;
import java.util.regex.Pattern;
import grails.util.Mixin
import grails.util.Holders
import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
#Transactional
#Mixin(ServiceCategory)
class BcvjobService {
def mailService
def holderService
def servletContext = SCH.servletContext
def String absPath = getAbsPath()
def String configPath = servletContext.getRealPath("/pipeline/bcvrun.prj.xml")
def outputMap = [:]
def prepareDirectory(Bcvjob job, String sessionId, List fileList, List directionList, Integer queueSize){
def configFile = new File (configPath)
def defaultConfig = new XmlParser().parse(configFile)
// Create directory structure
def inputLine = defaultConfig.BCV_Input
def input = inputLine.text().replaceAll(Pattern.compile('[\\.\\/\\\\]'), '')
def outputLine = defaultConfig.BCV_Output
def output = outputLine.text().replaceAll(Pattern.compile('[\\.\\/\\\\]'), '')
def folderPath = "${absPath}${sessionId}"
def inputPath = folderPath + "/" + input
def outputPath = folderPath + "/" + output
addResultsPath(sessionId, outputPath)
inputLine[0].value = inputPath
outputLine[0].value = outputPath
def inpath = defaultConfig.InPath // why there are two almost identical lines in bcv config?
inpath[0].value = inputPath
new File (inputPath).mkdirs()
new File (outputPath).mkdir()
new File(outputPath + "/simple_results.html").createNewFile()
File res = new File(outputPath + "/simple_results.html")
if (queueSize > 2 ){
res << ("Your task was submitted at ${new Date()}<p>")
res << ("Waiting in queue..<p>")
res << ("Please, bookmark this page to see the results later. Refresh the page to check if they are ready.")
}
else {
res << ("Your task was submitted at ${new Date()}<p>")
res << ("Running..<p>")
res << ("Please, bookmark this page to see the results later. Refresh the page to check if they are ready.")
}
res << ("<script>")
res << ("var interval = setInterval('location.reload()', '30000');")
res << ("</script>")
for (f in fileList){
new File (outputPath + "/" + f.getOriginalFilename().replaceAll(Pattern.compile('\\..*'), '').replaceAll("\\s+", "_")).mkdir()
}
uploadFiles(inputPath, fileList)
// Write custom configuration file
def vocabulary = defaultConfig.Vocabulary
def vocabularyPath = vocabulary[0].text().substring(0, vocabulary[0].text().lastIndexOf("/")+1)
def vocabularyName = job.vocabulary.replaceAll(/\s+/, "_")
vocabulary[0].value = vocabularyPath + vocabularyName + ".seq.fas"
def reads = defaultConfig.READS.find{'READS'}
reads.children.clear()
def counter = 0
for (f in fileList){
def stringDirection
if (directionList.get(counter) == "r"){
stringDirection = "reverse"
}
else stringDirection = "forward"
reads.appendNode('Read', [name:f.getOriginalFilename().replaceAll("\\s+", "_")]).appendNode('Direction', stringDirection)
counter++
}
def taxdb = defaultConfig.Database
if (job.taxdb == "full"){
taxdb[0].value = "all"
}
else if (job.taxdb == "named isolates"){
taxdb[0].value = "named"
}
def distance = defaultConfig.DistanceThreshold
distance[0].value = job.distance.toFloat()
def email = defaultConfig.Email
email[0].value = job.email
def mode = defaultConfig.Mode
mode[0].value = "PIPELINE"
def writer = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.with {
preserveWhitespace = true
expandEmptyElements = true
}
printer.print(defaultConfig)
def result = writer.toString()
new File(folderPath + "/bcvrun.prj.xml").write(result)
return outputPath
}
def Closure getWaitingPipeline = {Bcvjob job ->
def queueSize = Bcvjob.countByDateCreatedLessThanEquals(job.dateCreated) + Stapjob.countByDateCreatedLessThanEquals(job.dateCreated)
if(queueSize > 2){
println (" bcv waiting in queue; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
while (queueSize > 2){ // 1 running task + our task
sleep(5000)
queueSize = Bcvjob.countByDateCreatedLessThanEquals(job.dateCreated) + Stapjob.countByDateCreatedLessThanEquals(job.dateCreated)
}
println (" bcv finished waiting in queue; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
def res = new File(getResults(job.sessionId)).newWriter()
res.write("Your task was submitted at ${new Date()}<p>")
res.write("Running..<p>")
res.write("Please, bookmark this page to see the results later. Refresh the page to check if they are ready.")
res.close()
}
def returnCode = runPipeline(job.sessionId)
println (" bcv waiting pipeline finished; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
if (returnCode != 143){
zipResults(job.sessionId)
println (" bcv waiting results zipped; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
if (returnCode == 0){
if (job.email) {
sendResults(job.email, job.sessionId)
println (" bcv waiting results sent; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
}
else {
if (job.email) {
sendLogs(job.email, job.sessionId)
println (" bcv bad news sent; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
else if (returnCode != 143) {
sendLogs(job.sessionId) // send to weidewind
}
else {
println (" user left; sessionId ${job.sessionId} ")
}
}
def sessionId = job.sessionId
job.delete(flush:true)
holderService.setDone(sessionId)
}
def Closure getPipeline = {Bcvjob job ->
def returnCode = runPipeline(job.sessionId)
println (" bcv pipeline finished; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
if (returnCode != 143){
zipResults(job.sessionId)
println (" bcv results zipped; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
if (returnCode == 0){
if (job.email) {
sendResults(job.email, job.sessionId)
println (" bcv results sent; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
}
else {
if (job.email) {
sendLogs(job.email, job.sessionId, returnCode)
println (" bcv bad news sent; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
else if (returnCode != 143) {
sendLogs(job.sessionId) // send to weidewind
}
else {
println (" user left; sessionId ${job.sessionId} ")
}
}
def sessionId = job.sessionId
job.delete(flush:true)
holderService.setDone(sessionId)
}
private def addResultsPath(String sessionId, String outputPath){
outputMap.putAt(sessionId, outputPath)
}
def runPipeline(String sessionId){
println (" going to run bcv pipeline, sessionId ${sessionId}")
def command = "perl /store/home/popova/Programs/BCV_pipeline/pipeline.pl ${absPath}${sessionId} bcvrun.prj.xml >${absPath}pipelinelog.txt >2${absPath}pipelinerr.txt"// Create the String
holderService.procs[sessionId] = command.execute() // Call *execute* on the string
holderService.procs[sessionId].consumeProcessOutput( System.out, System.err ) //31.10
holderService.procs[sessionId].waitFor() // Wait for the command to finish
def exitValue = holderService.procs[sessionId].exitValue()
println "return code " + exitValue
holderService.deleteProc(sessionId)
return exitValue
}
def uploadFiles(String uploadPath, List fileList){
for (f in fileList){
if(f instanceof org.springframework.web.multipart.commons.CommonsMultipartFile){
def fileName = f.getOriginalFilename().replaceAll("\\s+", "_")
new FileOutputStream(uploadPath + "/" + fileName).leftShift( f.getInputStream() )
} else {
log.error("wrong attachment type [${f.getClass()}]");
}
}
}
def uploadSequenceFiles(String uploadPath, List fileList){
for (f in fileList){
if(f instanceof org.springframework.web.multipart.commons.CommonsMultipartFile){
def fileName = f.getOriginalFilename().replaceAll("\\s+", "_")
new FileOutputStream(uploadPath + "/" + fileName.replaceAll(Pattern.compile('\\..*'), '') + "/" + fileName).leftShift( f.getInputStream() )
} else {
log.error("wrong attachment type [${f.getClass()}]");
}
}
}
def sendResults(String email, String sessionId) {
def results = getZipResults(sessionId)
println "going to send files, sessionID ${sessionId} resultsPath ${results} time ${System.currentTimeMillis()}"
mailService.sendMail {
multipart true
to email
subject "BCV results"
body "Thanks for using BCV!\n Here are your results. \n Have a nice day!"
attachBytes 'results.zip','application/zip', new File(results).readBytes()
}
}
def sendExampleResults(String email, String folderName){
def results = getStorePath() + folderName + "/results.zip"
println "going to send files, folderName ${folderName} resultsPath ${results} time ${System.currentTimeMillis()}"
mailService.sendMail {
multipart true
to email
subject "Example BCV results"
body "Thanks for using BCV!\n Here are your example results. \n Have a nice day!"
attachBytes 'results.zip','application/zip', new File(results).readBytes()
}
}
def sendLogs(String email, String sessionId) {
//def logs = "${absPath}${sessionId}logfile"
def results = getZipResults(sessionId)
println "going to send logs, sessionID ${sessionId} logPath ${logs} time ${System.currentTimeMillis()}"
mailService.sendMail {
multipart true
to email
subject "BCV failed"
body "We are very sorry, but something has gone amiss. Here are some of your results, though."
attachBytes 'results.zip','application/zip', new File(results).readBytes()
}
//just in case there is no results at all and results.zip does not exist. Todo: catch mailService or zip exception
//else user left, auto-termination
mailService.sendMail {
multipart true
to "weidewind#gmail.com"
subject "BCV failed"
body "Achtung! email: ${email}, sessionId: ${sessionId}"
}
mailService.sendMail {
multipart true
to "weidewind#gmail.com"
subject "BCV failed"
body "Achtung! email: ${email}, sessionId: ${sessionId}"
attachBytes 'results.zip','application/zip', new File(results).readBytes()
}
println (" bcv bad news sent to webmaster; sessionId ${job.sessionId} time ${System.currentTimeMillis()}")
}
def sendLogs(String sessionId){
//just in case there is no results at all and results.zip does not exist. Todo: catch mailService or zip exception
mailService.sendMail {
multipart true
to "weidewind#gmail.com"
subject "BCV failed"
body "Achtung! sessionId: ${sessionId}"
}
println (" bcv bad news sent to webmaster; sessionId ${sessionId} time ${System.currentTimeMillis()}")
}
def zipResults(String sessionId){
def output = getOutput(sessionId)
def results = getZipResults(sessionId)
println "going to zip files, sessionID ${sessionId} time ${System.currentTimeMillis()}"
println (output)
def p = ~/.*\.(svg|html|with_names|cluster\.fasta)/
def filelist = []
def outputDir = new File(output)
outputDir.eachDir { chrom ->
def chromDir = new File(chrom.absolutePath)
chromDir.eachFileMatch(FileType.FILES, p){ tree ->
def splittedPath = tree.absolutePath.split('/')
println ("going to add ${splittedPath[splittedPath.size()-2]}/${splittedPath[splittedPath.size()-1]} from ${output} to zip list; sessionId ${sessionId}")
filelist.add("${splittedPath[splittedPath.size()-2]}/${splittedPath[splittedPath.size()-1]}")
}
}
filelist.add("simple_results.html")
println("results will be placed here ${results}")
def zipFile = new File("${results}")
new AntBuilder().zip( basedir: output,
destFile: zipFile.absolutePath,
includes: filelist.join( ' ' ) )
}
def getResults (String sessionId){
return outputMap.getAt(sessionId) + "/simple_results.html"
}
def getOutput(String sessionId){
return outputMap.getAt(sessionId)
}
def getZipResults(String sessionId){
return outputMap.getAt(sessionId) + "/results.zip"
}
def getExampleFolderName(List fileList){
def exampleFileNames = []
for (f in fileList){
exampleFileNames.add(f.name)
}
exampleFileNames.sort()
def folderName = ""
for (f in exampleFileNames){
folderName = folderName + (f.replaceAll(Pattern.compile('\\..*'), ''))
}
println folderName
return folderName
}
def checkInput(Bcvjob job, List fileList){
String errorMessage = ""
if (!job.validate()) {
errorMessage = "Some errors occured: "
job.errors.each {
errorMessage += "<p>" + it + "</p>"
}
//if (!isFloat(job.distance) || job.distance.toFloat() < 0 || job.distance.toFloat() > 0.1){
if ( job.distance < 0 || job.distance > 0.1){
errorMessage += "<p> Maximum distance must not be less than 0 or more than 0.1 </p>"
}
if (job.errors.hasFieldErrors("email")){
errorMessage += "<p>Your e-mail does not seem valid </p>"
}
if (job.errors.hasFieldErrors("files") || (fileList.size() == 0 & job.isExample == "false")){
errorMessage += "<p>Select at least one file </p>"
}
}
if (fileList.size() > 10){
errorMessage += "<p>Please, do not select more than 10 files at once. </p>"
}
if (job.isExample == "false"){
for (f in fileList) {
def name = f.getOriginalFilename()
int dot= name.lastIndexOf(".");
if (name.substring(dot+1) != "ab1"){
errorMessage += "<p>Unsupported extension: ${name} </p>"
}
def bytes = f.getBytes()
if (bytes[0] != 'A' || bytes[1] != 'B' || bytes[2] != 'I' || bytes[3] != 'F' || !(bytes[4]+bytes[5] >= 101)){
errorMessage += "<p>Not ABI: ${name} </p>"
}
}
}
return errorMessage
}
def isFloat(String value)
{
try
{
Float.parseFloat(value);
return true;
} catch(NumberFormatException nfe)
{
return false;
}
}
def getAbsPath(){
return Holders.config.absPath
}
def getStorePath(){
return Holders.config.storePath
}
def talkWork(){
ServiceCategory.talkWork()
}
def talkQueue(){
ServiceCategory.talkQueue()
}
}

Grails save() failure; can't figure out why

I am having a problem saving a Domain object. Below is my Controller code:
def onContactRequest = {
if(request.method == 'POST') {
if(User.findByUserTelephone(params.userTelephone)) {
User thisUser = User.findByUserTelephone(params.userTelephone)
Contact thisContact = new Contact()
thisContact.setContact(thisUser)
println("This Contact: " + thisContact.getContact());
thisContact.setBelongsTo(request.user)
println("This User: " + request.user)
if(thisContact.save(flush: true)) {
render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
} else {
render("There was a problem saving the Contact.")
if( !thisContact.save() ) {
thisContact.errors.each {
println it
}
}
}
} else {
User thisUser = new User()
thisUser.setUserName("Not Set")
thisUser.setUserTelephone(params.userTelephone)
thisUser.save()
Contact thisContact = new Contact()
thisContact.setContact(thisUser)
thisContact.setBelongsTo(request.user)
if(thisContact.save(flush: true)) {
render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
} else {
render("There was a problem saving the Contact.")
if( !thisContact.save() ) {
thisContact.errors.each {
println it + "\n"
}
}
}
}
} else {
}
The error message is printed with the following code; hence it's very ugly:
if( !thisContact.save() ) {
thisContact.errors.each {
println it + "\n"
}
}
From what I can tell, it's complaining that either the Contact or User instance is null; however that can't be true (look below)
This Contact: org.icc.callrz.User.User : 2
This User: org.icc.callrz.User.User : 1
Field 'user' in org.icc.callrz.Contact.Contact is:
static belongsTo = [
user: User
]
Error detail below:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'org.icc.callrz.Contact.Contact' on field 'user': rejected value [null]; codes [org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.error.user,org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable.error,contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,contact.user.nullable.error.user,contact.user.nullable.error.org.icc.callrz.User.User,contact.user.nullable.error,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.user,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable,contact.user.nullable.org.icc.callrz.Contact.Contact.user,contact.user.nullable.user,contact.user.nullable.org.icc.callrz.User.User,contact.user.nullable,nullable.org.icc.callrz.Contact.Contact.user,nullable.user,nullable.org.icc.callrz.User.User,nullable]; arguments [user,class org.icc.callrz.Contact.Contact]; default message [Property [{0}] of class [{1}] cannot be null]
Edit: I have no problem creating Contact domain objects using the 'generate-all' code.
SOLUTION: I had a look at the code in the view, and it looked like to create the ID was used, so I changed the code to:
thisContact.user.id = request.user.id
However, then I got an error: java.lang.NullPointerException: Cannot set property 'id' on null object but the output of println request.user was not blank so I wasn't sure why that was appearing.
I then changed the offending line to:
thisContact.user = request.user
Now everything is working. :)
Try replacing:
thisContact.setBelongsTo(request.user)
With:
thisContact.user = thisUser
The syntax you are using is wrong as far as I know, not to mention that you construct thisUser and then go on to use request.user instead.

Resources