Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 32: generateLink function should use grails.serverURL in configuration #84

Merged
merged 2 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
*/
package grails.plugin.springsecurity.ui

import grails.config.Config
import grails.core.support.GrailsConfigurationAware
import grails.plugin.springsecurity.authentication.dao.NullSaltSource
import grails.plugin.springsecurity.ui.strategy.MailStrategy
import grails.plugin.springsecurity.ui.strategy.PropertiesStrategy
import grails.plugin.springsecurity.ui.strategy.RegistrationCodeStrategy
import grails.util.Holders
import groovy.text.SimpleTemplateEngine
import org.springframework.security.authentication.dao.SaltSource

/**
* @author <a href='mailto:[email protected]'>Burt Beckwith</a>
*/
class RegisterController extends AbstractS2UiController {
class RegisterController extends AbstractS2UiController implements GrailsConfigurationAware {

static defaultAction = 'register'

Expand All @@ -40,6 +43,13 @@ class RegisterController extends AbstractS2UiController {
/** Dependency injection for the 'uiPropertiesStrategy' bean. */
PropertiesStrategy uiPropertiesStrategy

String serverURL

@Override
void setConfiguration(Config co) {
serverURL = co.getProperty('grails.serverURL', String)
}

def register(RegisterCommand registerCommand) {

if (!request.post) {
Expand Down Expand Up @@ -177,9 +187,25 @@ class RegisterController extends AbstractS2UiController {
redirect uri: registerPostResetUrl ?: successHandlerDefaultTargetUrl
}

protected String generateLink(String action, linkParams) {
createLink(base: "$request.scheme://$request.serverName:$request.serverPort$request.contextPath",
controller: 'register', action: action, params: linkParams)
/**
* Creates a grails application link from a set of attributes.
* @param action
* @param linkParams
* @param shouldUseServerUrl (optional) - If true, will utilize the configured grails.serverURL from application.yml if it exists otherwise the base url will be constructed the same as it always has been
* @return String representing the relative or absolute URL
*/
protected String generateLink(String action, Map linkParams, boolean shouldUseServerUrl = false) {
String base = "$request.scheme://$request.serverName:$request.serverPort$request.contextPath"

if (shouldUseServerUrl && serverURL) {
base = serverURL
}

createLink(
base: base,
controller: 'register',
action: action,
params: linkParams)
}

protected String evaluate(s, binding) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package grails.plugin.springsecurity.ui

import grails.plugin.springsecurity.SpringSecurityUtils
import grails.testing.web.controllers.ControllerUnitTest
import spock.lang.Specification
import spock.lang.Unroll

class RegisterControllerSpec extends Specification {
@Unroll
class RegisterControllerSpec extends Specification implements ControllerUnitTest<RegisterController> {

void setup() {
SpringSecurityUtils.setSecurityConfig [:] as ConfigObject
Expand Down Expand Up @@ -123,6 +126,25 @@ class RegisterControllerSpec extends Specification {
!RegisterController.passwordValidator(password, command)
}

void "verify generateLink for ('#action', #linkParams, '#shouldUseServerUrl') is #expectedUrl"() {
given: "the grails.serverUrl is set"
if (isConfigured) {
config.grails.serverURL = 'http://grails.org'
}

when: "the generateLink method is called"
def results = controller.generateLink(action, linkParams, shouldUseServerUrl)

then: "the configured grails.serverUrl is used if the absolute parameter is true"
results == expectedUrl

where:
isConfigured | shouldUseServerUrl | action | linkParams | expectedUrl
false | false | 'shipit' | [foo: 'foo', bar: 'bar'] | 'http://localhost:80/register/shipit?foo=foo&bar=bar'
false | true | 'shipit' | [foo: 'foo', bar: 'bar'] | 'http://localhost:80/register/shipit?foo=foo&bar=bar'
true | true | 'shipit' | [foo: 'foo', bar: 'bar'] | 'http://grails.org/register/shipit?foo=foo&bar=bar'
}

private void updateFromConfig() {
new RegisterController().afterPropertiesSet()
}
Expand Down