Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String authority
- static constraints = {
- //authority blank: false, unique: true
- }
- static mapping = {
- cache true
- }
- private static final long serialVersionUID = 1
- transient springSecurityService
- String username
- String password
- boolean enabled = true
- boolean accountExpired
- boolean accountLocked
- boolean passwordExpired
- String surname
- String email
- String country
- String city
- String profilePic
- String telephone
- static hasMany = [shop:Shop]
- Set<Role> getAuthorities() {
- UserRole.findAllByUser(this)*.role
- }
- def beforeInsert() {
- encodePassword()
- }
- def beforeUpdate() {
- if (isDirty('password')) {
- encodePassword()
- }
- }
- protected void encodePassword() {
- password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
- }
- static transients = ['springSecurityService']
- static constraints = {
- // password blank: false, password: true
- // username blank: false, unique: true
- }
- static mapping = {
- password column: '`password`'
- }
- private static final long serialVersionUID = 1
- User user
- Role role
- @Override
- boolean equals(other) {
- if (other instanceof UserRole) {
- other.userId == user?.id && other.roleId == role?.id
- }
- }
- @Override
- int hashCode() {
- def builder = new HashCodeBuilder()
- if (user) builder.append(user.id)
- if (role) builder.append(role.id)
- builder.toHashCode()
- }
- static UserRole get(long userId, long roleId) {
- criteriaFor(userId, roleId).get()
- }
- static boolean exists(long userId, long roleId) {
- criteriaFor(userId, roleId).count()
- }
- private static DetachedCriteria criteriaFor(long userId, long roleId) {
- UserRole.where {
- user == User.load(userId) &&
- role == Role.load(roleId)
- }
- }
- static UserRole create(User user, Role role) {
- def instance = new UserRole(user: user, role: role)
- instance.save()
- instance
- }
- static boolean remove(User u, Role r) {
- if (u != null && r != null) {
- UserRole.where { user == u && role == r }.deleteAll()
- }
- }
- static int removeAll(User u) {
- u == null ? 0 : UserRole.where { user == u }.deleteAll()
- }
- static int removeAll(Role r) {
- r == null ? 0 : UserRole.where { role == r }.deleteAll()
- }
- static constraints = {
- role validator: { Role r, UserRole ur ->
- if (ur.user?.id) {
- UserRole.withNewSession {
- if (UserRole.exists(ur.user.id, r.id)) {
- return ['userRole.exists']
- }
- }
- }
- }
- }
- static mapping = {
- id composite: ['user', 'role']
- version false
- }
- def init = { servletContext ->
- def adminRole = Role.findByAuthority("ROLE_ADMIN")
- if (!adminRole) {
- adminRole = new Role(authority: 'ROLE_ADMIN')
- adminRole.save(flush: true)
- }
- adminRole = Role.findByAuthority("ROLE_ADMIN")
- if (!User.get(1)) {
- def personInstance = new User(username: "me", password: "password")
- personInstance.save(flush: true)
- UserRole.create(personInstance,adminRole)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement