Table UserRole not populated in Grails 6

23 Views Asked by At

I'm developing Grails 6.1.2 application and I installed Spring security core plugin and followed all the installation steps mentioned in the documentation, I'm usingMYSQL, I used the Bootstrap file to save some credentials when the application starts, all the User,Role tables were populated successfully except table UserRole it's empty, I cleaned and built my application nothing fixed, I need to know how to fix this and the reason for making only this table not populated

here's the Bootstrap code:

    def userRole = Role.findByAuthority("ROLE_USER") ?: new Role(authority: "ROLE_USER").save(failOnError: true)
    def superAdminRole = Role.findByAuthority("ROLE_SUPERADMIN") ?: new Role(authority: "ROLE_SUPERADMIN").save(failOnError: true)
    def user = new User("user", "1").save(failOnError: true)
    def superAdmin = new User("admin", "2").save(failOnError: true)
    
    try {
        log.info('Creating user roles...')
        UserRole.create(user, userRole)
        UserRole.create(superAdmin, superAdminRole)
        log.info('User roles created successfully')
    } catch (Exception e) {
        log.error('Error creating user roles:', e)
        // Handle exception
    } 

Also the create from UserRole domain, this domain is automatically created when you run the s2-quickstart script

static UserRole create(User user, Role role, boolean flush = false) {
    def instance = new UserRole(user: user, role: role)
    instance.save(flush: flush)
    instance
}
0

There are 0 best solutions below