Lojza Ibg
Lojza Ibg

Reputation: 658

Grails - URL rewriting for all controllers in a package

I'm wondering if it's possible to rewrite URL for all controllers in a particular package. I need to change the URL of all controllers in "admin" package to "/admin/$controller" instead of "/$controller", so it can be secured by Spring Security. Thanks very much.

Regards, Lojza

Upvotes: 1

Views: 829

Answers (3)

Lojza Ibg
Lojza Ibg

Reputation: 658

So today I implemented the solution according to this article:

// AppCtx - check the article above
for (controller in AppCtx.grailsApplication.controllerClasses) {
    def cName = controller.logicalPropertyName
    def packageName = controller.packageName

    if (packageName.contains(".admin") || packageName.contains(".springsecurity")) {
        "/admin/${cName}/$action?/$id?"(controller: cName) {
            constraints {
            }
        }
    } else {
        "/${cName}/$action?/$id?"(controller: cName) {
            constraints {
                // apply constraints here
            }
        }
    }
}

Upvotes: 3

Chris
Chris

Reputation: 8109

Have a look at: Best way to create an Admin section in Grails

However in this case I always recommend to split up your admin area into an individual grails-embedded-plugin. In here you can do your magic with your custom UrlMapping-class. This will keep your main application clean.

Upvotes: 0

Kaleb Brasee
Kaleb Brasee

Reputation: 51945

That's not strictly necessary. You could add the @Secured annotation to each of the admin controllers:

@Secured(['ROLE_ADMIN'])
class AdminController1 { ... }

@Secured(['ROLE_ADMIN'])
class AdminController2 { ... }

Or if you're not using annotations, you could map each of the admin controllers to the Spring Security config directly:

/adminController1/**=ROLE_ADMIN
/adminController2/**=ROLE_ADMIN

But if you really want to put all these controllers under an /admin/ URL prefix, I think you could do it by adding a mapping that manually references each admin controller:

"/admin/adminController1/$action?/$id?"(controller: "adminController1")
"/admin/adminController2/$action?/$id?"(controller: "adminController2")

Upvotes: 0

Related Questions