Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:11+00:00 2026-06-14T22:38:11+00:00

I have 2 domain objects, User and SystemRights (It’s a many to many, so

  • 0

I have 2 domain objects, User and SystemRights (It’s a many to many, so 1 user can have many rights and 1 right can be owned by many users). I’m looking for a simple way to check if a user has the required rights.

User Domain

class User {

    static hasMany = [systemRights: SystemRight, enterpriseUsers: EnterpriseUser]

    String email;   
    String passwordHash;
}

SystemRight Domain

class SystemRight {

    public static final String LOGIN = "LOGIN"
    public static final String MODIFY_ALL_ENTERPRISES = "MODIFY_ALL_ENTERPRISES"
    public static final String ADMINISTER_SYSTEM = "ADMINISTER_SYSTEM"
    public static final String VALIDATE_SUPPLIER_SCORECARDS = "VALIDATE_SUPPLIER_SCORECARDS"

    static hasMany = [users:User]
    static belongsTo = User

    String name
}

The following did not work for me:

In User.class

public boolean hasRights(List<String> requiredRights) {

    def userHasRight = SystemRight.findByUserAndSystemRight (this, SystemRight.findByName(requiredRight));

    // Nor this

    def userHasRight = this.systemRights.contains(SystemRight.findByName(requiredRight));

}

Current Horrible Solution

public boolean hasRights(List<String> requiredRights) {

    for (String requiredRight : requiredRights) {

        def has = false

        for (SystemRight userRight : user.systemRights) {
            if (userRight.name == requiredRight) {
                has = true
                break;
            }
        }

        if (has == false) {
            return false;
        }            
    }

    return true        

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:38:13+00:00Added an answer on June 14, 2026 at 10:38 pm

    If you’re able/willing to change things up a bit, I’d highly recommend doing the following. It will make you’re life so much easier.

    First, remove the hasMany for SystemRight and User from both Domains and remove the belongsTo User from SystemRight.

    Next, create the Domain to represent the join table.

    class UserSystemRight {
       User user
       SystemRight systemRight
    
       boolean equals(other) {
          if (!(other instanceof UserSystemRight)) {
              return false
          }
          other.user?.id == user?.id && other.systemRight?.id == systemRight?.id
       }
    
       int hashCode() {
          def builder = new HashCodeBuilder()
          if (user) builder.append(user.id)
          if (systemRight) builder.append(systemRight.id)
          builder.toHashCode()
       }
    
    
       // add some more convenience methods here if you want like...
       static UserSystemRight get(long userId, long systemRightId, String systemRightName) {
           find 'from UserSystemRight where user.id=:userId and systemRight.id=:systemRightId and systemRight.name=:systemRightName',
                [userId: userId, systemRightId: systemRightId, systemRightName: systemRightName]
       }
    }
    

    Then, in your User class you can add this method:

    Set<SystemRight> getSystemRights() {
        UserSystemRight.findAllByUser(this).collect { it.systemRight } as Set
    }
    

    Then, add this to your SystemRight Domain:

    Set<User> getUsers() {
        UserSystemRight.findAllBySystemRight(this).collect { it.user } as Set
    }
    

    For a more detailed explenation of why this approach is full of win, aside from actually solving your problem, take a gander at this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain object User . I am looking for some way to
I have the following 2 objects: User and DomainUser User.java: package com.domain; import java.io.Serializable;
I am having problems persisting domain objects where I have a many-to-many relationship with
Let's say I have a domain class called User which can follow other User
I have two domain objects like below User.java Public class User{ private int userId;
Say I have domain objects corresponding to Posts and Users . Nevertheless, I have
I have written my application logic in domain objects (to enable multiple user interfaces
Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int
I have the following domain objects: public class UserSpecialization : PersonSpecialization, IUserSpecialization { public
Some of my PHP domain objects have properties which should be set from outside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.