UserController.groovy
上传用户:steveyhw
上传日期:2019-05-13
资源大小:307k
文件大小:4k
- /**
- * UserController.groovy
- * Actions over Person objects.
- * @author generated by plugin script
- * @auther Haotian Sun
- */
- class UserController {
- AuthenticateService authenticateService
-
- def index = { redirect(action:list,params:params) }
- // the delete, save and update actions only
- // accept POST requests
- def allowedMethods = [delete:'POST',
- save:'POST',
- update:'POST']
- def list = {
- //println "Enter list action in PersonController"
- if(!params.max)params.max = 10
- [ personList: Person.list( params ) ]
- }
- def show = {
- [ person : Person.get( params.id ) ]
- }
- /** person delete action, before removing an existing person,
- * he should be removed from those authorities which he is involved
- */
- def delete = {
- //println "Enter delete action in PersonController"
- def person = Person.get( params.id )
- def authPrincipal = authenticateService.principal()
- if(person) {
- //avoid self-delete if the logged-in user is an admin
- if(!(authPrincipal instanceof String) && authPrincipal.username!=person.username){
- def au=Authority.findAll()
- if(Authority.findAll()!=null){
- //firstly, delete this person from People_Authorities table.
- au.each{it.removeFromPeople(person)}
- }
- person.delete()
- flash.message = "Person ${params.id} deleted."
- redirect(action:list)
- }else{
- flash.message = "You can not delete yourself,please login with another admin and try again"
- redirect(action:list)
- }
- }
- else {
- flash.message = "Person not found with id ${params.id}"
- redirect(action:list)
- }
- }
- def edit = {
- def person = Person.get( params.id )
- if(!person) {
- flash.message = "Person not found with id ${params.id}"
- redirect(action:list)
- }
- else {
- def authorityList = Authority.list(params)
- return ['person': person,'authorityList':authorityList]
- }
- }
-
- /** person update action, added additional codes to update the user's roles*/
- def update = {
- //println "Entering update action in PersonController..."
- def person = Person.get( params.id )
- def oldpw = person.passwd
- if(person) {
- person.properties = params
- String ps = params.get("passwd")
- if(!ps.equals(oldpw)){
- def pass = authenticateService.passwordEncoder(ps)
- person.passwd=pass
- }
- Set paramsSet=params.keySet()
- Iterator itt = paramsSet.iterator()
- if(person.save()) {
- def au=Authority.findAll()
- au.each{it.removeFromPeople(person)}
- while(itt.hasNext()){
- String key = itt.next()
- if(params.get(key).equals("on")&&key.contains("ROLE")){
- def role = Authority.findByAuthority(key)
- role.addToPeople(person)
- }
- }
- redirect(action:show,id:person.id)
- }
- else {
- render(view:'edit',model:[person:person])
- }
- }
- else {
- flash.message = "Person not found with id ${params.id}"
- redirect(action:edit,id:params.id)
- }
- }//update
- def create = {
- def person = new Person()
- person.properties = params
- def authorityList= Authority.list(params)
- return ['person':person,'authorityList':authorityList]
- }
- /** person save action, added additional codes to save the user's roles*/
- def save = {
- //println "Enter SAVE action in PersonController..."
- def person = new Person()
- person.properties = params
- def pass = authenticateService.passwordEncoder(params.passwd)
- person.passwd=pass
- Set paramsSet=params.keySet()
- Iterator itt = paramsSet.iterator()
- if(person.save()) {
- while(itt.hasNext()){
- String key = itt.next()
- if(params.get(key).equals("on")&&key.contains("ROLE")){
- def role = Authority.findByAuthority(key)
- role.addToPeople(person)
- println "Role "+role.authority +" has been added"
- }
- }
- redirect(action:show,id:person.id)
- }
- else {
- def authorityList1= Authority.list(params)
- render(view:'create',model:[authorityList:authorityList1,person:person])
- }
- }//save
- }