PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/grails-app/controllers/com/immani/mydwich/RegistrationController.groovy

http://github.com/immani/mydwich
Groovy | 258 lines | 193 code | 40 blank | 25 comment | 13 complexity | c9bf13ee74a2e3536b22f25d9a1a7a74 MD5 | raw file
  1. package com.immani.mydwich
  2. //import com.megatome.grails.RecaptchaService
  3. import org.apache.shiro.crypto.hash.Sha256Hash
  4. class RegistrationController {
  5. def index = {
  6. redirect(controller: registeruser)
  7. }
  8. def geocoderService
  9. /**
  10. * Creates a new Company, User and Delivery Address
  11. */
  12. def registercompanyFlow = {
  13. companyinfo {
  14. on("next") {
  15. def coresults
  16. try {
  17. coresults = geocoderService.geocode(params.address, params.zip, params.city, params.country )
  18. }
  19. catch (Exception exception){
  20. flow.companyInstance = new Company(params)
  21. flow.companyInstance.validate()
  22. flash.error = exception.getMessage()
  23. return error()
  24. }
  25. flow.companyInstance = new Company(params + coresults)
  26. //TODO: regex pour éviter que le domain soit pourri - thierry@immani.com
  27. flow.companyInstance.validate() ? success() : error()
  28. }.to "userinfo"
  29. on("cancel").to "cancel"
  30. }
  31. userinfo {
  32. on("next") {
  33. if (params.passwordHash != params.confirmpassword){
  34. flash.error = "Password do not match"
  35. flow.userInstance = new User(params)
  36. return error()
  37. }
  38. flow.usernameleft = params.usernameleft
  39. params.username = params.usernameleft + "@" + flow.companyInstance.domain
  40. flow.userInstance = new User(params)
  41. flow.userInstance.passwordHash = new Sha256Hash(params.passwordHash).toHex()
  42. Role companyRole = Role.findByName("companyadmin")
  43. flow.userInstance.addToRoles(companyRole)
  44. flow.deliveryAddressInstance = new DeliveryAddress(flow.companyInstance.properties)
  45. flow.deliveryAddressInstance.name = ""
  46. flow.userInstance.validate() ? success() : error()
  47. }.to "deliveryinfo"
  48. on("back").to "companyinfo"
  49. on("cancel").to "cancel"
  50. }
  51. deliveryinfo {
  52. on("next") {
  53. def daresults
  54. try {
  55. daresults = geocoderService.geocode(params.address, params.zip, params.city, params.country )
  56. }
  57. catch (Exception exception){
  58. flow.deliveryAddressInstance.validate()
  59. flash.error = exception.getMessage()
  60. return error()
  61. }
  62. flow.deliveryAddressInstance = new DeliveryAddress(params + daresults)
  63. flow.deliveryAddressInstance.company = flow.companyInstance
  64. flow.deliveryAddressInstance.validate() ? success() : error()
  65. }.to "review"
  66. on("back") {
  67. Integer pos = flow.userInstance.username.indexOf('@')
  68. String leftusername = flow.userInstance.username.substring(0, pos)
  69. flow.userInstance.username = leftusername
  70. }.to "userinfo"
  71. on("cancel").to "cancel"
  72. }
  73. review {
  74. on("confirm").to "persist"
  75. on("success").to "end"
  76. }
  77. persist {
  78. action {
  79. emailConfirmationService.sendConfirmation(flow.userInstance.username, "Please confirm your email address", [from:"mydwich@immani.com"])
  80. flow.companyInstance.addToUsers(flow.userInstance)
  81. flow.companyInstance.addToDeliveryAddresses(flow.deliveryAddressInstance)
  82. flow.companyInstance.save()
  83. }
  84. on("success").to "end"
  85. }
  86. end {
  87. redirect(controller: "company")
  88. }
  89. cancel {
  90. redirect(uri: "/")
  91. }
  92. }
  93. def registerrestaurantFlow = {
  94. //TODO: Check Roles!!!
  95. restaurantinfo {
  96. on("next") {
  97. def restresults
  98. try {
  99. restresults = geocoderService.geocode(params.address, params.zip, params.city, params.country )
  100. }
  101. catch (Exception exception){
  102. flow.companyInstance = new Restaurant(params)
  103. flash.error = exception.getMessage()
  104. return error()
  105. }
  106. flow.restaurantInstance = new Restaurant(params + restresults)
  107. flow.restaurantInstance.validate() ? success() : error()
  108. }.to "userinfo"
  109. on("cancel").to "cancel"
  110. }
  111. userinfo {
  112. on("next") {
  113. if (params.passwordHash != params.confirmpassword){
  114. flash.error = "Password do not match"
  115. return error()
  116. }
  117. flow.userInstance = new User(params)
  118. flow.userInstance.passwordHash = new Sha256Hash(params.passwordHash).toHex()
  119. Role restaurantRole = Role.findByName("restaurant")
  120. flow.userInstance.addToRoles(restaurantRole)
  121. flow.userInstance.validate() ? success() : error()
  122. }.to "persist"
  123. on("back").to "restaurantinfo"
  124. on("cancel").to "cancel"
  125. }
  126. persist {
  127. action {
  128. emailConfirmationService.sendConfirmation(flow.userInstance.username, "Please confirm your email address", [from:"mydwich@immani.com"])
  129. flow.restaurantInstance.addToUsers(flow.userInstance)
  130. flow.restaurantInstance.save()
  131. }
  132. on("success").to "end"
  133. }
  134. end {
  135. redirect(controller: "anonymous_Restaurant")
  136. }
  137. cancel {
  138. redirect(url: "/index")
  139. }
  140. }
  141. def registeruserFlow = {
  142. mail {
  143. on("next") {
  144. String username = params.username
  145. Integer pos = username.indexOf('@')
  146. String domain = username.substring(pos + 1)
  147. def matchingcompanies = Company.findAllWhere(domain: domain)
  148. if (matchingcompanies.size() == 1){
  149. flow.companyInstance = matchingcompanies[0]
  150. flow.userInstance = new User(params)
  151. flow.userInstance.validate()
  152. if(flow.userInstance.hasErrors()) {
  153. if(flow.userInstance.errors.hasFieldErrors("username")){
  154. error()
  155. }
  156. else{
  157. flow.userInstance.clearErrors()
  158. success()
  159. }
  160. }
  161. }
  162. else if (matchingcompanies.size() == 0){
  163. flash.message = ${message(code: "flow.company.not.found", args: [domain])}
  164. // flash.message = "flow.company.not.found"
  165. // flash.args = ["domain"]
  166. return error()
  167. }
  168. }.to "userinfo"
  169. on("cancel").to "cancel"
  170. }
  171. userinfo {
  172. on("next") {
  173. if (params.passwordHash != params.confirmpassword){
  174. flash.error = "Password do not match"
  175. return error()
  176. }
  177. flow.userInstance.properties = params
  178. flow.userInstance.passwordHash = new Sha256Hash(params.passwordHash).toHex()
  179. Role companyRole = Role.findByName("company")
  180. flow.userInstance.addToRoles(companyRole)
  181. flow.userInstance.validate() ? success() : error()
  182. }.to "persist"
  183. on("back").to "mail"
  184. on("cancel").to "cancel"
  185. }
  186. persist {
  187. action {
  188. flow.companyInstance.addToUsers(flow.userInstance)
  189. flow.userInstance.save()
  190. emailConfirmationService.sendConfirmation(flow.userInstance.username, "Please confirm your email address", [from:"mydwich@immani.com"],flow.userInstance.id.toString())
  191. }
  192. on("success").to "end"
  193. }
  194. end {
  195. redirect(controller: "user")
  196. }
  197. cancel {
  198. redirect(url: "/")
  199. }
  200. }
  201. }
  202. /*
  203. RecaptchaService recaptchaService
  204. def index = {
  205. render(view:'/recaptcha')
  206. }
  207. def save = {
  208. def recaptchaOK = true
  209. if (!recaptchaService.verifyAnswer(session, request.getRemoteAddr(), params)) {
  210. recaptchaOK = false
  211. }
  212. else {
  213. recaptchaOK = true
  214. }
  215. render(view:'/recaptcha',model:[recaptcha:recaptchaOK])
  216. }
  217. */