Redirect page based on userlogin type

前端 未结 2 983
长发绾君心
长发绾君心 2021-01-26 05:05

Upon successful user login i want the page to redirect to /person/personCreate and it did work after adding the following code to Config.groovy.

<
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 05:52

    It seems that you're distinguishing between super humans and regular humans based on an attribute of the User class. As you're using spring security, a much more natural way to make this distinction is to define these as roles (AKA authorities) and assign them to each user as appropriate.

    You can then implement your post-login redirection action like this:

    import grails.plugin.springsecurity.SpringSecurityUtils
    
    class PersonController {
    
      def personCreate() {
        if (SpringSecurityUtils.ifAllGranted('SUPER_HUMAN')) {
          redirect action: 'superHumanHome'  
        } else {
          redirect action: 'humanHome'
        }
      }
    
      def superHumanHome() {
        // show the super human's home page
      }
    
      def humanHome() {
        // show the human's home page
      }
    }
    

    By the way, in your question, you said that personCreate is the action that should handle the redirection logic, and this is also that action that human's should be redirected to. This will create an infinite loop of redirection for human's, which is why I've redirected humans to a different action in the controller above.

提交回复
热议问题