001    package edu.nrao.sss.webapp;
002    
003    import java.io.IOException;
004    import javax.xml.parsers.ParserConfigurationException;
005    import javax.xml.xpath.XPathExpressionException;
006    import java.util.List;
007    
008    import org.xml.sax.SAXException;
009    
010    import edu.nrao.sss.util.XmlWrapper;
011    
012    /**
013      * A bean wrapper for the xml document returned by the User Db.
014      */
015    public class User extends XmlWrapper {
016            //XXX change this constant to real group name.
017            private static final String managerGroupName = "udb-admin";
018            
019            /** the elements that will be exposed. */
020            private static final String[][] PROPERTIES = {
021                    {"userId", "//user/@id" },
022                    {"firstName", "//first-name"},
023                    {"lastName", "//last-name"},
024                    {"authToken", "//authentication-token"},
025                    {"userGroups", "//user-group/group-name"}
026            };
027    
028            /**
029              * Will throw an IOException if the xml returned by the userdb does not
030              * include a valid userId. Valid means non-null and a length > 0.
031              */
032            public User(String authenticationToken, String userDbUrl) throws
033                    XPathExpressionException, SAXException, IOException,
034                    ParserConfigurationException
035            {
036                    super(userDbUrl + "/query?userByAuthenticationToken=" +
037                                    authenticationToken, "/query-result/user");
038    
039                    fillParameters(PROPERTIES);
040    
041                    String id = getUserId();
042                    if (id == null || id.length() == 0)
043                    {
044                            throw new IOException("Invalid Authentication Token");
045                    }
046            }
047    
048            public String getLastName() {
049                    List<String> list = get("lastName");
050                    String val = null;
051                    
052                    if (list != null && list.size() > 0)
053                            val = list.get(0);
054                    return val;
055            }
056    
057            public String getFirstName() {
058                    List<String> list = get("firstName");
059                    String val = null;
060                    
061                    if (list != null && list.size() > 0)
062                            val = list.get(0);
063                    return val;
064            }
065    
066            public String getUserId() {
067                    List<String> list = get("userId");
068                    String val = null;
069                    
070                    if (list != null && list.size() > 0)
071                            val = list.get(0);
072                    return val;
073            }
074            
075            public String getAuthToken() {
076                    List<String> list = get("authToken");
077                    String val = null;
078                    
079                    if (list != null && list.size() > 0)
080                            val = list.get(0);
081                    return val;
082            }
083            
084            public List<String> getGroupNames() {
085                    return get("userGroups");
086            }
087            
088            /**
089             * @return true if this user is in the Manger Group, false otherwise.
090             */
091            public boolean isManager()
092            {
093                    List<String> list = getGroupNames();
094                    if (list != null)
095                            return list.contains(managerGroupName);
096                    
097                    else
098                            return false;
099            }
100    }