コード例 #1
0
        public bool Delete()
        {
            if (ReferenceEquals(UserId, null))
            {
                return(true);
            }

            // Delete the user
            ISQLiteConnection conn = null;

            try {
                conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                int affected = conn.Execute("DELETE FROM User WHERE UserId = ?", UserId);

                if (affected > 0)
                {
                    // Delete associated sessions
                    Injection.Kernel.Get <ISessionRepository>().DeleteSessionsForUserId((int)UserId);

                    return(Injection.Kernel.Get <IUserRepository>().DeleteFromUserCache(this));
                }

                return(true);
            } catch (Exception e) {
                logger.Error(e);
            } finally {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }

            return(false);
        }
コード例 #2
0
        public bool UpdatePassword(string password)
        {
            string salt = GeneratePasswordSalt();
            string hash = ComputePasswordHash(password, salt);

            ISQLiteConnection conn = null;

            try {
                conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                int affected = conn.Execute("UPDATE User SET PasswordHash = ?, PasswordSalt = ? WHERE UserId = ?", hash, salt, this.UserId);

                if (affected > 0)
                {
                    this.PasswordHash = hash;
                    this.PasswordSalt = salt;

                    return(Injection.Kernel.Get <IUserRepository>().UpdateUserCache(this));
                }
            } catch (Exception e) {
                logger.Error(e);
            } finally {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }

            return(false);
        }
コード例 #3
0
        // Execute a query on the database, with optional logging
        private static int InternalExecuteQuery(this IDatabase database, bool logging, string query, params object[] args)
        {
            ISQLiteConnection conn = null;

            try {
                // Get database connection, execute and log query
                conn = database.GetSqliteConnection();

                if (logging)
                {
                    return(conn.ExecuteLogged(query, args));
                }
                else
                {
                    return(conn.Execute(query, args));
                }
            } catch (Exception e) {
                logger.Error("execute failed: " + query);
                logger.Error(e);
            } finally {
                database.CloseSqliteConnection(conn);
            }

            // Return 0 on exception, no rows affected
            return(0);
        }
コード例 #4
0
        // Update a user's role
        public bool UpdateRole(Role role)
        {
            ISQLiteConnection conn = null;

            try
            {
                conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                int affected = conn.Execute("UPDATE User SET Role = ? WHERE UserId = ?", role, this.UserId);

                if (affected > 0)
                {
                    this.Role = role;

                    return(Injection.Kernel.Get <IUserRepository>().UpdateUserCache(this));
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            finally
            {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }

            return(false);
        }
コード例 #5
0
        public bool UpdateLastfmSession(string sessionKey)
        {
            ISQLiteConnection conn = null;

            try
            {
                conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                int affected = conn.Execute("UPDATE User SET LastfmSession = ? WHERE UserName = ?", sessionKey, UserName);

                if (affected > 0)
                {
                    this.LastfmSession = sessionKey;

                    return(Injection.Kernel.Get <IUserRepository>().UpdateUserCache(this));
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            finally
            {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }

            return(false);
        }
コード例 #6
0
        /// <summary>
        /// Retrieve the server's GUID for URL forwarding, or generate a new one if none exists
        /// </summary>
        public static string GetServerGuid()
        {
            string guid = null;

            ISQLiteConnection conn = null;

            try
            {
                // Grab server GUID from the database
                conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                guid = conn.ExecuteScalar <string>("SELECT Guid FROM Server");
            }
            catch (Exception e)
            {
                logger.Error("Exception loading server GUID", e);
            }
            finally
            {
                Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
            }

            // If it doesn't exist, generate a new one
            if ((object)guid == null)
            {
                // Generate the GUID
                Guid guidObj = Guid.NewGuid();
                guid = guidObj.ToString();

                // Store the GUID in the database
                try
                {
                    conn = Injection.Kernel.Get <IDatabase>().GetSqliteConnection();
                    int affected = conn.Execute("INSERT INTO Server (Guid) VALUES (?)", guid);

                    if (affected == 0)
                    {
                        guid = null;
                    }
                }
                catch (Exception e)
                {
                    logger.Error("Exception saving guid", e);
                    guid = null;
                }
                finally
                {
                    Injection.Kernel.Get <IDatabase>().CloseSqliteConnection(conn);
                }
            }

            return(guid);
        }