Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getuserprincipal()

Status
Not open for further replies.

NotSQL

Technical User
May 17, 2005
205
GB
Hi im having problem retrieving the userid from the session, my code looks a bit like this.. Can anyone tell why it is always writing "anon" to the database no matter whos signed in.. The getuserprincipal() doesnt seem to work.

Any suggestions?


public IUserMessage getUser(HttpServletRequest request) {
IUserMessage message = new UserMessage();
String username = "";
if(this.user==null){

Principal principal = request.getUserPrincipal();

if(principal==null){
username = request.getParameter("user");
if(username==null || username.equals("")){

message.setMessage(ERROR_USER_NOT_LOGGED_IN);
user = new User();
user.setUserId("anon");
message.setUser(user);
return message;
}
} else {
username = principal.getName();
}

DatabaseManager databaseManager = new DatabaseManager();
if(message.getStatus()==Constants.STATUS_OK){
this.user = message.getUser();
}
} else {
message.setUser(this.user);
message.setStatus(Constants.STATUS_OK);
}

return message;
 
my code looks a bit like this..
It would be more help if you posted your actual code, rather than an approximation of it...


Anyway, I reformatted the code to make it a bit easier to read
Code:
public IUserMessage getUser(HttpServletRequest request) {
   IUserMessage message = new UserMessage();
   String username = "";

   if(this.user==null){     
      Principal principal =  request.getUserPrincipal();
            
      if(principal==null){
         [red]username[/red] = request.getParameter("user");

         if([green]username[/green]==null || [green]username[/green].equals("")){                   
            message.setMessage(ERROR_USER_NOT_LOGGED_IN);
            user = new User();
            user.setUserId("anon");
            message.setUser(user);
            return message;
         }

      } else {
         [red]username[/red] = principal.getName();
      }
            
      DatabaseManager databaseManager = new DatabaseManager();  
  
      if(message.getStatus()==Constants.STATUS_OK){
         this.user = message.getUser();
   }
   } else {
      message.setUser(this.user);
      message.setStatus(Constants.STATUS_OK);
   }
        
   return message;
}
One observation: apart from checking that it actually contains a value, username seems to be 'write-only storage'. Is this intentional, or should you be using it to set some value somewhere?

Might be worth stepping through in the debugger to see exactly where it is going wrong.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top