Permalink
Browse files

Add page for linking OAuth accounts to grails.org ones.

  • Loading branch information...
1 parent be12291 commit a68308bffa96d46d58fa3c65dc42b202770810d3 @pledbrook pledbrook committed Apr 22, 2012
@@ -48,8 +48,8 @@ grails.project.dependency.resolution = {
":resources:1.1.6",
":screencasts:0.5.6",
":searchable:0.6.3",
- ":shiro:1.1.3",
- ":shiro-oauth:0.1-SNAPSHOT",
+ ":shiro:1.2.0-SNAPSHOT",
+ ":shiro-oauth:0.1",
":simple-blog:0.1.5",
":springcache:1.3.1",
":spring-events:1.2",
@@ -66,7 +66,7 @@ grails.project.dependency.resolution = {
}
test ":geb:0.6.0",
- ":spock:0.6-rc-1", {
+ ":spock:0.6", {
excludes 'xml-apis'
}
@@ -106,9 +106,9 @@ class UrlMappings {
"/register"(controller: "user", action: "register")
"/user/create"(controller: "user", action: "createAccount")
"/oauth/success"(controller: "shiroOAuth", action: "onSuccess")
- "/oauth/linkaccount"(controller: "user") {
- action = [GET: "askToLinkOrCreateAccount", POST: "linkAccount"]
- }
+ "/oauth/linkaccount"(controller: "user", action: "askToLinkOrCreateAccount")
+ "/oauth/save/linkaccount"(controller: "user", action: "linkAccount")
+ "/oauth/save/createaccount"(controller: "user", action: "createAccount")
"/oauth/$action?"(controller: "oauth")
"/login"(controller: "user", action: "login")
"/unauthorized"(controller: "user", action: "unauthorized")
@@ -31,6 +31,7 @@ class JSecurityAuthFilters {
}else {
// Redirect to login page.
+ d.session["targetUri"] = targetUri
d.redirect(
controller: 'user',
action: 'login',
@@ -86,17 +86,19 @@ class UserController {
}
def profile() {
- def userInfo = UserInfo.findByUser(request.user)
- if(request.method == 'POST') {
- if(!userInfo) userInfo = new UserInfo(user:request.user)
+ def userId = SecurityUtils.subject.principals.oneByType(Number)
+ def user = User.get(userId)
+ def userInfo = UserInfo.findByUser(user)
+ if (request.method == 'POST') {
+ if (!userInfo) userInfo = new UserInfo(user: user)
userInfo.properties = params
userInfo.save()
- if(params.password) {
- request.user.password = DigestUtils.shaHex(params.password)
- request.user.save()
+ if (params.password) {
+ user.password = DigestUtils.shaHex(params.password)
+ user.save()
}
}
- return [user:request.user, userInfo:userInfo]
+ return [user: user, userInfo: userInfo]
}
@@ -183,14 +185,15 @@ class UserController {
* credentials are correct. Otherwise it redirects back to the page that
* asks the user for those credentials.
*/
- def linkAccount(AccountCommand cmd) {
+ def linkAccount(LoginAccountCommand cmd) {
if (!handleCommandForLinkingAccounts(cmd)) return
try {
- def userId = userService.loginUser(params.login, params.password)
+ def userId = userService.loginUser(cmd.login, cmd.password)
forward controller: "shiroOAuth", action: "linkAccount", params: [userId: userId]
}
- catch (AuthenticationException) {
+ catch (AuthenticationException ex) {
+ println ">> Authentication failed: ${ex.message}"
cmd.errors.reject "auth.invalid.login", "Username or password is invalid"
redirectToAskToLinkPage cmd
}
@@ -200,10 +203,10 @@ class UserController {
* Creates a new Shiro account and links it to the OAuth token that's in
* the current HTTP session.
*/
- def createAccount(AccountCommand cmd) {
+ def createAccount(NewAccountCommand cmd) {
if (!handleCommandForLinkingAccounts(cmd)) return
- def user = userService.createUser(params.login, params.email)
+ def user = userService.createUser(cmd.login, cmd.email)
forward controller: "shiroOAuth", action: "linkAccount", params: [userId: user.id]
}
@@ -252,6 +255,7 @@ class UserController {
}
}
} else {
+ if (params.targetUri) session["targetUri"] = params.targetUri
render(view:"login", model: [targetUri:params.targetUri])
}
}
@@ -305,17 +309,33 @@ class UserController {
}
@Validateable
-class AccountCommand {
+class LoginAccountCommand {
transient userService
String login
+ String password
String email
static constraints = {
- login nullable: false, blank: false, validator: { obj, val ->
+ login nullable: false, blank: false
+ password nullable: false, blank: false
+ }
+}
+
+@Validateable
+class NewAccountCommand {
+ transient userService
+
+ String login
+ String email
+
+ String password
+
+ static constraints = {
+ login nullable: false, blank: false, validator: { val, obj ->
obj.userService.isLoginUnique(val) ? null : "user.login.unique"
}
- email nullable: false, blank: false, validator: { obj, val ->
+ email nullable: false, blank: false, validator: { val, obj ->
obj.userService.isEmailUnique(val) ? null : "user.email.unique"
}
}
@@ -1,6 +1,9 @@
package org.grails.auth
import grails.plugin.springcache.annotations.Cacheable
+import org.apache.shiro.SecurityUtils
+import org.apache.shiro.authc.AuthenticationException
+import org.apache.shiro.authc.UsernamePasswordToken
import org.grails.meta.UserInfo
class UserService {
@@ -20,7 +23,7 @@ class UserService {
def createUser(login, email, password = null) {
def user = new User(
login: login,
- password: password ? DigestUtils.shaHex(password) : null,
+ password: password ? DigestUtils.shaHex(password) : "** none **",
email: email)
.addToRoles(Role.findByName(Role.EDITOR))
.addToRoles(Role.findByName(Role.OBSERVER))
@@ -52,15 +55,15 @@ class UserService {
* Returns <code>true</code> if it is.
*/
Boolean isLoginUnique(login) {
- return User.findByLogin(login)
+ return User.countByLogin(login) == 0
}
/**
* Determines whether the given email address is unique within the system.
* Returns <code>true</code> if it is.
*/
Boolean isEmailUnique(email) {
- return User.findByEmail(email)
+ return User.countByEmail(email) == 0
}
protected final getUserFromPrincipal(principal) {
@@ -35,23 +35,26 @@
<div class="formButtons">
<g:submitButton name="Submit" value="Login" />
</div>
- </div>
+ </g:form>
<g:form id="newAccountForm" class="userForm" url="[controller: 'user', action: 'createAccount']">
<h2>Create a new account</h2>
<div class="inputForm">
<p>
<span class="label"><label for="login">Username:</label></span>
- <g:textField class="textInput" name="login" />
+ <g:textField class="textInput" name="login" value="${login}"/>
</p>
<p>
<span class="label"><label for="email">Email:</label></span>
<g:textField class="textInput" name="email" />
</p>
+ <g:if test="${targetUri}">
+ <g:hiddenField name="targetUri" value="${targetUri}"/>
+ </g:if>
</div>
<div class="formButtons">
<g:submitButton name="Create"/>
</div>
- </div>
+ </g:form>
</div>
</body>
</html>

0 comments on commit a68308b

Please sign in to comment.