Class ProfileManager


  • public class ProfileManager
    extends java.lang.Object
    Class to manage and persist user profile data such as saved bags
    Author:
    Mark Woodbridge, Daniela Butano, Alex Kalderimis
    • Field Detail

      • profileCache

        protected CacheMap<java.lang.String,​Profile> profileCache
      • pathQueryFormat

        protected int pathQueryFormat
        Number determining format of queries in the database
    • Constructor Detail

      • ProfileManager

        public ProfileManager​(ObjectStore os,
                              ObjectStoreWriter userProfileOS)
        Construct a ProfileManager for the webapp
        Parameters:
        os - the ObjectStore to which the webapp is providing an interface
        userProfileOS - the object store that holds user profile information
    • Method Detail

      • getProductionObjectStore

        public ObjectStore getProductionObjectStore()
        Return the ObjectStore that was passed to the constructor.
        Returns:
        the ObjectStore from the constructor
      • getProfileObjectStoreWriter

        public ObjectStoreWriter getProfileObjectStoreWriter()
        Return the userprofile ObjectStoreWriter that was passed to the constructor.
        Returns:
        the userprofile ObjectStoreWriter from the constructor
      • getVersion

        public int getVersion()
        Returns the version number of the data format in the database for this ProfileManager.
        Returns:
        an int
      • hasProfile

        public boolean hasProfile​(java.lang.String username)
        Check whether a user already has a Profile
        Parameters:
        username - the username
        Returns:
        true if a profile exists
      • validPassword

        public boolean validPassword​(java.lang.String username,
                                     java.lang.String password)
        Validate a user's password A check should be made prior to this call to ensure a Profile exists
        Parameters:
        username - the username
        password - the password
        Returns:
        true if password is valid
      • setPassword

        public void setPassword​(java.lang.String username,
                                java.lang.String password)
        Change a user's password A check should be made prior to this call to ensure a Profile exists
        Parameters:
        username - the username
        password - the password
      • getPassword

        public java.lang.String getPassword​(java.lang.String username)
        Get a user's password A check should be made prior to this call to ensure a Profile exists
        Parameters:
        username - the username
        Returns:
        password the password
      • getProfile

        public Profile getProfile​(java.lang.String username,
                                  java.lang.String password,
                                  java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Get a user's Profile using a username, password and the classKeys.
        Parameters:
        username - the username
        password - the password
        classKeys - the classkeys
        Returns:
        the Profile, or null if one doesn't exist
      • getProfile

        public Profile getProfile​(java.lang.String username,
                                  java.lang.String password)
        Get a user's Profile using a username and password.
        Parameters:
        username - the username
        password - the password
        Returns:
        the Profile, or null if one doesn't exist
      • getProfile

        public Profile getProfile​(java.lang.String username)
        Get a user's Profile using a username
        Parameters:
        username - the username
        Returns:
        the Profile, or null if one doesn't exist
      • getProfile

        public Profile getProfile​(int id)
        Get a user's Profile using an ID
        Parameters:
        id - userprofile ID
        Returns:
        user's profile
      • deleteProfile

        public void deleteProfile​(Profile profile)
                           throws ObjectStoreException
        Completely remove a profile and all of its associated data from the data-store. Use with extreme caution.
        Parameters:
        profile - The profile to remove.
        Throws:
        ObjectStoreException - If it cannot be removed.
      • getProfile

        public Profile getProfile​(java.lang.String username,
                                  java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Get a user's Profile using a username
        Parameters:
        username - the username
        classKeys - the classkeys
        Returns:
        the Profile, or null if one doesn't exist
      • getTagManager

        public TagManager getTagManager()
        Return the TagManager for adding, removing and fetching Tags assigned to templates, bags and classes.
        Returns:
        the TagManager
      • saveProfile

        public void saveProfile​(Profile profile)
        Synchronise a user's Profile with the backing store
        Parameters:
        profile - the Profile
      • createNewProfile

        public Profile createNewProfile​(java.lang.String username,
                                        java.lang.String password)
        Create a new profile in db with username and password given in input
        Parameters:
        username - the user name
        password - the password
        Returns:
        new profile
      • createAnonymousProfile

        public Profile createAnonymousProfile()
        Create a profile not tied to an entry in the user db. For web services users.
        Returns:
        anon profile
      • createBasicLocalProfile

        public Profile createBasicLocalProfile​(java.lang.String username,
                                               java.lang.String password,
                                               java.lang.String apiKey)
        Create a new Profile with the given username, password and api-key. This profile will be a local standard user.
        Parameters:
        username - The name for this user.
        password - The password for this user.
        apiKey - The API key for this user.
        Returns:
        The profile.
      • createSuperUser

        public Profile createSuperUser​(java.lang.String username,
                                       java.lang.String password,
                                       java.lang.String apiKey)
        Create a super-user with the given username, password and API-key. The user will be marked as a local super-user.
        Parameters:
        username - The name for this user.
        password - The password for this user.
        apiKey - The API key for this user.
        Returns:
        The profile.
      • createProfile

        public void createProfile​(Profile profile)
        Creates a profile in the userprofile database.
        Parameters:
        profile - a Profile object
      • generateApiKey

        public java.lang.String generateApiKey​(Profile profile)
        Generate a new API access key for this profile and return it.
        Parameters:
        profile - The profile to generate the new API key for.
        Returns:
        A new API access key
      • generateSingleUseKey

        public java.lang.String generateSingleUseKey​(Profile profile)
        Generate a single use API key and store it in memory, before returning it.
        Parameters:
        profile - the user profile
        Returns:
        the generated key
      • generate24hrKey

        public java.lang.String generate24hrKey​(Profile profile)
        Generate a day token
        Parameters:
        profile - the profile which token is valid
        Returns:
        the token
      • removeTokensForProfile

        public void removeTokensForProfile​(Profile profile)
        Remove auth tokens for a specified users.
        Parameters:
        profile - users profile
      • tokenHasMoreUses

        public boolean tokenHasMoreUses​(java.lang.String token)
        Return whether the token given in input is suitable for using in the future.
        Parameters:
        token - the token to verify
        Returns:
        true if is suitable for using in the future.
      • generateReadOnlyAccessToken

        public java.lang.String generateReadOnlyAccessToken​(Profile profile,
                                                            java.lang.String message)
                                                     throws ObjectStoreException
        Return a permanent user access token, with ReadOnly permission.
        Parameters:
        profile - a users profile
        message - a message
        Returns:
        A token granting read-only access to resources.
        Throws:
        ObjectStoreException - oops
      • createProfileWithoutBags

        public void createProfileWithoutBags​(Profile profile)
        Creates a profile in the userprofile database without adding bag. Method used by the ProfileReadXml.
        Parameters:
        profile - a Profile object
      • getUserProfile

        public UserProfile getUserProfile​(java.lang.String username)
        Perform a query to retrieve a user's backing UserProfile
        Parameters:
        username - the username
        Returns:
        the relevant UserProfile
      • getUserProfile

        public UserProfile getUserProfile​(java.lang.Integer userId)
        Perform a query to retrieve a user's backing UserProfile
        Parameters:
        userId - the id of the user
        Returns:
        the relevant UserProfile
      • getProfileUserNames

        public java.util.List<java.lang.String> getProfileUserNames()
        Return a List of the usernames in all of the stored profiles.
        Returns:
        the usernames
      • getProfileUserName

        public java.lang.String getProfileUserName​(int profileId)
        Return the name of the user with the given internal DB id. If no user with that name exists, returns null.
        Parameters:
        profileId - the id of the profile.
        Returns:
        the name of the user, or null.
      • getSuperuser

        public java.lang.String getSuperuser()
        Return the super user name set in the properties file
        Returns:
        the superuser name
      • getSuperuserProfile

        public Profile getSuperuserProfile()
        Return the superuser profile set in the properties file
        Returns:
        the superuser profile
      • getAllSuperUsers

        public java.util.Collection<Profile> getAllSuperUsers()
                                                       throws ObjectStoreException
        Returns:
        All the profiles of users who are super-users.
        Throws:
        ObjectStoreException - If we have trouble accessing the data-store.
      • getSuperuserProfile

        public Profile getSuperuserProfile​(java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Return the super user
        Parameters:
        classKeys - the classkeys
        Returns:
        the superuser profile
      • createPasswordChangeToken

        public java.lang.String createPasswordChangeToken​(java.lang.String username)
        Creates a password change token assigned to the given username that will expire after a day.
        Parameters:
        username - the name of the user to create a password change token for
        Returns:
        a String containing the token
        Throws:
        java.lang.IllegalArgumentException - if the username does not match a profile
      • getUsernameForToken

        public java.lang.String getUsernameForToken​(java.lang.String token)
        Returns the username associated with the given token, if the token is valid.
        Parameters:
        token - the token
        Returns:
        the username associated with the token
        Throws:
        java.lang.IllegalArgumentException - if the token is invalid
      • changePasswordWithToken

        public java.lang.String changePasswordWithToken​(java.lang.String token,
                                                        java.lang.String password)
        Changes the password of a profile if the given token is valid.
        Parameters:
        token - the token
        password - the new password to apply to the account
        Returns:
        the username hat has the new password
        Throws:
        java.lang.IllegalArgumentException - if the token is invalid
      • evictFromCache

        public void evictFromCache​(Profile profile)
        Remove a profile from the cache
        Parameters:
        profile - the profile to remove
      • getDefaultPermission

        public static ProfileManager.ApiPermission getDefaultPermission​(Profile profile)
        Wrap a profile in the default permission level.
        Parameters:
        profile - The profile to wrap.
        Returns:
        The default permission for a particular profile.
      • grantPermission

        public ProfileManager.ApiPermission grantPermission​(java.lang.String issuer,
                                                            java.lang.String identity,
                                                            java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Grant permission to the given identity, creating a profile for this identity if it is not already available. By this point in the process, the code calling this method is required to have validated the identity claims of the issuer.
        Parameters:
        issuer - The client claiming this identity for a user.
        identity - The identity of the user.
        classKeys - The class keys for this service.
        Returns:
        permission to use this service.
      • getPermission

        public ProfileManager.ApiPermission getPermission​(java.lang.String token,
                                                          java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Get the level of permission granted by an access token.
        Parameters:
        token - The token supposedly associated with a user.
        classKeys - The class keys for this user.
        Returns:
        A permission object if authentication is successful.
        Throws:
        ProfileManager.AuthenticationException - if the authentication fails.
      • removePermanentToken

        public void removePermanentToken​(PermanentToken token)
        Parameters:
        token - permanent user token
      • getPermission

        public ProfileManager.ApiPermission getPermission​(java.lang.String username,
                                                          java.lang.String password,
                                                          java.util.Map<java.lang.String,​java.util.List<FieldDescriptor>> classKeys)
        Authenticate a user using username/password credentials.
        Parameters:
        username - The name of the authenticating user.
        password - The password this user is meant to have.
        classKeys - Class Keys for this mine's model.
        Returns:
        A representation of this user's permissions.
      • isProfileCached

        public boolean isProfileCached​(java.lang.String username)
        Check if the profile, whose username is given in input, has been cached by the profile cache
        Parameters:
        username - the user name
        Returns:
        true if the profile is in the cache
      • getSuperUsers

        public java.util.List<java.lang.String> getSuperUsers()
        Return a list of users with 'superuser' role
        Returns:
        the user list
      • getSuperUsersProfile

        public java.util.List<Profile> getSuperUsersProfile()
        Return a list of profile with 'superuser' role
        Returns:
        the profile list
      • getPreferences

        protected UserPreferences getPreferences​(Profile profile)
        Get the preferences for a profile.
        Parameters:
        profile - The profile to retrieve preferences for.
        Returns:
        A user-preferences map.