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 6183259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:20:03+00:00 2026-05-24T01:20:03+00:00

In my project certain users have dual roles, so if any such user logs

  • 0

In my project certain users have dual roles, so if any such user logs in how can I make him switch between those 2 ROLES he has so that he can perform certain operations which that particular ROLE provides.

Appreciate step by step process as I am really new to Grails. Any such literature online with example is highly appreciated.

UPDATE:-
WorkridersUser loadUserByUsername(String username, String roleName) throws UsernameNotFoundException {
// def conf = SpringSecurityUtils.securityConfig
//Class User = grailsApplication.getDomainClass(“Person”).clazz

    SchemeUser.withTransaction { status ->

        SchemeUser user = SchemeUser.findByUsername(username) 
        if (!user){ throw new UsernameNotFoundException('User not found', username)}

        UserProfile userProfile = UserProfile.findByEmail(user.username)
        Representative representative = Representative.findByUser(user)
        Organization organization = Organization.get(representative.organization.id)


        def authorities = user.authorities.collect {new GrantedAuthorityImpl(it.authority)}

        return new WorkridersUser(user.username, user.password, user.enabled, 
            !user.accountExpired, !user.passwordExpired, !user.accountLocked, 
            authorities ?: roleName, user.id, organization.companyName,userProfile) 
    } 
}

Thanks

Sri

  • 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-05-24T01:20:04+00:00Added an answer on May 24, 2026 at 1:20 am

    you need to override the loadUserByUsername method as follows:

    UserDetails loadUserByUsername(String username, String selectedRole) {
      // current selected role is stored in the member variable of the UserDetail class         
      this.selectedRole = selectedRole
      return loadUserByUsername(username)
    }
    

    to show all roles of current user within a select box write your own tag lib which should look like:

    def authorities = springSecurityService.principal.originalAuthorities
    if (authorities.size() > 1) {
        out << "<form name='switchRoleForm' action='switchRole' method='post'>"
        out << "<select name='roles'>"
        Role tmpRole
        authorities.each {
            tmpRole = Role.findByAuthority(it.authority)
            // access your current select role
                if (springSecurityService.principal.selectedRole== it.authority)
                out << "<option selected value='${it.authority}'>${tmpRole.name}</option>"
            else
                out << "<option value='${it.authority}'>${tmpRole.name}</option>"
        }
        out << "</select>"
        out << '<input class="switch" type="submit" value="switch"/></span>'
        out << "</form>"        
    }
    

    i described the switch logic in previous post.

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

Sidebar

Related Questions

Is there possible, to have some projects under a certain user.name and user.email and
I am doing a project for a certain bank. They do not have a
I have a maven project that contains a certain api I need to use
Visual Studio Setup project: I have to check certain system requirements before my setup
I am working on a project where the button needs to have a certain
I have a web application project. I am trying to find out why certain
I have setup an SVN server on my hosting account. Users can check in
In a project at work we have a certain value type class in our
I have a Python3 GUI, where user selects certain values to be statistically evaluated
I currently have a Rails Apps that lets users drag and drop certain elements

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.