RoleController.groovy
上传用户:steveyhw
上传日期:2019-05-13
资源大小:307k
文件大小:7k
源码类别:

PlugIns编程

开发平台:

Java

  1. /**
  2.  * RoleController.groovy 
  3.  * Actions over Authority objects.
  4.  * @author generated by plugin script
  5.  * @auther Haotian Sun
  6.  */            
  7. class RoleController {
  8.     def index = { redirect(action:list,params:params) }
  9.     // the delete, save and update actions only
  10.     // accept POST requests
  11.     def allowedMethods = [delete:'POST',
  12.                           save:'POST',
  13.                           update:'POST']
  14.     def list = {
  15.         if(!params.max)params.max = 10
  16.         [ authorityList: Authority.list( params ) ]
  17.     }
  18.     def show = {
  19.         [ authority : Authority.get( params.id ) ]
  20.     }
  21.     def delete = {
  22.         def authority = Authority.get( params.id )
  23.         String oldRole = authority.authority
  24.         if(authority) {
  25.           def rms = Requestmap.findAllByConfigAttributeLike('%'+oldRole+'%')
  26.           rms.each{it.configAttribute=it.configAttribute.replace(oldRole+',','');it.validate();}
  27.             authority.delete()
  28.             flash.message = "Authority ${params.id} deleted."
  29.             redirect(action:list)
  30.         }
  31.         else {
  32.             flash.message = "Authority not found with id ${params.id}"
  33.             redirect(action:list)
  34.         }
  35.     }
  36.     def edit = {
  37.         def authority = Authority.get( params.id )
  38.         if(!authority) {
  39.                 flash.message = "Authority not found with id ${params.id}"
  40.                 redirect(action:list)
  41.         }
  42.         else {
  43.             return [ authority : authority ]
  44.         }
  45.     }
  46. /** Authority update action. when updating an existing authority instance, the requestmaps which contain
  47.  *  them should also be updated.
  48.  */
  49.     def update = {
  50.         Authority.withTransaction {status ->
  51.             def authority = Authority.get(params.id)
  52.             String oldRole = authority.authority
  53.             if (authority) {
  54.                 authority.properties = params
  55.                 String role = params.authority
  56.                 authority.authority = 'ROLE_' + role.toUpperCase()
  57.                 String newRole = authority.authority
  58.                 def rms = Requestmap.findAllByConfigAttributeLike('%' + oldRole + ',%')
  59.                 rms.each {it.configAttribute = it.configAttribute.replace(oldRole, newRole); it.validate();}
  60.                 ArrayList urlList = new ArrayList()
  61.                 String[] urls = null
  62.                 if (params.url instanceof String) {
  63.                     urls = new String[1]
  64.                     urls[0] = params.url
  65.                     urlList.add(params.url)
  66.                 }
  67.                 else {
  68.                     urls = params.url
  69.                     if (params.url) {
  70.                         for (int i = 0; i < urls.length; i++) {
  71.                             urlList.add(urls[i])
  72.                         }
  73.                     }
  74.                 }
  75.                 if (authority.save()) {
  76.                     if (params.url) {
  77.                         def rms2 = Requestmap.findAllByConfigAttributeLike('%' + newRole + ',%')
  78.                         rms2.each {it.configAttribute = it.configAttribute.replace(newRole+',', ''); it.validate();}
  79.                         for (int i = 0; i < urls.length; i++) {
  80.                             String url = urls[i]
  81.                             def requestmap = Requestmap.findByUrlIlike("${url}")
  82.                             if (requestmap) {
  83.                                 requestmap.configAttribute = requestmap.configAttribute + authority.authority + ","
  84.                             }
  85.                             else {
  86.                                 requestmap = new Requestmap()
  87.                                 requestmap.url = url.toLowerCase()
  88.                                 requestmap.configAttribute = authority.authority + ","
  89.                             }
  90.                             if (requestmap.save()) {
  91.                                 //
  92.                             }
  93.                             else {
  94.                                 status.setRollbackOnly()
  95.                                 render(view: 'create', model: [authority: authority])
  96.                             }
  97.                         }
  98.                     }
  99.                     redirect(action: show, id: authority.id)
  100.                 }
  101.                 else {
  102.                     status.setRollbackOnly()
  103.                     render(view: 'edit', model: [authority: authority, urlList: urlList])
  104.                 }
  105.             }
  106.             else {
  107.                 flash.message = "Authority not found with id ${params.id}"
  108.                 redirect(action: edit, id: params.id)
  109.             }
  110.         }
  111.     }
  112.     def create = {
  113.         def authority = new Authority()
  114.         authority.authority=""
  115.         //authority.properties = params
  116.         return ['authority':authority]
  117.     }
  118. /** Authority save action*/
  119.     def save = {
  120.         Authority.withTransaction {status ->
  121.         def authority = new Authority()
  122.         String au=params.authority
  123.         authority.properties = params
  124.         //here translate user's input to the required Acegi format.
  125.         authority.authority='ROLE_'+au.toUpperCase() 
  126.         if (authority.save()) {
  127.                 String[] urls = null;
  128.                 if (params.url instanceof String) {
  129.                     urls = new String[1]
  130.                     urls[0] = params.url
  131.                 }
  132.                 else {
  133.                     urls = params.url
  134.                 }
  135.                 if (params.url) {
  136.                     for (int i = 0; i < urls.length; i++) {
  137.                         String url = urls[i]
  138.                         def requestmap = Requestmap.findByUrlIlike("${url}")
  139.                         if (requestmap) {
  140.                             requestmap.configAttribute = requestmap.configAttribute + authority.authority + ","
  141.                         }
  142.                         else {
  143.                             requestmap = new Requestmap()
  144.                             requestmap.url = url.toLowerCase()
  145.                             requestmap.configAttribute = authority.authority + ","
  146.                         }
  147.                         if (requestmap.save()) {
  148.                             //
  149.                         }
  150.                         else {
  151.                             status.setRollbackOnly()
  152.                             render(view: 'create', model: [authority: authority])
  153.                         }
  154.                     }
  155.                 }
  156.                 /* for(int i=0; i< configAttrs.length; i++){
  157.                      String configAttribute = configAttrs[i]
  158.                      formattedConfigAttrs+="ROLE_"+configAttribute.toUpperCase()+","
  159.                  }*/
  160.                 redirect(action: show, id: authority.id)
  161.             }
  162.             else {
  163.                 status.setRollbackOnly()
  164.                 authority.authority = au
  165.                 render(view: 'create', model: [authority: authority])
  166.             }
  167.         }
  168.     }
  169. }