Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. String authority
  2.  
  3. static constraints = {
  4. //authority blank: false, unique: true
  5. }
  6.  
  7. static mapping = {
  8. cache true
  9. }
  10.  
  11. private static final long serialVersionUID = 1
  12.  
  13. transient springSecurityService
  14.  
  15.  
  16. String username
  17. String password
  18. boolean enabled = true
  19. boolean accountExpired
  20. boolean accountLocked
  21. boolean passwordExpired
  22.  
  23. String surname
  24. String email
  25. String country
  26. String city
  27. String profilePic
  28. String telephone
  29.  
  30. static hasMany = [shop:Shop]
  31. Set<Role> getAuthorities() {
  32. UserRole.findAllByUser(this)*.role
  33. }
  34.  
  35. def beforeInsert() {
  36. encodePassword()
  37. }
  38.  
  39. def beforeUpdate() {
  40. if (isDirty('password')) {
  41. encodePassword()
  42. }
  43. }
  44.  
  45. protected void encodePassword() {
  46. password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
  47. }
  48.  
  49. static transients = ['springSecurityService']
  50.  
  51. static constraints = {
  52. // password blank: false, password: true
  53. // username blank: false, unique: true
  54. }
  55.  
  56. static mapping = {
  57. password column: '`password`'
  58. }
  59.  
  60. private static final long serialVersionUID = 1
  61.  
  62. User user
  63. Role role
  64.  
  65. @Override
  66. boolean equals(other) {
  67. if (other instanceof UserRole) {
  68. other.userId == user?.id && other.roleId == role?.id
  69. }
  70. }
  71.  
  72. @Override
  73. int hashCode() {
  74. def builder = new HashCodeBuilder()
  75. if (user) builder.append(user.id)
  76. if (role) builder.append(role.id)
  77. builder.toHashCode()
  78. }
  79.  
  80. static UserRole get(long userId, long roleId) {
  81. criteriaFor(userId, roleId).get()
  82. }
  83.  
  84. static boolean exists(long userId, long roleId) {
  85. criteriaFor(userId, roleId).count()
  86. }
  87.  
  88. private static DetachedCriteria criteriaFor(long userId, long roleId) {
  89. UserRole.where {
  90. user == User.load(userId) &&
  91. role == Role.load(roleId)
  92. }
  93. }
  94.  
  95. static UserRole create(User user, Role role) {
  96. def instance = new UserRole(user: user, role: role)
  97. instance.save()
  98. instance
  99. }
  100.  
  101. static boolean remove(User u, Role r) {
  102. if (u != null && r != null) {
  103. UserRole.where { user == u && role == r }.deleteAll()
  104. }
  105. }
  106.  
  107. static int removeAll(User u) {
  108. u == null ? 0 : UserRole.where { user == u }.deleteAll()
  109. }
  110.  
  111. static int removeAll(Role r) {
  112. r == null ? 0 : UserRole.where { role == r }.deleteAll()
  113. }
  114.  
  115. static constraints = {
  116. role validator: { Role r, UserRole ur ->
  117. if (ur.user?.id) {
  118. UserRole.withNewSession {
  119. if (UserRole.exists(ur.user.id, r.id)) {
  120. return ['userRole.exists']
  121. }
  122. }
  123. }
  124. }
  125. }
  126.  
  127. static mapping = {
  128. id composite: ['user', 'role']
  129. version false
  130. }
  131.  
  132. def init = { servletContext ->
  133. def adminRole = Role.findByAuthority("ROLE_ADMIN")
  134.  
  135. if (!adminRole) {
  136. adminRole = new Role(authority: 'ROLE_ADMIN')
  137. adminRole.save(flush: true)
  138. }
  139. adminRole = Role.findByAuthority("ROLE_ADMIN")
  140.  
  141. if (!User.get(1)) {
  142. def personInstance = new User(username: "me", password: "password")
  143. personInstance.save(flush: true)
  144. UserRole.create(personInstance,adminRole)
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement