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

  • Home
  • SEARCH
  • 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

I have setup an SVN server on my hosting account. Users can check in
We are working on a project that requires us to allow users to register
I have a freelance project where I have to build a news articles based
I'm investigating using Kohana for my next project. The site will consist of user
I'm using .net 2.0. This is a project that I have taken over for
I must use RightAWS for certain things. However, I can only get Paperclip uploads
Premise : The requirements for an upcoming project include the fact that no one
I am considering using ADO.Net Data Services in a project for the purpose of
I'm new to EF4 and haven't had any experience with it before. So, bear
I'm teaching someone better practices by refactoring a large project they worked on. One

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.