[58] | 1 | ${personClassImport} |
---|
| 2 | ${authorityClassImport} |
---|
| 3 | |
---|
| 4 | import org.springframework.security.providers.UsernamePasswordAuthenticationToken as AuthToken |
---|
| 5 | import org.springframework.security.context.SecurityContextHolder as SCH |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Registration controller. |
---|
| 9 | */ |
---|
| 10 | class RegisterController { |
---|
| 11 | |
---|
| 12 | def authenticateService |
---|
| 13 | def daoAuthenticationProvider |
---|
| 14 | def emailerService |
---|
| 15 | |
---|
| 16 | static Map allowedMethods = [save: 'POST', update: 'POST'] |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * User Registration Top page. |
---|
| 20 | */ |
---|
| 21 | def index = { |
---|
| 22 | |
---|
| 23 | // skip if already logged in |
---|
| 24 | if (authenticateService.isLoggedIn()) { |
---|
| 25 | redirect action: show |
---|
| 26 | return |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | if (session.id) { |
---|
| 30 | def person = new ${personClassName}() |
---|
| 31 | person.properties = params |
---|
| 32 | return [person: person] |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | redirect uri: '/' |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * User Information page for current user. |
---|
| 40 | */ |
---|
| 41 | def show = { |
---|
| 42 | |
---|
| 43 | // get user id from session's domain class. |
---|
| 44 | def user = authenticateService.userDomain() |
---|
| 45 | if (user) { |
---|
| 46 | render view: 'show', model: [person: ${personClassName}.get(user.id)] |
---|
| 47 | } |
---|
| 48 | else { |
---|
| 49 | redirect action: index |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Edit page for current user. |
---|
| 55 | */ |
---|
| 56 | def edit = { |
---|
| 57 | |
---|
| 58 | def person |
---|
| 59 | def user = authenticateService.userDomain() |
---|
| 60 | if (user) { |
---|
| 61 | person = ${personClassName}.get(user.id) |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if (!person) { |
---|
| 65 | flash.message = "[Illegal Access] User not found with id \${params.id}" |
---|
| 66 | redirect action: index |
---|
| 67 | return |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | [person: person] |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * update action for current user's edit page |
---|
| 75 | */ |
---|
| 76 | def update = { |
---|
| 77 | |
---|
| 78 | def person |
---|
| 79 | def user = authenticateService.userDomain() |
---|
| 80 | if (user) { |
---|
| 81 | person = ${personClassName}.get(user.id) |
---|
| 82 | } |
---|
| 83 | else { |
---|
| 84 | redirect action: index |
---|
| 85 | return |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if (!person) { |
---|
| 89 | flash.message = "[Illegal Access] User not found with id \${params.id}" |
---|
| 90 | redirect action: index, id: params.id |
---|
| 91 | return |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // if user want to change password. leave passwd field blank, passwd will not change. |
---|
| 95 | if (params.passwd && params.passwd.length() > 0 |
---|
| 96 | && params.repasswd && params.repasswd.length() > 0) { |
---|
| 97 | if (params.passwd == params.repasswd) { |
---|
| 98 | person.passwd = authenticateService.encodePassword(params.passwd) |
---|
| 99 | } |
---|
| 100 | else { |
---|
| 101 | person.passwd = '' |
---|
| 102 | flash.message = 'The passwords you entered do not match.' |
---|
| 103 | render view: 'edit', model: [person: person] |
---|
| 104 | return |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | person.userRealName = params.userRealName |
---|
| 109 | person.email = params.email |
---|
| 110 | if (params.emailShow) { |
---|
| 111 | person.emailShow = true |
---|
| 112 | } |
---|
| 113 | else { |
---|
| 114 | person.emailShow = false |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (person.save()) { |
---|
| 118 | redirect action: show, id: person.id |
---|
| 119 | } |
---|
| 120 | else { |
---|
| 121 | render view: 'edit', model: [person: person] |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Person save action. |
---|
| 127 | */ |
---|
| 128 | def save = { |
---|
| 129 | |
---|
| 130 | // skip if already logged in |
---|
| 131 | if (authenticateService.isLoggedIn()) { |
---|
| 132 | redirect action: show |
---|
| 133 | return |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | def person = new ${personClassName}() |
---|
| 137 | person.properties = params |
---|
| 138 | |
---|
| 139 | def config = authenticateService.securityConfig |
---|
| 140 | def defaultRole = config.security.defaultRole |
---|
| 141 | |
---|
| 142 | def role = ${authorityClassName}.findByAuthority(defaultRole) |
---|
| 143 | if (!role) { |
---|
| 144 | person.passwd = '' |
---|
| 145 | flash.message = 'Default Role not found.' |
---|
| 146 | render view: 'index', model: [person: person] |
---|
| 147 | return |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | if (params.captcha.toUpperCase() != session.captcha) { |
---|
| 151 | person.passwd = '' |
---|
| 152 | flash.message = 'Access code did not match.' |
---|
| 153 | render view: 'index', model: [person: person] |
---|
| 154 | return |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | if (params.passwd != params.repasswd) { |
---|
| 158 | person.passwd = '' |
---|
| 159 | flash.message = 'The passwords you entered do not match.' |
---|
| 160 | render view: 'index', model: [person: person] |
---|
| 161 | return |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | def pass = authenticateService.encodePassword(params.passwd) |
---|
| 165 | person.passwd = pass |
---|
| 166 | person.enabled = true |
---|
| 167 | person.emailShow = true |
---|
| 168 | person.description = '' |
---|
| 169 | if (person.save()) { |
---|
| 170 | role.addToPeople(person) |
---|
| 171 | if (config.security.useMail) { |
---|
| 172 | String emailContent = """You have signed up for an account at: |
---|
| 173 | |
---|
| 174 | \${request.scheme}://\${request.serverName}:\${request.serverPort}\${request.contextPath} |
---|
| 175 | |
---|
| 176 | Here are the details of your account: |
---|
| 177 | ------------------------------------- |
---|
| 178 | LoginName: \${person.username} |
---|
| 179 | Email: \${person.email} |
---|
| 180 | Full Name: \${person.userRealName} |
---|
| 181 | Password: \${params.passwd} |
---|
| 182 | """ |
---|
| 183 | |
---|
| 184 | def email = [ |
---|
| 185 | to: [person.email], // 'to' expects a List, NOT a single email address |
---|
| 186 | subject: "[\${request.contextPath}] Account Signed Up", |
---|
| 187 | text: emailContent // 'text' is the email body |
---|
| 188 | ] |
---|
| 189 | emailerService.sendEmails([email]) |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | person.save(flush: true) |
---|
| 193 | |
---|
| 194 | def auth = new AuthToken(person.username, params.passwd) |
---|
| 195 | def authtoken = daoAuthenticationProvider.authenticate(auth) |
---|
| 196 | SCH.context.authentication = authtoken |
---|
| 197 | redirect uri: '/' |
---|
| 198 | } |
---|
| 199 | else { |
---|
| 200 | person.passwd = '' |
---|
| 201 | render view: 'index', model: [person: person] |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|