// TODO: Why is this static?
        public static GloebitUser Authorize(string appKeyStr, UUID agentId, string token, string gloebitID)
        {
            string agentIdStr = agentId.ToString();

            // TODO: I think there has to be a better way to do this, but I'm not finding it right now.
            // By calling Get, we make sure that the user is in the map and has any additional data users store.
            GloebitUser localUser = GloebitUser.Get(appKeyStr, agentId);
            GloebitUser u;

            lock (s_userMap) {
                s_userMap.TryGetValue(agentIdStr, out u);
            }
            if (u == null)
            {
                m_log.DebugFormat("[GLOEBITMONEYMODULE] GloebitUser.Authorize() Did not find User in s_userMap.  User logged out.");
                u = localUser;  // User logged out.  Still want to store token.  Don't want to add back to map.
            }
            lock (u.userLock) {
                u.GloebitToken = token;
                u.GloebitID    = gloebitID;
                bool stored = GloebitUserData.Instance.Store(u);
                if (!stored)
                {
                    throw new Exception(String.Format("[GLOEBITMONEYMODULE] GloebitUser.Authorize Failed to store user {0}", agentIdStr));
                }
                localUser = new GloebitUser(u);
            }

            return(localUser);
        }
 private GloebitUser(GloebitUser copyFrom)
 {
     this.AppKey        = copyFrom.AppKey;
     this.PrincipalID   = copyFrom.PrincipalID;
     this.GloebitID     = copyFrom.GloebitID;
     this.GloebitToken  = copyFrom.GloebitToken;
     this.LastSessionID = copyFrom.LastSessionID;
 }
        public bool IsNewSession(UUID newSessionID)
        {
            m_log.InfoFormat("[GLOEBITMONEYMODULE] in IsNewSession for last:{0} current:{1}", this.LastSessionID, newSessionID.ToString());
            string newSessionIDStr = newSessionID.ToString();

            if (this.LastSessionID == newSessionIDStr)
            {
                m_log.DebugFormat("[GLOEBITMONEYMODULE] User is not new session");
                return(false);
            }
            // Before we return true, Ensure our cache is up to date
            GloebitUser.InvalidateCache(UUID.Parse(this.PrincipalID));
            GloebitUser u_from_db = GloebitUser.Get(this.AppKey, UUID.Parse(this.PrincipalID));

            if (u_from_db.LastSessionID == newSessionIDStr)
            {
                m_log.DebugFormat("[GLOEBITMONEYMODULE] User Cache was out of date.  Updated cache.  User is not new session");
                // cache was out of date.  update local user copy form db
                this.UpdateFrom(u_from_db);
                return(false);
            }
            else
            {
                m_log.DebugFormat("[GLOEBITMONEYMODULE] User is New Session");
                // we have a new session.  Store it and return true.

                // Code to ensure we update user in cache
                GloebitUser u;
                lock (s_userMap) {
                    s_userMap.TryGetValue(this.PrincipalID, out u);
                }
                if (u == null)
                {
                    m_log.DebugFormat("[GLOEBITMONEYMODULE] GloebitUser.IsNewSession() Did not find User in s_userMap to update.  User logged out.");
                    u = u_from_db;  // User logged out.  Still want to store token.  Don't want to add back to map.
                }
                lock (u.userLock) {
                    u.LastSessionID = newSessionIDStr;
                    bool stored = GloebitUserData.Instance.Store(u);
                    if (!stored)
                    {
                        throw new Exception(String.Format("[GLOEBITMONEYMODULE] GloebitUser.IsNewSession Failed to store user {0}", this.PrincipalID));
                    }
                    this.UpdateFrom(u);
                }

                return(true);
            }
        }
 public static GloebitUser Get(string appKeyStr, UUID agentID)
 {
     return(GloebitUser.Get(appKeyStr, agentID.ToString()));
 }
 public static GloebitUser Get(UUID appKey, string agentIdStr)
 {
     return(GloebitUser.Get(appKey.ToString(), agentIdStr));
 }
 public static GloebitUser Get(UUID appKey, UUID agentID)
 {
     return(GloebitUser.Get(appKey.ToString(), agentID.ToString()));
 }
 private void UpdateFrom(GloebitUser updateFrom)
 {
     this.GloebitID     = updateFrom.GloebitID;
     this.GloebitToken  = updateFrom.GloebitToken;
     this.LastSessionID = updateFrom.LastSessionID;
 }
        public static GloebitUser Get(string appKeyStr, string agentIdStr)
        {
            m_log.Info("[GLOEBITMONEYMODULE] in GloebitUser.Get");

            GloebitUser u;

            lock (s_userMap) {
                s_userMap.TryGetValue(agentIdStr, out u);
            }

            if (u == null)
            {
                m_log.DebugFormat("[GLOEBITMONEYMODULE] Looking for prior user for {0}", agentIdStr);
                string[] keys = new string[2] {
                    "AppKey", "PrincipalID"
                };
                string[] values = new string[2] {
                    appKeyStr, agentIdStr
                };
                GloebitUser[] users = GloebitUserData.Instance.Get(keys, values);

                switch (users.Length)
                {
                case 1:
                    u = users[0];
                    m_log.DebugFormat("[GLOEBITMONEYMODULE] FOUND USER TOKEN! {0} valid token? {1} --- SesionID{2}", u.PrincipalID, !String.IsNullOrEmpty(u.GloebitToken), u.LastSessionID);
                    break;

                case 0:
                    m_log.DebugFormat("[GLOEBITMONEYMODULE] CREATING NEW USER {0}", agentIdStr);
                    u = new GloebitUser(appKeyStr, agentIdStr, String.Empty, String.Empty, String.Empty);
                    break;

                default:
                    throw new Exception(String.Format("[GLOEBITMONEYMODULE] Failed to find exactly one prior token for {0}", agentIdStr));
                }

                // Store in map and return GloebitUser
                lock (s_userMap) {
                    // Make sure no one else has already loaded this user
                    GloebitUser alreadyLoadedUser;
                    s_userMap.TryGetValue(agentIdStr, out alreadyLoadedUser);
                    if (alreadyLoadedUser == null)
                    {
                        s_userMap[agentIdStr] = u;
                    }
                    else
                    {
                        u = alreadyLoadedUser;
                    }
                }
            }

            // Create a thread local copy of the user to return.
            GloebitUser localUser;

            lock (u.userLock) {
                localUser = new GloebitUser(u);
            }

            return(localUser);
        }