RoleController.groovy
上传用户:steveyhw
上传日期:2019-05-13
资源大小:307k
文件大小:7k
- /**
- * RoleController.groovy
- * Actions over Authority objects.
- * @author generated by plugin script
- * @auther Haotian Sun
- */
- class RoleController {
- 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 = {
- if(!params.max)params.max = 10
- [ authorityList: Authority.list( params ) ]
- }
- def show = {
- [ authority : Authority.get( params.id ) ]
- }
- def delete = {
- def authority = Authority.get( params.id )
- String oldRole = authority.authority
- if(authority) {
- def rms = Requestmap.findAllByConfigAttributeLike('%'+oldRole+'%')
- rms.each{it.configAttribute=it.configAttribute.replace(oldRole+',','');it.validate();}
- authority.delete()
- flash.message = "Authority ${params.id} deleted."
- redirect(action:list)
- }
- else {
- flash.message = "Authority not found with id ${params.id}"
- redirect(action:list)
- }
- }
- def edit = {
- def authority = Authority.get( params.id )
- if(!authority) {
- flash.message = "Authority not found with id ${params.id}"
- redirect(action:list)
- }
- else {
- return [ authority : authority ]
- }
- }
- /** Authority update action. when updating an existing authority instance, the requestmaps which contain
- * them should also be updated.
- */
- def update = {
- Authority.withTransaction {status ->
- def authority = Authority.get(params.id)
- String oldRole = authority.authority
- if (authority) {
- authority.properties = params
- String role = params.authority
- authority.authority = 'ROLE_' + role.toUpperCase()
- String newRole = authority.authority
- def rms = Requestmap.findAllByConfigAttributeLike('%' + oldRole + ',%')
- rms.each {it.configAttribute = it.configAttribute.replace(oldRole, newRole); it.validate();}
- ArrayList urlList = new ArrayList()
- String[] urls = null
- if (params.url instanceof String) {
- urls = new String[1]
- urls[0] = params.url
- urlList.add(params.url)
- }
- else {
- urls = params.url
- if (params.url) {
- for (int i = 0; i < urls.length; i++) {
- urlList.add(urls[i])
- }
- }
- }
- if (authority.save()) {
- if (params.url) {
- def rms2 = Requestmap.findAllByConfigAttributeLike('%' + newRole + ',%')
- rms2.each {it.configAttribute = it.configAttribute.replace(newRole+',', ''); it.validate();}
- for (int i = 0; i < urls.length; i++) {
- String url = urls[i]
- def requestmap = Requestmap.findByUrlIlike("${url}")
- if (requestmap) {
- requestmap.configAttribute = requestmap.configAttribute + authority.authority + ","
- }
- else {
- requestmap = new Requestmap()
- requestmap.url = url.toLowerCase()
- requestmap.configAttribute = authority.authority + ","
- }
- if (requestmap.save()) {
- //
- }
- else {
- status.setRollbackOnly()
- render(view: 'create', model: [authority: authority])
- }
- }
- }
- redirect(action: show, id: authority.id)
- }
- else {
- status.setRollbackOnly()
- render(view: 'edit', model: [authority: authority, urlList: urlList])
- }
- }
- else {
- flash.message = "Authority not found with id ${params.id}"
- redirect(action: edit, id: params.id)
- }
- }
- }
- def create = {
- def authority = new Authority()
- authority.authority=""
- //authority.properties = params
- return ['authority':authority]
- }
-
- /** Authority save action*/
- def save = {
- Authority.withTransaction {status ->
- def authority = new Authority()
- String au=params.authority
- authority.properties = params
- //here translate user's input to the required Acegi format.
- authority.authority='ROLE_'+au.toUpperCase()
- if (authority.save()) {
- String[] urls = null;
- if (params.url instanceof String) {
- urls = new String[1]
- urls[0] = params.url
- }
- else {
- urls = params.url
- }
- if (params.url) {
- for (int i = 0; i < urls.length; i++) {
- String url = urls[i]
- def requestmap = Requestmap.findByUrlIlike("${url}")
- if (requestmap) {
- requestmap.configAttribute = requestmap.configAttribute + authority.authority + ","
- }
- else {
- requestmap = new Requestmap()
- requestmap.url = url.toLowerCase()
- requestmap.configAttribute = authority.authority + ","
- }
- if (requestmap.save()) {
- //
- }
- else {
- status.setRollbackOnly()
- render(view: 'create', model: [authority: authority])
- }
- }
- }
- /* for(int i=0; i< configAttrs.length; i++){
- String configAttribute = configAttrs[i]
- formattedConfigAttrs+="ROLE_"+configAttribute.toUpperCase()+","
- }*/
- redirect(action: show, id: authority.id)
- }
- else {
- status.setRollbackOnly()
- authority.authority = au
- render(view: 'create', model: [authority: authority])
- }
- }
- }
- }