Home read the role value in grails
Reply: 0

read the role value in grails

user5010
1#
user5010 Published in May 27, 2018, 3:37 am

hello i'm working on a grails app, and it have users with roles, now i create the user i must chose a role for him from a select(dropdown), and i don't know how to affect the role chosen to the user, knowing that the user has it's own class, and so does the role, here is the code :

user.groovy :

class User {

    String username
    String password
    String nom
    String prenom
    String email
    String tel

    static hasMany = [roles : Role]

    static constraints = {
        username blank: false, unique: true
        password blank: false,display: false
        nom nullable: true
        prenom nullable: true
        email email:true, nullable:true
        tel nullable:true, maxSize:20,  matches:/[\+]{0,1}[0-9\s]{3,15}/
    }

    static mapping = {
        password column: '`password`'
        sort nom: "asc"
        affectations sort : "dateAffectation", order:"desc"
        intervention sort : "responsable", order:"desc"
    }
}

role.groovy :

class Role {

    String intitule
    String description

    static mapping = {
        cache true
        sort intitule: "asc"
    }

    static constraints = {
        intitule blank: false, unique: true
        description nullable: true, maxSize: 4000
    }
}

usercontroller.groovy :

import grails.validation.ValidationException

import static org.springframework.http.HttpStatus.*

class UserController {

UserService userService

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond userService.list(params), model:[userCount: userService.count()]
}

def show(Long id) {
    respond userService.get(id)
}

def create() {
    respond new User(params)
}

def save(User user) {
    if (user == null) {
        notFound()
        return
    }

    try {
        userService.save(user)
    } catch (ValidationException e) {
        respond user.errors, view:'create'
        return
    }

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), user.id])
            redirect user
        }
        '*' { respond user, [status: CREATED] }
    }
}

def edit(Long id) {
    respond userService.get(id)
}

def update(User user) {
    if (user == null) {
        notFound()
        return
    }

    try {
        userService.save(user)
    } catch (ValidationException e) {
        respond user.errors, view:'edit'
        return
    }

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), user.id])
            redirect user
        }
        '*'{ respond user, [status: OK] }
    }
}

def delete(Long id) {
    if (id == null) {
        notFound()
        return
    }

    userService.delete(id)

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), id])
            redirect action:"index", method:"GET"
        }
        '*'{ render status: NO_CONTENT }
    }
}

protected void notFound() {
    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
            redirect action: "index", method: "GET"
        }
        '*'{ render status: NOT_FOUND }
    }
}

def login() {

}

def handlelogin = {
    def user = User.findByUsername(params.username)
    if (!user) {
        flash.message = "User not found for userName: ${params.userName}"
        redirect(action:'login')
    }
    session.user = user
    redirect(controller:'user')
}

def logout = {
    if(session.user) {
        session.user = null
        redirect(action:'login')
    }
}
}

form in the user.gsp :

<form action="handlelogin">
                    <div class="form-group">
                        <label>Login</label>
                        <g:textField name="username" class="form-control" placeholder="Login"/>
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <g:passwordField name="password" class="form-control" placeholder="Password"/>
                    </div>
                    <g:actionSubmit action="handlelogin" value="Sign in" class="btn btn-success btn-flat m-b-30 m-t-30"/>
                </form>

PS : the guys that supervise me told me i don't have to work with the spring security plugin, plz help im stuck

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307484 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO