mirror of https://github.com/JustKato/FreePad.git
				
				
				
			* Previous commit
This commit is contained in:
		
							parent
							
								
									c3c9aacac3
								
							
						
					
					
						commit
						b710d24a2d
					
				| 
						 | 
				
			
			@ -1,8 +1,12 @@
 | 
			
		|||
package controllers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/sha512"
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"github.com/JustKato/FreePad/lib/helper"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -14,6 +18,45 @@ func AdminMiddleware(router *gin.RouterGroup) {
 | 
			
		|||
		// Check which route we are accessing
 | 
			
		||||
		fmt.Println(`Accesing: `, ctx.Request.RequestURI)
 | 
			
		||||
 | 
			
		||||
		// Check if the request is other than the login request
 | 
			
		||||
		if ctx.Request.RequestURI != "/admin/login" {
 | 
			
		||||
			// Check if the user is logged-in
 | 
			
		||||
 | 
			
		||||
			fmt.Println(`Checking if admin`)
 | 
			
		||||
 | 
			
		||||
			if !IsAdmin(ctx) {
 | 
			
		||||
				// Not an admin, redirect to homepage
 | 
			
		||||
				ctx.Redirect(http.StatusTemporaryRedirect, "/")
 | 
			
		||||
				ctx.Abort()
 | 
			
		||||
 | 
			
		||||
				fmt.Println(`Not an admin!`)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IsAdmin(ctx *gin.Context) bool {
 | 
			
		||||
	adminToken, err := ctx.Cookie("admin_token")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Encode the real token
 | 
			
		||||
	sha512Hasher := sha512.New()
 | 
			
		||||
	sha512Hasher.Write([]byte(helper.GetAdminToken()))
 | 
			
		||||
	hashHexToken := sha512Hasher.Sum(nil)
 | 
			
		||||
	trueToken := hex.EncodeToString(hashHexToken)
 | 
			
		||||
 | 
			
		||||
	// Check if the user's admin token matches the token
 | 
			
		||||
	if adminToken != "" && adminToken == trueToken {
 | 
			
		||||
		// Yep, it's the admin!
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Definitely not an admin
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -295,3 +295,30 @@ func CleanupPosts(age int) {
 | 
			
		|||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetAllPosts() []Post {
 | 
			
		||||
	// Initialize the list of posts
 | 
			
		||||
	postList := []Post{}
 | 
			
		||||
 | 
			
		||||
	// Get the posts storage directory
 | 
			
		||||
	storageDir := getStorageDirectory()
 | 
			
		||||
 | 
			
		||||
	// Read the directory listing
 | 
			
		||||
	files, err := os.ReadDir(storageDir)
 | 
			
		||||
	// Check if thereh as been an issues with reading the directory contents
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// Log the error
 | 
			
		||||
		fmt.Println("Error::GetAllPosts:", err)
 | 
			
		||||
		// Return an empty list to have a clean fallback
 | 
			
		||||
		return []Post{}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Go through all of the files
 | 
			
		||||
	for _, v := range files {
 | 
			
		||||
		// Process the file into a pad
 | 
			
		||||
		postList = append(postList, GetPost(v.Name()))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Return the post list
 | 
			
		||||
	return postList
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,78 @@
 | 
			
		|||
{{ template "inc/header.html" .}}
 | 
			
		||||
 | 
			
		||||
<style>
 | 
			
		||||
 | 
			
		||||
    .pad-instance {
 | 
			
		||||
        display: flex;
 | 
			
		||||
        flex-flow: row;
 | 
			
		||||
        justify-content: space-between;
 | 
			
		||||
        align-items: center;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #pad-list {
 | 
			
		||||
        max-height: 30rem;
 | 
			
		||||
        overflow-x: hidden;
 | 
			
		||||
        overflow-y: auto;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .pad-name {
 | 
			
		||||
        max-width: 30%;
 | 
			
		||||
        overflow: hidden;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
</style>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
 | 
			
		||||
    <main id="main-card" class="container rounded mt-5 shadow-sm">
 | 
			
		||||
        <div class="p-3">
 | 
			
		||||
 | 
			
		||||
            <a href="/" class="logo-container w-100 d-flex mb-4">
 | 
			
		||||
                <img src="/static/img/logo_transparent.png" alt="Logo" style="max-width: 50%; margin: 0 auto;" class="mx-auto">
 | 
			
		||||
            </a>
 | 
			
		||||
 | 
			
		||||
            <div class="form-group my-4 border-top p-3 border">
 | 
			
		||||
 | 
			
		||||
                <div class="pad-instance my-2 border-bottom">
 | 
			
		||||
                    <div class="pad-name col-5">
 | 
			
		||||
                        Pad Name
 | 
			
		||||
                    </div>
 | 
			
		||||
                    <div class="pad-last-modified col-5">
 | 
			
		||||
                        Create Date
 | 
			
		||||
                    </div>
 | 
			
		||||
                    <div class="col-2">
 | 
			
		||||
                        Actions
 | 
			
		||||
                    </div>
 | 
			
		||||
                </div>
 | 
			
		||||
 | 
			
		||||
                <div id="pad-list" >
 | 
			
		||||
                    {{ range $indx, $element := .padList }}
 | 
			
		||||
 | 
			
		||||
                        <div class="pad-instance my-2">
 | 
			
		||||
                            <div class="pad-name col-5">
 | 
			
		||||
                                <a href="/{{ $element.Name }}">
 | 
			
		||||
                                    {{ $element.Name }}
 | 
			
		||||
                                </a>
 | 
			
		||||
                            </div>
 | 
			
		||||
                            <div class="pad-last-modified col-5">
 | 
			
		||||
                                {{ $element.LastModified }}
 | 
			
		||||
                            </div>
 | 
			
		||||
                            <div class="col-2">
 | 
			
		||||
                                <a href="#" class="btn btn-danger">
 | 
			
		||||
                                    Delete
 | 
			
		||||
                                </a>
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </div>
 | 
			
		||||
    
 | 
			
		||||
                    {{ end }}
 | 
			
		||||
                </div>
 | 
			
		||||
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
 | 
			
		||||
    </main>
 | 
			
		||||
 | 
			
		||||
    {{ template "inc/theme-toggle.html" .}}
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
{{ template "inc/footer.html" .}}
 | 
			
		||||
		Loading…
	
		Reference in New Issue